prowdsponsor / esqueleto

Bare bones, type-safe EDSL for SQL queries on persistent backends.
http://hackage.haskell.org/package/esqueleto
BSD 3-Clause "New" or "Revised" License
178 stars 51 forks source link

Add HAVING support. #15

Closed himura closed 11 years ago

himura commented 11 years ago

I've added the having method to support HAVING clause with some aggregate functions like this:

ret <- select $
       from $ \(p `LeftOuterJoin` b) -> do
       on (p ^. PersonId ==. b ^. BlogPostAuthorId)
       let cnt = count (b ^. BlogPostId)
       groupBy (p ^. PersonId)
       having (cnt >. (val 0))
       orderBy [ asc cnt ]
       return (p, cnt)

It generates

SELECT "Person"."id", "Person"."name", "Person"."age", COUNT("BlogPost"."id")
FROM "Person" LEFT OUTER JOIN "BlogPost" ON "Person"."id" = "BlogPost"."authorId"
GROUP BY "Person"."id"
HAVING COUNT("BlogPost"."id") > ?
ORDER BY COUNT("BlogPost"."id") ASC
meteficha commented 11 years ago

Amazing, thanks! Released as esqueleto-1.2.2.