github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/main/11_relationship_one_to_one_setops.go.tpl (about) 1 {{- if or .Table.IsJoinTable .Table.IsView -}} 2 {{- else -}} 3 {{- range $rel := .Table.ToOneRelationships -}} 4 {{- $ltable := $.Aliases.Table $rel.Table -}} 5 {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} 6 {{- $relAlias := $ftable.Relationship $rel.Name -}} 7 {{- $col := $ltable.Column $rel.Column -}} 8 {{- $fcol := $ftable.Column $rel.ForeignColumn -}} 9 {{- $usesPrimitives := usesPrimitives $.Tables $rel.Table $rel.Column $rel.ForeignTable $rel.ForeignColumn -}} 10 {{- $schemaForeignTable := $rel.ForeignTable | $.SchemaTable -}} 11 {{- $foreignPKeyCols := (getTable $.Tables .ForeignTable).PKey.Columns }} 12 {{if $.AddGlobal -}} 13 // Set{{$relAlias.Local}}G of the {{$ltable.DownSingular}} to the related item. 14 // Sets o.R.{{$relAlias.Local}} to related. 15 {{- if not $.NoBackReferencing}} 16 // Adds o to related.R.{{$relAlias.Foreign}}. 17 {{- end}} 18 // Uses the global database handle. 19 func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}G({{if not $.NoContext}}ctx context.Context, {{end -}} insert bool, related *{{$ftable.UpSingular}}) error { 20 return o.Set{{$relAlias.Local}}({{if $.NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, insert, related) 21 } 22 23 {{end -}} 24 25 {{if $.AddPanic -}} 26 // Set{{$relAlias.Local}}P of the {{$ltable.DownSingular}} to the related item. 27 // Sets o.R.{{$relAlias.Local}} to related. 28 {{- if not $.NoBackReferencing}} 29 // Adds o to related.R.{{$relAlias.Foreign}}. 30 {{- end}} 31 // Panics on error. 32 func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}P({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, insert bool, related *{{$ftable.UpSingular}}) { 33 if err := o.Set{{$relAlias.Local}}({{if not $.NoContext}}ctx, {{end -}} exec, insert, related); err != nil { 34 panic(boil.WrapErr(err)) 35 } 36 } 37 38 {{end -}} 39 40 {{if and $.AddGlobal $.AddPanic -}} 41 // Set{{$relAlias.Local}}GP of the {{$ltable.DownSingular}} to the related item. 42 // Sets o.R.{{$relAlias.Local}} to related. 43 {{- if not $.NoBackReferencing}} 44 // Adds o to related.R.{{$relAlias.Foreign}}. 45 {{- end}} 46 // Uses the global database handle and panics on error. 47 func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}GP({{if not $.NoContext}}ctx context.Context, {{end -}} insert bool, related *{{$ftable.UpSingular}}) { 48 if err := o.Set{{$relAlias.Local}}({{if $.NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, insert, related); err != nil { 49 panic(boil.WrapErr(err)) 50 } 51 } 52 53 {{end -}} 54 55 // Set{{$relAlias.Local}} of the {{$ltable.DownSingular}} to the related item. 56 // Sets o.R.{{$relAlias.Local}} to related. 57 {{- if not $.NoBackReferencing}} 58 // Adds o to related.R.{{$relAlias.Foreign}}. 59 {{- end}} 60 func (o *{{$ltable.UpSingular}}) Set{{$relAlias.Local}}({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, insert bool, related *{{$ftable.UpSingular}}) error { 61 var err error 62 63 if insert { 64 {{if $usesPrimitives -}} 65 related.{{$fcol}} = o.{{$col}} 66 {{else -}} 67 queries.Assign(&related.{{$fcol}}, o.{{$col}}) 68 {{- end}} 69 70 if err = related.Insert({{if not $.NoContext}}ctx, {{end -}} exec, boil.Infer()); err != nil { 71 return errors.Wrap(err, "failed to insert into foreign table") 72 } 73 } else { 74 updateQuery := fmt.Sprintf( 75 "UPDATE {{$schemaForeignTable}} SET %s WHERE %s", 76 strmangle.SetParamNames("{{$.LQ}}", "{{$.RQ}}", {{if $.Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.ForeignColumn}}"{{"}"}}), 77 strmangle.WhereClause("{{$.LQ}}", "{{$.RQ}}", {{if $.Dialect.UseIndexPlaceholders}}2{{else}}0{{end}}, {{$ftable.DownSingular}}PrimaryKeyColumns), 78 ) 79 values := []interface{}{o.{{$col}}, related.{{$foreignPKeyCols | stringMap (aliasCols $ftable) | join ", related."}}{{"}"}} 80 81 {{if $.NoContext -}} 82 if boil.DebugMode { 83 fmt.Fprintln(boil.DebugWriter, updateQuery) 84 fmt.Fprintln(boil.DebugWriter, values) 85 } 86 {{else -}} 87 if boil.IsDebug(ctx) { 88 writer := boil.DebugWriterFrom(ctx) 89 fmt.Fprintln(writer, updateQuery) 90 fmt.Fprintln(writer, values) 91 } 92 {{end -}} 93 94 {{if $.NoContext -}} 95 if _, err = exec.Exec(updateQuery, values...); err != nil { 96 {{else -}} 97 if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil { 98 {{end -}} 99 return errors.Wrap(err, "failed to update foreign table") 100 } 101 102 {{if $usesPrimitives -}} 103 related.{{$fcol}} = o.{{$col}} 104 {{- else -}} 105 queries.Assign(&related.{{$fcol}}, o.{{$col}}) 106 {{- end}} 107 } 108 109 110 if o.R == nil { 111 o.R = &{{$ltable.DownSingular}}R{ 112 {{$relAlias.Local}}: related, 113 } 114 } else { 115 o.R.{{$relAlias.Local}} = related 116 } 117 118 {{if not $.NoBackReferencing -}} 119 if related.R == nil { 120 related.R = &{{$ftable.DownSingular}}R{ 121 {{$relAlias.Foreign}}: o, 122 } 123 } else { 124 related.R.{{$relAlias.Foreign}} = o 125 } 126 {{end -}} 127 128 return nil 129 } 130 131 {{- if .ForeignColumnNullable}} 132 {{if $.AddGlobal -}} 133 // Remove{{$relAlias.Local}}G relationship. 134 // Sets o.R.{{$relAlias.Local}} to nil. 135 {{- if not $.NoBackReferencing}} 136 // Removes o from all passed in related items' relationships struct. 137 {{- end}} 138 // Uses the global database handle. 139 func (o *{{$ltable.UpSingular}}) Remove{{$relAlias.Local}}G({{if not $.NoContext}}ctx context.Context, {{end -}} related *{{$ftable.UpSingular}}) error { 140 return o.Remove{{$relAlias.Local}}({{if $.NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, related) 141 } 142 143 {{end -}} 144 145 {{if $.AddPanic -}} 146 // Remove{{$relAlias.Local}}P relationship. 147 // Sets o.R.{{$relAlias.Local}} to nil. 148 {{- if not $.NoBackReferencing}} 149 // Removes o from all passed in related items' relationships struct. 150 {{- end}} 151 // Panics on error. 152 func (o *{{$ltable.UpSingular}}) Remove{{$relAlias.Local}}P({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, related *{{$ftable.UpSingular}}) { 153 if err := o.Remove{{$relAlias.Local}}({{if not $.NoContext}}ctx, {{end -}} exec, related); err != nil { 154 panic(boil.WrapErr(err)) 155 } 156 } 157 158 {{end -}} 159 160 {{if and $.AddGlobal $.AddPanic -}} 161 // Remove{{$relAlias.Local}}GP relationship. 162 // Sets o.R.{{$relAlias.Local}} to nil. 163 {{- if not $.NoBackReferencing}} 164 // Removes o from all passed in related items' relationships struct. 165 {{- end}} 166 // Uses the global database handle and panics on error. 167 func (o *{{$ltable.UpSingular}}) Remove{{$relAlias.Local}}GP({{if not $.NoContext}}ctx context.Context, {{end -}} related *{{$ftable.UpSingular}}) { 168 if err := o.Remove{{$relAlias.Local}}({{if $.NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, related); err != nil { 169 panic(boil.WrapErr(err)) 170 } 171 } 172 173 {{end -}} 174 175 // Remove{{$relAlias.Local}} relationship. 176 // Sets o.R.{{$relAlias.Local}} to nil. 177 {{- if not $.NoBackReferencing}} 178 // Removes o from all passed in related items' relationships struct. 179 {{- end}} 180 func (o *{{$ltable.UpSingular}}) Remove{{$relAlias.Local}}({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, related *{{$ftable.UpSingular}}) error { 181 var err error 182 183 queries.SetScanner(&related.{{$fcol}}, nil) 184 if {{if not $.NoRowsAffected}}_, {{end -}} err = related.Update({{if not $.NoContext}}ctx, {{end -}} exec, boil.Whitelist("{{.ForeignColumn}}")); err != nil { 185 return errors.Wrap(err, "failed to update local table") 186 } 187 188 if o.R != nil { 189 o.R.{{$relAlias.Local}} = nil 190 } 191 192 {{if not $.NoBackReferencing -}} 193 if related == nil || related.R == nil { 194 return nil 195 } 196 197 related.R.{{$relAlias.Foreign}} = nil 198 {{- end}} 199 200 return nil 201 } 202 {{end -}}{{/* if foreignkey nullable */}} 203 {{- end -}}{{/* range */}} 204 {{- end -}}{{/* join table */}}