[feat] add func GetDBConn() *db.Connection

This commit is contained in:
what 2023-12-04 14:57:59 +08:00
parent 640a878778
commit 1f119cf945
2 changed files with 15 additions and 9 deletions

View File

@ -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"

View File

@ -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