76 lines
2.3 KiB
Cheetah
76 lines
2.3 KiB
Cheetah
|
{{- define "name" -}}
|
||
|
{{ if .Replace }}Replace{{ else }}Create{{ end }}{{ if not .Return }}NoReturn{{ end }}_{{ .Suffix }}
|
||
|
{{- end -}}
|
||
|
|
||
|
{{- define "signature" -}}
|
||
|
{{- template "name" . }}({{ ctxparam .Args }}) (
|
||
|
{{ if .Return }}{{ param .Return }}, {{ end }}err error)
|
||
|
{{- end -}}
|
||
|
|
||
|
{{- define "invoke" -}}
|
||
|
{{- template "name" . }}({{ ctxarg .Args }})
|
||
|
{{ end -}}
|
||
|
|
||
|
{{- define "body" -}}
|
||
|
{{- if .NeedsNow }}
|
||
|
__now := obj.db.Hooks.Now().UTC()
|
||
|
{{ end -}}
|
||
|
{{ initnew .StaticFields }}
|
||
|
|
||
|
{{ embedplaceholders .Info }}
|
||
|
{{ embedsql .Info "__embed_stmt" }}
|
||
|
|
||
|
var __values []interface{}
|
||
|
__values = append(__values, {{ arg .StaticFields }})
|
||
|
|
||
|
{{ if .Struct.InsertableDynamicFields -}}
|
||
|
__optional_columns := __sqlbundle_Literals{Join: ", "}
|
||
|
__optional_placeholders := __sqlbundle_Literals{Join: ", "}
|
||
|
{{ range .Struct.InsertableDynamicFields }}
|
||
|
if optional.{{ .Name }}._set {
|
||
|
__values = append(__values, optional.{{ .Name }}.value())
|
||
|
__optional_columns.SQLs = append(__optional_columns.SQLs, __sqlbundle_Literal("{{ .Column }}"))
|
||
|
__optional_placeholders.SQLs = append(__optional_placeholders.SQLs, __sqlbundle_Literal("?"))
|
||
|
}
|
||
|
{{ end }}
|
||
|
if len(__optional_columns.SQLs) == 0 {
|
||
|
if __columns.SQL == nil {
|
||
|
__clause.SQL = __sqlbundle_Literal("DEFAULT VALUES")
|
||
|
}
|
||
|
} else {
|
||
|
__columns.SQL = __sqlbundle_Literals{Join: ", ", SQLs: []__sqlbundle_SQL{__columns.SQL, __optional_columns}}
|
||
|
__placeholders.SQL = __sqlbundle_Literals{Join: ", ", SQLs: []__sqlbundle_SQL{__placeholders.SQL, __optional_placeholders}}
|
||
|
}
|
||
|
{{ end -}}
|
||
|
|
||
|
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
|
||
|
obj.logStmt(__stmt, __values...)
|
||
|
|
||
|
{{ if not .Return }}
|
||
|
_, err = obj.driver.ExecContext(ctx, __stmt, __values...)
|
||
|
if err != nil {
|
||
|
return obj.makeErr(err)
|
||
|
}
|
||
|
return nil
|
||
|
{{ else -}}
|
||
|
{{ if .SupportsReturning }}
|
||
|
{{ init .Return }}
|
||
|
err = obj.driver.QueryRowContext(ctx, __stmt, __values...).Scan({{ addrof (flatten .Return) }})
|
||
|
if err != nil {
|
||
|
return nil, obj.makeErr(err)
|
||
|
}
|
||
|
return {{ arg .Return }}, nil
|
||
|
{{ else -}}
|
||
|
__res, err := obj.driver.ExecContext(ctx, __stmt, __values...)
|
||
|
if err != nil {
|
||
|
return nil, obj.makeErr(err)
|
||
|
}
|
||
|
__pk, err := __res.LastInsertId()
|
||
|
if err != nil {
|
||
|
return nil, obj.makeErr(err)
|
||
|
}
|
||
|
return obj.getLast{{ .Return.Type }}(ctx, __pk)
|
||
|
{{ end -}}
|
||
|
{{ end -}}
|
||
|
{{ end -}}
|