diff --git a/base/resource.go b/base/resource.go index dc38c31..e91b1fe 100644 --- a/base/resource.go +++ b/base/resource.go @@ -116,18 +116,23 @@ func (this Resource) HasField(code string) bool { // 开启事物 func (this Resource) BeginTransaction() (*db.Transaction, error) { - return this.GetDB().Connection.BeginTransaction() + return this.GetDBConn().BeginTransaction() } -// 获取资源对应的数据库连接 -func (this Resource) GetDB() *db.Builder { +// 获取资源链接 +func (this Resource) GetDBConn() *db.Connection { db := do.MustInvoke[db.DB](this.container) if this.IsSystem() { - return db.Connection("service-support").Query() + return db.Connection("service-support") } - return db.Connection("default").Query() + return db.Connection("default") +} + +// 获取资源对应的数据库连接 +func (this Resource) GetDBBuilder() *db.Builder { + return this.GetDBConn().Query() } // 获取资源对应的数据表 @@ -140,7 +145,7 @@ func (this Resource) GetTable() db.Expression { } func (this Resource) GetDBDriver() string { - return this.GetDB().Connection.GetConfig().Driver + return this.GetDBConn().GetConfig().Driver } func (this Resource) GetAuthDBTable(u contracts.User, params ...any) *db.Builder { @@ -158,7 +163,7 @@ func (this Resource) GetAuthDBTable(u contracts.User, params ...any) *db.Builder // GetDBTable("Test", contracts.User) func (this Resource) GetDBTable(params ...any) *db.Builder { - builder := this.GetDB() + builder := this.GetDBBuilder() var user contracts.User alias := this.Code @@ -225,7 +230,7 @@ func (this Resource) WithRolesCondition(b *db.Builder, t string, u contracts.Use items := do.MustInvoke[GetResRoles](this.container)(this.GetUuid(), u.Roles()...) subTables := lo.Reduce(items, func(carry string, item ResRole, _ int) string { - db := this.GetDB().Table(string(this.GetTable()), this.GetCode()).Select(db.Raw("`" + this.GetCode() + "`.*")) + db := this.GetDBBuilder().Table(string(this.GetTable()), this.GetCode()).Select(db.Raw("`" + this.GetCode() + "`.*")) joins := lo.Filter(GetResRelations(item.Uuid), func(item ResRelation, _ int) bool { return item.Type == "inner" || item.Type == "left" || item.Type == "right" diff --git a/resource.go b/resource.go index 4ead747..9f69630 100644 --- a/resource.go +++ b/resource.go @@ -47,8 +47,9 @@ type Resource interface { BeginTransaction() (*db.Transaction, error) - GetDB() *db.Builder GetTable() db.Expression + GetDBConn() *db.Connection + GetDBBuilder() *db.Builder GetDBTable(params ...any) *db.Builder GetAuthDBTable(u User, params ...any) *db.Builder GetStruct(extends ...reflect.StructField) any