on dev help sign in
array constructors
ee135aeEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+54−2
@@ -58,6 +58,8 @@ module Squeal.PostgreSQL.Expression
58 , nullIf
58 , nullIf
59 -- ** Collections
59 -- ** Collections
60 , array
60 , array
61+ , array1
62+ , array2
61 , cardinality
63 , cardinality
62 , index
64 , index
63 , row
65 , row
@@ -576,8 +578,58 @@ array
576 :: [Expression outer grp commons schemas params from ty]
578 :: [Expression outer grp commons schemas params from ty]
577 -- ^ array elements
579 -- ^ array elements
578 -> Expression outer grp commons schemas params from (null ('PGvararray ty))
580 -> Expression outer grp commons schemas params from (null ('PGvararray ty))
579−array xs = UnsafeExpression $
581+array xs = UnsafeExpression $ "ARRAY" <>
580− "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
582+ bracketed (commaSeparated (renderSQL <$> xs))
583+
584+{- | construct a 1-dimensional fixed length array
585+
586+>>> printSQL $ array1 (null_ :* false *: true)
587+ARRAY[NULL, FALSE, TRUE]
588+
589+>>> :type array1 (null_ :* false *: true)
590+array1 (null_ :* false *: true)
591+ :: Expression
592+ outer
593+ grp
594+ commons
595+ schemas
596+ params
597+ from
598+ (null ('PGfixarray '[3] ('Null 'PGbool)))
599+-}
600+array1
601+ :: (n ~ Length tys, SOP.All ((~) ty) tys)
602+ => NP (Expression outer grp commons schemas params from) tys
603+ -> Expression outer grp commons schemas params from (null ('PGfixarray '[n] ty))
604+array1 xs = UnsafeExpression $ "ARRAY" <>
605+ bracketed (renderCommaSeparated renderSQL xs)
606+
607+{- | construct a 1-dimensional fixed length array
608+
609+>>> printSQL $ array2 ((null_ :* false *: true) *: (false :* null_ *: true))
610+ARRAY[[NULL, FALSE, TRUE], [FALSE, NULL, TRUE]]
611+
612+>>> :type array2 ((null_ :* false *: true) *: (false :* null_ *: true))
613+array2 ((null_ :* false *: true) *: (false :* null_ *: true))
614+ :: Expression
615+ outer
616+ grp
617+ commons
618+ schemas
619+ params
620+ from
621+ (null ('PGfixarray '[2, 3] ('Null 'PGbool)))
622+-}
623+array2
624+ :: ( SOP.All ((~) tys) tyss
625+ , SOP.All SOP.SListI tyss
626+ , Length tyss ~ n1
627+ , SOP.All ((~) ty) tys
628+ , Length tys ~ n2 )
629+ => NP (NP (Expression outer grp commons schemas params from)) tyss
630+ -> Expression outer grp commons schemas params from (null ('PGfixarray '[n1,n2] ty))
631+array2 xss = UnsafeExpression $ "ARRAY" <>
632+ bracketed (renderCommaSeparatedConstraint @SOP.SListI (bracketed . renderCommaSeparated renderSQL) xss)
581
633
582-- | >>> printSQL $ cardinality (array [null_, false, true])
634-- | >>> printSQL $ cardinality (array [null_, false, true])
583-- cardinality(ARRAY[NULL, FALSE, TRUE])
635-- cardinality(ARRAY[NULL, FALSE, TRUE])
squeal-postgresql/src/Squeal/PostgreSQL/Render.hs+18
@@ -10,23 +10,27 @@ Rendering helper functions.
10
10
11{-# LANGUAGE
11{-# LANGUAGE
12 AllowAmbiguousTypes
12 AllowAmbiguousTypes
13+ , ConstraintKinds
13 , FlexibleContexts
14 , FlexibleContexts
14 , MagicHash
15 , MagicHash
15 , OverloadedStrings
16 , OverloadedStrings
16 , PolyKinds
17 , PolyKinds
17 , RankNTypes
18 , RankNTypes
18 , ScopedTypeVariables
19 , ScopedTypeVariables
20+ , TypeApplications
19#-}
21#-}
20
22
21module Squeal.PostgreSQL.Render
23module Squeal.PostgreSQL.Render
22 ( -- * Render
24 ( -- * Render
23 parenthesized
25 parenthesized
26+ , bracketed
24 , (<+>)
27 , (<+>)
25 , commaSeparated
28 , commaSeparated
26 , doubleQuoted
29 , doubleQuoted
27 , singleQuotedText
30 , singleQuotedText
28 , singleQuotedUtf8
31 , singleQuotedUtf8
29 , renderCommaSeparated
32 , renderCommaSeparated
33+ , renderCommaSeparatedConstraint
30 , renderCommaSeparatedMaybe
34 , renderCommaSeparatedMaybe
31 , renderNat
35 , renderNat
32 , renderSymbol
36 , renderSymbol
@@ -52,6 +56,10 @@ import qualified Data.ByteString.Char8 as Char8
52parenthesized :: ByteString -> ByteString
56parenthesized :: ByteString -> ByteString
53parenthesized str = "(" <> str <> ")"
57parenthesized str = "(" <> str <> ")"
54
58
59+-- | Square bracket a `ByteString`
60+bracketed :: ByteString -> ByteString
61+bracketed str = "[" <> str <> "]"
62+
55-- | Concatenate two `ByteString`s with a space between.
63-- | Concatenate two `ByteString`s with a space between.
56(<+>) :: ByteString -> ByteString -> ByteString
64(<+>) :: ByteString -> ByteString -> ByteString
57infixr 7 <+>
65infixr 7 <+>
@@ -84,6 +92,16 @@ renderCommaSeparated render
84 . hcollapse
92 . hcollapse
85 . hmap (K . render)
93 . hmap (K . render)
86
94
95+-- | Comma separate the renderings of a heterogeneous list.
96+renderCommaSeparatedConstraint
97+ :: forall c xs expression. (All c xs, SListI xs)
98+ => (forall x. c x => expression x -> ByteString)
99+ -> NP expression xs -> ByteString
100+renderCommaSeparatedConstraint render
101+ = commaSeparated
102+ . hcollapse
103+ . hcmap (Proxy @c) (K . render)
104+
87-- | Comma separate the `Maybe` renderings of a heterogeneous list, dropping
105-- | Comma separate the `Maybe` renderings of a heterogeneous list, dropping
88-- `Nothing`s.
106-- `Nothing`s.
89renderCommaSeparatedMaybe
107renderCommaSeparatedMaybe