Add stringAgg to PostgreSQL module (fixes #98).

This commit is contained in:
Felipe Lessa 2015-07-15 12:34:47 -03:00
parent 6c63f2c5ac
commit 3bfa1a9a43
2 changed files with 25 additions and 0 deletions

View File

@ -5,8 +5,11 @@
-- /Since: 2.2.8/
module Database.Esqueleto.PostgreSQL
( arrayAgg
, stringAgg
) where
import Data.String (IsString)
import Database.Esqueleto.Internal.Language
import Database.Esqueleto.Internal.Sql
@ -17,3 +20,15 @@ import Database.Esqueleto.Internal.Sql
-- /Since: 2.2.8/
arrayAgg :: SqlExpr (Value a) -> SqlExpr (Value [a])
arrayAgg = unsafeSqlFunction "array_agg"
-- | (@string_agg@) Concatenate input values separated by a
-- delimiter.
--
-- /Since: 2.2.8/
stringAgg
:: IsString s
=> SqlExpr (Value s) -- ^ Input values.
-> SqlExpr (Value s) -- ^ Delimiter.
-> SqlExpr (Value s) -- ^ Concatenation.
stringAgg expr delim = unsafeSqlFunction "string_agg" (expr, delim)

View File

@ -1330,6 +1330,16 @@ main = do
from $ \p -> do
return (EP.arrayAgg (p ^. PersonName))
liftIO $ L.sort ret `shouldBe` L.sort (map personName people)
it "stringAgg looks sane" $
run $ do
let people = [p1, p2, p3, p4, p5]
mapM_ insert people
[Value ret] <-
select $
from $ \p -> do
return (EP.stringAgg (p ^. PersonName) (val " "))
liftIO $ L.sort (words ret) `shouldBe` L.sort (map personName people)
#endif