WithVirtual(bool) 只是给 New() 打一个标记位,虚拟资源需要的表达式(子查询)没有 承载的地方,之前借用 table string 字段硬拼字符串。改成 NewVirtualResource(res, code, table, opts...) 专门构造:table 参数直接接收 exp.SQLExpression, GetTableExpr 对虚拟资源直接用它生成子查询表达式,不用再手工拼括号和字符串; container/conn/historyRoles 从传入的父资源上直接复用。 GetTableExpr 返回值类型从 exp.LiteralExpression 放宽成 exp.Aliaseable, 兼容 db.T(...)(IdentifierExpression)和 db.L(...)(LiteralExpression)两种 返回值。
34 lines
679 B
Go
34 lines
679 B
Go
package resx
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"git.fsdpf.net/go/db/exp"
|
|
"git.fsdpf.net/go/req"
|
|
)
|
|
|
|
func NewVirtualResource(res req.Resource, code string, table exp.SQLExpression, opts ...Option) req.Resource {
|
|
pRes, ok := res.(*resource)
|
|
if !ok {
|
|
panic("resx.NewVirtualResource: res 必须是 resx.New 构造出来的 req.Resource")
|
|
}
|
|
|
|
r := &resource{
|
|
container: pRes.container,
|
|
conn: pRes.conn,
|
|
historyRoles: pRes.historyRoles,
|
|
initOnce: &sync.Once{},
|
|
uuid: code,
|
|
code: code,
|
|
name: code,
|
|
table: code,
|
|
isResVirtual: true,
|
|
virtualTable: table,
|
|
primarykey: "id",
|
|
}
|
|
for _, opt := range opts {
|
|
opt(r)
|
|
}
|
|
return r
|
|
}
|