ent/dialect/sql: add ContainsFold to builder

Reviewed By: alexsn

Differential Revision: D16961184

fbshipit-source-id: f3f0c2bdd4950340ddebca929b8fed79256c7aeb
This commit is contained in:
Ariel Mashraki
2019-08-22 04:37:02 -07:00
committed by Facebook Github Bot
parent f1c1ead2bd
commit fb56e0c786
2 changed files with 30 additions and 6 deletions

View File

@@ -1002,32 +1002,42 @@ func (p *Predicate) Like(col, pattern string) *Predicate {
return p
}
// HasPrefix is an helper predicate that checks prefix using the LIKE predicate.
// HasPrefix is a helper predicate that checks prefix using the LIKE predicate.
func HasPrefix(col, prefix string) *Predicate {
return (&Predicate{}).HasPrefix(col, prefix)
}
// HasPrefix is an helper predicate that checks prefix using the LIKE predicate.
// HasPrefix is a helper predicate that checks prefix using the LIKE predicate.
func (p *Predicate) HasPrefix(col, prefix string) *Predicate {
return p.Like(col, prefix+"%")
}
// HasSuffix is an helper predicate that checks suffix using the LIKE predicate.
// HasSuffix is a helper predicate that checks suffix using the LIKE predicate.
func HasSuffix(col, suffix string) *Predicate { return (&Predicate{}).HasSuffix(col, suffix) }
// HasSuffix is an helper predicate that checks suffix using the LIKE predicate.
// HasSuffix is a helper predicate that checks suffix using the LIKE predicate.
func (p *Predicate) HasSuffix(col, suffix string) *Predicate {
return p.Like(col, "%"+suffix)
}
// Contains is an helper predicate that checks substring using the LIKE predicate.
// Contains is a helper predicate that checks substring using the LIKE predicate.
func Contains(col, sub string) *Predicate { return (&Predicate{}).Contains(col, sub) }
// Contains is an helper predicate that checks substring using the LIKE predicate.
// Contains is a helper predicate that checks substring using the LIKE predicate.
func (p *Predicate) Contains(col, sub string) *Predicate {
return p.Like(col, "%"+sub+"%")
}
// ContainsFold is a helper predicate that checks substring using the LIKE predicate.
func ContainsFold(col, sub string) *Predicate { return (&Predicate{}).ContainsFold(col, sub) }
// ContainsFold is a helper predicate that applies the LIKE predicate with case-folding.
// The recommendation is to avoid using it, and to use a dialect specific feature, like
// `ILIKE` in PostgreSQL, and `COLLATE` clause in MySQL.
func (p *Predicate) ContainsFold(col, sub string) *Predicate {
return p.Like(Lower(col), "%"+strings.ToLower(sub)+"%")
}
// Lower wraps the given column with the LOWER function.
//
// P().EQ(sql.Lower("name"), "a8m")

View File

@@ -184,6 +184,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(ContainsFold("name", "Ariel")),
wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) LIKE ?",
wantArgs: []interface{}{"%ariel%"},
},
{
input: Select().
From(Table("users")).
Where(ContainsFold("name", "Ariel").And().ContainsFold("nick", "Bar")),
wantQuery: "SELECT * FROM `users` WHERE LOWER(`name`) LIKE ? AND LOWER(`nick`) LIKE ?",
wantArgs: []interface{}{"%ariel%", "%bar%"},
},
{
input: Update("users").
Set("name", "foo").