2016-06-20 07:22:50 +02:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
2017-05-09 21:30:04 +02:00
|
|
|
// OneP returns a single {{$tableNameSingular}} record from the query, and panics on error.
|
|
|
|
func (q {{$tableNameSingular}}Query) OneP() (*{{$tableNameSingular}}) {
|
2016-09-14 10:08:30 +02:00
|
|
|
o, err := q.One()
|
|
|
|
if err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
2016-08-09 12:19:42 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return o
|
2016-08-09 12:19:42 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 21:30:04 +02:00
|
|
|
// One returns a single {{$tableNameSingular}} record from the query.
|
|
|
|
func (q {{$tableNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
|
2016-09-14 10:08:30 +02:00
|
|
|
o := &{{$tableNameSingular}}{}
|
2016-05-03 15:42:25 +02:00
|
|
|
|
2016-09-15 05:59:55 +02:00
|
|
|
queries.SetLimit(q.Query, 1)
|
2016-05-03 15:42:25 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
err := q.Bind(o)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == sql.ErrNoRows {
|
|
|
|
return nil, sql.ErrNoRows
|
|
|
|
}
|
|
|
|
return nil, errors.Wrap(err, "{{.PkgName}}: failed to execute a one query for {{.Table.Name}}")
|
|
|
|
}
|
2016-05-03 15:42:25 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{if not .NoHooks -}}
|
2016-09-15 05:59:55 +02:00
|
|
|
if err := o.doAfterSelectHooks(queries.GetExecutor(q.Query)); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
return o, err
|
|
|
|
}
|
|
|
|
{{- end}}
|
2016-08-28 14:02:21 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return o, nil
|
2016-04-17 11:25:09 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:19:42 +02:00
|
|
|
// AllP returns all {{$tableNameSingular}} records from the query, and panics on error.
|
2017-05-09 21:30:04 +02:00
|
|
|
func (q {{$tableNameSingular}}Query) AllP() {{$tableNameSingular}}Slice {
|
2016-09-14 10:08:30 +02:00
|
|
|
o, err := q.All()
|
|
|
|
if err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
2016-07-16 13:22:57 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return o
|
2016-07-16 13:22:57 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 01:29:59 +02:00
|
|
|
// All returns all {{$tableNameSingular}} records from the query.
|
2017-05-09 21:30:04 +02:00
|
|
|
func (q {{$tableNameSingular}}Query) All() ({{$tableNameSingular}}Slice, error) {
|
2017-04-05 04:44:36 +02:00
|
|
|
var o []*{{$tableNameSingular}}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
err := q.Bind(&o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "{{.PkgName}}: failed to assign all query results to {{$tableNameSingular}} slice")
|
|
|
|
}
|
|
|
|
|
|
|
|
{{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 {
|
2016-09-14 10:08:30 +02:00
|
|
|
return o, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{{- end}}
|
|
|
|
|
|
|
|
return o, nil
|
2016-04-17 11:25:09 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:19:42 +02:00
|
|
|
// CountP returns the count of all {{$tableNameSingular}} records in the query, and panics on error.
|
2017-05-09 21:30:04 +02:00
|
|
|
func (q {{$tableNameSingular}}Query) CountP() int64 {
|
2016-09-14 10:08:30 +02:00
|
|
|
c, err := q.Count()
|
|
|
|
if err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
2016-07-16 13:22:57 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return c
|
2016-07-16 13:22:57 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 01:29:59 +02:00
|
|
|
// Count returns the count of all {{$tableNameSingular}} records in the query.
|
2017-05-09 21:30:04 +02:00
|
|
|
func (q {{$tableNameSingular}}Query) Count() (int64, error) {
|
2016-09-14 10:08:30 +02:00
|
|
|
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
|
|
|
|
2016-09-14 18:14:30 +02:00
|
|
|
err := q.Query.QueryRow().Scan(&count)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "{{.PkgName}}: failed to count {{.Table.Name}} rows")
|
|
|
|
}
|
2016-05-10 12:20:29 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return count, nil
|
2016-04-17 11:25:09 +02:00
|
|
|
}
|
2016-07-16 13:22:57 +02:00
|
|
|
|
2016-08-09 12:19:42 +02:00
|
|
|
// Exists checks if the row exists in the table, and panics on error.
|
2017-05-09 21:30:04 +02:00
|
|
|
func (q {{$tableNameSingular}}Query) ExistsP() bool {
|
2016-09-14 10:08:30 +02:00
|
|
|
e, err := q.Exists()
|
|
|
|
if err != nil {
|
|
|
|
panic(boil.WrapErr(err))
|
|
|
|
}
|
2016-07-16 13:22:57 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return e
|
2016-07-16 13:22:57 +02:00
|
|
|
}
|
2016-08-03 12:23:43 +02:00
|
|
|
|
|
|
|
// Exists checks if the row exists in the table.
|
2017-05-09 21:30:04 +02:00
|
|
|
func (q {{$tableNameSingular}}Query) Exists() (bool, error) {
|
2016-09-14 10:08:30 +02:00
|
|
|
var count int64
|
2016-08-03 12:23:43 +02:00
|
|
|
|
2016-09-15 05:59:55 +02:00
|
|
|
queries.SetCount(q.Query)
|
|
|
|
queries.SetLimit(q.Query, 1)
|
2016-08-03 12:23:43 +02:00
|
|
|
|
2016-09-14 18:14:30 +02:00
|
|
|
err := q.Query.QueryRow().Scan(&count)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "{{.PkgName}}: failed to check if {{.Table.Name}} exists")
|
|
|
|
}
|
2016-08-03 12:23:43 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return count > 0, nil
|
2016-08-03 12:23:43 +02:00
|
|
|
}
|