From fb56e0c786730285747d46c30cb662e51b2b1c31 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Thu, 22 Aug 2019 04:37:02 -0700 Subject: [PATCH] ent/dialect/sql: add ContainsFold to builder Reviewed By: alexsn Differential Revision: D16961184 fbshipit-source-id: f3f0c2bdd4950340ddebca929b8fed79256c7aeb --- dialect/sql/builder.go | 22 ++++++++++++++++------ dialect/sql/builder_test.go | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/dialect/sql/builder.go b/dialect/sql/builder.go index f04239534..68600791a 100644 --- a/dialect/sql/builder.go +++ b/dialect/sql/builder.go @@ -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") diff --git a/dialect/sql/builder_test.go b/dialect/sql/builder_test.go index 281a6efe8..f9ee4c7c8 100644 --- a/dialect/sql/builder_test.go +++ b/dialect/sql/builder_test.go @@ -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").