增加数据创建能力

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

View File

@ -14,8 +14,8 @@ import (
"git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/database" "git.zhangdeman.cn/zhangdeman/database"
"git.zhangdeman.cn/zhangdeman/database/define" "git.zhangdeman.cn/zhangdeman/database/define"
"reflect"
"testing" "testing"
"time"
) )
func startTest() { func startTest() {
@ -78,8 +78,7 @@ func Test_execute_List(t *testing.T) {
Tx: nil, Tx: nil,
}) })
byteData, _ := json.Marshal(res) byteData, _ := json.Marshal(res)
tt := reflect.TypeOf(res) fmt.Println(string(byteData), err)
fmt.Println(tt.String(), res, err, string(byteData))
} }
func Test_execute_Detail(t *testing.T) { func Test_execute_Detail(t *testing.T) {
@ -114,6 +113,51 @@ func Test_execute_Detail(t *testing.T) {
Tx: nil, Tx: nil,
}) })
byteData, _ := json.Marshal(res) byteData, _ := json.Marshal(res)
tt := reflect.TypeOf(res) fmt.Println(res, err, string(byteData))
fmt.Println(tt.String(), 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))
} }

9
go.sum
View File

@ -38,7 +38,6 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU= github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU=
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4= github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4=
github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4=
@ -89,8 +88,6 @@ github.com/lestrrat-go/strftime v1.1.0 h1:gMESpZy44/4pXLO/m+sL0yBd1W6LjgjrrD4a68
github.com/lestrrat-go/strftime v1.1.0/go.mod h1:uzeIB52CeUJenCo1syghlugshMysrqUT51HlxphXVeI= github.com/lestrrat-go/strftime v1.1.0/go.mod h1:uzeIB52CeUJenCo1syghlugshMysrqUT51HlxphXVeI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
@ -143,8 +140,6 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/arch v0.9.0 h1:ub9TgUInamJ8mrZIGlBG6/4TqWeMszd4N8lNorbrr6k=
golang.org/x/arch v0.9.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/arch v0.10.0 h1:S3huipmSclq3PJMNe76NGwkBR504WFkQ5dhzWzP8ZW8= golang.org/x/arch v0.10.0 h1:S3huipmSclq3PJMNe76NGwkBR504WFkQ5dhzWzP8ZW8=
golang.org/x/arch v0.10.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/arch v0.10.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
@ -153,12 +148,8 @@ golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=