增加InputParam验证
This commit is contained in:
@ -13,6 +13,7 @@ import (
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/database/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/database/define"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -121,5 +122,69 @@ func (e *execute) Delete(ctx context.Context, inputParam *define.Api2SqlParam) (
|
||||
//
|
||||
// Date : 20:51 2024/8/21
|
||||
func (e *execute) Express(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
|
||||
if nil == e.databaseClientManager {
|
||||
return nil, errors.New("database client is nil, please use `SetDatabaseClientManager` set instance")
|
||||
}
|
||||
// 格式化 inputParam
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// formatAndValidateInputParam 格式化并校验输入参数
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:38 2024/8/22
|
||||
func (e *execute) formatAndValidateInputParam(inputParam *define.Api2SqlParam) error {
|
||||
if nil == inputParam {
|
||||
return errors.New("inputParam is nil")
|
||||
}
|
||||
if len(inputParam.DatabaseFlag) == 0 {
|
||||
return errors.New("databaseFlag is empty")
|
||||
}
|
||||
if len(inputParam.Table) == 0 {
|
||||
return errors.New("table is empty")
|
||||
}
|
||||
if len(inputParam.SqlType) == 0 {
|
||||
return errors.New("sqlType is empty")
|
||||
}
|
||||
databaseClient, err := e.databaseClientManager.GetDBClient(inputParam.DatabaseFlag)
|
||||
if nil != err {
|
||||
return errors.New(inputParam.DatabaseFlag + " : database client get fail -> " + err.Error())
|
||||
}
|
||||
// 尝试获取表结构, api 转 sql 要求必须开启表结构缓存, 否则无法确定相关数据如何解析
|
||||
if inputParam.TableColumnConfig, err = databaseClient.GetTableFieldList(inputParam.Table); nil != err {
|
||||
return errors.New(inputParam.DatabaseFlag + " : get table field list fail -> " + err.Error())
|
||||
}
|
||||
if len(inputParam.TableColumnConfig) == 0 {
|
||||
return errors.New(inputParam.DatabaseFlag + " : table field list is empty, please enable `CacheDataTableStructureConfig` or `SetTableColumnConfig`")
|
||||
}
|
||||
// 操作字段列表为空
|
||||
if len(inputParam.ColumnList) == 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)
|
||||
}
|
||||
}
|
||||
// 验证字段是否都正确
|
||||
tableColumnTable := make(map[string]bool)
|
||||
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 _, item := range inputParam.ValueList {
|
||||
if !tableColumnTable[item.Column] {
|
||||
return errors.New(item.Column + " : input column in `ValueList` is not found in table column list")
|
||||
}
|
||||
}
|
||||
// 验证 force no limit
|
||||
if inputParam.ForceNoLimit {
|
||||
inputParam.Limit = 0
|
||||
inputParam.WithCount = false
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user