Merge pull request #11 from maoe/count-with-expr
Add support for COUNT(expr)
This commit is contained in:
commit
54470aa931
@ -20,7 +20,7 @@ module Database.Esqueleto
|
|||||||
-- * @esqueleto@'s Language
|
-- * @esqueleto@'s Language
|
||||||
Esqueleto( where_, on, groupBy, orderBy, asc, desc, limit, offset
|
Esqueleto( where_, on, groupBy, orderBy, asc, desc, limit, offset
|
||||||
, sub_select, sub_selectDistinct, (^.), (?.)
|
, sub_select, sub_selectDistinct, (^.), (?.)
|
||||||
, val, isNothing, just, nothing, countRows, not_
|
, val, isNothing, just, nothing, countRows, count, not_
|
||||||
, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
|
, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
|
||||||
, (+.), (-.), (/.), (*.)
|
, (+.), (-.), (/.), (*.)
|
||||||
, like, (%), concat_, (++.)
|
, like, (%), concat_, (++.)
|
||||||
|
|||||||
@ -207,6 +207,9 @@ class (Functor query, Applicative query, Monad query) =>
|
|||||||
-- | @COUNT(*)@ value.
|
-- | @COUNT(*)@ value.
|
||||||
countRows :: Num a => expr (Value a)
|
countRows :: Num a => expr (Value a)
|
||||||
|
|
||||||
|
-- | @COUNT@.
|
||||||
|
count :: (Num a) => expr (Value typ) -> expr (Value a)
|
||||||
|
|
||||||
not_ :: expr (Value Bool) -> expr (Value Bool)
|
not_ :: expr (Value Bool) -> expr (Value Bool)
|
||||||
|
|
||||||
(==.) :: PersistField typ => expr (Value typ) -> expr (Value typ) -> expr (Value Bool)
|
(==.) :: PersistField typ => expr (Value typ) -> expr (Value typ) -> expr (Value Bool)
|
||||||
|
|||||||
@ -306,6 +306,8 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
|
|||||||
just (ERaw p f) = ERaw p f
|
just (ERaw p f) = ERaw p f
|
||||||
nothing = unsafeSqlValue "NULL"
|
nothing = unsafeSqlValue "NULL"
|
||||||
countRows = unsafeSqlValue "COUNT(*)"
|
countRows = unsafeSqlValue "COUNT(*)"
|
||||||
|
count (ERaw _ f) = ERaw Never $ \conn -> let (b, vals) = f conn
|
||||||
|
in ("COUNT" <> parens b, vals)
|
||||||
|
|
||||||
not_ (ERaw p f) = ERaw Never $ \conn -> let (b, vals) = f conn
|
not_ (ERaw p f) = ERaw Never $ \conn -> let (b, vals) = f conn
|
||||||
in ("NOT " <> parensM p b, vals)
|
in ("NOT " <> parensM p b, vals)
|
||||||
|
|||||||
18
test/Test.hs
18
test/Test.hs
@ -495,6 +495,24 @@ main = do
|
|||||||
, Entity p3k p3 { personAge = Just 7 }
|
, Entity p3k p3 { personAge = Just 7 }
|
||||||
, Entity p2k p2 { personAge = Just 0 } ]
|
, Entity p2k p2 { personAge = Just 0 } ]
|
||||||
|
|
||||||
|
it "GROUP BY works with COUNT" $
|
||||||
|
run $ do
|
||||||
|
p1k <- insert p1
|
||||||
|
p2k <- insert p2
|
||||||
|
p3k <- insert p3
|
||||||
|
replicateM_ 3 (insert $ BlogPost "" p1k)
|
||||||
|
replicateM_ 7 (insert $ BlogPost "" p3k)
|
||||||
|
ret <- select $
|
||||||
|
from $ \(p `LeftOuterJoin` b) -> do
|
||||||
|
on (p ^. PersonId ==. b ^. BlogPostAuthorId)
|
||||||
|
groupBy (p ^. PersonId)
|
||||||
|
let cnt = count (b ^. BlogPostId)
|
||||||
|
orderBy [ asc cnt ]
|
||||||
|
return (p, cnt)
|
||||||
|
liftIO $ ret `shouldBe` [ (Entity p2k p2, Value (0 :: Int))
|
||||||
|
, (Entity p1k p1, Value 3)
|
||||||
|
, (Entity p3k p3, Value 7) ]
|
||||||
|
|
||||||
describe "lists of values" $ do
|
describe "lists of values" $ do
|
||||||
it "IN works for valList" $
|
it "IN works for valList" $
|
||||||
run $ do
|
run $ do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user