storj/satellite/satellitedb/dbx/templates/golang.create-raw.tmpl
JT Olio d3b0691bbd satellite/dbx: import dbx templates
these are unchanged from storj.io/dbx.

we're importing them because in a later commit we
will change them, and it'd be nice to see that
diff as a separate commit.

Change-Id: I8315130ed6bab397bd65b9a1a90c29d130b8c02d
2020-11-29 18:09:33 -07:00

50 lines
1.2 KiB
Cheetah

{{- define "name" -}}
Raw{{ if .Replace }}Replace{{ else }}Create{{ end }}{{ if not .Return }}NoReturn{{ end }}_{{ .Suffix }}
{{- end -}}
{{- define "signature" -}}
{{- template "name" . }}({{ ctxparam .Arg }}) (
{{ if .Return }}{{ param .Return }}, {{ end }}err error)
{{- end -}}
{{- define "invoke" -}}
{{- template "name" . }}({{ ctxarg .Arg }})
{{ end -}}
{{- define "body" -}}
{{ initnew .Fields }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, {{ arg .Fields }})
{{ if not .Return }}
_, err = obj.driver.ExecContext(ctx, __stmt, {{ arg .Fields}})
if err != nil {
return obj.makeErr(err)
}
return nil
{{ else -}}
{{ if .SupportsReturning }}
{{ init .Return }}
err = obj.driver.QueryRowContext(ctx, __stmt, {{ arg .Fields }}).Scan({{ addrof (flatten .Return) }})
if err != nil {
return nil, obj.makeErr(err)
}
return {{ arg .Return }}, nil
{{ else -}}
__res, err := obj.driver.ExecContext(ctx, __stmt, {{ arg .Fields}})
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 -}}