Skip to content

Commit

Permalink
Add like and ilike (#146)
Browse files Browse the repository at this point in the history
I couldn't quite decide the right home for these operators, but have gone for `Rel8.Expr.Text` in the end. As they are standard SQL operators, I've also re-exported `like` and `ilike` from `Rel8`.

Fixes #144.
  • Loading branch information
ocharles committed Jan 31, 2022
1 parent 35e131e commit 5064673
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Rel8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ module Rel8
, (==.), (/=.), (==?), (/=?)
, in_
, boolExpr, caseExpr
, like, ilike

-- ** Ordering
, DBOrd
Expand Down Expand Up @@ -318,6 +319,7 @@ import Rel8.Expr.Ord
import Rel8.Expr.Order
import Rel8.Expr.Serialize
import Rel8.Expr.Sequence
import Rel8.Expr.Text ( like, ilike )
import Rel8.Generic.Rel8able ( KRel8able, Rel8able )
import Rel8.Order
import Rel8.Query
Expand Down
25 changes: 23 additions & 2 deletions src/Rel8/Expr/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ module Rel8.Expr.Text
, pgClientEncoding, quoteIdent, quoteLiteral, quoteNullable, regexpReplace
, regexpSplitToArray, repeat, replace, reverse, right, rpad, rtrim
, splitPart, strpos, substr, translate

-- * @LIKE@ and @ILIKE@
, like, ilike
)
where

-- base
import Data.Bool ( Bool )
import Data.Int ( Int32 )
import Data.Maybe ( Maybe( Nothing, Just ) )
import Prelude ()
import Prelude ( flip )

-- bytestring
import Data.ByteString ( ByteString )
Expand All @@ -49,7 +52,7 @@ infixr 6 ++.


-- | Matches regular expression, case sensitive
--
--
-- Corresponds to the @~.@ operator.
(~.) :: Expr Text -> Expr Text -> Expr Bool
(~.) = binaryOperator "~."
Expand Down Expand Up @@ -273,3 +276,21 @@ substr a b Nothing = function "substr" a b
-- | Corresponds to the @translate@ function.
translate :: Expr Text -> Expr Text -> Expr Text -> Expr Text
translate = function "translate"


-- | @like x y@ corresponds to the expression @y LIKE x@.
--
-- Note that the arguments to @like@ are swapped. This is to aid currying, so
-- you can write expressions like
-- @filter (like "Rel%" . packageName) =<< each haskellPackages@
like :: Expr Text -> Expr Text -> Expr Bool
like = flip (binaryOperator "LIKE")


-- | @ilike x y@ corresponds to the expression @y ILIKE x@.
--
-- Note that the arguments to @ilike@ are swapped. This is to aid currying, so
-- you can write expressions like
-- @filter (ilike "Rel%" . packageName) =<< each haskellPackages@
ilike :: Expr Text -> Expr Text -> Expr Bool
ilike = flip (binaryOperator "ILIKE")

0 comments on commit 5064673

Please sign in to comment.