2016-08-26 08:50:45 +02:00
{ { - if . Table . IsJoinTable - } }
{ { - else - } }
2016-09-14 10:08:30 +02:00
{ { - $dot : = . - } }
{ { - $table : = . Table - } }
{ { - range . Table . ToManyRelationships - } }
2016-09-23 06:47:50 +02:00
{ { - $txt : = txtsFromToMany $dot.Tables $table . - } }
2016-09-21 06:56:06 +02:00
{ { - $varNameSingular : = . Table | singular | camelCase - } }
{ { - $foreignVarNameSingular : = . ForeignTable | singular | camelCase } }
2017-01-05 05:31:29 +01:00
{ { - $foreignPKeyCols : = ( getTable $dot.Tables . ForeignTable ) . PKey . Columns - } }
{ { - $foreignSchemaTable : = . ForeignTable | $dot.SchemaTable } }
2017-01-10 06:06:47 +01:00
// Add { { $txt.Function.Name } } G adds the given related objects to the existing relationships
// of the { { $table.Name | singular } } , optionally inserting them as new records.
// Appends related to o.R. { { $txt.Function.Name } } .
// Sets related.R. { { $txt.Function.ForeignName } } appropriately.
// Uses the global database handle.
func (o * { { $txt.LocalTable.NameGo } } ) Add { { $txt.Function.Name } } G(insert bool, related ...* { { $txt.ForeignTable.NameGo } } ) error {
return o.Add { { $txt.Function.Name } } (boil.GetDB(), insert, related...)
}
// Add { { $txt.Function.Name } } P adds the given related objects to the existing relationships
// of the { { $table.Name | singular } } , optionally inserting them as new records.
// Appends related to o.R. { { $txt.Function.Name } } .
// Sets related.R. { { $txt.Function.ForeignName } } appropriately.
// Panics on error.
func (o * { { $txt.LocalTable.NameGo } } ) Add { { $txt.Function.Name } } P(exec boil.Executor, insert bool, related ...* { { $txt.ForeignTable.NameGo } } ) {
if err := o.Add { { $txt.Function.Name } } (exec, insert, related...); err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
2017-01-10 06:06:47 +01:00
}
}
// Add { { $txt.Function.Name } } GP adds the given related objects to the existing relationships
// of the { { $table.Name | singular } } , optionally inserting them as new records.
// Appends related to o.R. { { $txt.Function.Name } } .
// Sets related.R. { { $txt.Function.ForeignName } } appropriately.
// Uses the global database handle and panics on error.
func (o * { { $txt.LocalTable.NameGo } } ) Add { { $txt.Function.Name } } GP(insert bool, related ...* { { $txt.ForeignTable.NameGo } } ) {
if err := o.Add { { $txt.Function.Name } } (boil.GetDB(), insert, related...); err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
2017-01-10 06:06:47 +01:00
}
}
2016-09-23 06:47:50 +02:00
// Add { { $txt.Function.Name } } adds the given related objects to the existing relationships
2016-08-26 08:50:45 +02:00
// of the { { $table.Name | singular } } , optionally inserting them as new records.
2016-09-23 06:47:50 +02:00
// Appends related to o.R. { { $txt.Function.Name } } .
// Sets related.R. { { $txt.Function.ForeignName } } appropriately.
func (o * { { $txt.LocalTable.NameGo } } ) Add { { $txt.Function.Name } } (exec boil.Executor, insert bool, related ...* { { $txt.ForeignTable.NameGo } } ) error {
2016-09-14 10:08:30 +02:00
var err error
for _, rel := range related {
if insert {
2017-01-05 05:31:29 +01:00
{ { if not . ToJoinTable - } }
rel. { { $txt.Function.ForeignAssignment } } = o. { { $txt.Function.LocalAssignment } }
{ { if . ForeignColumnNullable - } }
rel. { { $txt.ForeignTable.ColumnNameGo } } .Valid = true
{ { end - } }
{ { end - } }
2016-09-14 10:08:30 +02:00
if err = rel.Insert(exec); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Prefix("failed to insert into foreign table", err)
2016-09-14 10:08:30 +02:00
}
} { { if not . ToJoinTable } } else {
2017-01-05 05:31:29 +01:00
updateQuery := fmt.Sprintf(
"UPDATE { { $foreignSchemaTable } } SET %s WHERE %s",
strmangle.SetParamNames(" { { $dot.LQ } } ", " { { $dot.RQ } } ", { { if $dot.Dialect.IndexPlaceholders } } 1 { { else } } 0 { { end } } , []string { { "{" } } " { { . ForeignColumn } } " { { "}" } } ),
strmangle.WhereClause(" { { $dot.LQ } } ", " { { $dot.RQ } } ", { { if $dot.Dialect.IndexPlaceholders } } 2 { { else } } 0 { { end } } , { { $foreignVarNameSingular } } PrimaryKeyColumns),
)
values := []interface { } { o . { { $txt.LocalTable.ColumnNameGo } } , rel . { { $foreignPKeyCols | stringMap $dot.StringFuncs.titleCase | join ", rel." } } { { "}" } }
if boil.DebugMode {
2017-09-02 17:27:27 +02:00
fmt.Fprintln(boil.DebugWriter, updateQuery)
fmt.Fprintln(boil.DebugWriter, values)
2017-01-05 05:31:29 +01:00
}
if _, err = exec.Exec(updateQuery, values...); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Prefix("failed to update foreign table", err)
2016-09-14 10:08:30 +02:00
}
2017-01-05 05:31:29 +01:00
rel. { { $txt.Function.ForeignAssignment } } = o. { { $txt.Function.LocalAssignment } }
{ { if . ForeignColumnNullable - } }
rel. { { $txt.ForeignTable.ColumnNameGo } } .Valid = true
{ { end - } }
2016-09-14 10:08:30 +02:00
} { { end - } }
}
{ { if . ToJoinTable - } }
for _, rel := range related {
query := "insert into { { . JoinTable | $dot.SchemaTable } } ( { { . JoinLocalColumn | $dot.Quotes } } , { { . JoinForeignColumn | $dot.Quotes } } ) values { { if $dot.Dialect.IndexPlaceholders } } ($1, $2) { { else } } (?, ?) { { end } } "
2016-09-23 06:47:50 +02:00
values := []interface { } { { "{" } } o. { { $txt.LocalTable.ColumnNameGo } } , rel. { { $txt.ForeignTable.ColumnNameGo } } }
2016-09-14 10:08:30 +02:00
if boil.DebugMode {
2017-09-02 17:27:27 +02:00
fmt.Fprintln(boil.DebugWriter, query)
fmt.Fprintln(boil.DebugWriter, values)
2016-09-14 10:08:30 +02:00
}
_, err = exec.Exec(query, values...)
if err != nil {
2018-02-07 15:35:46 +01:00
return errors.Prefix("failed to insert into join table", err)
2016-09-14 10:08:30 +02:00
}
}
{ { end - } }
2016-09-21 05:37:28 +02:00
if o.R == nil {
2016-09-21 06:56:06 +02:00
o.R = & { { $varNameSingular } } R {
2016-09-23 06:47:50 +02:00
{ { $txt.Function.Name } } : related,
2016-09-14 10:08:30 +02:00
}
} else {
2016-09-23 06:47:50 +02:00
o.R. { { $txt.Function.Name } } = append(o.R. { { $txt.Function.Name } } , related...)
2016-09-14 10:08:30 +02:00
}
{ { if . ToJoinTable - } }
for _, rel := range related {
if rel.R == nil {
2016-09-21 06:56:06 +02:00
rel.R = & { { $foreignVarNameSingular } } R {
2016-09-23 06:47:50 +02:00
{ { $txt.Function.ForeignName } } : { { $txt.LocalTable.NameGo } } Slice { { "{" } } o { { "}" } } ,
2016-09-14 10:08:30 +02:00
}
} else {
2016-09-23 06:47:50 +02:00
rel.R. { { $txt.Function.ForeignName } } = append(rel.R. { { $txt.Function.ForeignName } } , o)
2016-09-14 10:08:30 +02:00
}
}
{ { else - } }
for _, rel := range related {
if rel.R == nil {
2016-09-21 06:56:06 +02:00
rel.R = & { { $foreignVarNameSingular } } R {
2016-09-23 06:47:50 +02:00
{ { $txt.Function.ForeignName } } : o,
2016-09-14 10:08:30 +02:00
}
} else {
2016-09-23 06:47:50 +02:00
rel.R. { { $txt.Function.ForeignName } } = o
2016-09-14 10:08:30 +02:00
}
}
{ { end - } }
return nil
2016-08-26 08:50:45 +02:00
}
2016-09-14 10:08:30 +02:00
{ { - if ( or . ForeignColumnNullable . ToJoinTable ) } }
2017-01-10 06:06:47 +01:00
// Set { { $txt.Function.Name } } G removes all previously related items of the
// { { $table.Name | singular } } replacing them completely with the passed
// in related items, optionally inserting them as new records.
// Sets o.R. { { $txt.Function.ForeignName } } 's { { $txt.Function.Name } } accordingly.
// Replaces o.R. { { $txt.Function.Name } } with related.
// Sets related.R. { { $txt.Function.ForeignName } } 's { { $txt.Function.Name } } accordingly.
// Uses the global database handle.
func (o * { { $txt.LocalTable.NameGo } } ) Set { { $txt.Function.Name } } G(insert bool, related ...* { { $txt.ForeignTable.NameGo } } ) error {
return o.Set { { $txt.Function.Name } } (boil.GetDB(), insert, related...)
}
// Set { { $txt.Function.Name } } P removes all previously related items of the
// { { $table.Name | singular } } replacing them completely with the passed
// in related items, optionally inserting them as new records.
// Sets o.R. { { $txt.Function.ForeignName } } 's { { $txt.Function.Name } } accordingly.
// Replaces o.R. { { $txt.Function.Name } } with related.
// Sets related.R. { { $txt.Function.ForeignName } } 's { { $txt.Function.Name } } accordingly.
// Panics on error.
func (o * { { $txt.LocalTable.NameGo } } ) Set { { $txt.Function.Name } } P(exec boil.Executor, insert bool, related ...* { { $txt.ForeignTable.NameGo } } ) {
if err := o.Set { { $txt.Function.Name } } (exec, insert, related...); err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
2017-01-10 06:06:47 +01:00
}
}
// Set { { $txt.Function.Name } } GP removes all previously related items of the
// { { $table.Name | singular } } replacing them completely with the passed
// in related items, optionally inserting them as new records.
// Sets o.R. { { $txt.Function.ForeignName } } 's { { $txt.Function.Name } } accordingly.
// Replaces o.R. { { $txt.Function.Name } } with related.
// Sets related.R. { { $txt.Function.ForeignName } } 's { { $txt.Function.Name } } accordingly.
// Uses the global database handle and panics on error.
func (o * { { $txt.LocalTable.NameGo } } ) Set { { $txt.Function.Name } } GP(insert bool, related ...* { { $txt.ForeignTable.NameGo } } ) {
if err := o.Set { { $txt.Function.Name } } (boil.GetDB(), insert, related...); err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
2017-01-10 06:06:47 +01:00
}
}
2016-09-23 06:47:50 +02:00
// Set { { $txt.Function.Name } } removes all previously related items of the
2016-08-26 08:50:45 +02:00
// { { $table.Name | singular } } replacing them completely with the passed
// in related items, optionally inserting them as new records.
2016-09-23 06:47:50 +02:00
// Sets o.R. { { $txt.Function.ForeignName } } 's { { $txt.Function.Name } } accordingly.
// Replaces o.R. { { $txt.Function.Name } } with related.
// Sets related.R. { { $txt.Function.ForeignName } } 's { { $txt.Function.Name } } accordingly.
func (o * { { $txt.LocalTable.NameGo } } ) Set { { $txt.Function.Name } } (exec boil.Executor, insert bool, related ...* { { $txt.ForeignTable.NameGo } } ) error {
2016-09-14 10:08:30 +02:00
{ { if . ToJoinTable - } }
query := "delete from { { . JoinTable | $dot.SchemaTable } } where { { . JoinLocalColumn | $dot.Quotes } } = { { if $dot.Dialect.IndexPlaceholders } } $1 { { else } } ? { { end } } "
2016-09-23 06:47:50 +02:00
values := []interface { } { { "{" } } o. { { $txt.LocalTable.ColumnNameGo } } }
2016-09-14 10:08:30 +02:00
{ { else - } }
query := "update { { . ForeignTable | $dot.SchemaTable } } set { { . ForeignColumn | $dot.Quotes } } = null where { { . ForeignColumn | $dot.Quotes } } = { { if $dot.Dialect.IndexPlaceholders } } $1 { { else } } ? { { end } } "
2016-09-23 06:47:50 +02:00
values := []interface { } { { "{" } } o. { { $txt.LocalTable.ColumnNameGo } } }
2016-09-14 10:08:30 +02:00
{ { end - } }
if boil.DebugMode {
2017-09-02 17:27:27 +02:00
fmt.Fprintln(boil.DebugWriter, query)
fmt.Fprintln(boil.DebugWriter, values)
2016-09-14 10:08:30 +02:00
}
_, err := exec.Exec(query, values...)
if err != nil {
2018-02-07 15:35:46 +01:00
return errors.Prefix("failed to remove relationships before set", err)
2016-09-14 10:08:30 +02:00
}
{ { if . ToJoinTable - } }
2016-09-23 06:47:50 +02:00
remove { { $txt.Function.Name } } From { { $txt.Function.ForeignName } } Slice(o, related)
2017-01-25 08:04:59 +01:00
if o.R != nil {
o.R. { { $txt.Function.Name } } = nil
}
2016-09-14 10:08:30 +02:00
{ { else - } }
2016-09-21 05:37:28 +02:00
if o.R != nil {
2016-09-23 06:47:50 +02:00
for _, rel := range o.R. { { $txt.Function.Name } } {
rel. { { $txt.ForeignTable.ColumnNameGo } } .Valid = false
2016-09-14 10:08:30 +02:00
if rel.R == nil {
continue
}
2016-09-23 06:47:50 +02:00
rel.R. { { $txt.Function.ForeignName } } = nil
2016-09-14 10:08:30 +02:00
}
2016-09-23 06:47:50 +02:00
o.R. { { $txt.Function.Name } } = nil
2016-09-14 10:08:30 +02:00
}
{ { end - } }
2016-09-23 06:47:50 +02:00
return o.Add { { $txt.Function.Name } } (exec, insert, related...)
2016-08-26 08:50:45 +02:00
}
2017-01-10 06:06:47 +01:00
// Remove { { $txt.Function.Name } } G relationships from objects passed in.
// Removes related items from R. { { $txt.Function.Name } } (uses pointer comparison, removal does not keep order)
// Sets related.R. { { $txt.Function.ForeignName } } .
// Uses the global database handle.
func (o * { { $txt.LocalTable.NameGo } } ) Remove { { $txt.Function.Name } } G(related ...* { { $txt.ForeignTable.NameGo } } ) error {
return o.Remove { { $txt.Function.Name } } (boil.GetDB(), related...)
}
// Remove { { $txt.Function.Name } } P relationships from objects passed in.
// Removes related items from R. { { $txt.Function.Name } } (uses pointer comparison, removal does not keep order)
// Sets related.R. { { $txt.Function.ForeignName } } .
// Panics on error.
func (o * { { $txt.LocalTable.NameGo } } ) Remove { { $txt.Function.Name } } P(exec boil.Executor, related ...* { { $txt.ForeignTable.NameGo } } ) {
if err := o.Remove { { $txt.Function.Name } } (exec, related...); err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
2017-01-10 06:06:47 +01:00
}
}
// Remove { { $txt.Function.Name } } GP relationships from objects passed in.
// Removes related items from R. { { $txt.Function.Name } } (uses pointer comparison, removal does not keep order)
// Sets related.R. { { $txt.Function.ForeignName } } .
// Uses the global database handle and panics on error.
func (o * { { $txt.LocalTable.NameGo } } ) Remove { { $txt.Function.Name } } GP(related ...* { { $txt.ForeignTable.NameGo } } ) {
if err := o.Remove { { $txt.Function.Name } } (boil.GetDB(), related...); err != nil {
2018-02-07 15:35:46 +01:00
panic(errors.Err(err))
2017-01-10 06:06:47 +01:00
}
}
2016-09-23 06:47:50 +02:00
// Remove { { $txt.Function.Name } } relationships from objects passed in.
// Removes related items from R. { { $txt.Function.Name } } (uses pointer comparison, removal does not keep order)
// Sets related.R. { { $txt.Function.ForeignName } } .
func (o * { { $txt.LocalTable.NameGo } } ) Remove { { $txt.Function.Name } } (exec boil.Executor, related ...* { { $txt.ForeignTable.NameGo } } ) error {
2016-09-14 10:08:30 +02:00
var err error
{ { if . ToJoinTable - } }
query := fmt.Sprintf(
"delete from { { . JoinTable | $dot.SchemaTable } } where { { . JoinLocalColumn | $dot.Quotes } } = { { if $dot.Dialect.IndexPlaceholders } } $1 { { else } } ? { { end } } and { { . JoinForeignColumn | $dot.Quotes } } in (%s)",
2017-01-25 03:57:45 +01:00
strmangle.Placeholders(dialect.IndexPlaceholders, len(related), 2, 1),
2016-09-14 10:08:30 +02:00
)
2016-09-23 06:47:50 +02:00
values := []interface { } { { "{" } } o. { { $txt.LocalTable.ColumnNameGo } } }
2017-01-25 07:45:20 +01:00
for _, rel := range related {
values = append(values, rel. { { $txt.ForeignTable.ColumnNameGo } } )
}
2016-09-14 10:08:30 +02:00
if boil.DebugMode {
2017-09-02 17:27:27 +02:00
fmt.Fprintln(boil.DebugWriter, query)
fmt.Fprintln(boil.DebugWriter, values)
2016-09-14 10:08:30 +02:00
}
_, err = exec.Exec(query, values...)
if err != nil {
2018-02-07 15:35:46 +01:00
return errors.Prefix("failed to remove relationships before set", err)
2016-09-14 10:08:30 +02:00
}
{ { else - } }
for _, rel := range related {
2016-09-23 06:47:50 +02:00
rel. { { $txt.ForeignTable.ColumnNameGo } } .Valid = false
2016-09-14 10:08:30 +02:00
{ { if not . ToJoinTable - } }
if rel.R != nil {
2016-09-23 06:47:50 +02:00
rel.R. { { $txt.Function.ForeignName } } = nil
2016-09-14 10:08:30 +02:00
}
{ { end - } }
if err = rel.Update(exec, " { { . ForeignColumn } } "); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
2016-09-14 10:08:30 +02:00
}
}
{ { end - } }
{ { if . ToJoinTable - } }
2016-09-23 06:47:50 +02:00
remove { { $txt.Function.Name } } From { { $txt.Function.ForeignName } } Slice(o, related)
2016-09-14 10:08:30 +02:00
{ { end - } }
2016-09-21 05:37:28 +02:00
if o.R == nil {
2016-09-14 10:08:30 +02:00
return nil
}
for _, rel := range related {
2016-09-23 06:47:50 +02:00
for i, ri := range o.R. { { $txt.Function.Name } } {
2016-09-14 10:08:30 +02:00
if rel != ri {
continue
}
2016-09-23 06:47:50 +02:00
ln := len(o.R. { { $txt.Function.Name } } )
2016-09-14 10:08:30 +02:00
if ln > 1 && i < ln-1 {
2016-09-23 06:47:50 +02:00
o.R. { { $txt.Function.Name } } [i] = o.R. { { $txt.Function.Name } } [ln-1]
2016-09-14 10:08:30 +02:00
}
2016-09-23 06:47:50 +02:00
o.R. { { $txt.Function.Name } } = o.R. { { $txt.Function.Name } } [:ln-1]
2016-09-14 10:08:30 +02:00
break
}
}
return nil
2016-08-26 08:50:45 +02:00
}
2016-08-29 08:26:44 +02:00
2016-09-14 10:08:30 +02:00
{ { if . ToJoinTable - } }
2016-09-23 06:47:50 +02:00
func remove { { $txt.Function.Name } } From { { $txt.Function.ForeignName } } Slice(o * { { $txt.LocalTable.NameGo } } , related []* { { $txt.ForeignTable.NameGo } } ) {
2016-09-14 10:08:30 +02:00
for _, rel := range related {
if rel.R == nil {
continue
}
2016-09-23 06:47:50 +02:00
for i, ri := range rel.R. { { $txt.Function.ForeignName } } {
{ { if $txt.Function.UsesBytes - } }
if 0 != bytes.Compare(o. { { $txt.Function.LocalAssignment } } , ri. { { $txt.Function.LocalAssignment } } ) {
2016-09-18 06:00:47 +02:00
{ { else - } }
2016-09-23 06:47:50 +02:00
if o. { { $txt.Function.LocalAssignment } } != ri. { { $txt.Function.LocalAssignment } } {
2016-09-18 06:00:47 +02:00
{ { end - } }
2016-09-14 10:08:30 +02:00
continue
}
2016-09-23 06:47:50 +02:00
ln := len(rel.R. { { $txt.Function.ForeignName } } )
2016-09-14 10:08:30 +02:00
if ln > 1 && i < ln-1 {
2016-09-23 06:47:50 +02:00
rel.R. { { $txt.Function.ForeignName } } [i] = rel.R. { { $txt.Function.ForeignName } } [ln-1]
2016-09-14 10:08:30 +02:00
}
2016-09-23 06:47:50 +02:00
rel.R. { { $txt.Function.ForeignName } } = rel.R. { { $txt.Function.ForeignName } } [:ln-1]
2016-09-14 10:08:30 +02:00
break
}
}
2016-08-29 08:26:44 +02:00
}
2016-09-14 10:08:30 +02:00
{ { end - } } { { - / * if ToJoinTable * / - } }
{ { - end - } } { { - / * if nullable foreign key * / - } }
{ { - end - } } { { - / * range relationships * / - } }
2016-09-09 14:31:51 +02:00
{ { - end - } } { { - / * if IsJoinTable * / - } }