common math and aggregation sql functions

This commit is contained in:
Sam Anklesaria 2013-07-02 19:39:17 +09:00
parent dc5baefd48
commit 30c321ee03
4 changed files with 31 additions and 0 deletions

View File

@ -43,6 +43,7 @@ module Database.Esqueleto
, val, isNothing, just, nothing, countRows, count, not_
, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
, (+.), (-.), (/.), (*.)
, random_, sum_, round_, ceil_, floor_, avg_, min_, max_
, like, (%), concat_, (++.)
, subList_select, subList_selectDistinct, valList
, in_, notIn, exists, notExists

View File

@ -231,6 +231,16 @@ class (Functor query, Applicative query, Monad query) =>
(/.) :: PersistField a => expr (Value a) -> expr (Value a) -> expr (Value a)
(*.) :: PersistField a => expr (Value a) -> expr (Value a) -> expr (Value a)
random_ :: PersistField a => expr (Value a)
round_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
ceil_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
floor_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
sum_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
min_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
max_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
avg_ :: (PersistField a, PersistField b) => expr (Value a) -> expr (Value b)
-- | @LIKE@ operator.
like :: (PersistField s, IsString s) => expr (Value s) -> expr (Value s) -> expr (Value Bool)
-- | The string @'%'@. May be useful while using 'like' and

View File

@ -333,6 +333,15 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
(/.) = unsafeSqlBinOp " / "
(*.) = unsafeSqlBinOp " * "
random_ = unsafeSqlValue "RANDOM()"
sum_ = unsafeSqlFunction "SUM"
round_ = unsafeSqlFunction "ROUND_"
ceil_ = unsafeSqlFunction "CEILING"
floor_ = unsafeSqlFunction "FLOOR"
avg_ = unsafeSqlFunction "AGV"
min_ = unsafeSqlFunction "MIN"
max_ = unsafeSqlFunction "MAX"
like = unsafeSqlBinOp " LIKE "
(%) = unsafeSqlValue "'%'"
concat_ = unsafeSqlFunction "CONCAT"

View File

@ -265,6 +265,17 @@ main = do
return p
liftIO $ ret `shouldBe` [ p3e ]
it "works with sum_" $
run $ do
_ <- insert' p1
_ <- insert' p2
_ <- insert' p3
_ <- insert' p4
ret <- select $
from $ \p->
return $ sum_ (p ^. PersonAge)
liftIO $ ret `shouldBe` [ Value (36 + 17 + 17 :: Int) ]
it "works with isNothing" $
run $ do
_ <- insert' p1