Skip to content

Commit ad10920

Browse files
authored
feat: add queryWrapper (#3)
* feat: add queryWrapper * feat: update gobatis version v0.2.6 to v0.2.7
1 parent 69d6791 commit ad10920

File tree

10 files changed

+380
-12
lines changed

10 files changed

+380
-12
lines changed

go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@ module github.com/acmestack/gobatis-plus
33
go 1.18
44

55
require (
6+
github.com/acmestack/gobatis v0.2.7
7+
github.com/go-sql-driver/mysql v1.6.0
68
github.com/spf13/pflag v1.0.5
79
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7
810
k8s.io/klog/v2 v2.2.0
911
)
1012

1113
require (
14+
github.com/Masterminds/goutils v1.1.1 // indirect
15+
github.com/Masterminds/semver/v3 v3.1.1 // indirect
16+
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
1217
github.com/go-logr/logr v0.2.0 // indirect
18+
github.com/google/uuid v1.1.1 // indirect
19+
github.com/huandu/xstrings v1.3.1 // indirect
20+
github.com/imdario/mergo v0.3.11 // indirect
21+
github.com/mitchellh/copystructure v1.0.0 // indirect
22+
github.com/mitchellh/reflectwalk v1.0.0 // indirect
23+
github.com/shopspring/decimal v1.2.0 // indirect
24+
github.com/spf13/cast v1.3.1 // indirect
25+
github.com/xfali/loadbalance v0.0.1 // indirect
26+
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 // indirect
1327
golang.org/x/mod v0.2.0 // indirect
1428
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8 // indirect
1529
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect

go.sum

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,53 @@
1+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
2+
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
3+
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
4+
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
5+
github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8=
6+
github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
7+
github.com/acmestack/gobatis v0.2.7 h1:aec/toZ5lZTmccI1WQ5jPPQjdITrwbIEPkt5dSajeaE=
8+
github.com/acmestack/gobatis v0.2.7/go.mod h1:vEEXPWzVzeDoFpYD2FoOfGfCyEuLtSiMIbP6jqO44Xg=
9+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
110
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
211
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
312
github.com/go-logr/logr v0.2.0 h1:QvGt2nLcHH0WK9orKa+ppBPAxREcH364nPUedEpK0TY=
413
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
14+
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
15+
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
516
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
617
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
718
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
19+
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
20+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
21+
github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs=
22+
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
23+
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
24+
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
825
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
926
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1027
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
28+
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
29+
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
30+
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
31+
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
32+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
33+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
34+
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
35+
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
36+
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
37+
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
1138
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
1239
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
40+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
41+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
42+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
43+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
44+
github.com/xfali/loadbalance v0.0.1 h1:UVOuuDipJ740KyTaBUxTeW6UEC+fukiQCOn/5YQgexA=
45+
github.com/xfali/loadbalance v0.0.1/go.mod h1:yrzHHRZMdt2wBpLnBDeU2zbjnRYeNvUWV6sq6+3KVG0=
1346
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
1447
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1548
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
49+
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 h1:bXoxMPcSLOq08zI3/c5dEBT6lE4eh+jOh886GHrn6V8=
50+
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
1651
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
1752
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
1853
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -32,7 +67,10 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV
3267
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3368
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3469
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
70+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3571
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
72+
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
73+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3674
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7 h1:RGb68G3yotdQggcyenx9y0+lnVJCXXcLa6geXOMlf5o=
3775
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
3876
k8s.io/klog/v2 v2.2.0 h1:XRvcwJozkgZ1UQJmfMGpvRthQHOvihEhYtDfAaxMz/A=

pkg/constants/keyword.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package constants
2+
3+
const (
4+
And = "and"
5+
Or = "or"
6+
In = "in"
7+
Not = "not"
8+
Like = " like "
9+
Eq = "="
10+
Ne = "<>"
11+
Gt = ">"
12+
Ge = ">="
13+
Lt = "<"
14+
Le = "<="
15+
IsNull = "is null"
16+
IsNotNull = "is not null"
17+
GroupBy = "group by"
18+
Having = "having"
19+
OrderBy = "order by"
20+
Exists = "exists"
21+
Between = "between"
22+
Asc = "asc"
23+
Desc = "desc"
24+
)

pkg/generator/gobatis-gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
)
3434

3535
var (
36-
gobatisImports = []string{"github.com/xfali/gobatis"}
36+
gobatisImports = []string{"github.com/acmestack/gobatis"}
3737
)
3838

3939
type gobatisAnnotion struct {

pkg/mapper/base.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@
1717

1818
package mapper
1919

20-
import "context"
20+
type Base[T any] interface {
21+
Insert(entity T) int64
2122

22-
type BaseMapper[T any] interface {
23-
Insert(ctx context.Context, entity T) int64
23+
InsertBatch(entities ...T) (int64, int64)
2424

25-
InsertBatch(ctx context.Context, entities ...T) (int64, int64)
25+
DeleteById(id any) int64
2626

27-
DeleteById(ctx context.Context, id any) int64
27+
DeleteBatchIds(ids []any) int64
2828

29-
DeleteBatchIds(ctx context.Context, ids []any) int64
29+
UpdateById(entity T) int64
3030

31-
UpdateById(ctx context.Context, entity T) int64
31+
SelectById(id any) T
3232

33-
SelectById(ctx context.Context, id any) T
33+
SelectBatchIds(ids []any) []T
3434

35-
SelectBatchIds(ctx context.Context, ids []any) []T
35+
SelectOne(entity T) T
3636

37-
SelectOne(ctx context.Context, entity T) T
37+
SelectCount(entity T) int64
3838

39-
SelectCount(ctx context.Context, entity T) int64
39+
SelectList(queryWrapper QueryWrapper[T]) ([]T, error)
4040
}

pkg/mapper/base_mapper.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package mapper
2+
3+
import (
4+
"context"
5+
"github.com/acmestack/gobatis"
6+
)
7+
8+
type BaseMapper[T any] struct {
9+
SessMgr *gobatis.SessionManager
10+
Ctx context.Context
11+
Columns []string
12+
}
13+
14+
func (userMapper *BaseMapper[T]) Insert(entity T) int64 {
15+
return 0
16+
}
17+
18+
func (userMapper *BaseMapper[T]) InsertBatch(entities ...T) (int64, int64) {
19+
return 0, 0
20+
}
21+
func (userMapper *BaseMapper[T]) DeleteById(id any) int64 {
22+
return 0
23+
}
24+
func (userMapper *BaseMapper[T]) DeleteBatchIds(ids []any) int64 {
25+
return 0
26+
}
27+
func (userMapper *BaseMapper[T]) UpdateById(entity T) int64 {
28+
return 0
29+
}
30+
func (userMapper *BaseMapper[T]) SelectById(id any) T {
31+
return *new(T)
32+
}
33+
func (userMapper *BaseMapper[T]) SelectBatchIds(ids []any) []T {
34+
var arr []T
35+
return arr
36+
}
37+
func (userMapper *BaseMapper[T]) SelectOne(entity T) T {
38+
return *new(T)
39+
}
40+
func (userMapper *BaseMapper[T]) SelectCount(entity T) int64 {
41+
return 0
42+
}
43+
44+
func (userMapper *BaseMapper[T]) SelectList(queryWrapper *QueryWrapper[T]) ([]T, error) {
45+
if queryWrapper == nil {
46+
queryWrapper = &QueryWrapper[T]{}
47+
queryWrapper.init()
48+
}
49+
sess := userMapper.SessMgr.NewSession()
50+
var arr []T
51+
err := sess.Select(queryWrapper.SqlBuild.String()).Param(queryWrapper.Entity).Result(&arr)
52+
if err != nil {
53+
return nil, err
54+
}
55+
return arr, nil
56+
}

pkg/mapper/query_wrapper.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package mapper
2+
3+
import (
4+
"github.com/acmestack/gobatis-plus/pkg/constants"
5+
"github.com/acmestack/gobatis/builder"
6+
"reflect"
7+
)
8+
9+
type QueryWrapper[T any] struct {
10+
Columns []string
11+
Entity *T
12+
SqlBuild *builder.SQLFragment
13+
TableName string
14+
}
15+
16+
func (queryWrapper *QueryWrapper[T]) Eq(column string, val any) Wrapper[T] {
17+
queryWrapper.setCondition(column, val, constants.Eq)
18+
return queryWrapper
19+
}
20+
21+
func (queryWrapper *QueryWrapper[T]) Ne(column string, val any) Wrapper[T] {
22+
queryWrapper.setCondition(column, val, constants.Ne)
23+
return queryWrapper
24+
}
25+
26+
func (queryWrapper *QueryWrapper[T]) Gt(column string, val any) Wrapper[T] {
27+
queryWrapper.setCondition(column, val, constants.Gt)
28+
return queryWrapper
29+
}
30+
31+
func (queryWrapper *QueryWrapper[T]) Ge(column string, val any) Wrapper[T] {
32+
queryWrapper.setCondition(column, val, constants.Ge)
33+
return queryWrapper
34+
}
35+
36+
func (queryWrapper *QueryWrapper[T]) Lt(column string, val any) Wrapper[T] {
37+
queryWrapper.setCondition(column, val, constants.Lt)
38+
return queryWrapper
39+
}
40+
41+
func (queryWrapper *QueryWrapper[T]) Le(column string, val any) Wrapper[T] {
42+
queryWrapper.setCondition(column, val, constants.Le)
43+
return queryWrapper
44+
}
45+
46+
func (queryWrapper *QueryWrapper[T]) Like(column string, val any) Wrapper[T] {
47+
s := val.(string)
48+
queryWrapper.setCondition(column, "%"+s+"%", constants.Like)
49+
return queryWrapper
50+
}
51+
52+
func (queryWrapper *QueryWrapper[T]) NotLike(column string, val any) Wrapper[T] {
53+
s := val.(string)
54+
queryWrapper.setCondition(column, "%"+s+"%", constants.Not+constants.Like)
55+
return queryWrapper
56+
}
57+
58+
func (queryWrapper *QueryWrapper[T]) LikeLeft(column string, val any) Wrapper[T] {
59+
s := val.(string)
60+
queryWrapper.setCondition(column, "%"+s, constants.Like)
61+
return queryWrapper
62+
}
63+
64+
func (queryWrapper *QueryWrapper[T]) LikeRight(column string, val any) Wrapper[T] {
65+
s := val.(string)
66+
queryWrapper.setCondition(column, s+"%", constants.Like)
67+
return queryWrapper
68+
}
69+
70+
func (queryWrapper *QueryWrapper[T]) And() Wrapper[T] {
71+
queryWrapper.SqlBuild = queryWrapper.SqlBuild.And()
72+
return queryWrapper
73+
}
74+
75+
func (queryWrapper *QueryWrapper[T]) Or() Wrapper[T] {
76+
queryWrapper.SqlBuild = queryWrapper.SqlBuild.Or()
77+
return queryWrapper
78+
}
79+
80+
func (queryWrapper *QueryWrapper[T]) Select(columns ...string) Wrapper[T] {
81+
queryWrapper.SqlBuild.Select(columns...)
82+
return queryWrapper
83+
}
84+
85+
func (queryWrapper *QueryWrapper[T]) init() {
86+
if queryWrapper.Entity == nil {
87+
queryWrapper.Entity = new(T)
88+
}
89+
if queryWrapper.TableName == "" {
90+
queryWrapper.setTableName()
91+
}
92+
}
93+
94+
func (queryWrapper *QueryWrapper[T]) setCondition(column string, val any, conditionType string) {
95+
queryWrapper.init()
96+
entityValueRef := reflect.ValueOf(queryWrapper.Entity).Elem()
97+
entityRef := reflect.TypeOf(queryWrapper.Entity).Elem()
98+
numField := entityRef.NumField()
99+
for i := 0; i < numField; i++ {
100+
field := entityRef.Field(i)
101+
filedName := field.Tag.Get("xfield")
102+
if filedName == column {
103+
setField(entityValueRef, field, val)
104+
}
105+
}
106+
key := getConditionKey(column, entityRef.Name(), conditionType)
107+
queryWrapper.SqlBuild = queryWrapper.SqlBuild.Where(key)
108+
}
109+
110+
func getConditionKey(column string, name string, conditionType string) string {
111+
key := column + conditionType + "#{" + name + "." + column + "}"
112+
return key
113+
}
114+
115+
func setField(entityValueRef reflect.Value, field reflect.StructField, val any) {
116+
ft := field.Type
117+
switch ft.Kind() {
118+
case reflect.String:
119+
entityValueRef.FieldByName(field.Name).SetString(val.(string))
120+
case reflect.Int:
121+
i := val.(int)
122+
entityValueRef.FieldByName(field.Name).SetInt(int64(i))
123+
}
124+
}
125+
126+
func (queryWrapper *QueryWrapper[T]) setTableName() {
127+
// todo The future is through annotations get the tableName
128+
entityRef := reflect.TypeOf(queryWrapper.Entity).Elem()
129+
tableName := entityRef.Field(0).Tag
130+
queryWrapper.TableName = string(tableName)
131+
132+
queryWrapper.checkColumns()
133+
134+
queryWrapper.SqlBuild = builder.Select(queryWrapper.Columns...).From(string(tableName))
135+
}
136+
137+
func (queryWrapper *QueryWrapper[T]) checkColumns() {
138+
if len(queryWrapper.Columns) == 0 {
139+
entityRef := reflect.TypeOf(queryWrapper.Entity).Elem()
140+
numField := entityRef.NumField()
141+
for i := 0; i < numField; i++ {
142+
field := entityRef.Field(i)
143+
filedName := field.Tag.Get("xfield")
144+
if filedName != "" {
145+
queryWrapper.Columns = append(queryWrapper.Columns, filedName)
146+
}
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)