sqlboiler/templates/12_insert.tpl

128 lines
4.3 KiB
Smarty
Raw Normal View History

{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
2016-08-04 05:23:55 +02:00
// InsertG a single record. See Insert for whitelist behavior description.
2016-08-01 07:10:10 +02:00
func (o *{{$tableNameSingular}}) InsertG(whitelist ... string) error {
return o.Insert(boil.GetDB(), whitelist...)
}
2016-08-04 05:23:55 +02:00
// InsertGP a single record, and panics on error. See Insert for whitelist
// behavior description.
2016-08-01 07:10:10 +02:00
func (o *{{$tableNameSingular}}) InsertGP(whitelist ... string) {
if err := o.Insert(boil.GetDB(), whitelist...); err != nil {
panic(boil.WrapErr(err))
}
}
2016-08-04 05:23:55 +02:00
// InsertP a single record using an executor, and panics on error. See Insert
// for whitelist behavior description.
func (o *{{$tableNameSingular}}) InsertP(exec boil.Executor, whitelist ... string) {
if err := o.Insert(exec, whitelist...); err != nil {
panic(boil.WrapErr(err))
}
}
2016-08-01 07:10:10 +02:00
// Insert a single record using an executor.
2016-08-04 05:23:55 +02:00
// Whitelist behavior: If a whitelist is provided, only those columns supplied are inserted
// No whitelist behavior: Without a whitelist, columns are inferred by the following rules:
2016-09-01 03:12:46 +02:00
// - All columns without a default value are included (i.e. name, age)
// - All columns with a default, but non-zero are included (i.e. health = 75)
2016-08-01 07:10:10 +02:00
func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string) error {
if o == nil {
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
}
var err error
{{- template "timestamp_insert_helper" . }}
2016-08-29 14:38:19 +02:00
{{if not .NoHooks -}}
if err := o.doBeforeInsertHooks(exec); err != nil {
return err
}
{{- end}}
2016-09-03 21:24:18 +02:00
nzDefaults := boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o)
2016-09-03 21:24:18 +02:00
key := makeCacheKey(whitelist, nzDefaults)
{{$varNameSingular}}InsertCacheMut.RLock()
cache, cached := {{$varNameSingular}}InsertCache[key]
{{$varNameSingular}}InsertCacheMut.RUnlock()
if !cached {
wl, returnColumns := strmangle.InsertColumnSet(
{{$varNameSingular}}Columns,
{{$varNameSingular}}ColumnsWithDefault,
{{$varNameSingular}}ColumnsWithoutDefault,
nzDefaults,
whitelist,
)
typ := reflect.TypeOf(o)
cache.valueMapping = boil.BindMapping(typ, {{$varNameSingular}}Mapping, wl)
cache.retMapping = boil.BindMapping(typ, {{$varNameSingular}}Mapping, returnColumns)
cache.query = fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), strmangle.Placeholders(len(wl), 1, 1))
if len(cache.retMapping) != 0 {
{{if .UseLastInsertID -}}
cache.query += fmt.Sprintf(` RETURNING %s`, strings.Join(returnColumns, ","))
{{else -}}
cache.retQuery = fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, `","`), strmangle.WhereClause(1, {{$varNameSingular}}AutoIncPrimaryKeys))
{{end -}}
}
}
2016-08-14 01:27:34 +02:00
{{if .UseLastInsertID}}
2016-08-13 23:23:36 +02:00
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, ins)
2016-09-02 09:09:42 +02:00
fmt.Fprintln(boil.DebugWriter, boil.GetStructValues(o, wl...))
2016-08-13 23:23:36 +02:00
}
2016-09-02 09:09:42 +02:00
result, err := exec.Exec(ins, boil.GetStructValues(o, wl...)...)
2016-08-13 23:23:36 +02:00
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
}
2016-09-03 21:24:18 +02:00
if len(cache.retMapping) == 0 {
2016-08-29 14:38:19 +02:00
{{if not .NoHooks -}}
2016-09-03 21:24:18 +02:00
return o.doAfterInsertHooks(exec)
{{else -}}
2016-08-29 14:38:19 +02:00
return nil
2016-09-03 21:24:18 +02:00
{{end -}}
2016-08-13 23:23:36 +02:00
}
lastID, err := result.LastInsertId()
if err != nil || lastID == 0 || len({{$varNameSingular}}AutoIncPrimaryKeys) != 1 {
2016-08-13 23:23:36 +02:00
return ErrSyncFail
}
2016-09-03 21:24:18 +02:00
err = exec.QueryRow(cache.retQuery, lastID).Scan(boil.GetStructPointers(o, returnColumns...))
2016-08-13 23:23:36 +02:00
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
}
{{else}}
2016-09-03 21:24:18 +02:00
if len(cache.retMapping) != 0 {
err = exec.QueryRow(cache.query, boil.GetStructValues(o, wl...)...).Scan(boil.GetStructPointers(o, returnColumns...)...)
} else {
2016-09-03 21:24:18 +02:00
_, err = exec.Exec(cache.query, boil.GetStructValues(o, wl...)...)
}
2016-02-23 13:38:24 +01:00
if boil.DebugMode {
2016-08-13 18:16:10 +02:00
fmt.Fprintln(boil.DebugWriter, ins)
2016-09-02 09:09:42 +02:00
fmt.Fprintln(boil.DebugWriter, boil.GetStructValues(o, wl...))
}
2016-02-23 13:38:24 +01:00
if err != nil {
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
2016-02-23 13:38:24 +01:00
}
2016-08-13 23:23:36 +02:00
{{end}}
2016-02-23 13:38:24 +01:00
2016-09-03 21:24:18 +02:00
{{$varNameSingular}}InsertCacheMut.Lock()
{{$varNameSingular}}InsertCache[key] = cache
{{$varNameSingular}}InsertCacheMut.Unlock()
2016-08-29 14:38:19 +02:00
{{if not .NoHooks -}}
return o.doAfterInsertHooks(exec)
2016-08-28 12:48:50 +02:00
{{- else -}}
return nil
{{- end}}
2016-02-23 13:38:24 +01:00
}