sqlboiler/cmds/templates/find.tpl
2016-03-22 23:18:41 -07:00

17 lines
728 B
Smarty

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