sqlboiler/cmds/templates/find.tpl

29 lines
1.2 KiB
Smarty
Raw Normal View History

{{- if hasPrimaryKey .Table.PKey -}}
2016-03-23 07:18:41 +01:00
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
{{- $dbName := singular .Table.Name -}}
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
// {{$tableNameSingular}}Find retrieves a single record by ID.
func {{$tableNameSingular}}Find({{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
return {{$tableNameSingular}}FindX(boil.GetDB(), {{camelCaseCommaList "" .Table.PKey.Columns}}, selectCols...)
2016-04-24 07:33:05 +02:00
}
func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
2016-04-24 07:33:05 +02:00
var {{$varNameSingular}} *{{$tableNameSingular}}
mods := []qs.QueryMod{
qs.Select(selectCols...),
qs.Table("{{.Table.Name}}"),
qs.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{camelCaseCommaList "" .Table.PKey.Columns}}),
}
2016-04-24 07:33:05 +02:00
q := NewQueryX(exec, mods...)
err := boil.ExecQueryOne(q).Scan(boil.GetStructPointers(&{{$varNameSingular}}, selectCols...)...)
if err != nil {
2016-03-23 07:18:41 +01:00
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
}
return {{$varNameSingular}}, nil
}
{{- end -}}