Skip to content

Commit

Permalink
v1.3.6: break change: sqlkit use generic
Browse files Browse the repository at this point in the history
  • Loading branch information
mizuki1412 committed Apr 1, 2022
1 parent 32f5823 commit 4cb5471
Show file tree
Hide file tree
Showing 22 changed files with 314 additions and 398 deletions.
13 changes: 13 additions & 0 deletions class/constraints/generic_constraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package constraints

type Number interface {
Integer | Float
}

type Integer interface {
int | int8 | int16 | int32 | int64
}

type Float interface {
float32 | float64
}
51 changes: 51 additions & 0 deletions goland-live-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,57 @@ func (dao *Dao) scanOne(sql string, args []any) *$bean$ {
////
```

## dao_new
```
type Dao struct {
sqlkit.Dao[$name$]
}
const (
ResultDefault byte = iota
ResultNone
)
func New(tx ...*sqlx.Tx) *Dao {
dao := &Dao{}
if len(tx) > 0 {
dao.TX = tx[0]
}
dao.Cascade = func(obj *$name$) {
switch dao.ResultType {
case ResultDefault:
case ResultNone:
}
}
return dao
}
func NewWithSchema(schema string,tx ...*sqlx.Tx) *Dao {
dao := New(tx...)
dao.SetSchema(schema)
return dao
}
```

## dao_new_no_cascade
```
type Dao struct {
sqlkit.Dao[$name$]
}
func New(tx ...*sqlx.Tx) *Dao {
dao := &Dao{}
if len(tx) > 0 {
dao.TX = tx[0]
}
return dao
}
func NewWithSchema(schema string,tx ...*sqlx.Tx) *Dao {
dao := New(tx...)
dao.SetSchema(schema)
return dao
}
```

## dao_demo
```
func (dao *Dao) FindById(id int32) *$bean$ {
Expand Down
2 changes: 1 addition & 1 deletion library/mathkit/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GroupNum(sum int, group int) int {
}
}

// 保留小数
// FloatRound 保留小数
func FloatRound(val float64, num int32) float64 {
if math.IsNaN(val) {
return 0
Expand Down
46 changes: 12 additions & 34 deletions mod/common/admindivision/dao/areadao/areadao.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,33 @@
package areadao

import (
"github.com/jmoiron/sqlx"
"github.com/mizuki1412/go-core-kit/class"
"github.com/mizuki1412/go-core-kit/class/exception"
"github.com/mizuki1412/go-core-kit/mod/common/admindivision/model"
"github.com/mizuki1412/go-core-kit/service/sqlkit"
)

/// auto template
type Dao struct {
sqlkit.Dao
sqlkit.Dao[model.Area]
}

func New(schema string, tx ...*sqlkit.Dao) *Dao {
func New(tx ...*sqlx.Tx) *Dao {
dao := &Dao{}
dao.NewHelper(schema, tx...)
return dao
}
func (dao *Dao) scan(sql string, args []any) []*model.Area {
rows := dao.Query(sql, args...)
list := make([]*model.Area, 0, 5)
defer rows.Close()
for rows.Next() {
m := &model.Area{}
err := rows.StructScan(m)
if err != nil {
panic(exception.New(err.Error()))
}
list = append(list, m)
if len(tx) > 0 {
dao.TX = tx[0]
}
return list
return dao
}
func (dao *Dao) scanOne(sql string, args []any) *model.Area {
rows := dao.Query(sql, args...)
defer rows.Close()
for rows.Next() {
m := model.Area{}
err := rows.StructScan(&m)
if err != nil {
panic(exception.New(err.Error()))
}
return &m
}
return nil
func NewWithSchema(schema string, tx ...*sqlx.Tx) *Dao {
dao := New(tx...)
dao.SetSchema(schema)
return dao
}

////

func (dao *Dao) FindById(id class.String) *model.Area {
sql, args := sqlkit.Builder().Select("code,name").From(dao.GetTableD("area")).Where("code=?", id).MustSql()
return dao.scanOne(sql, args)
return dao.ScanOne(sql, args)
}

func (dao *Dao) FindCodeByName(name, ccode, pcode string) string {
Expand All @@ -68,5 +46,5 @@ func (dao *Dao) FindCodeByName(name, ccode, pcode string) string {

func (dao *Dao) ListByCity(id class.String) []*model.Area {
sql, args := sqlkit.Builder().Select("code,name").From(dao.GetTableD("area")).Where("city=?", id).OrderBy("code").MustSql()
return dao.scan(sql, args)
return dao.ScanList(sql, args)
}
46 changes: 12 additions & 34 deletions mod/common/admindivision/dao/citydao/citydao.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,33 @@
package citydao

import (
"github.com/jmoiron/sqlx"
"github.com/mizuki1412/go-core-kit/class"
"github.com/mizuki1412/go-core-kit/class/exception"
"github.com/mizuki1412/go-core-kit/mod/common/admindivision/model"
"github.com/mizuki1412/go-core-kit/service/sqlkit"
)

/// auto template
type Dao struct {
sqlkit.Dao
sqlkit.Dao[model.City]
}

func New(schema string, tx ...*sqlkit.Dao) *Dao {
func New(tx ...*sqlx.Tx) *Dao {
dao := &Dao{}
dao.NewHelper(schema, tx...)
return dao
}
func (dao *Dao) scan(sql string, args []any) []*model.City {
rows := dao.Query(sql, args...)
list := make([]*model.City, 0, 5)
defer rows.Close()
for rows.Next() {
m := &model.City{}
err := rows.StructScan(m)
if err != nil {
panic(exception.New(err.Error()))
}
list = append(list, m)
if len(tx) > 0 {
dao.TX = tx[0]
}
return list
return dao
}
func (dao *Dao) scanOne(sql string, args []any) *model.City {
rows := dao.Query(sql, args...)
defer rows.Close()
for rows.Next() {
m := model.City{}
err := rows.StructScan(&m)
if err != nil {
panic(exception.New(err.Error()))
}
return &m
}
return nil
func NewWithSchema(schema string, tx ...*sqlx.Tx) *Dao {
dao := New(tx...)
dao.SetSchema(schema)
return dao
}

////

func (dao *Dao) FindById(id class.String) *model.City {
sql, args := sqlkit.Builder().Select("*").From(dao.GetTableD("city")).Where("code=?", id).MustSql()
return dao.scanOne(sql, args)
return dao.ScanOne(sql, args)
}

func (dao *Dao) FindCodeByName(name, pcode string) string {
Expand All @@ -68,5 +46,5 @@ func (dao *Dao) FindCodeByName(name, pcode string) string {

func (dao *Dao) ListByProvince(id class.String) []*model.City {
sql, args := sqlkit.Builder().Select("*").From(dao.GetTableD("city")).Where("province=?", id).OrderBy("code").MustSql()
return dao.scan(sql, args)
return dao.ScanList(sql, args)
}
58 changes: 16 additions & 42 deletions mod/common/admindivision/dao/provincedao/provincedao.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,45 @@
package provincedao

import (
"github.com/jmoiron/sqlx"
"github.com/mizuki1412/go-core-kit/class"
"github.com/mizuki1412/go-core-kit/class/exception"
"github.com/mizuki1412/go-core-kit/mod/common/admindivision/dao/citydao"
"github.com/mizuki1412/go-core-kit/mod/common/admindivision/model"
"github.com/mizuki1412/go-core-kit/service/sqlkit"
)

/// auto template
type Dao struct {
sqlkit.Dao
sqlkit.Dao[model.Province]
}

const (
ResultDefault byte = iota
ResultNone
)

func New(schema string, tx ...*sqlkit.Dao) *Dao {
func New(tx ...*sqlx.Tx) *Dao {
dao := &Dao{}
dao.NewHelper(schema, tx...)
return dao
}
func (dao *Dao) cascade(obj *model.Province) {
switch dao.ResultType {
case ResultDefault:
obj.Cities = citydao.New(dao.Schema).ListByProvince(obj.Code)
if len(tx) > 0 {
dao.TX = tx[0]
}
}
func (dao *Dao) scan(sql string, args []any) []*model.Province {
rows := dao.Query(sql, args...)
list := make([]*model.Province, 0, 5)
defer rows.Close()
for rows.Next() {
m := &model.Province{}
err := rows.StructScan(m)
if err != nil {
panic(exception.New(err.Error()))
dao.Cascade = func(obj *model.Province) {
switch dao.ResultType {
case ResultDefault:
obj.Cities = citydao.NewWithSchema(dao.Schema).ListByProvince(obj.Code)
}
list = append(list, m)
}
for i := range list {
dao.cascade(list[i])
}
return list
return dao
}
func (dao *Dao) scanOne(sql string, args []any) *model.Province {
rows := dao.Query(sql, args...)
defer rows.Close()
for rows.Next() {
m := model.Province{}
err := rows.StructScan(&m)
if err != nil {
panic(exception.New(err.Error()))
}
dao.cascade(&m)
return &m
}
return nil
func NewWithSchema(schema string, tx ...*sqlx.Tx) *Dao {
dao := New(tx...)
dao.SetSchema(schema)
return dao
}

////

func (dao *Dao) FindById(id class.String) *model.Province {
sql, args := sqlkit.Builder().Select("*").From(dao.GetTableD("province")).Where("code=?", id).MustSql()
return dao.scanOne(sql, args)
return dao.ScanOne(sql, args)
}

func (dao *Dao) FindCodeByName(name string) string {
Expand All @@ -84,5 +58,5 @@ func (dao *Dao) FindCodeByName(name string) string {

func (dao *Dao) ListAll() []*model.Province {
sql, args := sqlkit.Builder().Select("*").From(dao.GetTableD("province")).OrderBy("code").MustSql()
return dao.scan(sql, args)
return dao.ScanList(sql, args)
}
5 changes: 2 additions & 3 deletions mod/common/admindivision/division_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/mizuki1412/go-core-kit/mod/common/admindivision/dao/provincedao"
"github.com/mizuki1412/go-core-kit/service/restkit/context"
"github.com/mizuki1412/go-core-kit/service/restkit/router"
"github.com/mizuki1412/go-core-kit/service/sqlkit"
)

func Init(router *router.Router) {
Expand All @@ -21,7 +20,7 @@ func Init(router *router.Router) {
}

func ListAllProvinceCity(ctx *context.Context) {
ctx.JsonSuccess(provincedao.New(sqlkit.SchemaDefault).ListAll())
ctx.JsonSuccess(provincedao.New().ListAll())
}

type listAreaParam struct {
Expand All @@ -31,5 +30,5 @@ type listAreaParam struct {
func listArea(ctx *context.Context) {
params := listAreaParam{}
ctx.BindForm(&params)
ctx.JsonSuccess(areadao.New(sqlkit.SchemaDefault).ListByCity(class.String{String: params.CityCode, Valid: true}))
ctx.JsonSuccess(areadao.New().ListByCity(class.String{String: params.CityCode, Valid: true}))
}
28 changes: 10 additions & 18 deletions mod/setting/dao/settingdao/settingdao.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
package settingdao

import (
"github.com/jmoiron/sqlx"
"github.com/mizuki1412/go-core-kit/class/exception"
"github.com/mizuki1412/go-core-kit/library/jsonkit"
"github.com/mizuki1412/go-core-kit/service/sqlkit"
)

/** more_setting: id, data */

/// auto template
type Dao struct {
sqlkit.Dao
sqlkit.Dao[map[string]any]
}

func New(schema string, tx ...*sqlkit.Dao) *Dao {
func New(tx ...*sqlx.Tx) *Dao {
dao := &Dao{}
dao.NewHelper(schema, tx...)
if len(tx) > 0 {
dao.TX = tx[0]
}
return dao
}
func (dao *Dao) scanOne(sql string, args []interface{}) *map[string]interface{} {
rows := dao.Query(sql, args...)
defer rows.Close()
for rows.Next() {
m := map[string]interface{}{}
err := rows.MapScan(m)
if err != nil {
panic(exception.New(err.Error()))
}
return &m
}
return nil
func NewWithSchema(schema string, tx ...*sqlx.Tx) *Dao {
dao := New(tx...)
dao.SetSchema(schema)
return dao
}

////

func (dao *Dao) Set(data map[string]interface{}) {
sql, args, err := sqlkit.Builder().Update(dao.GetTableD("more_setting")).Set("data", jsonkit.ToString(data)).Where("id=?", 1).ToSql()
if err != nil {
Expand Down
Loading

0 comments on commit 4cb5471

Please sign in to comment.