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:
Hafiz Shafruddin
2022-06-29 18:24:20 +10:00
committed by GitHub
parent 84b05c05cd
commit 5dbfa24b46
2 changed files with 63 additions and 0 deletions

View File

@@ -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")