sqlboiler/cmds/templates/find.tpl

17 lines
617 B
Smarty
Raw Normal View History

{{- $tableName := .Table -}}
// {{titleCase $tableName}}Find retrieves a single record by ID.
func {{titleCase $tableName}}Find(db boil.DB, id int) (*{{titleCase $tableName}}, error) {
if id == 0 {
return nil, errors.New("{{.PkgName}}: no id provided for {{$tableName}} select")
}
{{$varName := camelCase $tableName}}
var {{$varName}} *{{titleCase $tableName}}
err := db.Select(&{{$varName}}, `SELECT {{selectParamNames $tableName .Columns}} WHERE id=$1`, id)
if err != nil {
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{$tableName}}: %s", err)
}
return {{$varName}}, nil
}