重构: 虚拟资源改用 NewVirtualResource 显式构造,移除 WithVirtual

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)两种
返回值。
This commit is contained in:
2026-08-20 15:58:03 +08:00
parent acb55bb53a
commit 5cdf1cc00d
4 changed files with 46 additions and 15 deletions
+4 -5
View File
@@ -565,14 +565,13 @@ func (t *hooksTest) TestVirtualResource_SkipsRolesConditionAndOnAfter() {
}, nil
})
res := resx.New(app, "User", "users",
resx.WithConn("default"),
resx.WithVirtual(true),
baseRes := resx.New(app, "User", "users", resx.WithConn("default"))
res := resx.NewVirtualResource(baseRes, "User", db.From(db.T("users")),
resx.WithFields(resx.NewResField("name", "User", resx.FieldWithName("姓名"), resx.FieldWithDataType(req.ResString))),
)
// 实际生成的 SQL(虚拟资源用字面量拼表名,且没有 owned_user 权限条件):
// UPDATE (users) AS `User` SET `name`='新名字' WHERE (`id` = 1)
// 实际生成的 SQL(虚拟资源拿子查询拼表名,且没有 owned_user 权限条件):
// UPDATE (SELECT * FROM `users`) AS `User` SET `name`='新名字' WHERE (`id` = 1)
sql, _, _ := res.GetDBTable(base.GetAnonymous()).Update().
Set(db.Record{"name": "新名字"}).
Where(db.C("id").Eq(1)).