2016-06-20 07:22:50 +02:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
2016-09-24 09:52:18 +02:00
|
|
|
{{- $schemaTable := .Table.Name | .SchemaTable}}
|
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 {
|
2016-09-14 10:08:30 +02:00
|
|
|
return o.Insert(boil.GetDB(), whitelist...)
|
2016-05-02 08:34:25 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-09-14 10:08:30 +02:00
|
|
|
if err := o.Insert(boil.GetDB(), whitelist...); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
2016-07-16 13:22:57 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-09-14 10:08:30 +02:00
|
|
|
if err := o.Insert(exec, whitelist...); err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
2016-08-04 05:23:55 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-09-14 10:08:30 +02:00
|
|
|
if o == nil {
|
|
|
|
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
{{- template "timestamp_insert_helper" . }}
|
|
|
|
|
|
|
|
{{if not .NoHooks -}}
|
|
|
|
if err := o.doBeforeInsertHooks(exec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
{{- end}}
|
|
|
|
|
2016-09-15 05:59:55 +02:00
|
|
|
nzDefaults := queries.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o)
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
key := makeCacheKey(whitelist, nzDefaults)
|
|
|
|
{{$varNameSingular}}InsertCacheMut.RLock()
|
|
|
|
cache, cached := {{$varNameSingular}}InsertCache[key]
|
|
|
|
{{$varNameSingular}}InsertCacheMut.RUnlock()
|
|
|
|
|
|
|
|
if !cached {
|
|
|
|
wl, returnColumns := strmangle.InsertColumnSet(
|
2017-03-16 08:54:55 +01:00
|
|
|
{{$varNameSingular}}Columns,
|
2016-09-14 10:08:30 +02:00
|
|
|
{{$varNameSingular}}ColumnsWithDefault,
|
|
|
|
{{$varNameSingular}}ColumnsWithoutDefault,
|
|
|
|
nzDefaults,
|
|
|
|
whitelist,
|
|
|
|
)
|
|
|
|
|
2016-09-15 05:59:55 +02:00
|
|
|
cache.valueMapping, err = queries.BindMapping({{$varNameSingular}}Type, {{$varNameSingular}}Mapping, wl)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-15 05:59:55 +02:00
|
|
|
cache.retMapping, err = queries.BindMapping({{$varNameSingular}}Type, {{$varNameSingular}}Mapping, returnColumns)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-02 03:18:47 +01:00
|
|
|
if len(wl) != 0 {
|
2017-03-19 16:12:06 +01:00
|
|
|
cache.query = fmt.Sprintf("INSERT INTO {{$schemaTable}} ({{.LQ}}%s{{.RQ}}) %%sVALUES (%s)%%s", strings.Join(wl, "{{.RQ}},{{.LQ}}"), strmangle.Placeholders(dialect.IndexPlaceholders, len(wl), 1, 1))
|
2017-03-02 03:18:47 +01:00
|
|
|
} else {
|
|
|
|
{{if eq .DriverName "mysql" -}}
|
|
|
|
cache.query = "INSERT INTO {{$schemaTable}} () VALUES ()"
|
|
|
|
{{else -}}
|
|
|
|
cache.query = "INSERT INTO {{$schemaTable}} DEFAULT VALUES"
|
|
|
|
{{end -}}
|
|
|
|
}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
2017-03-31 10:27:34 +02:00
|
|
|
var queryOutput, queryReturning string
|
2017-03-19 16:12:06 +01:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if len(cache.retMapping) != 0 {
|
|
|
|
{{if .UseLastInsertID -}}
|
2017-03-14 11:48:38 +01:00
|
|
|
cache.retQuery = fmt.Sprintf("SELECT {{.LQ}}%s{{.RQ}} FROM {{$schemaTable}} WHERE %s", strings.Join(returnColumns, "{{.RQ}},{{.LQ}}"), strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns))
|
2017-03-18 13:18:04 +01:00
|
|
|
{{else -}}
|
2017-03-19 16:12:06 +01:00
|
|
|
{{if ne .DriverName "mssql" -}}
|
2017-03-31 10:27:34 +02:00
|
|
|
queryReturning = fmt.Sprintf(" RETURNING {{.LQ}}%s{{.RQ}}", strings.Join(returnColumns, "{{.RQ}},{{.LQ}}"))
|
2017-03-19 16:12:06 +01:00
|
|
|
{{else -}}
|
|
|
|
queryOutput = fmt.Sprintf("OUTPUT INSERTED.{{.LQ}}%s{{.RQ}} ", strings.Join(returnColumns, "{{.RQ}},INSERTED.{{.LQ}}"))
|
|
|
|
{{end -}}
|
2017-04-05 04:42:49 +02:00
|
|
|
{{end -}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(wl) != 0 {
|
|
|
|
cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value := reflect.Indirect(reflect.ValueOf(o))
|
2016-09-15 05:59:55 +02:00
|
|
|
vals := queries.ValuesFromMapping(value, cache.valueMapping)
|
2016-09-25 01:10:26 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if boil.DebugMode {
|
|
|
|
fmt.Fprintln(boil.DebugWriter, cache.query)
|
|
|
|
fmt.Fprintln(boil.DebugWriter, vals)
|
|
|
|
}
|
|
|
|
|
2016-09-25 01:10:26 +02:00
|
|
|
{{if .UseLastInsertID -}}
|
|
|
|
{{- $canLastInsertID := .Table.CanLastInsertID -}}
|
|
|
|
{{if $canLastInsertID -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
result, err := exec.Exec(cache.query, vals...)
|
2016-09-25 01:10:26 +02:00
|
|
|
{{else -}}
|
|
|
|
_, err = exec.Exec(cache.query, vals...)
|
|
|
|
{{- end}}
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
|
|
|
|
}
|
2016-09-15 06:46:03 +02:00
|
|
|
|
2016-09-25 01:10:26 +02:00
|
|
|
{{if $canLastInsertID -}}
|
|
|
|
var lastID int64
|
|
|
|
{{- end}}
|
2016-09-15 06:46:03 +02:00
|
|
|
var identifierCols []interface{}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
if len(cache.retMapping) == 0 {
|
2016-09-15 06:46:03 +02:00
|
|
|
goto CacheNoHooks
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 01:10:26 +02:00
|
|
|
{{if $canLastInsertID -}}
|
2016-09-15 06:46:03 +02:00
|
|
|
lastID, err = result.LastInsertId()
|
2016-09-14 17:36:36 +02:00
|
|
|
if err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
return ErrSyncFail
|
|
|
|
}
|
2016-09-25 01:10:26 +02:00
|
|
|
|
|
|
|
{{$colName := index .Table.PKey.Columns 0 -}}
|
2016-09-17 05:13:44 +02:00
|
|
|
{{- $col := .Table.GetColumn $colName -}}
|
2016-09-25 01:10:26 +02:00
|
|
|
{{- $colTitled := $colName | titleCase}}
|
2016-09-17 05:13:44 +02:00
|
|
|
o.{{$colTitled}} = {{$col.Type}}(lastID)
|
2016-09-25 01:10:26 +02:00
|
|
|
if lastID != 0 && len(cache.retMapping) == 1 && cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] {
|
|
|
|
goto CacheNoHooks
|
|
|
|
}
|
|
|
|
{{- end}}
|
|
|
|
|
2016-09-17 05:13:44 +02:00
|
|
|
identifierCols = []interface{}{
|
|
|
|
{{range .Table.PKey.Columns -}}
|
2016-09-25 01:10:26 +02:00
|
|
|
o.{{. | titleCase}},
|
2016-09-17 05:13:44 +02:00
|
|
|
{{end -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 01:10:26 +02:00
|
|
|
if boil.DebugMode {
|
|
|
|
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
|
|
|
|
fmt.Fprintln(boil.DebugWriter, identifierCols...)
|
|
|
|
}
|
2016-09-14 17:36:36 +02:00
|
|
|
|
2016-09-25 01:10:26 +02:00
|
|
|
err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to populate default values for {{.Table.Name}}")
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
{{else}}
|
|
|
|
if len(cache.retMapping) != 0 {
|
2016-09-15 05:59:55 +02:00
|
|
|
err = exec.QueryRow(cache.query, vals...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
|
2016-09-14 10:08:30 +02:00
|
|
|
} else {
|
|
|
|
_, err = exec.Exec(cache.query, vals...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
|
|
|
|
}
|
|
|
|
{{end}}
|
2016-09-25 01:10:26 +02:00
|
|
|
|
2016-09-15 06:46:03 +02:00
|
|
|
{{if .UseLastInsertID -}}
|
|
|
|
CacheNoHooks:
|
|
|
|
{{- end}}
|
2016-09-14 10:08:30 +02:00
|
|
|
if !cached {
|
|
|
|
{{$varNameSingular}}InsertCacheMut.Lock()
|
|
|
|
{{$varNameSingular}}InsertCache[key] = cache
|
|
|
|
{{$varNameSingular}}InsertCacheMut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
{{if not .NoHooks -}}
|
|
|
|
return o.doAfterInsertHooks(exec)
|
|
|
|
{{- else -}}
|
|
|
|
return nil
|
|
|
|
{{- end}}
|
2016-02-23 13:38:24 +01:00
|
|
|
}
|