sqlboiler/cmds/templates/find.tpl

17 lines
635 B
Smarty
Raw Normal View History

{{- $tableName := .TableName -}}
// {{makeGoColName $tableName}}Find retrieves a single record by ID.
func {{makeGoColName $tableName}}Find(db *sqlx.DB, id int) (*{{makeGoColName $tableName}}, error) {
if id == 0 {
return nil, errors.New("model: no id provided for {{$tableName}} select")
}
{{$varName := makeGoVarName $tableName}}
var {{$varName}} *{{makeGoColName $tableName}}
err := db.Select(&{{$varName}}, `SELECT {{makeSelectParamNames $tableName .TableData}} WHERE id=$1`, id)
if err != nil {
return nil, fmt.Errorf("models: unable to select from {{$tableName}}: %s", err)
}
return {{$varName}}, nil
}