sqlboiler/templates/03_finishers.tpl

118 lines
2.9 KiB
Smarty
Raw Normal View History

{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
// OneP returns a single {{$tableNameSingular}} record from the query, and panics on error.
func (q {{$tableNameSingular}}Query) OneP() (*{{$tableNameSingular}}) {
o, err := q.One()
if err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
}
return o
}
// One returns a single {{$tableNameSingular}} record from the query.
func (q {{$tableNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
o := &{{$tableNameSingular}}{}
2016-09-15 05:59:55 +02:00
queries.SetLimit(q.Query, 1)
err := q.Bind(o)
if err != nil {
2018-02-07 15:35:46 +01:00
if errors.Is(err, sql.ErrNoRows) {
2017-05-10 17:21:29 +02:00
return nil, nil
}
2018-02-07 15:35:46 +01:00
return nil, errors.Prefix("{{.PkgName}}: failed to execute a one query for {{.Table.Name}}", err)
}
{{if not .NoHooks -}}
2016-09-15 05:59:55 +02:00
if err := o.doAfterSelectHooks(queries.GetExecutor(q.Query)); err != nil {
return o, err
}
{{- end}}
return o, nil
}
// AllP returns all {{$tableNameSingular}} records from the query, and panics on error.
func (q {{$tableNameSingular}}Query) AllP() {{$tableNameSingular}}Slice {
o, err := q.All()
if err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
}
return o
}
// All returns all {{$tableNameSingular}} records from the query.
func (q {{$tableNameSingular}}Query) All() ({{$tableNameSingular}}Slice, error) {
var o []*{{$tableNameSingular}}
err := q.Bind(&o)
if err != nil {
2018-02-07 15:35:46 +01:00
return nil, errors.Prefix("{{.PkgName}}: failed to assign all query results to {{$tableNameSingular}} slice", err)
}
{{if not .NoHooks -}}
if len({{$varNameSingular}}AfterSelectHooks) != 0 {
for _, obj := range o {
2016-09-15 05:59:55 +02:00
if err := obj.doAfterSelectHooks(queries.GetExecutor(q.Query)); err != nil {
return o, err
}
}
}
{{- end}}
return o, nil
}
// CountP returns the count of all {{$tableNameSingular}} records in the query, and panics on error.
func (q {{$tableNameSingular}}Query) CountP() int64 {
c, err := q.Count()
if err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
}
return c
}
// Count returns the count of all {{$tableNameSingular}} records in the query.
func (q {{$tableNameSingular}}Query) Count() (int64, error) {
var count int64
2016-05-10 12:20:29 +02:00
2016-09-15 05:59:55 +02:00
queries.SetSelect(q.Query, nil)
queries.SetCount(q.Query)
2016-05-10 12:20:29 +02:00
err := q.Query.QueryRow().Scan(&count)
if err != nil {
2018-02-07 15:35:46 +01:00
return 0, errors.Prefix("{{.PkgName}}: failed to count {{.Table.Name}} rows", err)
}
2016-05-10 12:20:29 +02:00
return count, nil
}
// Exists checks if the row exists in the table, and panics on error.
func (q {{$tableNameSingular}}Query) ExistsP() bool {
e, err := q.Exists()
if err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
}
return e
}
2016-08-03 12:23:43 +02:00
// Exists checks if the row exists in the table.
func (q {{$tableNameSingular}}Query) Exists() (bool, error) {
var count int64
2016-08-03 12:23:43 +02:00
2016-09-15 05:59:55 +02:00
queries.SetCount(q.Query)
2017-07-20 16:50:55 +02:00
queries.SetSelect(q.Query, []string{})
2016-09-15 05:59:55 +02:00
queries.SetLimit(q.Query, 1)
2016-08-03 12:23:43 +02:00
err := q.Query.QueryRow().Scan(&count)
if err != nil {
2018-02-07 15:35:46 +01:00
return false, errors.Prefix("{{.PkgName}}: failed to check if {{.Table.Name}} exists", err)
}
2016-08-03 12:23:43 +02:00
return count > 0, nil
2016-08-03 12:23:43 +02:00
}