2016-03-18 12:26:48 +01:00
|
|
|
{{- $tableNameSingular := titleCaseSingular .Table -}}
|
|
|
|
// {{$tableNameSingular}}Insert inserts a single record.
|
|
|
|
func {{$tableNameSingular}}Insert(db boil.DB, o *{{$tableNameSingular}}) (int, error) {
|
2016-02-23 13:38:24 +01:00
|
|
|
if o == nil {
|
2016-03-18 12:26:48 +01:00
|
|
|
return 0, errors.New("{{.PkgName}}: no {{.Table}} provided for insertion")
|
2016-02-23 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var rowID int
|
2016-03-18 12:26:48 +01:00
|
|
|
err := db.QueryRow(`INSERT INTO {{.Table}} ({{insertParamNames .Columns}}) VALUES({{insertParamFlags .Columns}}) RETURNING id`, {{insertParamVariables "o." .Columns}}).Scan(&rowID)
|
2016-02-23 13:38:24 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2016-03-18 12:26:48 +01:00
|
|
|
return 0, fmt.Errorf("{{.PkgName}}: unable to insert {{.Table}}: %s", err)
|
2016-02-23 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rowID, nil
|
|
|
|
}
|