mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql: escape EqualFold on Postgres (#2860)
* adds tests for equalFold predicates * ensure EqualFold escapes the argument on Postgres * adds tests on builder as well
This commit is contained in:
@@ -1703,14 +1703,17 @@ func (p *Predicate) EqualFold(col, sub string) *Predicate {
|
||||
// We assume the CHARACTER SET is configured to utf8mb4,
|
||||
// because this how it is defined in dialect/sql/schema.
|
||||
b.Ident(col).WriteString(" COLLATE utf8mb4_general_ci = ")
|
||||
b.Arg(strings.ToLower(sub))
|
||||
case dialect.Postgres:
|
||||
b.Ident(col).WriteString(" ILIKE ")
|
||||
w, _ := escape(sub)
|
||||
b.Arg(strings.ToLower(w))
|
||||
default: // SQLite.
|
||||
f.Lower(col)
|
||||
b.WriteString(f.String())
|
||||
b.WriteOp(OpEQ)
|
||||
b.Arg(strings.ToLower(sub))
|
||||
}
|
||||
b.Arg(strings.ToLower(sub))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1019,6 +1019,22 @@ func TestBuilder(t *testing.T) {
|
||||
wantQuery: `SELECT * FROM "users" WHERE "name" ILIKE $1 OR "name" ILIKE $2`,
|
||||
wantArgs: []interface{}{"bar", "baz"},
|
||||
},
|
||||
{
|
||||
input: Dialect(dialect.Postgres).
|
||||
Select().
|
||||
From(Table("users")).
|
||||
Where(Or(EqualFold("name", "BAR%"), EqualFold("name", "%BAZ"))),
|
||||
wantQuery: `SELECT * FROM "users" WHERE "name" ILIKE $1 OR "name" ILIKE $2`,
|
||||
wantArgs: []interface{}{"bar\\%", "\\%baz"},
|
||||
},
|
||||
{
|
||||
input: Dialect(dialect.Postgres).
|
||||
Select().
|
||||
From(Table("users")).
|
||||
Where(Or(EqualFold("name", "BAR\\"), EqualFold("name", "\\BAZ"))),
|
||||
wantQuery: `SELECT * FROM "users" WHERE "name" ILIKE $1 OR "name" ILIKE $2`,
|
||||
wantArgs: []interface{}{"bar\\\\", "\\\\baz"},
|
||||
},
|
||||
{
|
||||
input: Dialect(dialect.MySQL).
|
||||
Select().
|
||||
|
||||
Reference in New Issue
Block a user