增加数据创建能力

This commit is contained in:
2024-09-06 14:49:36 +08:00
parent 38f3f763bf
commit 375aed7160
3 changed files with 73 additions and 26 deletions

View File

@ -17,7 +17,6 @@ import (
"git.zhangdeman.cn/zhangdeman/database/define"
"git.zhangdeman.cn/zhangdeman/wrapper"
"gorm.io/gorm"
"reflect"
)
var (
@ -90,11 +89,12 @@ func (e *execute) Run(ctx context.Context, inputParam *define.Api2SqlParam) (any
// Author : go_developer@163.com<白茶清欢>
//
// 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) ([]map[string]any, error) {
var (
err error
tx *gorm.DB
optionList []define.SetOption
val []map[string]any
)
if tx, err = e.getTx(ctx, inputParam); nil != err {
return nil, err
@ -102,9 +102,6 @@ func (e *execute) List(ctx context.Context, inputParam *define.Api2SqlParam) (an
if optionList, err = e.getOptionList(ctx, inputParam); nil != err {
return nil, err
}
// 动态生成结果解析结构体
st := e.dynamicGenerateStruct(inputParam.ColumnList)
val := st.ToStructDefaultSliceValue()
if err = e.baseDao.List(tx, &val, optionList...); nil != err {
return nil, err
}
@ -133,17 +130,16 @@ func (e *execute) List(ctx context.Context, inputParam *define.Api2SqlParam) (an
func (e *execute) Detail(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
inputParam.Limit = 1 // 限制查询一条
var (
res any
err error
list []map[string]any
err error
)
if res, err = e.List(ctx, inputParam); nil != err {
if list, err = e.List(ctx, inputParam); nil != err {
return nil, err
}
reflectVal := reflect.ValueOf(res)
if reflectVal.Len() == 0 {
if len(list) == 0 {
return nil, gorm.ErrRecordNotFound
}
return reflectVal.Index(0).Interface(), nil
return list[0], nil
}
// Insert 插入
@ -152,7 +148,23 @@ func (e *execute) Detail(ctx context.Context, inputParam *define.Api2SqlParam) (
//
// Date : 20:52 2024/8/21
func (e *execute) Insert(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
return nil, nil
var (
err error
tx *gorm.DB
)
if tx, err = e.getTx(ctx, inputParam); nil != err {
return nil, err
}
// 动态生成结果解析结构体
insertVal := map[string]any{}
for _, itemColumn := range inputParam.ConditionList {
insertVal[itemColumn.Column] = itemColumn.Value
}
if err = e.baseDao.Create(tx, &insertVal, database.WithTable(inputParam.Table)); nil != err {
return nil, err
}
return insertVal, nil
}
// Update 更新

View File

@ -14,8 +14,8 @@ import (
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/database"
"git.zhangdeman.cn/zhangdeman/database/define"
"reflect"
"testing"
"time"
)
func startTest() {
@ -78,8 +78,7 @@ func Test_execute_List(t *testing.T) {
Tx: nil,
})
byteData, _ := json.Marshal(res)
tt := reflect.TypeOf(res)
fmt.Println(tt.String(), res, err, string(byteData))
fmt.Println(string(byteData), err)
}
func Test_execute_Detail(t *testing.T) {
@ -114,6 +113,51 @@ func Test_execute_Detail(t *testing.T) {
Tx: nil,
})
byteData, _ := json.Marshal(res)
tt := reflect.TypeOf(res)
fmt.Println(tt.String(), res, err, string(byteData))
fmt.Println(res, err, string(byteData))
}
func Test_execute_Insert(t *testing.T) {
startTest()
res, err := Exec.Insert(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,
ForceNoLimit: false,
OrderField: "id",
OrderRule: "desc",
WithCount: false,
ConditionList: []define.SqlCondition{
define.SqlCondition{
Column: "flag",
Operate: "",
Value: fmt.Sprintf("unit_test_flag_%v", time.Now().Unix()),
},
define.SqlCondition{
Column: "description",
Operate: "",
Value: "单元测试生成项目",
},
},
TableColumnConfig: nil,
Tx: nil,
})
byteData, _ := json.Marshal(res)
fmt.Println(res, err, string(byteData))
}