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
This commit is contained in:
JT Olio 2020-11-29 13:10:59 -07:00
parent 5d8a67a4f7
commit d3b0691bbd
16 changed files with 1048 additions and 1 deletions

View File

@ -23,7 +23,7 @@ import (
"storj.io/storj/private/tagsql"
)
// Prevent conditional imports from causing build failures
// Prevent conditional imports from causing build failures.
var _ = strconv.Itoa
var _ = strings.LastIndex
var _ = fmt.Sprint

View File

@ -0,0 +1,49 @@
{{- 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 -}}

View File

@ -0,0 +1,75 @@
{{- 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 -}}

View File

@ -0,0 +1,38 @@
{{- define "signature" -}}
All_{{ .Suffix }}({{ ctxparam .Args }}) (
rows {{ sliceof .Row }}, err error)
{{- end -}}
{{- define "invoke" -}}
All_{{ .Suffix }}({{ ctxarg .Args }})
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
__rows, err := obj.driver.QueryContext(ctx, __stmt, __values...)
if err != nil {
return nil, obj.makeErr(err)
}
defer __rows.Close()
for __rows.Next() {
{{ initnew .Row }}
err = __rows.Scan({{ addrof (flatten .Row) }})
if err != nil {
return nil, obj.makeErr(err)
}
rows = append(rows, {{ arg .Row }})
}
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return rows, nil
{{ end -}}

View File

@ -0,0 +1,26 @@
{{- define "signature" -}}
Count_{{ .Suffix }}({{ ctxparam .Args }}) (
count int64, err error)
{{- end -}}
{{- define "invoke" -}}
Count_{{ .Suffix }}({{ ctxarg .Args }})
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
err = obj.driver.QueryRowContext(ctx, __stmt, __values...).Scan(&count)
if err != nil {
return 0, obj.makeErr(err)
}
return count, nil
{{ end -}}

View File

@ -0,0 +1,40 @@
{{- define "signature" -}}
First_{{ .Suffix }}({{ ctxparam .Args }}) (
{{ param .Row }}, err error)
{{- end -}}
{{- define "invoke" -}}
First_{{ .Suffix }}({{ ctxarg .Args }})
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
__rows, err := obj.driver.QueryContext(ctx, __stmt, __values...)
if err != nil {
return nil, obj.makeErr(err)
}
defer __rows.Close()
if !__rows.Next() {
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return nil, nil
}
{{ init .Row }}
err = __rows.Scan({{ addrof (flatten .Row) }})
if err != nil {
return nil, obj.makeErr(err)
}
return {{ arg .Row }}, nil
{{ end -}}

View File

@ -0,0 +1,25 @@
{{- define "signature" -}}
Has_{{ .Suffix }}({{ ctxparam .Args }}) (
has bool, err error)
{{- end -}}
{{- define "invoke" -}}
Has_{{ .Suffix }}({{ ctxarg .Args }})
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
err = obj.driver.QueryRowContext(ctx, __stmt, __values...).Scan(&has)
if err != nil {
return false, obj.makeErr(err)
}
return has, nil
{{ end -}}

View File

@ -0,0 +1,20 @@
{{- define "signature" -}}
getLast{{ .Return.Type }}(ctx context.Context,
pk int64) (
{{ param .Return }}, err error)
{{- end -}}
{{- define "body" -}}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, pk)
{{ init .Return }}
err = obj.driver.QueryRowContext(ctx, __stmt, pk).Scan({{ addrof (flatten .Return) }})
if err != nil {
return {{ zero .Return }}, obj.makeErr(err)
}
return {{ arg .Return }}, nil
{{ end -}}

View File

@ -0,0 +1,41 @@
{{- define "signature" -}}
Limited_{{ .Suffix }}({{ ctxparam .Args }},
limit int, offset int64) (
rows {{ sliceof .Row }}, err error)
{{- end -}}
{{- define "invoke" -}}
Limited_{{ .Suffix }}({{ ctxarg .Args }}, limit, offset)
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
__values = append(__values, limit, offset)
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
__rows, err := obj.driver.QueryContext(ctx, __stmt, __values...)
if err != nil {
return nil, obj.makeErr(err)
}
defer __rows.Close()
for __rows.Next() {
{{ initnew .Row }}
err = __rows.Scan({{ addrof (flatten .Row) }})
if err != nil {
return nil, obj.makeErr(err)
}
rows = append(rows, {{ arg .Row }})
}
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return rows, nil
{{ end -}}

