1 Star 0 Fork 0

go-genie/sqlx-pg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
kvcondition.go 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
lijun 提交于 2024-02-20 11:16 . feat:modify import path
package tag
import (
"gitee.com/go-genie/kvcondition"
"gitee.com/go-genie/sqlx"
"gitee.com/go-genie/sqlx-pg/pgbuilder"
"gitee.com/go-genie/sqlx/builder"
)
func SelectByKVCondition(db sqlx.DBExecutor, kvc kvcondition.KVCondition, modelWithTag ModelWithTag, idCol *builder.Column) *pgbuilder.StmtSelect {
if kvc.Node == nil {
return nil
}
s := pgbuilder.Use(db)
keyCol := modelWithTag.FieldKey()
valueCol := modelWithTag.FieldValue()
var visit func(node kvcondition.Node) builder.SqlCondition
visit = func(node kvcondition.Node) builder.SqlCondition {
switch n := node.(type) {
case *kvcondition.Condition:
switch n.Operator {
case kvcondition.ConditionOperatorOR:
return builder.Or(
idCol.In(s.Select(idCol).From(modelWithTag).Where(visit(n.Left))),
visit(n.Right),
)
case kvcondition.ConditionOperatorAND:
return builder.And(
idCol.In(s.Select(idCol).From(modelWithTag).Where(visit(n.Left))),
visit(n.Right),
)
}
return nil
case *kvcondition.Rule:
k := string(n.Key)
v := string(n.Value)
switch n.Operator {
case kvcondition.OperatorExists:
return keyCol.Eq(k)
case kvcondition.OperatorEqual:
return builder.And(
keyCol.Eq(k),
valueCol.Eq(v),
)
case kvcondition.OperatorNotEqual:
return builder.And(
keyCol.Eq(k),
valueCol.Neq(v),
)
case kvcondition.OperatorContains:
return builder.And(
keyCol.Eq(k),
valueCol.Like(v),
)
case kvcondition.OperatorStartsWith:
return builder.And(
keyCol.Eq(k),
valueCol.RightLike(v),
)
case kvcondition.OperatorEndsWith:
return builder.And(
keyCol.Eq(k),
valueCol.LeftLike(v),
)
}
return nil
}
return nil
}
return s.Select(idCol).From(modelWithTag).Where(visit(kvc.Node))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/go-genie/sqlx-pg.git
git@gitee.com:go-genie/sqlx-pg.git
go-genie
sqlx-pg
sqlx-pg
v1.0.6

搜索帮助

0d507c66 1850385 C8b1a773 1850385