2016-06-20 07:22:50 +02:00
{ { - $tableNameSingular : = . Table . Name | singular | titleCase - } }
{ { - $varNameSingular : = . Table . Name | singular | camelCase - } }
2016-06-27 08:56:05 +02:00
{ { - $colDefs : = sqlColDefinitions . Table . Columns . Table . PKey . Columns - } }
2017-02-25 07:27:05 +01:00
{ { - $pkNames : = $colDefs.Names | stringMap . StringFuncs . camelCase | stringMap . StringFuncs . replaceReserved - } }
2016-09-24 09:52:18 +02:00
{ { - $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) {
2016-09-14 10:08:30 +02:00
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 } } {
2016-09-14 10:08:30 +02:00
retobj, err := Find { { $tableNameSingular } } (boil.GetDB(), { { $pkNames | join ", " } } , selectCols...)
if err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
2016-09-14 10:08:30 +02:00
}
2016-07-16 13:22:57 +02:00
2016-09-14 10:08:30 +02:00
return retobj
2016-07-16 13:22:57 +02:00
}
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) {
2016-09-14 10:08:30 +02:00
{ { $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 ", " } } )
2016-09-14 10:08:30 +02:00
err := q.Bind( { { $varNameSingular } } Obj)
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
2016-09-14 10:08:30 +02:00
}
2018-02-07 15:35:46 +01:00
return nil, errors.Prefix(" { { . PkgName } } : unable to select from { { . Table . Name } } ", err)
2016-09-14 10:08:30 +02:00
}
return { { $varNameSingular } } Obj, nil
2016-02-29 12:45:28 +01:00
}
2016-07-16 13:22:57 +02:00
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 } } {
2016-09-14 10:08:30 +02:00
retobj, err := Find { { $tableNameSingular } } (exec, { { $pkNames | join ", " } } , selectCols...)
if err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
2016-09-14 10:08:30 +02:00
}
2016-07-16 13:22:57 +02:00
2016-09-14 10:08:30 +02:00
return retobj
2016-07-16 13:22:57 +02:00
}
2017-05-08 22:30:40 +02:00
// FindOne { { $tableNameSingular } } retrieves a single record using filters.
func FindOne { { $tableNameSingular } } (exec boil.Executor, filters { { $tableNameSingular } } Filter) (* { { $tableNameSingular } } , error) {
2017-05-09 21:30:04 +02:00
obj := & { { $tableNameSingular } } { }
2017-05-08 22:30:40 +02:00
2017-05-09 21:30:04 +02:00
err := { { $tableNameSingular } } NewQuery(exec).
Where(filters).
Limit(1).
Bind(obj)
2017-05-08 22:30:40 +02:00
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
2017-05-08 22:30:40 +02:00
}
2018-02-07 15:35:46 +01:00
return nil, errors.Prefix(" { { . PkgName } } : unable to select from { { . Table . Name } } ", err)
2017-05-08 22:30:40 +02:00
}
2017-05-09 21:30:04 +02:00
return obj, nil
2017-05-08 22:30:40 +02:00
}
// FindOne { { $tableNameSingular } } G retrieves a single record using filters.
func FindOne { { $tableNameSingular } } G(filters { { $tableNameSingular } } Filter) (* { { $tableNameSingular } } , error) {
return FindOne { { $tableNameSingular } } (boil.GetDB(), filters)
}
// FindOne { { $tableNameSingular } } OrInit retrieves a single record using filters, or initializes a new record if one is not found.
func FindOne { { $tableNameSingular } } OrInit(exec boil.Executor, filters { { $tableNameSingular } } Filter) (* { { $tableNameSingular } } , error) {
{ { $varNameSingular } } Obj, err := FindOne { { $tableNameSingular } } (exec, filters)
2017-05-10 17:21:29 +02:00
if err != nil {
2017-05-08 22:30:40 +02:00
return nil, err
}
if { { $varNameSingular } } Obj == nil {
{ { $varNameSingular } } Obj = & { { $tableNameSingular } } { }
objR := reflect.ValueOf( { { $varNameSingular } } Obj).Elem()
r := reflect.ValueOf(filters)
for i := 0; i < r.NumField(); i++ {
f := r.Field(i)
if f.Elem().IsValid() {
objR.FieldByName(r.Type().Field(i).Name).Set(f.Elem())
}
}
}
return { { $varNameSingular } } Obj, nil
}
// FindOne { { $tableNameSingular } } OrInit retrieves a single record using filters, or initializes a new record if one is not found.
func FindOne { { $tableNameSingular } } OrInitG(filters { { $tableNameSingular } } Filter) (* { { $tableNameSingular } } , error) {
return FindOne { { $tableNameSingular } } OrInit(boil.GetDB(), filters)
}
// FindOne { { $tableNameSingular } } OrInit retrieves a single record using filters, or initializes and inserts a new record if one is not found.
func FindOne { { $tableNameSingular } } OrCreate(exec boil.Executor, filters { { $tableNameSingular } } Filter) (* { { $tableNameSingular } } , error) {
{ { $varNameSingular } } Obj, err := FindOne { { $tableNameSingular } } OrInit(exec, filters)
if err != nil {
return nil, err
}
if { { $varNameSingular } } Obj.IsNew() {
err := { { $varNameSingular } } Obj.Insert(exec)
if err != nil {
return nil, err
}
}
return { { $varNameSingular } } Obj, nil
}
// FindOne { { $tableNameSingular } } OrInit retrieves a single record using filters, or initializes and inserts a new record if one is not found.
func FindOne { { $tableNameSingular } } OrCreateG(filters { { $tableNameSingular } } Filter) (* { { $tableNameSingular } } , error) {
return FindOne { { $tableNameSingular } } OrCreate(boil.GetDB(), filters)
}