v1.1
This commit is contained in:
@@ -1,17 +1,34 @@
|
||||
|
||||
func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context,session sqlx.Session, data *{{.upperStartCamelObject}}) (sql.Result,error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
{{if .withCache}}{{.keys}}
|
||||
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
|
||||
if session != nil{
|
||||
return session.ExecCtx(ctx,query,{{.expressionValues}})
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, {{.expressionValues}})
|
||||
}, {{.keyValues}}){{else}}
|
||||
query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
|
||||
if session != nil{
|
||||
return session.ExecCtx(ctx,query,{{.expressionValues}})
|
||||
}
|
||||
return m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
|
||||
data.DelState = globalkey.DelStateNo
|
||||
m.insertUUID(data)
|
||||
{{if .withCache}}{{.keys}}
|
||||
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
|
||||
if session != nil{
|
||||
return session.ExecCtx(ctx,query,{{.expressionValues}})
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, {{.expressionValues}})
|
||||
}, {{.keyValues}}){{else}}
|
||||
query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
|
||||
if session != nil{
|
||||
return session.ExecCtx(ctx,query,{{.expressionValues}})
|
||||
}
|
||||
return m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
|
||||
}
|
||||
func (m *default{{.upperStartCamelObject}}Model) insertUUID(data *{{.upperStartCamelObject}}) {
|
||||
t := reflect.TypeOf(data).Elem()
|
||||
v := reflect.ValueOf(data).Elem()
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
sf := t.Field(i)
|
||||
if sf.Tag.Get("db") == "id" {
|
||||
f := v.Field(i)
|
||||
if f.IsValid() && f.CanSet() && f.Kind() == reflect.String {
|
||||
if f.String() == "" {
|
||||
f.SetString(uuid.NewString())
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user