增加list 查询 - 待测试
This commit is contained in:
@ -10,7 +10,9 @@ package api2sql
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/database"
|
||||
"git.zhangdeman.cn/zhangdeman/database/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/database/define"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
@ -23,6 +25,7 @@ var (
|
||||
|
||||
type execute struct {
|
||||
databaseClientManager abstract.IWrapperClient // 全部数据库管理的实例
|
||||
baseDao database.BaseDao // 基础dao
|
||||
}
|
||||
|
||||
// SetDatabaseClientManager 设置数据库连接管理实例
|
||||
@ -72,8 +75,43 @@ 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) {
|
||||
|
||||
return nil, nil
|
||||
var (
|
||||
err error
|
||||
tx *gorm.DB
|
||||
optionList []database.SetOption
|
||||
)
|
||||
if tx, err = e.getTx(ctx, inputParam); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
if optionList, err = e.getOptionList(ctx, inputParam); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
// 动态生成结果解析结构体
|
||||
st := wrapper.NewDynamic()
|
||||
for _, columnConfig := range inputParam.ColumnList {
|
||||
tag := fmt.Sprintf(`gorm:%v json:%v`, columnConfig.Column, columnConfig.Alias)
|
||||
switch columnConfig.Type {
|
||||
case "int", "int8", "int16", "int32", "int64":
|
||||
st.AddInt(columnConfig.Column, tag, "")
|
||||
case "uint", "uint8", "uint16", "uint32", "uint64":
|
||||
st.AddUint(columnConfig.Column, tag, "")
|
||||
case "bool":
|
||||
st.AddBool(columnConfig.Column, tag, "")
|
||||
case "float":
|
||||
st.AddBool(columnConfig.Column, tag, "")
|
||||
case "string":
|
||||
st.AddString(columnConfig.Column, tag, "")
|
||||
case "map":
|
||||
st.AddMap(columnConfig.Column, tag, "")
|
||||
case "slice":
|
||||
st.AddSlice(columnConfig.Column, tag, "")
|
||||
}
|
||||
}
|
||||
val := st.ToStructDefaultSliceValue()
|
||||
if err = e.baseDao.List(tx, &val, optionList...); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
// Detail 详情
|
||||
@ -181,26 +219,22 @@ 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.ColumnTable) == 0 && wrapper.ArrayType[string]([]string{
|
||||
if len(inputParam.ColumnList) == 0 && wrapper.ArrayType[string]([]string{
|
||||
consts.SqlTypeList, consts.SqlTypeDetail,
|
||||
}).Has(inputParam.SqlType) >= 0 {
|
||||
for _, itemParam := range inputParam.TableColumnConfig {
|
||||
inputParam.ColumnTable[itemParam.ColumnName] = itemParam.ColumnName
|
||||
}
|
||||
return errors.New("column list is empty")
|
||||
}
|
||||
// 验证字段是否都正确
|
||||
tableColumnTable := make(map[string]bool)
|
||||
for _, itemColumn := range inputParam.TableColumnConfig {
|
||||
tableColumnTable[itemColumn.ColumnName] = true
|
||||
}
|
||||
for columnName, _ := range inputParam.ColumnTable {
|
||||
if !tableColumnTable[columnName] {
|
||||
return errors.New(columnName + " : input column not found in table column list")
|
||||
for _, columnConfig := range inputParam.ColumnList {
|
||||
if !tableColumnTable[columnConfig.Column] {
|
||||
return errors.New(columnConfig.Column + " : 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")
|
||||
if len(columnConfig.Alias) == 0 {
|
||||
columnConfig.Alias = columnConfig.Column
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@ -221,3 +255,26 @@ func (e *execute) getTx(ctx context.Context, inputParam *define.Api2SqlParam) (*
|
||||
}
|
||||
return e.databaseClientManager.GetSlaveClient(ctx, inputParam.DatabaseFlag)
|
||||
}
|
||||
|
||||
// getOptionList 设置where条件
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:31 2024/8/23
|
||||
func (e *execute) getOptionList(ctx context.Context, inputParam *define.Api2SqlParam) ([]database.SetOption, error) {
|
||||
optionList := []database.SetOption{
|
||||
database.WithTable(inputParam.Table),
|
||||
}
|
||||
// 设置 limit offset
|
||||
if !inputParam.ForceNoLimit && inputParam.Limit > 0 {
|
||||
optionList = append(optionList, database.WithLimit(inputParam.Limit, inputParam.Offset))
|
||||
}
|
||||
for _, item := range inputParam.ConditionList {
|
||||
optionFunc, err := database.WithAnyCondition(item.Column, item.Operate, item.Value)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
optionList = append(optionList, optionFunc)
|
||||
}
|
||||
return optionList, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user