diff --git a/api2sql/execute.go b/api2sql/execute.go index c47cf47..f76a8bf 100644 --- a/api2sql/execute.go +++ b/api2sql/execute.go @@ -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) +} diff --git a/define/api2sql.go b/define/api2sql.go index 5879c6b..10e2476 100644 --- a/define/api2sql.go +++ b/define/api2sql.go @@ -7,6 +7,8 @@ // Date : 2024-08-21 16:05 package define +import "gorm.io/gorm" + // Api2SqlParam 接口转sql的输入配置 // // Author : go_developer@163.com<白茶清欢> @@ -42,6 +44,7 @@ type Api2SqlParam struct { WithCount bool `json:"with_count"` // 是否返回数据总量, 仅 sqlType = list 生效 ValueList []*Api2SqlParamValue `json:"value_list"` // 字段列表 TableColumnConfig []*ColumnInfo `json:"table_column_config"` // 表字段配置 + Tx *gorm.DB `json:"-"` // 前后已有的数据库连接, 直接复用 } // Api2SqlParamValue ... diff --git a/go.mod b/go.mod index 2b93cdd..2565c09 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 toolchain go1.21.3 require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240821082758-8bf75bab08fb + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5 git.zhangdeman.cn/zhangdeman/logger v0.0.0-20240725055115-98eb52ae307a git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd diff --git a/go.sum b/go.sum index 4b1bd57..9dfadee 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240817091513-491f455a23c0 h1:U12XDt git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240817091513-491f455a23c0/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240821082758-8bf75bab08fb h1:dNnVYynCQhLSbtNUVQsvDIthkNgpTrAJF4dAEj08FdE= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240821082758-8bf75bab08fb/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5 h1:pmIHln0gWW+5xAB762h3WDsRkZuYLUDndvJDsGMKoOY= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI= git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda/go.mod h1:dT0rmHcJ9Z9IqWeMIt7YzR88nKkNV2V3dfG0j9Q6lK0= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=