-
Notifications
You must be signed in to change notification settings - Fork 964
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
Conversation
from slack discussion: https://app.slack.com/client/T029RQSE6/C01FMSQDT53/thread/C01FMSQDT53-1651387195.136259 |
doc/md/predicates.md
Outdated
@@ -205,6 +205,42 @@ The above code will produce the following SQL query: | |||
SELECT DISTINCT `pets`.`id`, `pets`.`owner_id`, `pets`.`name`, `pets`.`age`, `pets`.`species` FROM `pets` WHERE `name` LIKE '_B%' | |||
``` | |||
|
|||
#### Escape hatch for `WHERE` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd merge it with the custom predicate section above. Also, the ExprP
is one way of doing it, you can also achieve the same thing with P(func(b *sql.Builder) { ... })
- which gives more control over how identifiers and placeholders are formatted. I can suggest another example option for writing it if you like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this?
{
input: Select("id").From(Table("users")).Where(P(func(b *Builder) {
b.WriteString("DATE(last_login_at) >= ?").Arg("2022-05-03")
})),
wantQuery: "SELECT `id` FROM `users` WHERE DATE(last_login_at) >= ?",
wantArgs: []interface{}{"2022-05-03"},
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be this instead
{
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"},
},
@a8m I've added examples using both |
Looks like a timeout issue in the lint?
|
doc/md/predicates.md
Outdated
You may also perform a more complex sql query, for example `DATE_ADD()` | ||
|
||
```go | ||
events := r.ent.Event.Query(). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
events := r.ent.Event.Query(). | |
events := client.Event.Query(). |
doc/md/predicates.md
Outdated
```go | ||
events := r.ent.Event.Query(). | ||
Where(func(s *entsql.Selector) { | ||
s.Where(entsql.ExprP("DATE_ADD(date, INTERVAL duration MINUTE) BETWEEN ? AND ?", startDateVar, endDateVar)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s.Where(entsql.ExprP("DATE_ADD(date, INTERVAL duration MINUTE) BETWEEN ? AND ?", startDateVar, endDateVar)) | |
s.Where(entsql.ExprP("DATE_ADD(date, INTERVAL duration MINUTE) BETWEEN ? AND ?", start, end)) |
doc/md/predicates.md
Outdated
|
||
```go | ||
events := r.ent.Event.Query(). | ||
Where(func(s *entsql.Selector) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where(func(s *entsql.Selector) { | |
Where(func(s *sql.Selector) { |
doc/md/predicates.md
Outdated
For example, if you want to use builtin sql functions such as a `DATE()` function to a `WHERE` statement. | ||
It can be achieved by using either `P()` or `ExprP()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
10000 | For example, if you want to use builtin sql functions such as a `DATE()` function to a `WHERE` statement. |
It can be achieved by using either `P()` or `ExprP()`. | |
For example, in order to use built-in SQL functions such as `DATE()`, use one of the following options: |
doc/md/predicates.md
Outdated
1. `P()` | ||
|
||
Using `P()` gives more flexibility in how you arrange the strings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. `P()` | |
Using `P()` gives more flexibility in how you arrange the strings. | |
1\. Pass a dialect-aware predicate function using the `sql.P` option: |
doc/md/predicates.md
Outdated
Select("id"). | ||
From(Table("users")). | ||
Where(P(func(b *Builder) { | ||
b.WriteString("DATE(").Ident("last_login_at").WriteString(") >= ").Arg("2022-05-03") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b.WriteString("DATE(").Ident("last_login_at").WriteString(") >= ").Arg("2022-05-03") | |
b.WriteString("DATE(").Ident("last_login_at").WriteByte(')').WriteOp(OpGTE).Arg(value) |
doc/md/predicates.md
Outdated
Select("id"). | ||
From(Table("users")). | ||
Where(P(func(b *Builder) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align this example with the one below.
doc/md/predicates.md
Outdated
```go | ||
users := client.User.Query(). | ||
Where(func(s *ent.Selector) { | ||
s.Where(sql.ExprP("DATE(last_login_at >= ?", dateVar)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s.Where(sql.ExprP("DATE(last_login_at >= ?", dateVar)) | |
s.Where(sql.ExprP("DATE(last_login_at >= ?", value)) |
doc/md/predicates.md
Outdated
|
||
```go | ||
users := client.User.Query(). | ||
Where(func(s *ent.Selector) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where(func(s *ent.Selector) { | |
Where(func(s *sql.Selector) { |
doc/md/predicates.md
Outdated
2. `ExprP()` | ||
|
||
The same thing can be achieved with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2. `ExprP()` | |
The same thing can be achieved with | |
2\. Inline a predicate expression using the `ExprP()` option: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @a8m, thank you for all the feedback. I have made the changes as you suggested.
However for this, I do not understand the wording Invalid a predicate expression...
. Are we invalidating anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the confusion. I think I meant “inline”, but accidentally wrote “invalid”.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no worries! pushed all commits for review
Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
Merged! I'm really sorry for the delayed review, and want to thank you for the contribution, @gmhafiz 🙏 |
The
ExprP()
function is documented inbuilder.go
and includes an example.This pull request intends to make
ExprP()
visible in the website and provides two additional examples using sql date functions.