Skip to content

Commit

Permalink
Merge pull request #239 from wubin1989/main
Browse files Browse the repository at this point in the history
add BuildWhereClause api
  • Loading branch information
wubin1989 authored Aug 4, 2024
2 parents 7655980 + 9ff695f commit ac31b85
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
32 changes: 32 additions & 0 deletions toolkit/pagination/gorm/paginate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ResponseContext interface {
Fields([]string) ResponseContext
Distinct([]string) ResponseContext
Response(interface{}) Page
BuildWhereClause() (string, []interface{})
Error() error
}

Expand Down Expand Up @@ -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 {
Expand Down
87 changes: 87 additions & 0 deletions toolkit/pagination/gorm/paginate_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit ac31b85

Please sign in to comment.