sqlboiler/templates/12_relationship_to_many_setops.tpl

338 lines
13 KiB
Smarty
Raw Permalink Normal View History

2016-08-26 08:50:45 +02:00
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $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}}
{{- $foreignPKeyCols := (getTable $dot.Tables .ForeignTable).PKey.Columns -}}
{{- $foreignSchemaTable := .ForeignTable | $dot.SchemaTable}}
// 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))
}
}
// 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))
}
}
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 {
var err error
for _, rel := range related {
if insert {
{{if not .ToJoinTable -}}
rel.{{$txt.Function.ForeignAssignment}} = o.{{$txt.Function.LocalAssignment}}
{{if .ForeignColumnNullable -}}
rel.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
{{end -}}
{{end -}}
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)
}
}{{if not .ToJoinTable}} else {
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 {
fmt.Fprintln(boil.DebugWriter, updateQuery)
fmt.Fprintln(boil.DebugWriter, values)
}
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)
}
rel.{{$txt.Function.ForeignAssignment}} = o.{{$txt.Function.LocalAssignment}}
{{if .ForeignColumnNullable -}}
rel.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
{{end -}}
}{{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}}}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, query)
fmt.Fprintln(boil.DebugWriter, values)
}
_, 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)
}
}
{{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,
}
} else {
2016-09-23 06:47:50 +02:00
o.R.{{$txt.Function.Name}} = append(o.R.{{$txt.Function.Name}}, related...)
}
{{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{{"}"}},
}
} else {
2016-09-23 06:47:50 +02:00
rel.R.{{$txt.Function.ForeignName}} = append(rel.R.{{$txt.Function.ForeignName}}, o)
}
}
{{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,
}
} else {
2016-09-23 06:47:50 +02:00
rel.R.{{$txt.Function.ForeignName}} = o
}
}
{{end -}}
return nil
2016-08-26 08:50:45 +02:00
}
{{- if (or .ForeignColumnNullable .ToJoinTable)}}
// 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))
}
}
// 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))
}
}
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 {
{{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}}}
{{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}}}
{{end -}}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, query)
fmt.Fprintln(boil.DebugWriter, values)
}
_, 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)
}
{{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
}
{{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
if rel.R == nil {
continue
}
2016-09-23 06:47:50 +02:00
rel.R.{{$txt.Function.ForeignName}} = nil
}
2016-09-23 06:47:50 +02:00
o.R.{{$txt.Function.Name}} = nil
}
{{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
}
// 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))
}
}
// 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))
}
}
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 {
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)",
strmangle.Placeholders(dialect.IndexPlaceholders, len(related), 2, 1),
)
2016-09-23 06:47:50 +02:00
values := []interface{}{{"{"}}o.{{$txt.LocalTable.ColumnNameGo}}}
for _, rel := range related {
values = append(values, rel.{{$txt.ForeignTable.ColumnNameGo}})
}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, query)
fmt.Fprintln(boil.DebugWriter, values)
}
_, 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)
}
{{else -}}
for _, rel := range related {
2016-09-23 06:47:50 +02:00
rel.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false
{{if not .ToJoinTable -}}
if rel.R != nil {
2016-09-23 06:47:50 +02:00
rel.R.{{$txt.Function.ForeignName}} = nil
}
{{end -}}
if err = rel.Update(exec, "{{.ForeignColumn}}"); err != nil {
2018-02-07 15:35:46 +01:00
return errors.Err(err)
}
}
{{end -}}
{{if .ToJoinTable -}}
2016-09-23 06:47:50 +02:00
remove{{$txt.Function.Name}}From{{$txt.Function.ForeignName}}Slice(o, related)
{{end -}}
2016-09-21 05:37:28 +02:00
if o.R == nil {
return nil
}
for _, rel := range related {
2016-09-23 06:47:50 +02:00
for i, ri := range o.R.{{$txt.Function.Name}} {
if rel != ri {
continue
}
2016-09-23 06:47:50 +02:00
ln := len(o.R.{{$txt.Function.Name}})
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-23 06:47:50 +02:00
o.R.{{$txt.Function.Name}} = o.R.{{$txt.Function.Name}}[:ln-1]
break
}
}
return nil
2016-08-26 08:50:45 +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}}) {
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}}) {
{{else -}}
2016-09-23 06:47:50 +02:00
if o.{{$txt.Function.LocalAssignment}} != ri.{{$txt.Function.LocalAssignment}} {
{{end -}}
continue
}
2016-09-23 06:47:50 +02:00
ln := len(rel.R.{{$txt.Function.ForeignName}})
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-23 06:47:50 +02:00
rel.R.{{$txt.Function.ForeignName}} = rel.R.{{$txt.Function.ForeignName}}[:ln-1]
break
}
}
}
{{end -}}{{- /* if ToJoinTable */ -}}
{{- end -}}{{- /* if nullable foreign key */ -}}
{{- end -}}{{- /* range relationships */ -}}
{{- end -}}{{- /* if IsJoinTable */ -}}