Skip to content

Commit

Permalink
Add Expressions method to Setters
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Jan 2, 2024
1 parent 155b487 commit 49546df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
9 changes: 1 addition & 8 deletions gen/bobgen-mysql/templates/models/100_blocks.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
func (s {{$tAlias.UpSingular}}Setter) Apply(q *dialect.UpdateQuery) {
{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s/um" $.Dialect)}}
{{- range $column := .Table.Columns -}}
{{if $column.Generated}}{{continue}}{{end -}}
{{$colAlias := $tAlias.Column $column.Name -}}
if !s.{{$colAlias}}.IsUnset() {
um.Set("{{$table.Name}}", "{{$column.Name}}").ToArg(s.{{$colAlias}}).Apply(q)
}
{{end -}}
um.Set(s.Expressions("{{$table.Name}}")...).Apply(q)
}
{{- end}}
44 changes: 29 additions & 15 deletions gen/templates/models/01_types.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,6 @@ func (s {{$tAlias.UpSingular}}Setter) Overwrite(t *{{$tAlias.UpSingular}}) {
{{end -}}
}

{{block "setter_update_mod" . -}}
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
func (s {{$tAlias.UpSingular}}Setter) Apply(q *dialect.UpdateQuery) {
{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s/um" $.Dialect)}}
{{- range $column := $table.Columns -}}
{{if $column.Generated}}{{continue}}{{end -}}
{{$colAlias := $tAlias.Column $column.Name -}}
if !s.{{$colAlias}}.IsUnset() {
um.Set("{{$column.Name}}").ToArg(s.{{$colAlias}}).Apply(q)
}
{{end -}}
}
{{- end}}

{{block "setter_insert_mod" . -}}
{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s/im" $.Dialect)}}
{{$table := .Table}}
Expand All @@ -150,6 +135,35 @@ func (s {{$tAlias.UpSingular}}Setter) Insert() bob.Mod[*dialect.InsertQuery] {
}
{{- end}}

{{block "setter_update_mod" . -}}
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
func (s {{$tAlias.UpSingular}}Setter) Apply(q *dialect.UpdateQuery) {
um.Set(s.Expressions()...).Apply(q)
}
{{- end}}

{{block "setter_expressions" . -}}
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
func (s {{$tAlias.UpSingular}}Setter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, {{len $table.NonGeneratedColumns}})

{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s/um" $.Dialect)}}
{{range $column := $table.Columns -}}
{{if $column.Generated}}{{continue}}{{end -}}
{{$colAlias := $tAlias.Column $column.Name -}}
if !s.{{$colAlias}}.IsUnset() {
exprs = append(exprs, {{$.Dialect}}.Quote(append(prefix, "{{$column.Name}}")...).EQ({{$.Dialect}}.Arg(s.{{$colAlias}})))
}

{{end -}}

return exprs
}
{{- end}}


{{- end}}

type {{$tAlias.DownSingular}}ColumnNames struct {
Expand Down

0 comments on commit 49546df

Please sign in to comment.