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:
Alex Snast
2019-08-27 10:47:29 -07:00
committed by Facebook Github Bot
parent a79c1c20c0
commit 373769dfaf
159 changed files with 232 additions and 642 deletions

View File

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

View File

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