增加获取数据库连接的方法

This commit is contained in:
2024-08-23 12:26:03 +08:00
parent 21ad50b273
commit e5233b4fae
4 changed files with 29 additions and 18 deletions

View File

@ -14,7 +14,7 @@ import (
"git.zhangdeman.cn/zhangdeman/database/abstract"
"git.zhangdeman.cn/zhangdeman/database/define"
"git.zhangdeman.cn/zhangdeman/wrapper"
"strings"
"gorm.io/gorm"
)
var (
@ -72,17 +72,7 @@ func (e *execute) Run(ctx context.Context, inputParam *define.Api2SqlParam) (any
//
// Date : 20:52 2024/8/21
func (e *execute) List(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
replaceTable := map[string]string{
"{FIELD_LIST}": "`" + strings.Join(inputParam.ColumnList, "` , `"),
"{TABLE}": inputParam.Table,
}
sqlTplList := []string{
define.SqlSelectBaseTpl,
}
if len(inputParam.ValueList) > 0 {
// where 条件
sqlTplList = append(sqlTplList, define.SqlWhereTpl)
}
return nil, nil
}
@ -191,11 +181,11 @@ func (e *execute) formatAndValidateInputParam(inputParam *define.Api2SqlParam) e
//
// Date : 11:48 2024/8/22
func (e *execute) validateColumn(inputParam *define.Api2SqlParam) error {
if len(inputParam.ColumnList) == 0 && wrapper.ArrayType[string]([]string{
if len(inputParam.ColumnTable) == 0 && wrapper.ArrayType[string]([]string{
consts.SqlTypeList, consts.SqlTypeDetail,
}).Has(inputParam.SqlType) >= 0 {
for _, itemParam := range inputParam.TableColumnConfig {
inputParam.ColumnList = append(inputParam.ColumnList, itemParam.ColumnName)
inputParam.ColumnTable[itemParam.ColumnName] = itemParam.ColumnName
}
}
// 验证字段是否都正确
@ -203,9 +193,9 @@ func (e *execute) validateColumn(inputParam *define.Api2SqlParam) error {
for _, itemColumn := range inputParam.TableColumnConfig {
tableColumnTable[itemColumn.ColumnName] = true
}
for _, item := range inputParam.ColumnList {
if !tableColumnTable[item] {
return errors.New(item + " : input column not found in table column list")
for columnName, _ := range inputParam.ColumnTable {
if !tableColumnTable[columnName] {
return errors.New(columnName + " : input column not found in table column list")
}
}
for _, item := range inputParam.ValueList {
@ -215,3 +205,19 @@ func (e *execute) validateColumn(inputParam *define.Api2SqlParam) error {
}
return nil
}
// getTx 获取数据库连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:20 2024/8/23
func (e *execute) getTx(ctx context.Context, inputParam *define.Api2SqlParam) (*gorm.DB, error) {
if nil != inputParam.Tx {
return inputParam.Tx, nil
}
if inputParam.ForceMaster || // 强制操作主库
wrapper.ArrayType[string]([]string{consts.SqlTypeDelete, consts.SqlTypeInsert, consts.SqlTypeUpdate}).Has(inputParam.SqlType) >= 0 { // 写操作
return e.databaseClientManager.GetMasterClient(ctx, inputParam.DatabaseFlag)
}
return e.databaseClientManager.GetSlaveClient(ctx, inputParam.DatabaseFlag)
}