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 时一起解决。
54 lines
904 B
Go
54 lines
904 B
Go
package condition
|
|
|
|
import (
|
|
"git.fsdpf.net/go/reflux/valuex"
|
|
"git.fsdpf.net/go/req"
|
|
)
|
|
|
|
type TokenValue interface {
|
|
GetParam(k string) valuex.Accessor
|
|
User() req.User
|
|
}
|
|
|
|
type tValueOpt func(option *tValue)
|
|
|
|
func WithUser(user req.User) tValueOpt {
|
|
return func(option *tValue) {
|
|
option.user = user
|
|
}
|
|
}
|
|
|
|
func NewTokenValue(x valuex.Accessor, opts ...tValueOpt) TokenValue {
|
|
tv := &tValue{src: x}
|
|
if x == nil {
|
|
tv.src = valuex.Nil
|
|
} else if up, ok := x.(interface{ User() req.User }); ok {
|
|
tv.user = up.User()
|
|
}
|
|
for _, opt := range opts {
|
|
if opt != nil {
|
|
opt(tv)
|
|
}
|
|
}
|
|
if tv.user == nil {
|
|
panic("user is required")
|
|
}
|
|
return tv
|
|
}
|
|
|
|
type tValue struct {
|
|
src valuex.Accessor
|
|
user req.User
|
|
}
|
|
|
|
func (this *tValue) GetParam(k string) valuex.Accessor {
|
|
if v, ok := this.src.Lookup(k); ok {
|
|
return v
|
|
}
|
|
return valuex.Nil
|
|
}
|
|
|
|
func (this *tValue) User() req.User {
|
|
return this.user
|
|
}
|