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

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

merged 14 commits into from
Jun 29, 2022

Conversation

gmhafiz
Copy link
Contributor
@gmhafiz gmhafiz commented May 3, 2022

The ExprP() function is documented in builder.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.

@gmhafiz
Copy link
Contributor Author
gmhafiz commented May 4, 2022

@@ -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`
Copy link
Member

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.

Copy link
Contributor Author
@gmhafiz gmhafiz May 5, 2022

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"},
},

Copy link
Contributor Author

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"},
},

@gmhafiz
Copy link
Contributor Author
gmhafiz commented May 6, 2022

@a8m I've added examples using both P() and ExprP() to docs and builder_test.go

@gmhafiz
Copy link
Contributor Author
gmhafiz commented May 29, 2022

Looks like a timeout issue in the lint?

run golangci-lint
  Running [/home/runner/golangci-lint-1.45.2-linux-amd64/golangci-lint run --out-format=github-actions] in [] ...
  level=error msg="Timeout exceeded: try increasing it by passing --timeout option"
  
  Error: golangci-lint exit with code 4
  Ran golangci-lint in 196379ms

You may also perform a more complex sql query, for example `DATE_ADD()`

```go
events := r.ent.Event.Query().
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
events := r.ent.Event.Query().
events := client.Event.Query().

```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))
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
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))


```go
events := r.ent.Event.Query().
Where(func(s *entsql.Selector) {
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
Where(func(s *entsql.Selector) {
Where(func(s *sql.Selector) {

Comment on lines 92 to 93
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()`.
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
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:

Comment on lines 95 to 97
1. `P()`

Using `P()` gives more flexibility in how you arrange the strings.
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
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:

Select("id").
From(Table("users")).
Where(P(func(b *Builder) {
b.WriteString("DATE(").Ident("last_login_at").WriteString(") >= ").Arg("2022-05-03")
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
b.WriteString("DATE(").Ident("last_login_at").WriteString(") >= ").Arg("2022-05-03")
b.WriteString("DATE(").Ident("last_login_at").WriteByte(')').WriteOp(OpGTE).Arg(value)

Comment on lines 100 to 102
Select("id").
From(Table("users")).
Where(P(func(b *Builder) {
Copy link
Member

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.

```go
users := client.User.Query().
Where(func(s *ent.Selector) {
s.Where(sql.ExprP("DATE(last_login_at >= ?", dateVar))
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
s.Where(sql.ExprP("DATE(last_login_at >= ?", dateVar))
s.Where(sql.ExprP("DATE(last_login_at >= ?", value))


```go
users := client.User.Query().
Where(func(s *ent.Selector) {
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
Where(func(s *ent.Selector) {
Where(func(s *sql.Selector) {

Comment on lines 113 to 115
2. `ExprP()`

The same thing can be achieved with
Copy link
Member
@a8m a8m May 29, 2022

Choose a reason for hiding this comment

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

Suggested change
2. `ExprP()`
The same thing can be achieved with
2\. Inline a predicate expression using the `ExprP()` option:

Copy link
Contributor Author

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?

Copy link
Member

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”.

Copy link
Contributor Author

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>
@a8m a8m merged commit 5dbfa24 into ent:master Jun 29, 2022
@a8m
Copy link
Member
a8m commented Jun 29, 2022

Merged! I'm really sorry for the delayed review, and want to thank you for the contribution, @gmhafiz 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0