Add support for lower() function

This commit is contained in:
Alexandr Kurilin 2015-04-03 14:58:24 -07:00
parent 69a4ec0c44
commit b46c52d7b2
4 changed files with 26 additions and 3 deletions

View File

@ -44,7 +44,7 @@ module Database.Esqueleto
, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
, (+.), (-.), (/.), (*.)
, random_, round_, ceiling_, floor_
, min_, max_, sum_, avg_
, min_, max_, sum_, avg_, lower_
, coalesce, coalesceDefault
, like, (%), concat_, (++.)
, subList_select, subList_selectDistinct, valList

View File

@ -255,6 +255,7 @@ class (Functor query, Applicative query, Monad query) =>
min_ :: (PersistField a) => expr (Value a) -> expr (Value (Maybe a))
max_ :: (PersistField a) => expr (Value a) -> expr (Value (Maybe a))
avg_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value (Maybe b))
lower_ :: (PersistField a, IsString a) => expr (Value a) -> expr (Value a)
-- | @COALESCE@ function. Evaluates the arguments in order and
-- returns the value of the first non-NULL expression, or NULL

View File

@ -368,13 +368,15 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
(*.) = unsafeSqlBinOp " * "
random_ = unsafeSqlValue "RANDOM()"
sum_ = unsafeSqlFunction "SUM"
round_ = unsafeSqlFunction "ROUND"
ceiling_ = unsafeSqlFunction "CEILING"
floor_ = unsafeSqlFunction "FLOOR"
avg_ = unsafeSqlFunction "AVG"
sum_ = unsafeSqlFunction "SUM"
min_ = unsafeSqlFunction "MIN"
max_ = unsafeSqlFunction "MAX"
avg_ = unsafeSqlFunction "AVG"
lower_ = unsafeSqlFunction "LOWER"
coalesce = unsafeSqlFunction "COALESCE"
coalesceDefault exprs = unsafeSqlFunction "COALESCE" . (exprs ++) . return . just

View File

@ -44,6 +44,7 @@ import Test.Hspec
import qualified Control.Monad.Trans.Resource as R
import qualified Data.Set as S
import qualified Data.List as L
import Data.Char (toLower, toUpper)
-- Test schema
@ -374,6 +375,25 @@ main = do
return $ joinV $ max_ (p ^. PersonAge)
liftIO $ ret `shouldBe` [ Value $ Just (36 :: Int) ]
it "works with lower_" $
run $ do
p1e <- insert' p1
p2e@(Entity _ bob) <- insert' $ Person "bob" (Just 36) Nothing 1
-- lower(name) == 'john'
ret <- select $
from $ \p-> do
where_ (lower_ (p ^. PersonName) ==. val (map toLower $ personName p1))
return p
liftIO $ ret `shouldBe` [ p1e ]
-- name == lower('BOB')
ret <- select $
from $ \p-> do
where_ (p ^. PersonName ==. lower_ (val $ map toUpper $ personName bob))
return p
liftIO $ ret `shouldBe` [ p2e ]
it "works with random_" $
run $ do
#if defined(WITH_POSTGRESQL) || defined(WITH_MYSQL)