Skip to content

Commit

Permalink
v2.0.4: add wherePGArrayIn, squirrel.or
Browse files Browse the repository at this point in the history
  • Loading branch information
mizuki1412 committed Feb 22, 2024
1 parent b9fb37e commit 7cf6185
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
25 changes: 25 additions & 0 deletions doc/go-core-kit-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,31 @@ if len(param.Departments) > 0 {
- update set时:`Set("extend",squirrel.Expr("'{}'::jsonb"))` or `Set("extend","{}")`
- class.mapString在插入数据库时将用jsonb格式,并且不是完全替换,而是merge的方式(```coalesce(extend, '{}'::jsonb) || '$param'::jsonb```)。如果要删除其中的key,需要设置key为null。 merge时只会merge顶层的keys。

## code demo

```go
builder := dao.Select().OrderByDesc("dt")
if len(param.Tags) > 0 {
builder = builder.WherePGArrayIn("tags", param.Tags)
}
if !param.Start.IsZero() {
builder = builder.Where("dt>=?", param.Start)
}
if !param.End.IsZero() {
builder = builder.Where(squirrel.Lt{"dt": param.End})
}
if param.Search != "" {
builder = builder.Where(squirrel.Or{
squirrel.Like{"content": "%" + param.Search + "%"},
squirrel.Like{"remark": "%" + param.Search + "%"},
})
}

// set expr
dao.Update().Set("phone", squirrel.Expr("null")).Set("username", squirrel.Expr("null")).Where("id=?", id).Exec()

```

# 框架内可配置函数或变量

## restkit
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/Masterminds/squirrel"
"github.com/mizuki1412/go-core-kit/v2/cli"
"github.com/mizuki1412/go-core-kit/v2/cmd"
"github.com/mizuki1412/go-core-kit/v2/mod/common/admindivision"
Expand All @@ -27,6 +28,12 @@ func main() {
Use: "test",
Run: func(cmd *cobra.Command, args []string) {
//snippet.RESPTest()
f := "bb"
s, _, _ := squirrel.Select("*").Where(squirrel.GtOrEq{"a": 1}).Where(squirrel.Or{
squirrel.NotEq{"b": "abc"},
squirrel.Like{f: "%abc"},
}).ToSql()
println(s)
},
})
cli.AddChildCMD(cmd.FrontDaoCMDNext("http://localhost:10000/v3/api-docs"))
Expand Down
5 changes: 3 additions & 2 deletions mod/user/dao/departmentdao/departmentdao.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package departmentdao

import (
"github.com/Masterminds/squirrel"
"github.com/mizuki1412/go-core-kit/v2/mod/user/model"
"github.com/mizuki1412/go-core-kit/v2/service/sqlkit"
)
Expand Down Expand Up @@ -41,9 +42,9 @@ func New(cascadeType byte, ds ...*sqlkit.DataSource) Dao {
}

func (dao Dao) ListByParent(id int32) []*model.Department {
return dao.Select().Where("parent=?", id).OrderBy("no").OrderBy("id").List()
return dao.Select().Where(squirrel.Eq{"parent": id}).OrderBy("no").OrderBy("id").List()
}

func (dao Dao) ListAll() []*model.Department {
return dao.Select().Where("id>0").OrderBy("parent").OrderBy("no").OrderBy("id").List()
return dao.Select().Where(squirrel.Gt{"id": 0}).OrderBy("parent").OrderBy("no").OrderBy("id").List()
}
3 changes: 2 additions & 1 deletion mod/user/dao/roledao/roledao.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package roledao

import (
"fmt"
"github.com/Masterminds/squirrel"
"github.com/mizuki1412/go-core-kit/v2/mod/user/dao/departmentdao"
"github.com/mizuki1412/go-core-kit/v2/mod/user/model"
"github.com/mizuki1412/go-core-kit/v2/service/sqlkit"
Expand Down Expand Up @@ -32,7 +33,7 @@ func New(cascadeType byte, ds ...*sqlkit.DataSource) Dao {
}

func (dao Dao) FindByName(name string) *model.Role {
return dao.Select().Where("name=?", name).Limit(1).One()
return dao.Select().Where(squirrel.Eq{"name": name}).Limit(1).One()
}
func (dao Dao) ListFromRootDepart(id int32) []*model.Role {
where := fmt.Sprintf(`id>0 and department in ( with recursive t(id) as( values(%d) union all select d.id from %s d, t where t.id=d.parent) select id from t )`, id, departmentdao.New(departmentdao.ResultDefault, dao.DataSource()).Table())
Expand Down
24 changes: 22 additions & 2 deletions service/sqlkit/dao_builder_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (dao SelectDao[T]) resetColumns(fields ...string) SelectDao[T] {

func (dao SelectDao[T]) whereNLogicDel() SelectDao[T] {
if dao.modelMeta.logicDelKey.Key != "" {
return dao.Where(dao.modelMeta.logicDelKey.Key+"<>?", dao.LogicDelVal[0])
return dao.Where(squirrel.NotEq{dao.modelMeta.logicDelKey.Key: dao.LogicDelVal[0]})
}
return dao
}
Expand All @@ -225,10 +225,30 @@ func (dao SelectDao[T]) WhereUnnestNotIn(key string, arr any) SelectDao[T] {
return dao.whereUnnest(arr, key, "NOT IN")
}

// WherePGArrayIn 用于PG中array类型数据的包含比较
func (dao SelectDao[T]) WherePGArrayIn(key string, arr any) SelectDao[T] {
switch dao.dataSource.Driver {
case sqlconst.Postgres:
s, v := pgArray(arr)
return dao.Where(fmt.Sprintf("%s @> %s", dao.modelMeta.escapeName(key), s), v...)
default:
panic(exception.New("WherePGArrayIn not supported"))
}
}
func (dao SelectDao[T]) WherePGArrayNotIn(key string, arr any) SelectDao[T] {
switch dao.dataSource.Driver {
case sqlconst.Postgres:
s, v := pgArray(arr)
return dao.Where(fmt.Sprintf("not (%s @> %s)", dao.modelMeta.escapeName(key), s), v...)
default:
panic(exception.New("WherePGArrayNotIn not supported"))
}
}

func (dao SelectDao[T]) WhereIn(key string, sub SubQueryInterface) SelectDao[T] {
sql, args := sub.sqlOriginPlaceholder()
return dao.Where(squirrel.Expr(key+" IN ("+sql+")", args...))
}
func (dao SelectDao[T]) WhereLike(field string, val string) SelectDao[T] {
return dao.Where(field+" LIKE ?", "%"+val+"%")
return dao.Where(squirrel.Like{field: "%" + val + "%"})
}
1 change: 1 addition & 0 deletions service/sqlkit/dialect_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
)

// 生成pg的array表达式
func pgArray(arr any) (string, []any) {
var suffix string
var args []any
Expand Down

0 comments on commit 7cf6185

Please sign in to comment.