Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dialect/sql/builder: make sql.In() with empty args fallback to False() #2735

Merged
merged 5 commits into from Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion dialect/sql/builder.go
Expand Up @@ -1535,8 +1535,10 @@ func In(col string, args ...interface{}) *Predicate {

// In appends the `IN` predicate.
func (p *Predicate) In(col string, args ...interface{}) *Predicate {
// if no arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len(args) == 0 {
return p
return p.False()
Comment on lines -1539 to +1541
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also remove this if-statement from the codegen.

Copy link
Contributor Author

@Liooo Liooo Jul 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also remove this if-statement from the codegen.

Really agreed, fixed in 52c502b 🙏

Comment on lines -1539 to +1541
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return p
return p.False()
// If no arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
return p.False()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks added in f1fcc1c 👍

}
return p.Append(func(b *Builder) {
b.Ident(col).WriteOp(OpIn)
Expand Down
8 changes: 8 additions & 0 deletions dialect/sql/builder_test.go
Expand Up @@ -2004,6 +2004,14 @@ func TestReusePredicates(t *testing.T) {
wantQuery: `SELECT * FROM "users" WHERE "a" = $1 OR "b" = $2`,
wantArgs: []interface{}{"a", "b"},
},
{
p: Or(
EQ("a", "a"),
In("b"),
),
wantQuery: `SELECT * FROM "users" WHERE "a" = $1 OR FALSE`,
wantArgs: []interface{}{"a"},
},
{
p: And(
EQ("active", true),
Expand Down
6 changes: 0 additions & 6 deletions entc/gen/template/dialect/sql/predicate.tmpl
Expand Up @@ -18,12 +18,6 @@ in the LICENSE file in the root directory of this source tree.
{{- $storage := $.Scope.Storage -}}
func(s *sql.Selector) {
{{- if $op.Variadic }}
// if not arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len({{ $arg }}) == 0 {
s.Where(sql.False())
return
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run go generate ./... from the root of the project.

v := make([]interface{}, len({{ $arg }}))
for i := range v {
v[i] = {{ $arg }}[i]
Expand Down
12 changes: 0 additions & 12 deletions entc/integration/cascadelete/ent/comment/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/cascadelete/ent/post/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/cascadelete/ent/user/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/config/ent/user/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/customid/ent/account/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/customid/ent/blob/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/customid/ent/car/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/customid/ent/device/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/customid/ent/doc/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/customid/ent/group/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/customid/ent/intsid/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/customid/ent/mixinid/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions entc/integration/customid/ent/note/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.