sqlboiler/templates/insert.tpl

21 lines
569 B
Smarty
Raw Normal View History

2016-02-23 13:38:24 +01:00
{{- $tableName := .TableName -}}
func Insert{{makeGoColName $tableName}}(o *{{makeGoColName $tableName}}, db *sqlx.DB) (int, error) {
if o == nil {
return 0, errors.New("No {{makeGoColName $tableName}} provided for insertion")
}
var rowID int
err := db.QueryRow(`
INSERT INTO {{$tableName}}
({{makeGoInsertParamNames .TableData}})
VALUES({{makeGoInsertParamFlags .TableData}})
RETURNING id
`)
if err != nil {
return 0, fmt.Errorf("Unable to insert {{$tableName}}: %s", err)
}
return rowID, nil
}