Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add model method InsertGetLastInsertId #4486

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tools/goctl/model/sql/gen/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
"expressionValues": strings.Join(expressionValues, ", "),
"keys": strings.Join(keys, "\n"),
"keyValues": strings.Join(keyVars, ", "),
"upperPrimaryKey": util.SafeString(table.PrimaryKey.Field.Name.ToCamel()),
"withGetLastInsertId": table.PrimaryKey.AutoIncrement && table.PrimaryKey.DataType == "uint64",
"data": table,
})
if err != nil {
Expand All @@ -80,6 +82,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)

insertMethodOutput, err := util.With("insertMethod").Parse(text).Execute(map[string]any{
"upperStartCamelObject": camel,
"withGetLastInsertId": table.PrimaryKey.AutoIncrement && table.PrimaryKey.DataType == "uint64",
"data": table,
})
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions tools/goctl/model/sql/template/tpl/insert.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,22 @@ func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, dat
ret,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
return ret,err
}

{{if .withGetLastInsertId}}
func (m *default{{.upperStartCamelObject}}Model) InsertGetLastInsertId(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) {
{{if .withCache}}{{.keys}}
ret, err := 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)
return conn.ExecCtx(ctx, query, {{.expressionValues}})
}, {{.keyValues}}){{else}}query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet)
ret,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
if err != nil {
return nil, err
}
id, err := ret.LastInsertId()
if err == nil {
data.{{.upperPrimaryKey}} = uint64(id)
}
return ret,err
}
{{end}}
3 changes: 2 additions & 1 deletion tools/goctl/model/sql/template/tpl/interface-insert.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)
Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)
{{if .withGetLastInsertId}}InsertGetLastInsertId(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error){{end}}