From 9ff695ffb8523f7fbc6d39d6349a76504f2b2556 Mon Sep 17 00:00:00 2001 From: wubin1989 <328454505@qq.com> Date: Mon, 5 Aug 2024 00:00:30 +0800 Subject: [PATCH] add BuildWhereClause api --- go.mod | 1 + go.sum | 3 + toolkit/pagination/gorm/paginate.go | 32 +++++++++ toolkit/pagination/gorm/paginate_test.go | 87 ++++++++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 toolkit/pagination/gorm/paginate_test.go diff --git a/go.mod b/go.mod index ab8da28c..e0a603f8 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( ) require ( + github.com/DATA-DOG/go-sqlmock v1.5.2 github.com/XiaoMi/pegasus-go-client v0.0.0-20220519103347-ba0e68465cd5 github.com/allegro/bigcache/v3 v3.1.0 github.com/auxten/postgresql-parser v1.0.1 diff --git a/go.sum b/go.sum index 1bf28cda..0125ad78 100644 --- a/go.sum +++ b/go.sum @@ -631,6 +631,8 @@ github.com/ClickHouse/clickhouse-go/v2 v2.23.2 h1:+DAKPMnxLS7pduQZsrJc8OhdLS2L9M github.com/ClickHouse/clickhouse-go/v2 v2.23.2/go.mod h1:aNap51J1OM3yxQJRgM+AlP/MPkGBCL8A74uQThoQhR0= github.com/CloudyKit/fastprinter v0.0.0-20170127035650-74b38d55f37a/go.mod h1:EFZQ978U7x8IRnstaskI3IysnWY5Ao3QgZUKOXlsAdw= github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w= +github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= +github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM= github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= @@ -1284,6 +1286,7 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE= github.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE= github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= diff --git a/toolkit/pagination/gorm/paginate.go b/toolkit/pagination/gorm/paginate.go index 380a86f9..cd64f325 100644 --- a/toolkit/pagination/gorm/paginate.go +++ b/toolkit/pagination/gorm/paginate.go @@ -25,6 +25,7 @@ type ResponseContext interface { Fields([]string) ResponseContext Distinct([]string) ResponseContext Response(interface{}) Page + BuildWhereClause() (string, []interface{}) Error() error } @@ -275,6 +276,37 @@ func (r *resContext) Response(res interface{}) Page { return page } +func (r *resContext) BuildWhereClause() (statement string, args []interface{}) { + p := r.Pagination + query := r.Statement + p.Config = defaultConfig(p.Config) + p.Config.Statement = query.Statement + if p.Config.DefaultSize == 0 { + p.Config.DefaultSize = 10 + } + + if p.Config.FieldWrapper == "" && p.Config.ValueWrapper == "" { + defaultWrapper := "LOWER(%s)" + wrappers := map[string]string{ + "sqlite": defaultWrapper, + "mysql": defaultWrapper, + "postgres": "LOWER((%s)::text)", + } + p.Config.FieldWrapper = defaultWrapper + if wrapper, ok := wrappers[query.Dialector.Name()]; ok { + p.Config.FieldWrapper = wrapper + } + } + + pr, err := parseRequest(r.Parameter, *p.Config) + if err != nil { + r.error = err + return "", nil + } + causes := createCauses(pr) + return causes.WhereString, causes.Params +} + // New Pagination instance func New(params ...interface{}) *Pagination { if len(params) >= 1 { diff --git a/toolkit/pagination/gorm/paginate_test.go b/toolkit/pagination/gorm/paginate_test.go new file mode 100644 index 00000000..e1bf1e96 --- /dev/null +++ b/toolkit/pagination/gorm/paginate_test.go @@ -0,0 +1,87 @@ +package gorm + +import ( + "fmt" + "github.com/DATA-DOG/go-sqlmock" + "github.com/wubin1989/gorm" + "github.com/wubin1989/postgres" + "testing" +) + +type Parameter struct { + Page int64 `json:"page" form:"page"` + Size int64 `json:"size" form:"size"` + Sort string `json:"sort" form:"sort"` + Order string `json:"order" form:"order"` + Fields string `json:"fields" form:"fields"` + Filters string `json:"filters" form:"filters"` +} + +func (receiver Parameter) GetPage() int64 { + return receiver.Page +} + +func (receiver Parameter) GetSize() int64 { + return receiver.Size +} + +func (receiver Parameter) GetSort() string { + return receiver.Sort +} + +func (receiver Parameter) GetOrder() string { + return receiver.Order +} + +func (receiver Parameter) GetFields() string { + return receiver.Fields +} + +func (receiver Parameter) GetFilters() interface{} { + return receiver.Filters +} + +func (receiver Parameter) IParameterInstance() { + +} + +func Test_resContext_BuildWhereClause(t *testing.T) { + pg := New(&Config{ + FieldSelectorEnabled: true, + }) + filters := make([][]interface{}, 0) + filters = append(filters, []interface{}{ + "sys_role_id", + "=", + 123, + }) + filters = append(filters, []interface{}{ + "and", + }) + filters = append(filters, []interface{}{ + "deleted_at", + "is", + "null", + }) + filterContent, _ := json.Marshal(filters) + mockDB, _, _ := sqlmock.New() + dialector := postgres.New(postgres.Config{ + Conn: mockDB, + DriverName: "postgres", + }) + db, _ := gorm.Open(dialector, &gorm.Config{}) + resCxt := pg.With(db).Request(Parameter{ + Page: 0, + Size: 0, + Sort: "", + Order: "", + Fields: "", + Filters: string(filterContent), + }) + statement, args := resCxt.BuildWhereClause() + if resCxt.Error() != nil { + panic(resCxt.Error()) + } + fmt.Println(statement) + fmt.Println(args) +}