8000 dialect/sql: support passing selectors to basic predicates by a8m · Pull Request #2237 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dialect/sql: support passing selectors to basic predicates #2237

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

Merged
merged 1 commit into from
Dec 25, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions dialect/sql/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ func (p *Predicate) EQ(col string, arg interface{}) *Predicate {
return p.Append(func(b *Builder) {
b.Ident(col)
b.WriteOp(OpEQ)
b.Arg(arg)
p.arg(b, arg)
})
}

Expand All @@ -1350,7 +1350,7 @@ func (p *Predicate) NEQ(col string, arg interface{}) *Predicate {
return p.Append(func(b *Builder) {
b.Ident(col)
b.WriteOp(OpNEQ)
b.Arg(arg)
p.arg(b, arg)
})
}

Expand All @@ -1374,7 +1374,7 @@ func (p *Predicate) LT(col string, arg interface{}) *Predicate {
return p.Append(func(b *Builder) {
b.Ident(col)
p.WriteOp(OpLT)
b.Arg(arg)
p.arg(b, arg)
})
}

Exp 8000 and All @@ -1398,7 +1398,7 @@ func (p *Predicate) LTE(col string, arg interface{}) *Predicate {
return p.Append(func(b *Builder) {
b.Ident(col)
p.WriteOp(OpLTE)
b.Arg(arg)
p.arg(b, arg)
})
}

Expand All @@ -1422,7 +1422,7 @@ func (p *Predicate) GT(col string, arg interface{}) *Predicate {
return p.Append(func(b *Builder) {
b.Ident(col)
p.WriteOp(OpGT)
b.Arg(arg)
p.arg(b, arg)
})
}

Expand All @@ -1446,7 +1446,7 @@ func (p *Predicate) GTE(col string, arg interface{}) *Predicate {
return p.Append(func(b *Builder) {
b.Ident(col)
p.WriteOp(OpGTE)
b.Arg(arg)
p.arg(b, arg)
})
}

Expand Down Expand Up @@ -1767,6 +1767,18 @@ func (p *Predicate) Query() (string, []interface{}) {
return p.String(), p.args
}

// arg calls Builder.Arg, but wraps `a` with parens in case of a Selector.
func (*Predicate) arg(b *Builder, a interface{}) {
switch a.(type) {
case *Selector:
b.Nested(func(b *Builder) {
b.Arg(a)
})
default:
b.Arg(a)
}
}

// clone returns a shallow clone of p.
func (p *Predicate) clone() *Predicate {
if p == nil {
Expand Down
25 changes: 25 additions & 0 deletions dialect/sql/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,31 @@ func TestBuilder(t *testing.T) {
wantQuery: "UPDATE `users` SET `name` = ?, `age` = ? WHERE `name` = ?",
wantArgs: []interface{}{"foo", 10, "foo"},
},
{
input: Dialect(dialect.Postgres).
Update("users").
Add("rank", 10).
Where(
Or(
EQ("rank", Select("rank").From(Table("ranks")).Where(EQ("name", "foo"))),
GT("score", Select("score").From(Table("scores")).Where(GT("count", 0))),
),
),
wantQuery: `UPDATE "users" SET "rank" = COALESCE("users"."rank", 0) + $1 WHERE "rank" = (SELECT "rank" FROM "ranks" WHERE "name" = $2) OR "score" > (SELECT "score" FROM "scores" WHERE "count" > $3)`,
wantArgs: []interface{}{10, "foo", 0},
},
{
input: Update("users").
Add("rank", 10).
Where(
Or(
EQ("rank", Select("rank").From(Table("ranks")).Where(EQ("name", "foo"))),
GT("score", Select("score").From(Table("scores")).Where(GT("count", 0))),
),
),
wantQuery: "UPDATE `users` SET `rank` = COALESCE(`users`.`rank`, 0) + ? WHERE `rank` = (SELECT `rank` FROM `ranks` WHERE `name` = ?) OR `score` > (SELECT `score` FROM `scores` WHERE `count` > ?)",
wantArgs: []interface{}{10, "foo", 0},
},
{
input: Dialect(dialect.Postgres).
Update("users").
Expand Down
0