sqlboiler/templates/14_find.tpl

56 lines
2.3 KiB
Smarty
Raw Permalink Normal View History

{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase | stringMap .StringFuncs.replaceReserved -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
2016-09-15 06:20:35 +02:00
// Find{{$tableNameSingular}}G retrieves a single record by ID.
2016-09-01 03:16:21 +02:00
func Find{{$tableNameSingular}}G({{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
return Find{{$tableNameSingular}}(boil.GetDB(), {{$pkNames | join ", "}}, selectCols...)
2016-04-24 07:33:05 +02:00
}
2016-09-15 06:20:35 +02:00
// Find{{$tableNameSingular}}GP retrieves a single record by ID, and panics on error.
2016-09-01 03:16:21 +02:00
func Find{{$tableNameSingular}}GP({{$pkArgs}}, selectCols ...string) *{{$tableNameSingular}} {
retobj, err := Find{{$tableNameSingular}}(boil.GetDB(), {{$pkNames | join ", "}}, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return retobj
}
2016-09-15 06:20:35 +02:00
// Find{{$tableNameSingular}} 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-09-01 03:16:21 +02:00
func Find{{$tableNameSingular}}(exec boil.Executor, {{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
{{$varNameSingular}}Obj := &{{$tableNameSingular}}{}
sel := "*"
if len(selectCols) > 0 {
sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
}
query := fmt.Sprintf(
"select %s from {{.Table.Name | .SchemaTable}} where {{if .Dialect.IndexPlaceholders}}{{whereClause .LQ .RQ 1 .Table.PKey.Columns}}{{else}}{{whereClause .LQ .RQ 0 .Table.PKey.Columns}}{{end}}", sel,
)
2016-09-15 05:59:55 +02:00
q := queries.Raw(exec, query, {{$pkNames | join ", "}})
err := q.Bind({{$varNameSingular}}Obj)
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}}Obj, nil
}
2016-09-15 06:20:35 +02:00
// Find{{$tableNameSingular}}P retrieves a single record by ID with an executor, and panics on error.
2016-09-01 03:16:21 +02:00
func Find{{$tableNameSingular}}P(exec boil.Executor, {{$pkArgs}}, selectCols ...string) *{{$tableNameSingular}} {
retobj, err := Find{{$tableNameSingular}}(exec, {{$pkNames | join ", "}}, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return retobj
}