From 30c321ee03235bd77bce91fe1a2e4e28464be5c9 Mon Sep 17 00:00:00 2001 From: Sam Anklesaria Date: Tue, 2 Jul 2013 19:39:17 +0900 Subject: [PATCH] common math and aggregation sql functions --- src/Database/Esqueleto.hs | 1 + src/Database/Esqueleto/Internal/Language.hs | 10 ++++++++++ src/Database/Esqueleto/Internal/Sql.hs | 9 +++++++++ test/Test.hs | 11 +++++++++++ 4 files changed, 31 insertions(+) diff --git a/src/Database/Esqueleto.hs b/src/Database/Esqueleto.hs index 59830de..82f8df6 100644 --- a/src/Database/Esqueleto.hs +++ b/src/Database/Esqueleto.hs @@ -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 diff --git a/src/Database/Esqueleto/Internal/Language.hs b/src/Database/Esqueleto/Internal/Language.hs index a5c7df9..546c40f 100644 --- a/src/Database/Esqueleto/Internal/Language.hs +++ b/src/Database/Esqueleto/Internal/Language.hs @@ -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 diff --git a/src/Database/Esqueleto/Internal/Sql.hs b/src/Database/Esqueleto/Internal/Sql.hs index 193448e..0b5505e 100644 --- a/src/Database/Esqueleto/Internal/Sql.hs +++ b/src/Database/Esqueleto/Internal/Sql.hs @@ -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" diff --git a/test/Test.hs b/test/Test.hs index 05f12a5..050e5c3 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -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