2016-05-02 08:34:25 +02:00
|
|
|
{{- if hasPrimaryKey .Table.PKey -}}
|
2016-03-23 07:18:41 +01:00
|
|
|
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
2016-05-02 08:34:25 +02:00
|
|
|
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
|
2016-03-18 12:26:48 +01:00
|
|
|
// {{$tableNameSingular}}Insert inserts a single record.
|
2016-04-19 04:02:32 +02:00
|
|
|
func (o *{{$tableNameSingular}}) Insert(whitelist ... string) error {
|
2016-05-02 08:34:25 +02:00
|
|
|
return o.InsertX(boil.GetDB(), whitelist...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *{{$tableNameSingular}}) InsertX(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-05-06 11:31:51 +02:00
|
|
|
var wl []string
|
|
|
|
|
|
|
|
wl = append(wl, whitelist...)
|
2016-05-02 08:34:25 +02:00
|
|
|
if len(whitelist) == 0 {
|
2016-05-06 11:31:51 +02:00
|
|
|
wl = append(wl, {{$varNameSingular}}ColumnsWithoutDefault...)
|
2016-04-17 11:25:09 +02:00
|
|
|
}
|
|
|
|
|
2016-05-06 11:31:51 +02:00
|
|
|
wl = append(boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o), wl...)
|
2016-06-07 09:08:00 +02:00
|
|
|
wl = boil.SortByKeys({{$varNameSingular}}Columns, wl)
|
2016-04-17 11:25:09 +02:00
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
// Only return the columns with default values that are not in the insert whitelist
|
2016-05-06 11:31:51 +02:00
|
|
|
returnColumns := boil.SetComplement({{$varNameSingular}}ColumnsWithDefault, wl)
|
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-05-06 11:31:51 +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-05-02 08:34:25 +02:00
|
|
|
{{if supportsResultObject .DriverName}}
|
|
|
|
if len(returnColumns) != 0 {
|
2016-05-17 12:00:56 +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-05-06 11:31:51 +02:00
|
|
|
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, ","), boil.WhereClause(wl))
|
2016-05-17 12:00:56 +02:00
|
|
|
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-05-17 12:00:56 +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-06-02 23:07:51 +02:00
|
|
|
err = exec.QueryRow(ins, boil.GetStructValues(o, wl...)...).Scan(boil.GetStructPointers(o, returnColumns...)...)
|
2016-05-02 08:34:25 +02:00
|
|
|
} else {
|
|
|
|
_, err = exec.Exec(ins, {{insertParamVariables "o." .Table.Columns}})
|
|
|
|
}
|
|
|
|
{{end}}
|
2016-02-23 13:38:24 +01:00
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
if boil.DebugMode {
|
2016-06-07 08:21:00 +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-05-02 08:34:25 +02:00
|
|
|
{{- end -}}
|