2016-08-03 12:23:43 +02:00
{ { - $tableNameSingular : = . Table . Name | singular | titleCase - } }
2017-05-08 22:30:40 +02:00
{ { - $varNameSingular : = . Table . Name | singular | camelCase - } }
2016-08-03 12:23:43 +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-08-21 08:28:47 +02:00
{ { - $pkArgs : = joinSlices " " $pkNames $colDefs.Types | join ", " - } }
2016-09-24 09:52:18 +02:00
{ { - $schemaTable : = . Table . Name | . SchemaTable } }
2016-08-03 12:23:43 +02:00
// { { $tableNameSingular } } Exists checks if the { { $tableNameSingular } } row exists.
func { { $tableNameSingular } } Exists(exec boil.Executor, { { $pkArgs } } ) (bool, error) {
2016-09-14 10:08:30 +02:00
var exists bool
2017-03-14 11:53:35 +01:00
{ { if eq . DriverName "mssql" - } }
sql := "select case when exists(select top(1) 1 from { { $schemaTable } } where { { if . Dialect . IndexPlaceholders } } { { whereClause . LQ . RQ 1 . Table . PKey . Columns } } { { else } } { { whereClause . LQ . RQ 0 . Table . PKey . Columns } } { { end } } ) then 1 else 0 end"
{ { - else - } }
2016-09-14 10:08:30 +02:00
sql := "select exists(select 1 from { { $schemaTable } } where { { if . Dialect . IndexPlaceholders } } { { whereClause . LQ . RQ 1 . Table . PKey . Columns } } { { else } } { { whereClause . LQ . RQ 0 . Table . PKey . Columns } } { { end } } limit 1)"
2017-03-14 11:53:35 +01:00
{ { - end } }
2016-08-13 18:58:18 +02:00
2016-09-14 10:08:30 +02:00
if boil.DebugMode {
2017-08-23 22:37:33 +02:00
qStr, err := InterpolateParams(sql, { { $pkNames | join ", " } } )
2017-07-06 21:15:00 +02:00
if err != nil {
return false, err
}
fmt.Fprintln(boil.DebugWriter, qStr)
2016-09-14 10:08:30 +02:00
}
2016-08-13 18:58:18 +02:00
2016-09-14 10:08:30 +02:00
row := exec.QueryRow(sql, { { $pkNames | join ", " } } )
2016-08-03 12:23:43 +02:00
2016-09-14 10:08:30 +02:00
err := row.Scan(&exists)
if err != nil {
return false, errors.Wrap(err, " { { . PkgName } } : unable to check if { { . Table . Name } } exists")
}
2016-08-03 12:23:43 +02:00
2016-09-14 10:08:30 +02:00
return exists, nil
2016-08-03 12:23:43 +02:00
}
// { { $tableNameSingular } } ExistsG checks if the { { $tableNameSingular } } row exists.
func { { $tableNameSingular } } ExistsG( { { $pkArgs } } ) (bool, error) {
2016-09-14 10:08:30 +02:00
return { { $tableNameSingular } } Exists(boil.GetDB(), { { $pkNames | join ", " } } )
2016-08-03 12:23:43 +02:00
}
// { { $tableNameSingular } } ExistsGP checks if the { { $tableNameSingular } } row exists. Panics on error.
func { { $tableNameSingular } } ExistsGP( { { $pkArgs } } ) bool {
2016-09-14 10:08:30 +02:00
e, err := { { $tableNameSingular } } Exists(boil.GetDB(), { { $pkNames | join ", " } } )
if err != nil {
panic(boil.WrapErr(err))
}
2016-08-03 12:23:43 +02:00
2016-09-14 10:08:30 +02:00
return e
2016-08-03 12:23:43 +02:00
}
// { { $tableNameSingular } } ExistsP checks if the { { $tableNameSingular } } row exists. Panics on error.
func { { $tableNameSingular } } ExistsP(exec boil.Executor, { { $pkArgs } } ) bool {
2016-09-14 10:08:30 +02:00
e, err := { { $tableNameSingular } } Exists(exec, { { $pkNames | join ", " } } )
if err != nil {
panic(boil.WrapErr(err))
}
2016-08-03 12:23:43 +02:00
2016-09-14 10:08:30 +02:00
return e
2016-08-03 12:23:43 +02:00
}
2017-05-08 22:30:40 +02:00
// IsNew() checks if record exists in db (aka if its primary key is set).
func (o * { { $tableNameSingular } } ) IsNew() bool {
r := reflect.ValueOf(o).Elem()
for i := 0; i < r.NumField(); i++ {
column := r.Type().Field(i).Tag.Get("boil")
for _, pkColumn := range { { $varNameSingular } } PrimaryKeyColumns {
if column == pkColumn {
field := r.Field(i)
if field.Interface() != reflect.Zero(field.Type()).Interface() {
return false
}
}
}
}
return true
}
// Save() inserts the record if it does not exist, or updates it if it does.
func (o * { { $tableNameSingular } } ) Save(exec boil.Executor, whitelist ...string) error {
if o.IsNew() {
return o.Insert(exec, whitelist...)
} else {
return o.Update(exec, whitelist...)
}
}
// SaveG() inserts the record if it does not exist, or updates it if it does.
func (o * { { $tableNameSingular } } ) SaveG(whitelist ...string) error {
if o.IsNew() {
return o.InsertG(whitelist...)
} else {
return o.UpdateG(whitelist...)
}
}