mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
ent/gen: adding EqualFold predicate for string fields
Summary: Similar to `ContainsFold` predicate requires `--storage=sql` argument to be passed to entc. Reviewed By: a8m Differential Revision: D17074805 fbshipit-source-id: ced299154417fe2c9007cd6a7a504f53c8b2ef98
This commit is contained in:
committed by
Facebook Github Bot
parent
a79c1c20c0
commit
373769dfaf
@@ -1024,6 +1024,14 @@ func (p *Predicate) HasSuffix(col, suffix string) *Predicate {
|
||||
return p.Like(col, "%"+suffix)
|
||||
}
|
||||
|
||||
// EqualFold is a helper predicate that applies the "=" predicate with case-folding.
|
||||
func EqualFold(col, sub string) *Predicate { return (&Predicate{}).EqualFold(col, sub) }
|
||||
|
||||
// EqualFold is a helper predicate that applies the "=" predicate with case-folding.
|
||||
func (p *Predicate) EqualFold(col, sub string) *Predicate {
|
||||
return p.EQ(Lower(col), strings.ToLower(sub))
|
||||
}
|
||||
|
||||
// Contains is a helper predicate that checks substring using the LIKE predicate.
|
||||
func Contains(col, sub string) *Predicate { return (&Predicate{}).Contains(col, sub) }
|
||||
|
||||
|
||||
@@ -188,6 +188,20 @@ func TestBuilder(t *testing.T) {
|
||||
wantQuery: "UPDATE `users` SET `name` = ? WHERE `nickname` LIKE ? AND `lastname` LIKE ?",
|
||||
wantArgs: []interface{}{"foo", "a8m%", "%mash%"},
|
||||
},
|
||||
{
|
||||
input: Select().
|
||||
From(Table("users")).
|
||||
Where(EqualFold("name", "Alex")),
|
||||
wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) = ?",
|
||||
wantArgs: []interface{}{"alex"},
|
||||
},
|
||||
{
|
||||
input: Select().
|
||||
From(Table("users")).
|
||||
Where(EqualFold("name", "BAR").Or().EqualFold("name", "BAZ")),
|
||||
wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) = ? OR LOWER(`name`) = ?",
|
||||
wantArgs: []interface{}{"bar", "baz"},
|
||||
},
|
||||
{
|
||||
input: Select().
|
||||
From(Table("users")).
|
||||
|
||||
Reference in New Issue
Block a user