From 6f9a560e597e45d9346f915a0534c95c805cd99b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 30 Aug 2024 14:50:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E6=9F=A5=E8=AF=A2=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api2sql/execute.go | 67 +++++++++++++++++++++++++++-------------- api2sql/execute_test.go | 47 ++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 23 deletions(-) diff --git a/api2sql/execute.go b/api2sql/execute.go index 157fb8a..7e8b37c 100644 --- a/api2sql/execute.go +++ b/api2sql/execute.go @@ -17,6 +17,7 @@ import ( "git.zhangdeman.cn/zhangdeman/database/define" "git.zhangdeman.cn/zhangdeman/wrapper" "gorm.io/gorm" + "reflect" ) var ( @@ -87,27 +88,7 @@ func (e *execute) List(ctx context.Context, inputParam *define.Api2SqlParam) (an return nil, err } // 动态生成结果解析结构体 - st := wrapper.NewDynamic() - 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, "") - } - } + st := e.dynamicGenerateStruct(inputParam.ColumnList) val := st.ToStructDefaultSliceValue() if err = e.baseDao.List(tx, &val, optionList...); 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 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 插入 @@ -279,3 +272,33 @@ func (e *execute) getOptionList(ctx context.Context, inputParam *define.Api2SqlP } 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 +} diff --git a/api2sql/execute_test.go b/api2sql/execute_test.go index ceb4192..d1c4925 100644 --- a/api2sql/execute_test.go +++ b/api2sql/execute_test.go @@ -18,7 +18,7 @@ import ( "testing" ) -func Test_execute_Run(t *testing.T) { +func startTest() { clientManager := database.NewWrapperClient() if err := clientManager.AddWithConfig("TEST_DATABASE", nil, &define.Database{ Master: &define.Driver{ @@ -44,6 +44,10 @@ func Test_execute_Run(t *testing.T) { }, }) Exec.SetDatabaseClientManager(clientManager) +} + +func Test_execute_List(t *testing.T) { + startTest() res, err := Exec.List(context.Background(), &define.Api2SqlParam{ DatabaseFlag: "TEST_DATABASE", Table: "project", @@ -57,6 +61,47 @@ func Test_execute_Run(t *testing.T) { Alias: "project_flag", 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, Offset: 0,