sqlboiler/templates/07_find.tpl

58 lines
2.2 KiB
Smarty
Raw Normal View History

{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
2016-03-23 07:18:41 +01:00
{{- $dbName := singular .Table.Name -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
2016-08-01 07:10:10 +02:00
// {{$tableNameSingular}}FindG retrieves a single record by ID.
func {{$tableNameSingular}}FindG({{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
return {{$tableNameSingular}}Find(boil.GetDB(), {{$pkNames | join ", "}}, selectCols...)
2016-04-24 07:33:05 +02:00
}
2016-08-01 07:10:10 +02:00
// {{$tableNameSingular}}FindGP retrieves a single record by ID, and panics on error.
func {{$tableNameSingular}}FindGP({{$pkArgs}}, selectCols ...string) *{{$tableNameSingular}} {
2016-08-05 07:03:37 +02:00
retobj, err := {{$tableNameSingular}}Find(boil.GetDB(), {{$pkNames | join ", "}}, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
2016-08-05 07:03:37 +02:00
return retobj
}
2016-08-01 07:10:10 +02:00
// {{$tableNameSingular}}Find retrieves a single record by ID with an executor.
2016-08-02 11:56:55 +02:00
// If selectCols is empty Find will return all columns.
2016-08-01 07:10:10 +02:00
func {{$tableNameSingular}}Find(exec boil.Executor, {{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
{{$varNameSingular}} := &{{$tableNameSingular}}{}
2016-08-08 08:36:11 +02:00
sel := "*"
if len(selectCols) > 0 {
sel = strings.Join(strmangle.IdentQuoteSlice(selectCols), ",")
}
query := fmt.Sprintf(
`select %s from "{{.Table.Name}}" where {{whereClause 1 .Table.PKey.Columns}}`, sel,
2016-08-08 08:36:11 +02:00
)
2016-08-13 18:58:18 +02:00
q := boil.SQL(query, {{$pkNames | join ", "}})
2016-08-08 08:36:11 +02:00
boil.SetExecutor(q, exec)
2016-08-08 08:07:15 +02:00
err := q.Bind({{$varNameSingular}})
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, sql.ErrNoRows
}
return nil, errors.Wrap(err, "{{.PkgName}}: unable to select from {{.Table.Name}}")
}
return {{$varNameSingular}}, nil
}
2016-08-01 07:10:10 +02:00
// {{$tableNameSingular}}FindP retrieves a single record by ID with an executor, and panics on error.
func {{$tableNameSingular}}FindP(exec boil.Executor, {{$pkArgs}}, selectCols ...string) *{{$tableNameSingular}} {
2016-08-05 07:03:37 +02:00
retobj, err := {{$tableNameSingular}}Find(exec, {{$pkNames | join ", "}}, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
2016-08-05 07:03:37 +02:00
return retobj
}