sqlboiler/cmds/templates/insert.tpl

17 lines
662 B
Smarty
Raw Normal View History

2016-03-23 07:18:41 +01:00
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
// {{$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-23 07:18:41 +01:00
return 0, errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
2016-02-23 13:38:24 +01:00
}
var rowID int
2016-03-23 07:18:41 +01:00
err := db.QueryRow(`INSERT INTO {{.Table.Name}} ({{insertParamNames .Table.Columns}}) VALUES({{insertParamFlags .Table.Columns}}) RETURNING id`, {{insertParamVariables "o." .Table.Columns}}).Scan(&rowID)
2016-02-23 13:38:24 +01:00
if err != nil {
2016-03-23 07:18:41 +01:00
return 0, fmt.Errorf("{{.PkgName}}: unable to insert {{.Table.Name}}: %s", err)
2016-02-23 13:38:24 +01:00
}
return rowID, nil
}