github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/main/10_relationship_to_one_setops.go.tpl (about)

     1  {{- if or .Table.IsJoinTable .Table.IsView -}}
     2  {{- else -}}
     3  	{{- range $fkey := .Table.FKeys -}}
     4  		{{- $ltable := $.Aliases.Table $fkey.Table -}}
     5  		{{- $ftable := $.Aliases.Table $fkey.ForeignTable -}}
     6  		{{- $rel := $ltable.Relationship $fkey.Name -}}
     7  		{{- $arg := printf "maybe%s" $ltable.UpSingular -}}
     8  		{{- $col := $ltable.Column $fkey.Column -}}
     9  		{{- $fcol := $ftable.Column $fkey.ForeignColumn -}}
    10  		{{- $usesPrimitives := usesPrimitives $.Tables $fkey.Table $fkey.Column $fkey.ForeignTable $fkey.ForeignColumn -}}
    11  		{{- $schemaTable := $fkey.Table | $.SchemaTable }}
    12  {{if $.AddGlobal -}}
    13  // Set{{$rel.Foreign}}G of the {{$ltable.DownSingular}} to the related item.
    14  // Sets o.R.{{$rel.Foreign}} to related.
    15  {{- if not $.NoBackReferencing}}
    16  // Adds o to related.R.{{$rel.Local}}.
    17  {{- end}}
    18  // Uses the global database handle.
    19  func (o *{{$ltable.UpSingular}}) Set{{$rel.Foreign}}G({{if not $.NoContext}}ctx context.Context, {{end -}} insert bool, related *{{$ftable.UpSingular}}) error {
    20  	return o.Set{{$rel.Foreign}}({{if $.NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, insert, related)
    21  }
    22  
    23  {{end -}}
    24  
    25  {{if $.AddPanic -}}
    26  // Set{{$rel.Foreign}}P of the {{$ltable.DownSingular}} to the related item.
    27  // Sets o.R.{{$rel.Foreign}} to related.
    28  {{- if not $.NoBackReferencing}}
    29  // Adds o to related.R.{{$rel.Local}}.
    30  {{- end}}
    31  // Panics on error.
    32  func (o *{{$ltable.UpSingular}}) Set{{$rel.Foreign}}P({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, insert bool, related *{{$ftable.UpSingular}}) {
    33  	if err := o.Set{{$rel.Foreign}}({{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{{$rel.Foreign}}GP of the {{$ltable.DownSingular}} to the related item.
    42  // Sets o.R.{{$rel.Foreign}} to related.
    43  {{- if not $.NoBackReferencing}}
    44  // Adds o to related.R.{{$rel.Local}}.
    45  {{- end}}
    46  // Uses the global database handle and panics on error.
    47  func (o *{{$ltable.UpSingular}}) Set{{$rel.Foreign}}GP({{if not $.NoContext}}ctx context.Context, {{end -}} insert bool, related *{{$ftable.UpSingular}}) {
    48  	if err := o.Set{{$rel.Foreign}}({{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{{$rel.Foreign}} of the {{$ltable.DownSingular}} to the related item.
    56  // Sets o.R.{{$rel.Foreign}} to related.
    57  {{- if not $.NoBackReferencing}}
    58  // Adds o to related.R.{{$rel.Local}}.
    59  {{- end}}
    60  func (o *{{$ltable.UpSingular}}) Set{{$rel.Foreign}}({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, insert bool, related *{{$ftable.UpSingular}}) error {
    61  	var err error
    62  	if insert {
    63  		if err = related.Insert({{if not $.NoContext}}ctx, {{end -}} exec, boil.Infer()); err != nil {
    64  			return errors.Wrap(err, "failed to insert into foreign table")
    65  		}
    66  	}
    67  
    68  	updateQuery := fmt.Sprintf(
    69  		"UPDATE {{$schemaTable}} SET %s WHERE %s",
    70  		strmangle.SetParamNames("{{$.LQ}}", "{{$.RQ}}", {{if $.Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.Column}}"{{"}"}}),
    71  		strmangle.WhereClause("{{$.LQ}}", "{{$.RQ}}", {{if $.Dialect.UseIndexPlaceholders}}2{{else}}0{{end}}, {{$ltable.DownSingular}}PrimaryKeyColumns),
    72  	)
    73  	values := []interface{}{related.{{$fcol}}, o.{{$.Table.PKey.Columns | stringMap (aliasCols $ltable) | join ", o."}}{{"}"}}
    74  
    75  	{{if $.NoContext -}}
    76  	if boil.DebugMode {
    77  		fmt.Fprintln(boil.DebugWriter, updateQuery)
    78  		fmt.Fprintln(boil.DebugWriter, values)
    79  	}
    80  	{{else -}}
    81  	if boil.IsDebug(ctx) {
    82  		writer := boil.DebugWriterFrom(ctx)
    83  		fmt.Fprintln(writer, updateQuery)
    84  		fmt.Fprintln(writer, values)
    85  	}
    86  	{{end -}}
    87  
    88  	{{if $.NoContext -}}
    89  	if _, err = exec.Exec(updateQuery, values...); err != nil {
    90  		return errors.Wrap(err, "failed to update local table")
    91  	}
    92  	{{- else -}}
    93  	if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil {
    94  		return errors.Wrap(err, "failed to update local table")
    95  	}
    96  	{{- end}}
    97  
    98  	{{if $usesPrimitives -}}
    99  	o.{{$col}} = related.{{$fcol}}
   100  	{{else -}}
   101  	queries.Assign(&o.{{$col}}, related.{{$fcol}})
   102  	{{end -}}
   103  
   104  	if o.R == nil {
   105  		o.R = &{{$ltable.DownSingular}}R{
   106  			{{$rel.Foreign}}: related,
   107  		}
   108  	} else {
   109  		o.R.{{$rel.Foreign}} = related
   110  	}
   111  
   112  	{{if not $.NoBackReferencing -}}
   113  	{{if .Unique -}}
   114  	if related.R == nil {
   115  		related.R = &{{$ftable.DownSingular}}R{
   116  			{{$rel.Local}}: o,
   117  		}
   118  	} else {
   119  		related.R.{{$rel.Local}} = o
   120  	}
   121  	{{else -}}
   122  	if related.R == nil {
   123  		related.R = &{{$ftable.DownSingular}}R{
   124  			{{$rel.Local}}: {{$ltable.UpSingular}}Slice{{"{"}}o{{"}"}},
   125  		}
   126  	} else {
   127  		related.R.{{$rel.Local}} = append(related.R.{{$rel.Local}}, o)
   128  	}
   129  	{{- end}}
   130  	{{- end}}
   131  
   132  	return nil
   133  }
   134  
   135  		{{- if .Nullable}}
   136  {{if $.AddGlobal -}}
   137  // Remove{{$rel.Foreign}}G relationship.
   138  // Sets o.R.{{$rel.Foreign}} to nil.
   139  {{- if not $.NoBackReferencing}}
   140  // Removes o from all passed in related items' relationships struct.
   141  {{- end}}
   142  // Uses the global database handle.
   143  func (o *{{$ltable.UpSingular}}) Remove{{$rel.Foreign}}G({{if not $.NoContext}}ctx context.Context, {{end -}} related *{{$ftable.UpSingular}}) error {
   144  	return o.Remove{{$rel.Foreign}}({{if $.NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, related)
   145  }
   146  
   147  {{end -}}
   148  
   149  {{if $.AddPanic -}}
   150  // Remove{{$rel.Foreign}}P relationship.
   151  // Sets o.R.{{$rel.Foreign}} to nil.
   152  {{- if not $.NoBackReferencing}}
   153  // Removes o from all passed in related items' relationships struct.
   154  {{- end}}
   155  // Panics on error.
   156  func (o *{{$ltable.UpSingular}}) Remove{{$rel.Foreign}}P({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, related *{{$ftable.UpSingular}}) {
   157  	if err := o.Remove{{$rel.Foreign}}({{if not $.NoContext}}ctx, {{end -}} exec, related); err != nil {
   158  		panic(boil.WrapErr(err))
   159  	}
   160  }
   161  
   162  {{end -}}
   163  
   164  {{if and $.AddGlobal $.AddPanic -}}
   165  // Remove{{$rel.Foreign}}GP relationship.
   166  // Sets o.R.{{$rel.Foreign}} to nil.
   167  {{- if not $.NoBackReferencing}}
   168  // Removes o from all passed in related items' relationships struct.
   169  {{- end}}
   170  // Uses the global database handle and panics on error.
   171  func (o *{{$ltable.UpSingular}}) Remove{{$rel.Foreign}}GP({{if not $.NoContext}}ctx context.Context, {{end -}} related *{{$ftable.UpSingular}}) {
   172  	if err := o.Remove{{$rel.Foreign}}({{if $.NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, related); err != nil {
   173  		panic(boil.WrapErr(err))
   174  	}
   175  }
   176  
   177  {{end -}}
   178  
   179  // Remove{{$rel.Foreign}} relationship.
   180  // Sets o.R.{{$rel.Foreign}} to nil.
   181  {{- if not $.NoBackReferencing}}
   182  // Removes o from all passed in related items' relationships struct.
   183  {{- end}}
   184  func (o *{{$ltable.UpSingular}}) Remove{{$rel.Foreign}}({{if $.NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, related *{{$ftable.UpSingular}}) error {
   185  	var err error
   186  
   187  	queries.SetScanner(&o.{{$col}}, nil)
   188  	{{if $.NoContext -}}
   189  	if {{if not $.NoRowsAffected}}_, {{end -}} err = o.Update(exec, boil.Whitelist("{{.Column}}")); err != nil {
   190  	{{else -}}
   191  	if {{if not $.NoRowsAffected}}_, {{end -}} err = o.Update(ctx, exec, boil.Whitelist("{{.Column}}")); err != nil {
   192  	{{end -}}
   193  		return errors.Wrap(err, "failed to update local table")
   194  	}
   195  
   196  	if o.R != nil {
   197  		o.R.{{$rel.Foreign}} = nil
   198  	}
   199  	if related == nil || related.R == nil {
   200  		return nil
   201  	}
   202  
   203  	{{if not $.NoBackReferencing -}}
   204  	{{if .Unique -}}
   205  	related.R.{{$rel.Local}} = nil
   206  	{{else -}}
   207  	for i, ri := range related.R.{{$rel.Local}} {
   208  		{{if $usesPrimitives -}}
   209  		if o.{{$col}} != ri.{{$col}} {
   210  		{{else -}}
   211  		if queries.Equal(o.{{$col}}, ri.{{$col}}) {
   212  		{{end -}}
   213  			continue
   214  		}
   215  
   216  		ln := len(related.R.{{$rel.Local}})
   217  		if ln > 1 && i < ln-1 {
   218  			related.R.{{$rel.Local}}[i] = related.R.{{$rel.Local}}[ln-1]
   219  		}
   220  		related.R.{{$rel.Local}} = related.R.{{$rel.Local}}[:ln-1]
   221  		break
   222  	}
   223  	{{end -}}
   224  	{{end -}}
   225  
   226  	return nil
   227  }
   228  {{end -}}{{/* if foreignkey nullable */}}
   229  {{- end -}}{{/* range */}}
   230  {{- end -}}{{/* join table */}}