重构: Condition/Engine 迁移到 db-v2 的表达式树,替换掉旧的 db.Raw 拼字符串
ToSql 之前是手工拼接 SQL 字符串(string 类型的 conditions、手动加括号、 手动拼 AND/OR),改成用 exp.NewExpressionList 构造表达式树,跟 db-v2 其余部分统一走"先建表达式树、由方言渲染成 SQL"这条路,不再自己维护一份 字符串拼接逻辑。ConditionType/ConditionOperator 相应改成 exp.ExpressionListType/ exp.BooleanOperation 的类型别名。 TokenValue 从 condition_expr.go 里独立成 token_value.go,从 GetParam(k string) req.GlobalParams 改成 GetParam(k string) valuex.Accessor (配合 reflux/valuex),跟其它 "-v2" 项目的属性访问方式统一。engine 包 配合适配(sqlite3 用 db/engine 的新连接管理,SelectDataset 替代 db.Connection),删掉了不再需要的 EngineParam(老 GlobalParams 包装, 新 TokenValue 已经不需要这层)。 已知问题:engine.go 在没有外部传入 GlobalParams 时用 nil user 构造兜底 值,跟 NewTokenValue 现在强制要求非 nil user 冲突,TestEngine 会 panic ——这个先不修,等后面调整 Engine.Execute 让调用方能传 user 时一起解决。
This commit is contained in:
+14
-22
@@ -5,15 +5,16 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.fsdpf.net/go/db"
|
||||
"git.fsdpf.net/go/db/exp"
|
||||
"git.fsdpf.net/go/utils"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type ConditionType string
|
||||
type ConditionType = exp.ExpressionListType
|
||||
|
||||
const (
|
||||
OR ConditionType = "OR"
|
||||
AND ConditionType = "AND"
|
||||
AND ConditionType = exp.AndType
|
||||
OR ConditionType = exp.OrType
|
||||
)
|
||||
|
||||
type Condition struct {
|
||||
@@ -92,44 +93,35 @@ func (this Condition) IsAlwaysRight() bool {
|
||||
return !lo.Some(flag, []bool{false})
|
||||
}
|
||||
|
||||
// 生成 SQL 语句
|
||||
// ToSql 生成 SQL 语句
|
||||
func (this Condition) ToSql(m TokenValue) db.Expression {
|
||||
conditions := []string{}
|
||||
conditions := []exp.Expression{}
|
||||
// 表达式
|
||||
for _, item := range this.exprs {
|
||||
if item.IsIgnoreEmptyParma(m) {
|
||||
continue
|
||||
}
|
||||
conditions = append(conditions, string(item.ToSql(m)))
|
||||
conditions = append(conditions, item.ToSql(m))
|
||||
}
|
||||
// 条件子集
|
||||
for _, item := range this.childrens {
|
||||
conditions = append(conditions, string(item.ToSql(m)))
|
||||
conditions = append(conditions, item.ToSql(m))
|
||||
}
|
||||
// 去除无效的表达式
|
||||
conditions = lo.Filter(conditions, func(item string, _ int) bool { return item != "" })
|
||||
// 组合 SQL
|
||||
sql := strings.Join(conditions, " "+string(this.Type())+" ")
|
||||
|
||||
if sql == "" {
|
||||
if this.IsEmpty() {
|
||||
if this.parent == nil && this.Type() == OR {
|
||||
return db.Raw("false")
|
||||
return db.V(false)
|
||||
} else if this.parent == nil && this.Type() == AND {
|
||||
return db.Raw("true")
|
||||
return db.V(true)
|
||||
}
|
||||
return db.Raw("")
|
||||
}
|
||||
|
||||
// 包裹 SQL, 避免语法表达错误
|
||||
if this.parent == nil || this.Type() == OR {
|
||||
sql = "(" + sql + ")"
|
||||
return nil
|
||||
}
|
||||
|
||||
if this.describe != "" {
|
||||
sql += "/*" + this.describe + "*/"
|
||||
return db.L("? /* ? */", exp.NewExpressionList(this.typ, conditions...), db.L(this.describe))
|
||||
}
|
||||
|
||||
return db.Raw(sql)
|
||||
return exp.NewExpressionList(this.typ, conditions...)
|
||||
}
|
||||
|
||||
// 设置条件表达式
|
||||
|
||||
Reference in New Issue
Block a user