View File

@ -0,0 +1,48 @@
{{- define "signature" -}}
Get_{{ .Suffix }}({{ ctxparam .Args }}) (
{{ param .Row }}, err error)
{{- end -}}
{{- define "invoke" -}}
Get_{{ .Suffix }}({{ ctxarg .Args }})
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
__rows, err := obj.driver.QueryContext(ctx, __stmt, __values...)
if err != nil {
return nil, obj.makeErr(err)
}
defer __rows.Close()
if !__rows.Next() {
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return nil, makeErr(sql.ErrNoRows)
}
{{ init .Row }}
err = __rows.Scan({{ addrof (flatten .Row) }})
if err != nil {
return nil, obj.makeErr(err)
}
if __rows.Next() {
return nil, tooManyRows({{ printf "%q" .Suffix }})
}
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return {{ arg .Row }}, nil
{{ end -}}

View File

@ -0,0 +1,26 @@
{{- define "signature" -}}
Get_{{ .Suffix }}({{ ctxparam .Args }}) (
{{ param .Row }}, err error)
{{- end -}}
{{- define "invoke" -}}
Get_{{ .Suffix }}({{ ctxarg .Args }})
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
{{ init .Row }}
err = obj.driver.QueryRowContext(ctx, __stmt, __values...).Scan({{ addrof (flatten .Row) }})
if err != nil {
return {{ zero .Row }}, obj.makeErr(err)
}
return {{ arg .Row }}, nil
{{ end -}}

View File

