增加数据详情查询能力

This commit is contained in:
白茶清欢 2024-08-30 14:50:51 +08:00
parent c9d6c4c845
commit 6f9a560e59
2 changed files with 91 additions and 23 deletions

View File

@ -17,6 +17,7 @@ import (
"git.zhangdeman.cn/zhangdeman/database/define" "git.zhangdeman.cn/zhangdeman/database/define"
"git.zhangdeman.cn/zhangdeman/wrapper" "git.zhangdeman.cn/zhangdeman/wrapper"
"gorm.io/gorm" "gorm.io/gorm"
"reflect"
) )
var ( var (
@ -87,27 +88,7 @@ func (e *execute) List(ctx context.Context, inputParam *define.Api2SqlParam) (an
return nil, err return nil, err
} }
// 动态生成结果解析结构体 // 动态生成结果解析结构体
st := wrapper.NewDynamic() st := e.dynamicGenerateStruct(inputParam.ColumnList)
for _, columnConfig := range inputParam.ColumnList {
tag := fmt.Sprintf(`%v`, columnConfig.Alias)
column := wrapper.String(columnConfig.Column).SnakeCaseToCamel()
switch columnConfig.Type {
case "int", "int8", "int16", "int32", "int64":
st.AddInt(column, tag, "")
case "uint", "uint8", "uint16", "uint32", "uint64":
st.AddUint(column, tag, "")
case "bool":
st.AddBool(column, tag, "")
case "float":
st.AddBool(column, tag, "")
case "string":
st.AddString(column, tag, "")
case "map":
st.AddMap(column, tag, "")
case "slice":
st.AddSlice(column, tag, "")
}
}
val := st.ToStructDefaultSliceValue() val := st.ToStructDefaultSliceValue()
if err = e.baseDao.List(tx, &val, optionList...); nil != err { if err = e.baseDao.List(tx, &val, optionList...); nil != err {
return nil, err return nil, err
@ -121,7 +102,19 @@ func (e *execute) List(ctx context.Context, inputParam *define.Api2SqlParam) (an
// //
// Date : 20:52 2024/8/21 // Date : 20:52 2024/8/21
func (e *execute) Detail(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) { func (e *execute) Detail(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
return nil, nil inputParam.Limit = 1 // 限制查询一条
var (
res any
err error
)
if res, err = e.List(ctx, inputParam); nil != err {
return nil, err
}
reflectVal := reflect.ValueOf(res)
if reflectVal.Len() == 0 {
return nil, gorm.ErrRecordNotFound
}
return reflectVal.Index(0).Interface(), nil
} }
// Insert 插入 // Insert 插入
@ -279,3 +272,33 @@ func (e *execute) getOptionList(ctx context.Context, inputParam *define.Api2SqlP
} }
return optionList, nil return optionList, nil
} }
// dynamicGenerateStruct 生成动态结构体
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:19 2024/8/30
func (e *execute) dynamicGenerateStruct(columnList []*define.ColumnConfig) *wrapper.DynamicStruct {
st := wrapper.NewDynamic()
for _, columnConfig := range columnList {
tag := fmt.Sprintf(`%v`, columnConfig.Alias)
column := wrapper.String(columnConfig.Column).SnakeCaseToCamel()
switch columnConfig.Type {
case "int", "int8", "int16", "int32", "int64":
st.AddInt(column, tag, "")
case "uint", "uint8", "uint16", "uint32", "uint64":
st.AddUint(column, tag, "")
case "bool":
st.AddBool(column, tag, "")
case "float":
st.AddBool(column, tag, "")
case "string":
st.AddString(column, tag, "")
case "map":
st.AddMap(column, tag, "")
case "slice":
st.AddSlice(column, tag, "")
}
}
return st
}

View File

@ -18,7 +18,7 @@ import (
"testing" "testing"
) )
func Test_execute_Run(t *testing.T) { func startTest() {
clientManager := database.NewWrapperClient() clientManager := database.NewWrapperClient()
if err := clientManager.AddWithConfig("TEST_DATABASE", nil, &define.Database{ if err := clientManager.AddWithConfig("TEST_DATABASE", nil, &define.Database{
Master: &define.Driver{ Master: &define.Driver{
@ -44,6 +44,10 @@ func Test_execute_Run(t *testing.T) {
}, },
}) })
Exec.SetDatabaseClientManager(clientManager) Exec.SetDatabaseClientManager(clientManager)
}
func Test_execute_List(t *testing.T) {
startTest()
res, err := Exec.List(context.Background(), &define.Api2SqlParam{ res, err := Exec.List(context.Background(), &define.Api2SqlParam{
DatabaseFlag: "TEST_DATABASE", DatabaseFlag: "TEST_DATABASE",
Table: "project", Table: "project",
@ -57,6 +61,47 @@ func Test_execute_Run(t *testing.T) {
Alias: "project_flag", Alias: "project_flag",
Type: "string", Type: "string",
}, },
&define.ColumnConfig{
Column: "connect_timeout",
Alias: "timeout",
Type: "string",
},
},
Limit: 0,
Offset: 0,
ForceNoLimit: false,
OrderField: "id",
OrderRule: "desc",
WithCount: false,
ConditionList: nil,
TableColumnConfig: nil,
Tx: nil,
})
byteData, _ := json.Marshal(res)
tt := reflect.TypeOf(res)
fmt.Println(tt.String(), res, err, string(byteData))
}
func Test_execute_Detail(t *testing.T) {
startTest()
res, err := Exec.Detail(context.Background(), &define.Api2SqlParam{
DatabaseFlag: "TEST_DATABASE",
Table: "project",
ForceMaster: false,
InputSql: "",
TableSplitConfig: define.TableSplitConfig{},
SqlType: consts.SqlTypeList,
ColumnList: []*define.ColumnConfig{
&define.ColumnConfig{
Column: "flag",
Alias: "project_flag",
Type: "string",
},
&define.ColumnConfig{
Column: "description",
Alias: "desc",
Type: "string",
},
}, },
Limit: 0, Limit: 0,
Offset: 0, Offset: 0,