mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
doc/md: add docs on using expression predicates in custom WHERE clauses (#2520)
* Add docs on using ExprP() for custom WHERE sql statement * Add ExprP() examples using integration test * move custom predicate example and show examples using both P() and ExprP() * fix to greater or equal than * rephrase wording and fix sql builder * Update doc/md/predicates.md Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com> * Update doc/md/predicates.md * Update doc/md/predicates.md * Update doc/md/predicates.md * Update doc/md/predicates.md Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user