2016-06-20 07:22:50 +02:00
|
|
|
{{- $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-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) {
|
|
|
|
if err := o.Insert(boil.GetDB(), whitelist...); err != nil {
|
2016-07-16 13:22:57 +02:00
|
|
|
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:
|
|
|
|
// - All columns without a default value are inferred (i.e. name, age)
|
|
|
|
// - All columns with a default, but non-zero are inferred (i.e. health = 75)
|
2016-08-01 07:10:10 +02:00
|
|
|
func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string) error {
|
2016-04-17 11:25:09 +02:00
|
|
|
if o == nil {
|
2016-05-02 08:34:25 +02:00
|
|
|
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
|
2016-04-17 11:25:09 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 07:09:05 +02:00
|
|
|
wl, returnColumns := o.generateInsertColumns(whitelist...)
|
2016-05-02 08:34:25 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if err := o.doBeforeCreateHooks(); err != nil {
|
|
|
|
return err
|
2016-04-17 11:25:09 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), boil.GenerateParamFlags(len(wl), 1))
|
2016-04-17 11:25:09 +02:00
|
|
|
|
2016-06-20 07:22:50 +02:00
|
|
|
{{if driverUsesLastInsertID .DriverName}}
|
2016-05-02 08:34:25 +02:00
|
|
|
if len(returnColumns) != 0 {
|
2016-07-13 18:51:40 +02:00
|
|
|
result, err := exec.Exec(ins, boil.GetStructValues(o, wl...)...)
|
2016-05-02 08:34:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to insert into {{.Table.Name}}: %s", err)
|
|
|
|
}
|
2016-02-23 13:38:24 +01:00
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
lastId, err := result.lastInsertId()
|
|
|
|
if err != nil || lastId == 0 {
|
2016-07-13 18:51:40 +02:00
|
|
|
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, `","`), boil.WhereClause(wl))
|
|
|
|
rows, err := exec.Query(sel, boil.GetStructValues(o, wl...)...)
|
2016-05-02 08:34:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to insert into {{.Table.Name}}: %s", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2016-04-13 15:51:58 +02:00
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
i := 0
|
|
|
|
ptrs := boil.GetStructPointers(o, returnColumns...)
|
|
|
|
for rows.Next() {
|
|
|
|
if err := rows.Scan(ptrs[i]); err != nil {
|
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to get result of insert, scan failed for column %s index %d: %s\n\n%#v", returnColumns[i], i, err, ptrs)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
} else if {{$varNameSingular}}AutoIncPrimKey != "" {
|
|
|
|
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s=$1`, strings.Join(returnColumns, ","), {{$varNameSingular}}AutoIncPrimaryKey, lastId)
|
|
|
|
}
|
|
|
|
} else {
|
2016-07-13 18:51:40 +02:00
|
|
|
_, err = exec.Exec(ins, boil.GetStructValues(o, wl...)...)
|
2016-05-02 08:34:25 +02:00
|
|
|
}
|
|
|
|
{{else}}
|
|
|
|
if len(returnColumns) != 0 {
|
|
|
|
ins = ins + fmt.Sprintf(` RETURNING %s`, strings.Join(returnColumns, ","))
|
2016-07-13 18:51:40 +02:00
|
|
|
err = exec.QueryRow(ins, boil.GetStructValues(o, wl...)...).Scan(boil.GetStructPointers(o, returnColumns...)...)
|
2016-05-02 08:34:25 +02:00
|
|
|
} else {
|
2016-06-20 07:57:46 +02:00
|
|
|
_, err = exec.Exec(ins, {{.Table.Columns | columnNames | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
|
2016-05-02 08:34:25 +02:00
|
|
|
}
|
|
|
|
{{end}}
|
2016-02-23 13:38:24 +01:00
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
if boil.DebugMode {
|
2016-07-13 18:51:40 +02:00
|
|
|
fmt.Fprintln(boil.DebugWriter, ins, boil.GetStructValues(o, wl...))
|
2016-06-02 23:07:51 +02:00
|
|
|
}
|
|
|
|
|
2016-02-23 13:38:24 +01:00
|
|
|
if err != nil {
|
2016-05-02 08:34:25 +02:00
|
|
|
return fmt.Errorf("{{.PkgName}}: unable to insert into {{.Table.Name}}: %s", err)
|
2016-02-23 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2016-04-13 15:51:58 +02:00
|
|
|
if err := o.doAfterCreateHooks(); err != nil {
|
2016-05-02 08:34:25 +02:00
|
|
|
return err
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
return nil
|
2016-02-23 13:38:24 +01:00
|
|
|
}
|
2016-07-13 18:51:40 +02:00
|
|
|
|
|
|
|
// generateInsertColumns generates the whitelist columns and return columns for an insert statement
|
2016-08-04 05:44:42 +02:00
|
|
|
// the return columns are used to get values that are assigned within the database during the
|
|
|
|
// insert to keep the struct in sync with what's in the db.
|
|
|
|
// with a whitelist:
|
|
|
|
// - the whitelist is used for the insert columns
|
|
|
|
// - the return columns are the result of (columns with default values - the whitelist)
|
|
|
|
// without a whitelist:
|
|
|
|
// - start with columns without a default as these always need to be inserted
|
|
|
|
// - add all columns that have a default in the database but that are non-zero in the struct
|
|
|
|
// - the return columns are the result of (columns with default values - the previous set)
|
2016-07-14 07:09:05 +02:00
|
|
|
func (o *{{$tableNameSingular}}) generateInsertColumns(whitelist ...string) ([]string, []string) {
|
2016-08-04 05:55:25 +02:00
|
|
|
if len(whitelist) != 0 {
|
|
|
|
return whitelist, boil.SetComplement({{$varNameSingular}}ColumnsWithDefault, whitelist)
|
|
|
|
}
|
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
var wl []string
|
|
|
|
|
2016-08-04 05:55:25 +02:00
|
|
|
wl = append(wl, {{$varNameSingular}}ColumnsWithoutDefault...)
|
2016-07-13 18:51:40 +02:00
|
|
|
|
|
|
|
wl = append(boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o), wl...)
|
|
|
|
wl = boil.SortByKeys({{$varNameSingular}}Columns, wl)
|
|
|
|
|
|
|
|
// Only return the columns with default values that are not in the insert whitelist
|
|
|
|
rc := boil.SetComplement({{$varNameSingular}}ColumnsWithDefault, wl)
|
|
|
|
|
|
|
|
return wl, rc
|
|
|
|
}
|