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-02 08:34:25 +02:00
|
|
|
if len(whitelist) == 0 {
|
2016-05-05 11:01:24 +02:00
|
|
|
whitelist = {{$varNameSingular}}ColumnsWithoutDefault
|
2016-04-17 11:25:09 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
nzDefaultSet := boil.NonZeroDefaultSet({{$varNameSingular}}ColumnsWithDefault, o)
|
|
|
|
if len(nzDefaultSet) != 0 {
|
|
|
|
whitelist = append(nzDefaultSet, whitelist...)
|
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
|
|
|
|
returnColumns := boil.SetComplement({{$varNameSingular}}ColumnsWithDefault, whitelist)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if err := o.doBeforeCreateHooks(); err != nil {
|
|
|
|
return err
|
2016-04-17 11:25:09 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} (%s) VALUES (%s)`, strings.Join(whitelist, ","), boil.GenerateParamFlags(len(whitelist), 1))
|
2016-04-17 11:25:09 +02:00
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
{{if supportsResultObject .DriverName}}
|
|
|
|
if len(returnColumns) != 0 {
|
|
|
|
result, err := exec.Exec(ins, boil.GetStructValues(o, whitelist...))
|
|
|
|
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 {
|
|
|
|
sel := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, ","), boil.WhereClause(whitelist))
|
|
|
|
rows, err := exec.Query(sel, boil.GetStructValues(o, whitelist...))
|
|
|
|
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 {
|
|
|
|
_, err = exec.Exec(ins, boil.GetStructValues(o, whitelist...))
|
|
|
|
}
|
|
|
|
{{else}}
|
|
|
|
if len(returnColumns) != 0 {
|
|
|
|
ins = ins + fmt.Sprintf(` RETURNING %s`, strings.Join(returnColumns, ","))
|
|
|
|
err = exec.QueryRow(ins, boil.GetStructValues(o, whitelist...)).Scan(boil.GetStructPointers(o, returnColumns...))
|
|
|
|
} else {
|
|
|
|
_, err = exec.Exec(ins, {{insertParamVariables "o." .Table.Columns}})
|
|
|
|
}
|
|
|
|
{{end}}
|
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 -}}
|