From 30c321ee03235bd77bce91fe1a2e4e28464be5c9 Mon Sep 17 00:00:00 2001 From: Sam Anklesaria Date: Tue, 2 Jul 2013 19:39:17 +0900 Subject: [PATCH 1/3] 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 From 4377f2cffabb555c197693ffb41422ca848c69dc Mon Sep 17 00:00:00 2001 From: Sam Anklesaria Date: Tue, 2 Jul 2013 19:47:18 +0900 Subject: [PATCH 2/3] fixed a typo --- src/Database/Esqueleto/Internal/Sql.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Esqueleto/Internal/Sql.hs b/src/Database/Esqueleto/Internal/Sql.hs index 0b5505e..6403602 100644 --- a/src/Database/Esqueleto/Internal/Sql.hs +++ b/src/Database/Esqueleto/Internal/Sql.hs @@ -338,7 +338,7 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where round_ = unsafeSqlFunction "ROUND_" ceil_ = unsafeSqlFunction "CEILING" floor_ = unsafeSqlFunction "FLOOR" - avg_ = unsafeSqlFunction "AGV" + avg_ = unsafeSqlFunction "AVG" min_ = unsafeSqlFunction "MIN" max_ = unsafeSqlFunction "MAX" From 3641f3632601211cf64babe9bf7c5f44e9e777d3 Mon Sep 17 00:00:00 2001 From: Sam Anklesaria Date: Wed, 3 Jul 2013 15:46:22 +0900 Subject: [PATCH 3/3] added tests for math and aggregation functions --- src/Database/Esqueleto.hs | 2 +- src/Database/Esqueleto/Internal/Language.hs | 2 +- src/Database/Esqueleto/Internal/Sql.hs | 4 +- test/Test.hs | 43 +++++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/Database/Esqueleto.hs b/src/Database/Esqueleto.hs index eb4d31c..c781dc7 100644 --- a/src/Database/Esqueleto.hs +++ b/src/Database/Esqueleto.hs @@ -43,7 +43,7 @@ module Database.Esqueleto , val, isNothing, just, nothing, countRows, count, not_ , (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.) , (+.), (-.), (/.), (*.) - , random_, sum_, round_, ceil_, floor_, avg_, min_, max_ + , random_, sum_, round_, ceiling_, 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 546c40f..f0d0c1a 100644 --- a/src/Database/Esqueleto/Internal/Language.hs +++ b/src/Database/Esqueleto/Internal/Language.hs @@ -234,7 +234,7 @@ class (Functor query, Applicative query, Monad query) => 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) + ceiling_ :: (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) diff --git a/src/Database/Esqueleto/Internal/Sql.hs b/src/Database/Esqueleto/Internal/Sql.hs index 6403602..79f104b 100644 --- a/src/Database/Esqueleto/Internal/Sql.hs +++ b/src/Database/Esqueleto/Internal/Sql.hs @@ -335,8 +335,8 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where random_ = unsafeSqlValue "RANDOM()" sum_ = unsafeSqlFunction "SUM" - round_ = unsafeSqlFunction "ROUND_" - ceil_ = unsafeSqlFunction "CEILING" + round_ = unsafeSqlFunction "ROUND" + ceiling_ = unsafeSqlFunction "CEILING" floor_ = unsafeSqlFunction "FLOOR" avg_ = unsafeSqlFunction "AVG" min_ = unsafeSqlFunction "MIN" diff --git a/test/Test.hs b/test/Test.hs index 050e5c3..d36bdec 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -276,6 +276,49 @@ main = do return $ sum_ (p ^. PersonAge) liftIO $ ret `shouldBe` [ Value (36 + 17 + 17 :: Int) ] + it "works with sum_" $ + run $ do + _ <- insert' p1 + _ <- insert' p2 + _ <- insert' p3 + _ <- insert' p4 + ret <- select $ + from $ \p-> + return $ avg_ (p ^. PersonAge) + liftIO $ ret `shouldBe` [ Value ((36 + 17 + 17) / 3 :: Double) ] + + it "works with min_" $ + run $ do + _ <- insert' p1 + _ <- insert' p2 + _ <- insert' p3 + _ <- insert' p4 + ret <- select $ + from $ \p-> + return $ min_ (p ^. PersonAge) + liftIO $ ret `shouldBe` [ Value (17 :: Int) ] + + it "works with max_" $ + run $ do + _ <- insert' p1 + _ <- insert' p2 + _ <- insert' p3 + _ <- insert' p4 + ret <- select $ + from $ \p-> + return $ max_ (p ^. PersonAge) + liftIO $ ret `shouldBe` [ Value (36 :: Int) ] + + it "works with random_" $ + run $ do + ret <- select $ return (random_ :: SqlExpr (Value Int)) + return () + + it "works with round_" $ + run $ do + ret <- select $ return $ round_ (val (16.2 :: Double)) + liftIO $ ret `shouldBe` [ Value (16 :: Double) ] + it "works with isNothing" $ run $ do _ <- insert' p1