8000 Add docs on using ExprP() for custom WHERE sql statement by gmhafiz · Pull Request #2520 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add docs on using ExprP() for custom WHERE sql statement #2520

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 14 commits into from
Jun 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions dialect/sql/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,34 @@ func TestBuilder(t *testing.T) {
wantQuery: `SELECT * FROM "test" WHERE nlevel("path") > $1`,
wantArgs: []interface{}{1},
},
{
input: Select("id").From(Table("users")).Where(ExprP("DATE(last_login_at) >= ?", "2022-05-03")),
wantQuery: "SELECT `id` FROM `users` WHERE DATE(last_login_at) >= ?",
wantArgs: []interface{}{"2022-05-03"},
},
{
input: Select("id").
From(Table("users")).
Where(P(func(b *Builder) {
b.WriteString("DATE(").Ident("last_login_at").WriteString(") >= ").Arg("2022-05-03")
})),
wantQuery: "SELECT `id` FROM `users` WHERE DATE(`last_login_at`) >= ?",
wantArgs: []interface{}{"2022-05-03"},
},
{
input: Select("id").From(Table("events")).Where(ExprP("DATE_ADD(date, INTERVAL duration MINUTE) BETWEEN ? AND ?", "2022-05-03", "2022-05-04")),
wantQuery: "SELECT `id` FROM `events` WHERE DATE_ADD(date, INTERVAL duration MINUTE) BETWEEN ? AND ?",
wantArgs: []interface{}{"2022-05-03", "2022-05-04"},
},
{
input: Select("id").
From(Table("events")).
Where(P(func(b *Builder) {
b.WriteString("DATE_ADD(date, INTERVAL duration MINUTE) BETWEEN ").Arg("2022-05-03").WriteString(" AND ").Arg("2022-05-04")
})),
wantQuery: "SELECT `id` FROM `events` WHERE DATE_ADD(date, INTERVAL duration MINUTE) BETWEEN ? AND ?",
wantArgs: []interface{}{"2022-05-03", "2022-05-04"},
},
{
input: func() Querier {
t1, t2 := Table("users").Schema("s1"), Table("pets").Schema("s2")
Expand Down
35 changes: 35 additions & 0 deletions doc/md/predicates.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,41 @@ client.Pet.

Custom predicates can be useful if you want to write your own dialect-specific logic or to control the executed queries.

For example, in order to use built-in SQL functions such as `DATE()`, use one of the following options:

1. Pass a dialect-aware predicate function using the `sql.P` option:

```go
users := client.User.Query().
Select(user.FieldID).
Where(sql.P(func(b *sql.Builder) {
b.WriteString("DATE(").Ident("last_login_at").WriteByte(')').WriteOp(OpGTE).Arg(value)
})).
AllX(ctx)
```

The above code will produce the following SQL query:

```sql
SELECT `id` FROM `users` WHERE DATE(`last_login_at`) >= ?
```

2. Inline a predicate expression using the `ExprP()` option:

```go
users := client.User.Query().
Select(user.FieldID).
Where(func(s *sql.Selector) {
s.Where(sql.ExprP("DATE(last_login_at >= ?", value))
}).
AllX(ctx)
```

The above code will produce the same SQL query:

```sql
SELECT `id` FROM `users` WHERE DATE(`last_login_at`) >= ?
```
#### Get all pets of users 1, 2 and 3

```go
Expand Down
0