From 2397ddf12b723df0679a91f74e555fd6241e24cf Mon Sep 17 00:00:00 2001 From: Maciej Bielecki Date: Thu, 16 Apr 2015 21:30:03 +0200 Subject: [PATCH] Add ILIKE operator --- src/Database/Esqueleto.hs | 2 +- src/Database/Esqueleto/Internal/Language.hs | 5 ++++- src/Database/Esqueleto/Internal/Sql.hs | 1 + test/Test.hs | 17 ++++++++++++++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Database/Esqueleto.hs b/src/Database/Esqueleto.hs index 3e599c8..adbcb5d 100644 --- a/src/Database/Esqueleto.hs +++ b/src/Database/Esqueleto.hs @@ -46,7 +46,7 @@ module Database.Esqueleto , random_, round_, ceiling_, floor_ , min_, max_, sum_, avg_, lower_ , coalesce, coalesceDefault - , like, (%), concat_, (++.) + , like, ilike, (%), concat_, (++.) , subList_select, subList_selectDistinct, valList , in_, notIn, exists, notExists , set, (=.), (+=.), (-=.), (*=.), (/=.) diff --git a/src/Database/Esqueleto/Internal/Language.hs b/src/Database/Esqueleto/Internal/Language.hs index 541955c..6b789f6 100644 --- a/src/Database/Esqueleto/Internal/Language.hs +++ b/src/Database/Esqueleto/Internal/Language.hs @@ -276,6 +276,9 @@ class (Functor query, Applicative query, Monad query) => -- | @LIKE@ operator. like :: (PersistField s, IsString s) => expr (Value s) -> expr (Value s) -> expr (Value Bool) + -- | @ILIKE@ operator (case-insensitive @LIKE@). + -- Supported by PostgreSQL only. + ilike :: (PersistField s, IsString s) => expr (Value s) -> expr (Value s) -> expr (Value Bool) -- | The string @'%'@. May be useful while using 'like' and -- concatenation ('concat_' or '++.', depending on your -- database). Note that you always to type the parenthesis, @@ -391,7 +394,7 @@ infixl 6 +., -. infixr 5 ++. infix 4 ==., >=., >., <=., <., !=. infixr 3 &&., =., +=., -=., *=., /=. -infixr 2 ||., `like` +infixr 2 ||., `like`, `ilike` infixl 2 `InnerJoin`, `CrossJoin`, `LeftOuterJoin`, `RightOuterJoin`, `FullOuterJoin` -- | Syntax sugar for 'case_'. diff --git a/src/Database/Esqueleto/Internal/Sql.hs b/src/Database/Esqueleto/Internal/Sql.hs index 9da7d90..03cdab7 100644 --- a/src/Database/Esqueleto/Internal/Sql.hs +++ b/src/Database/Esqueleto/Internal/Sql.hs @@ -410,6 +410,7 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where coalesceDefault exprs = unsafeSqlFunctionParens "COALESCE" . (exprs ++) . return . just like = unsafeSqlBinOp " LIKE " + ilike = unsafeSqlBinOp " ILIKE " (%) = unsafeSqlValue "'%'" concat_ = unsafeSqlFunction "CONCAT" (++.) = unsafeSqlBinOp " || " diff --git a/test/Test.hs b/test/Test.hs index bc65d07..9ce4911 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -853,7 +853,7 @@ main = do #endif #endif - describe "text functions" $ + describe "text functions" $ do it "like, (%) and (++.) work on a simple example" $ run $ do [p1e, p2e, p3e, p4e] <- mapM insert' [p1, p2, p3, p4] @@ -868,6 +868,21 @@ main = do nameContains "i" [p4e, p3e] nameContains "iv" [p4e] +#if defined(WITH_POSTGRESQL) + it "ilike, (%) and (++.) work on a simple example on PostgreSQL" $ + run $ do + [p1e, p2e, p3e, p4e, p5e] <- mapM insert' [p1, p2, p3, p4, p5] + let nameContains t expected = do + ret <- select $ + from $ \p -> do + where_ (p ^. PersonName `ilike` (%) ++. val t ++. (%)) + orderBy [asc (p ^. PersonName)] + return p + liftIO $ ret `shouldBe` expected + nameContains "mi" [p3e, p5e] + nameContains "JOHN" [p1e] +#endif + describe "delete" $ it "works on a simple example" $ run $ do