sqlboiler/templates/03_finishers.tpl

49 lines
1.5 KiB
Smarty
Raw Normal View History

{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
// One returns a single {{$varNameSingular}} record from the query.
func (q {{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
o := &{{$tableNameSingular}}{}
2016-05-10 12:20:29 +02:00
boil.SetLimit(q.Query, 1)
2016-05-10 12:20:29 +02:00
res := boil.ExecQueryOne(q.Query)
2016-06-07 06:38:17 +02:00
err := boil.BindOne(res, boil.Select(q.Query), o)
2016-05-10 12:20:29 +02:00
if err != nil {
return nil, fmt.Errorf("{{.PkgName}}: failed to execute a one query for {{.Table.Name}}: %s", err)
}
2016-05-10 12:20:29 +02:00
return o, nil
}
// All returns all {{$tableNameSingular}} records from the query.
func (q {{$varNameSingular}}Query) All() ({{$varNameSingular}}Slice, error) {
2016-06-07 06:38:17 +02:00
var o {{$varNameSingular}}Slice
2016-05-10 12:20:29 +02:00
res, err := boil.ExecQueryAll(q.Query)
if err != nil {
return nil, fmt.Errorf("{{.PkgName}}: failed to execute an all query for {{.Table.Name}}: %s", err)
}
defer res.Close()
2016-05-10 12:20:29 +02:00
2016-06-07 06:38:17 +02:00
err = boil.BindAll(res, boil.Select(q.Query), &o)
2016-05-10 12:20:29 +02:00
if err != nil {
return nil, fmt.Errorf("{{.PkgName}}: failed to assign all query results to {{$tableNameSingular}} slice: %s", err)
}
return o, nil
}
// Count returns the count of all {{$tableNameSingular}} records in the query.
func (q {{$varNameSingular}}Query) Count() (int64, error) {
2016-05-10 12:20:29 +02:00
var count int64
boil.SetCount(q.Query)
err := boil.ExecQueryOne(q.Query).Scan(&count)
if err != nil {
return 0, fmt.Errorf("{{.PkgName}}: failed to count {{.Table.Name}} rows: %s", err)
}
return count, nil
}