@ -0,0 +1,52 @@
{{- define "signature" -}}
Paged_{{ .Suffix }}({{ ctxparam .Args }},
limit int, start *{{ .Continuation.Type }}) (
rows {{ sliceof .Row }}, next *{{ .Continuation.Type }}, err error)
{{- end -}}
{{- define "invoke" -}}
Paged_{{ .Suffix }}({{ ctxarg .Args }}, limit, start)
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
{{ embedsql .FirstInfo "__embed_first_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
var __stmt string
if start != nil && start._set {
__values = append(__values, {{ .Continuation | rename "start" | flatten | slice 0 -1 | value | comma }}limit)
__stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
} else {
__values = append(__values, limit)
__stmt = __sqlbundle_Render(obj.dialect, __embed_first_stmt)
}
obj.logStmt(__stmt, __values...)
__rows, err := obj.driver.QueryContext(ctx, __stmt, __values...)
if err != nil {
return nil, nil, obj.makeErr(err)
}
defer __rows.Close()
{{ declare .Continuation }}
{{ .Continuation.Name }}._set = true
for __rows.Next() {
{{ initnew .Row }}
err = __rows.Scan({{ .Row | flatten | addrof | comma }}{{ .Continuation | flatten | slice 0 -1 | addrof }})
if err != nil {
return nil, nil, obj.makeErr(err)
}
rows = append(rows, {{ arg .Row }})
next = {{ addrof .Continuation }}
}
if err := __rows.Err(); err != nil {
return nil, nil, obj.makeErr(err)
}
return rows, next, nil
{{ end -}}

View File

@ -0,0 +1,48 @@
{{- define "signature" -}}
Find_{{ .Suffix }}({{ ctxparam .Args }}) (
{{ param .Row }}, err error)
{{- end -}}
{{- define "invoke" -}}
Find_{{ .Suffix }}({{ ctxarg .Args }})
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
__rows, err := obj.driver.QueryContext(ctx, __stmt, __values...)
if err != nil {
return nil, obj.makeErr(err)
}
defer __rows.Close()
if !__rows.Next() {
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return nil, nil
}
{{ init .Row }}
err = __rows.Scan({{ addrof (flatten .Row) }})
if err != nil {
return nil, obj.makeErr(err)
}
if __rows.Next() {
return nil, tooManyRows({{ printf "%q" .Suffix }})
}
if err := __rows.Err(); err != nil {
return nil, obj.makeErr(err)
}
return {{ arg .Row }}, nil
{{ end -}}

View File

@ -0,0 +1,29 @@
{{- define "signature" -}}
Find_{{ .Suffix }}({{ ctxparam .Args }}) (
{{ param .Row }}, err error)
{{- end -}}
{{- define "invoke" -}}
Find_{{ .Suffix }}({{ ctxarg .Args }})
{{- end -}}
{{- define "body" }}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
var __values []interface{}
{{ embedvalues .Args "__values" }}
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
{{ init .Row }}
err = obj.driver.QueryRowContext(ctx, __stmt, __values...).Scan({{ addrof (flatten .Row) }})
if err == sql.ErrNoRows {
return {{ zero .Row }}, nil
}
if err != nil {
return {{ zero .Row }}, obj.makeErr(err)
}
return {{ arg .Row }}, nil
{{ end -}}

View File

@ -0,0 +1,437 @@
{{- $options := .Options -}}
// AUTOGENERATED BY storj.io/dbx
// DO NOT EDIT.
package {{ .Package }}
import (
"bytes"
"context"
"database/sql"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"unicode"
"sync"
{{- range .ExtraImports }}
{{ . }}
{{- end }}
)
// Prevent conditional imports from causing build failures.
var _ = strconv.Itoa
var _ = strings.LastIndex
var _ = fmt.Sprint
var _ sync.Mutex
var (
WrapErr = func(err *Error) error {return err}
Logger func(format string, args ...interface{})
errTooManyRows = errors.New("too many rows")
errUnsupportedDriver = errors.New("unsupported driver")
errEmptyUpdate = errors.New("empty update")
)
func logError(format string, args ...interface{}) {
if Logger != nil {
Logger(format, args...)
}
}
type ErrorCode int
const (
ErrorCode_Unknown ErrorCode = iota
ErrorCode_UnsupportedDriver
ErrorCode_NoRows
ErrorCode_TxDone
ErrorCode_TooManyRows
ErrorCode_ConstraintViolation
ErrorCode_EmptyUpdate
)
type Error struct {
Err error
Code ErrorCode
Driver string
Constraint string
QuerySuffix string
}
func (e *Error) Error() string {
return e.Err.Error()
}
func wrapErr(e *Error) error {
if WrapErr == nil {
return e
}
return WrapErr(e)
}
func makeErr(err error) error {
if err == nil {
return nil
}
e := &Error{Err: err}
switch err {
case sql.ErrNoRows:
e.Code = ErrorCode_NoRows
case sql.ErrTxDone:
e.Code = ErrorCode_TxDone
}
return wrapErr(e)
}
func unsupportedDriver(driver string) error {
return wrapErr(&Error{
Err: errUnsupportedDriver,
Code: ErrorCode_UnsupportedDriver,
Driver: driver,
})
}
func emptyUpdate() error {
return wrapErr(&Error{
Err: errEmptyUpdate,
Code: ErrorCode_EmptyUpdate,
})
}
func tooManyRows(query_suffix string) error {
return wrapErr(&Error{
Err: errTooManyRows,
Code: ErrorCode_TooManyRows,
QuerySuffix: query_suffix,
})
}
func constraintViolation(err error, constraint string) error {
return wrapErr(&Error{
Err: err,
Code: ErrorCode_ConstraintViolation,
Constraint: constraint,
})
}
type driver interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
var (
notAPointer = errors.New("destination not a pointer")
lossyConversion = errors.New("lossy conversion")
)
type DB struct {
*sql.DB
dbMethods
Hooks struct {
Now func() time.Time
}
}
func Open(driver, source string) (db *DB, err error) {
var sql_db *sql.DB
switch driver {
{{- range .Dialects }}
case {{ .Name | printf "%q" }}:
sql_db, err = open{{ .Name }}(source)
{{- end }}
default:
return nil, unsupportedDriver(driver)
}
if err != nil {
return nil, makeErr(err)
}
defer func(sql_db *sql.DB) {
if err != nil {
sql_db.Close()
}
}(sql_db)
if err := sql_db.Ping(); err != nil {
return nil, makeErr(err)
}
db = &DB{
DB: sql_db,
}
db.Hooks.Now = time.Now
switch driver {
{{- range .Dialects }}
case {{ .Name | printf "%q" }}:
db.dbMethods = new{{ .Name }}(db)
{{- end }}
default:
return nil, unsupportedDriver(driver)
}
return db, nil
}
func (obj *DB) Close() (err error) {
return obj.makeErr(obj.DB.Close())
}
func (obj *DB) Open(ctx context.Context) (*Tx, error) {
tx, err := obj.DB.BeginTx(ctx, nil)
if err != nil {
return nil, obj.makeErr(err)
}
return &Tx{
Tx: tx,
txMethods: obj.wrapTx(tx),
}, nil
}
{{ if $options.SupportRx }}
func (obj *DB) NewRx() *Rx {
return &Rx{db: obj}
}
{{ end }}
func DeleteAll(ctx context.Context, db *DB) (int64, error) {
tx, err := db.Open(ctx)
if err != nil {
return 0, err
}
defer func() {
if err == nil {
err = db.makeErr(tx.Commit())
return
}
if err_rollback := tx.Rollback(); err_rollback != nil {
logError("delete-all: rollback failed: %v", db.makeErr(err_rollback))
}
}()
return tx.deleteAll(ctx)
}
type Tx struct {
Tx *sql.Tx
txMethods
}
type dialectTx struct {
tx *sql.Tx
}
func (tx *dialectTx) Commit() (err error) {
return makeErr(tx.tx.Commit())
}
func (tx *dialectTx) Rollback() (err error) {
return makeErr(tx.tx.Rollback())
}
{{- range .Dialects }}
{{- $dbtype := .Name | printf "%sDB" -}}
{{- $txtype := .Name | printf "%sTx" -}}
{{- $impltype := .Name | printf "%sImpl" }}
type {{ $impltype }} struct {
db *DB
dialect __sqlbundle_{{ .Name }}
driver driver
}
func (obj *{{ $impltype }}) Rebind(s string) string {
return obj.dialect.Rebind(s)
}
func (obj *{{ $impltype }}) logStmt(stmt string, args... interface{}) {
{{ .Name }}LogStmt(stmt, args...)
}
func (obj *{{ $impltype }}) makeErr(err error) error {
constraint, ok := obj.isConstraintError(err)
if ok {
return constraintViolation(err, constraint)
}
return makeErr(err)
}
type {{ $dbtype }} struct {
db *DB
*{{ $impltype }}
}
func new{{ .Name }}(db *DB) *{{ $dbtype }} {
return &{{ $dbtype }}{
db: db,
{{ $impltype }}: &{{ $impltype }}{
db: db,
driver: db.DB,
},
}
}
func (obj *{{ $dbtype }}) Schema() string {
return `{{ .SchemaSQL }}`
}
func (obj *{{ $dbtype }}) wrapTx(tx *sql.Tx) txMethods {
return &{{ $txtype }}{
dialectTx: dialectTx{tx: tx},
{{ $impltype }}: &{{ $impltype }}{
db: obj.db,
driver: tx,
},
}
}
type {{ $txtype }} struct {
dialectTx
*{{ $impltype }}
}
func {{ .Name }}LogStmt(stmt string, args ...interface{}) {
// TODO: render placeholders
if Logger != nil {
out := fmt.Sprintf("stmt: %s\nargs: %v\n", stmt, pretty(args))
Logger(out)
}
}
{{- end }}
type pretty []interface{}
func (p pretty) Format(f fmt.State, c rune) {
fmt.Fprint(f, "[")
nextval:
for i, val := range p {
if i > 0 {
fmt.Fprint(f, ", ")
}
rv := reflect.ValueOf(val)
if rv.Kind() == reflect.Ptr {
if rv.IsNil() {
fmt.Fprint(f, "NULL")
continue
}
val = rv.Elem().Interface()
}
switch v := val.(type) {
case string:
fmt.Fprintf(f, "%q", v)
case time.Time:
fmt.Fprintf(f, "%s", v.Format(time.RFC3339Nano))
case []byte:
for _, b := range v {
if !unicode.IsPrint(rune(b)) {
fmt.Fprintf(f, "%#x", v)
continue nextval
}
}
fmt.Fprintf(f, "%q", v)
default:
fmt.Fprintf(f, "%v", v)
}
}
fmt.Fprint(f, "]")
}
{{ range .Structs }}
{{- $struct := .Name -}}
{{- $createstruct := .CreateStructName -}}
{{- $updatestruct := .UpdateStructName }}
type {{ $struct }} struct {
{{- range .Fields }}
{{ .Name }} {{ .Type }}
{{- end }}
{{- if $options.SupportUserdata }}
userdata_mu sync.Mutex
userdata interface{}
{{- end}}
}
func ({{ $struct }}) _Table() string { return "{{ .Table }}" }
{{- if .InsertableOptionalFields }}
type {{ $createstruct }} struct {
{{- range .InsertableOptionalFields }}
{{ .Name }} {{ .StructName }}
{{- end }}
}
{{- end }}
type {{ $updatestruct }} struct {
{{- range .UpdatableFields }}
{{ .Name }} {{ .StructName }}
{{- end }}
}
{{- range .Fields }}
{{- $fstruct := .StructName }}
{{- $ctor := printf "%s_%s" .ModelName .Name }}
type {{ $fstruct }} struct {
_set bool
_null bool
_value {{ .Type }}
}
func {{ $ctor }}(v {{ .CtorValue }}) {{ $fstruct }} {
{{- if .MutateFn }}
v = {{ .MutateFn }}(v)
{{- end }}
return {{ $fstruct }}{ _set: true, _value: {{ if .TakeAddr }}&{{ end }}v }
}
{{ if .Nullable }}
func {{ $ctor }}_Raw(v {{ .Type }}) {{ $fstruct }} {
if v == nil {
return {{ $ctor }}_Null()
}
return {{ $ctor }}({{ if .TakeAddr }}*{{ end }}v)
}
func {{ $ctor }}_Null() {{ $fstruct }} {
return {{ $fstruct }}{ _set: true, _null: true }
}
func (f {{ $fstruct }}) isnull() bool { return !f._set || f._null || f._value == nil }
{{ end }}
func (f {{ $fstruct }}) value() interface{} { if !f._set || f._null { return nil }; return f._value }
func ({{ $fstruct }}) _Column() string { return "{{ .Column }}" }
{{- end -}}
{{- end }}
func toUTC(t time.Time) time.Time {
return t.UTC()
}
func toDate(t time.Time) time.Time {
// keep up the minute portion so that translations between timezones will
// continue to reflect properly.
return t.Truncate(time.Minute)
}
//
// runtime support for building sql statements
//
{{ .SQLSupport }}
//
// end runtime support for building sql statements
//

View File

@ -0,0 +1,93 @@
{{- define "name" -}}
Update{{ if not .Return }}NoReturn{{ end }}_{{ .Suffix }}
{{- end -}}
{{- define "signature" -}}
{{- template "name" . }}({{ ctxparam .Args }},
update {{ .Struct.UpdateStructName }}) (
{{ if .Return }}{{ param .Return }}, {{ end }}err error)
{{- end -}}
{{- define "invoke" -}}
{{- template "name" . }}({{ ctxarg .Args }}, update)
{{- end -}}
{{- define "body" -}}
{{ embedplaceholders .Info }}
{{ embedsql .Info "__embed_stmt" }}
__sets_sql := __sqlbundle_Literals{Join: ", "}
var __values []interface{}
var __args []interface{}
{{ range .Struct.UpdatableFields }}
if update.{{ .Name }}._set {
__values = append(__values, update.{{ .Name }}.value())
__sets_sql.SQLs = append(__sets_sql.SQLs, __sqlbundle_Literal("{{ .Column }} = ?"))
}
{{ end }}
{{- if .NeedsNow }}
__now := obj.db.Hooks.Now().UTC()
{{ end -}}
{{ range .AutoFields }}
__values = append(__values, {{ .InitVal }})
__sets_sql.SQLs = append(__sets_sql.SQLs, __sqlbundle_Literal("{{ .Name }} = ?"))
{{ end }}
{{ if not .AutoFields }}
if len(__sets_sql.SQLs) == 0 {
{{- if .Return }}
return nil, emptyUpdate()
{{- else }}
return emptyUpdate()
{{- end }}
}
{{ end }}
{{ embedvalues .Args "__args" }}
__values = append(__values, __args...)
__sets.SQL = __sets_sql
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 }}
{{ init .Return }}
{{- if .SupportsReturning }}
err = obj.driver.QueryRowContext(ctx, __stmt, __values...).Scan({{ addrof (flatten .Return) }})
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, obj.makeErr(err)
}
{{- else }}
_, err = obj.driver.ExecContext(ctx, __stmt, __values...)
if err != nil {
return nil, obj.makeErr(err)
}
{{ embedsql .InfoGet "__embed_stmt_get" }}
var __stmt_get = __sqlbundle_Render(obj.dialect, __embed_stmt_get)
obj.logStmt("(IMPLIED) " + __stmt_get, __args...)
err = obj.driver.QueryRowContext(ctx, __stmt_get, __args...).Scan({{ addrof (flatten .Return) }})
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, obj.makeErr(err)
}
{{- end }}
return {{ arg .Return }}, nil
{{- end }}
{{- end -}}