Skip to content

Commit fef0a80

Browse files
authored
Merge pull request #323 from stephenafamo/nil-model-slice
Fix issues with empty modelSlice methods
2 parents 303c289 + ca17655 commit fef0a80

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4646

4747
- Updated error constant generation to employ specific error types for making error matching easier. (thanks @mbezhanov)
4848
- Collation in `clause.OrderDef` is now a string not an expression and is always quoted
49+
- Calling `UpdateAll`, `DeleteAll` and `ReloadAll` on an empty model slice now returns nil without running any queries.
4950

5051
### Removed
5152

@@ -54,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5455
### Fixed
5556

5657
- Fix random value generation for pq.Float64Array factory (thanks @felipeparaujo)
58+
- Using the `UpdateMod()` and `DeleteMod()` methods on an empty model slice now appends `WHERE primary_key IN NULL` which will return no results. Instead of `WHERE primary_key IN ()` which is a syntax error.
5759

5860
## [v0.29.0] - 2024-11-20
5961

gen/templates/models/07_slice_methods.go.tpl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ func (o {{$tAlias.UpSingular}}Slice) AfterQueryHook(ctx context.Context, exec bo
3131
{{$pkCols := $table.Constraints.Primary.Columns}}
3232
{{$multiPK := gt (len $pkCols) 1}}
3333
func (o {{$tAlias.UpSingular}}Slice) pkIN() dialect.Expression {
34-
return {{if $multiPK}}{{$.Dialect}}.Group({{end}}{{- range $i, $col := $pkCols -}}{{if gt $i 0}}, {{end}}{{$.Dialect}}.Quote("{{$table.Name}}", "{{$col}}"){{end}}{{if $multiPK}}){{end -}}
34+
if len(o) == 0 {
35+
return {{$.Dialect}}.Raw("NULL")
36+
}
37+
38+
return {{if $multiPK}}{{$.Dialect}}.Group({{end}}{{- range $i, $col := $pkCols -}}{{if gt $i 0}}, {{end}}{{$.Dialect}}.Quote("{{$table.Name}}", "{{$col}}"){{end}}{{if $multiPK}}){{end -}}
3539
.In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error){
3640
pkPairs := make([]bob.Expression, len(o))
3741
for i, row := range o {
@@ -131,18 +135,30 @@ func (o {{$tAlias.UpSingular}}Slice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
131135
{{$table := .Table}}
132136
{{$tAlias := .Aliases.Table $table.Key -}}
133137
func (o {{$tAlias.UpSingular}}Slice) UpdateAll(ctx context.Context, exec bob.Executor, vals {{$tAlias.UpSingular}}Setter) error {
138+
if len(o) == 0 {
139+
return nil
140+
}
141+
134142
_, err := {{$tAlias.UpPlural}}.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
135143
return err
136144
}
137145
{{- end}}
138146

139147
func (o {{$tAlias.UpSingular}}Slice) DeleteAll(ctx context.Context, exec bob.Executor) error {
148+
if len(o) == 0 {
149+
return nil
150+
}
151+
140152
_, err := {{$tAlias.UpPlural}}.Delete(o.DeleteMod()).Exec(ctx, exec)
141153
return err
142154
}
143155

144156

145157
func (o {{$tAlias.UpSingular}}Slice) ReloadAll(ctx context.Context, exec bob.Executor) error {
158+
if len(o) == 0 {
159+
return nil
160+
}
161+
146162
o2, err := {{$tAlias.UpPlural}}.Query(sm.Where(o.pkIN())).All(ctx, exec)
147163
if err != nil {
148164
return err

0 commit comments

Comments
 (0)