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

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/abstract"
"git.zhangdeman.cn/zhangdeman/database/define" "git.zhangdeman.cn/zhangdeman/database/define"
"git.zhangdeman.cn/zhangdeman/wrapper" "git.zhangdeman.cn/zhangdeman/wrapper"
"strings" "gorm.io/gorm"
) )
var ( var (
@ -72,17 +72,7 @@ func (e *execute) Run(ctx context.Context, inputParam *define.Api2SqlParam) (any
// //
// Date : 20:52 2024/8/21 // Date : 20:52 2024/8/21
func (e *execute) List(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) { 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 return nil, nil
} }
@ -191,11 +181,11 @@ func (e *execute) formatAndValidateInputParam(inputParam *define.Api2SqlParam) e
// //
// Date : 11:48 2024/8/22 // Date : 11:48 2024/8/22
func (e *execute) validateColumn(inputParam *define.Api2SqlParam) error { 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, consts.SqlTypeList, consts.SqlTypeDetail,
}).Has(inputParam.SqlType) >= 0 { }).Has(inputParam.SqlType) >= 0 {
for _, itemParam := range inputParam.TableColumnConfig { 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 { for _, itemColumn := range inputParam.TableColumnConfig {
tableColumnTable[itemColumn.ColumnName] = true tableColumnTable[itemColumn.ColumnName] = true
} }
for _, item := range inputParam.ColumnList { for columnName, _ := range inputParam.ColumnTable {
if !tableColumnTable[item] { if !tableColumnTable[columnName] {
return errors.New(item + " : input column not found in table column list") return errors.New(columnName + " : input column not found in table column list")
} }
} }
for _, item := range inputParam.ValueList { for _, item := range inputParam.ValueList {
@ -215,3 +205,19 @@ func (e *execute) validateColumn(inputParam *define.Api2SqlParam) error {
} }
return nil 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)
}

View File

@ -7,6 +7,8 @@
// Date : 2024-08-21 16:05 // Date : 2024-08-21 16:05
package define package define
import "gorm.io/gorm"
// Api2SqlParam 接口转sql的输入配置 // Api2SqlParam 接口转sql的输入配置
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
@ -42,6 +44,7 @@ type Api2SqlParam struct {
WithCount bool `json:"with_count"` // 是否返回数据总量, 仅 sqlType = list 生效 WithCount bool `json:"with_count"` // 是否返回数据总量, 仅 sqlType = list 生效
ValueList []*Api2SqlParamValue `json:"value_list"` // 字段列表 ValueList []*Api2SqlParamValue `json:"value_list"` // 字段列表
TableColumnConfig []*ColumnInfo `json:"table_column_config"` // 表字段配置 TableColumnConfig []*ColumnInfo `json:"table_column_config"` // 表字段配置
Tx *gorm.DB `json:"-"` // 前后已有的数据库连接, 直接复用
} }
// Api2SqlParamValue ... // Api2SqlParamValue ...

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.3 toolchain go1.21.3
require ( 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/logger v0.0.0-20240725055115-98eb52ae307a
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd

2
go.sum
View File

@ -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-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 h1:dNnVYynCQhLSbtNUVQsvDIthkNgpTrAJF4dAEj08FdE=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240821082758-8bf75bab08fb/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= 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 h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI=
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda/go.mod h1:dT0rmHcJ9Z9IqWeMIt7YzR88nKkNV2V3dfG0j9Q6lK0= 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= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=