on dev help sign in
array constructors
commitee135aeca112135caac132e4880c2780dd570b54 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateTue 19 Mar 2019, 11:31 (8 years ago) parent71d515e
2 files changed+72−2
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+54−2 squeal-postgresql/src/Squeal/PostgreSQL/Render.hs+18
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+54−2
@@ -58,6 +58,8 @@ module Squeal.PostgreSQL.Expression
5858 , nullIf
5959 -- ** Collections
6060 , array
61+ , array1
62+ , array2
6163 , cardinality
6264 , index
6365 , row
@@ -576,8 +578,58 @@ array
576578 :: [Expression outer grp commons schemas params from ty]
577579 -- ^ array elements
578580 -> Expression outer grp commons schemas params from (null ('PGvararray ty))
579−array xs = UnsafeExpression $
580− "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
581+array xs = UnsafeExpression $ "ARRAY" <>
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)
581633
582634-- | >>> printSQL $ cardinality (array [null_, false, true])
583635-- cardinality(ARRAY[NULL, FALSE, TRUE])
squeal-postgresql/src/Squeal/PostgreSQL/Render.hs+18
@@ -10,23 +10,27 @@ Rendering helper functions.
1010
1111{-# LANGUAGE
1212 AllowAmbiguousTypes
13+ , ConstraintKinds
1314 , FlexibleContexts
1415 , MagicHash
1516 , OverloadedStrings
1617 , PolyKinds
1718 , RankNTypes
1819 , ScopedTypeVariables
20+ , TypeApplications
1921#-}
2022
2123module Squeal.PostgreSQL.Render
2224 ( -- * Render
2325 parenthesized
26+ , bracketed
2427 , (<+>)
2528 , commaSeparated
2629 , doubleQuoted
2730 , singleQuotedText
2831 , singleQuotedUtf8
2932 , renderCommaSeparated
33+ , renderCommaSeparatedConstraint
3034 , renderCommaSeparatedMaybe
3135 , renderNat
3236 , renderSymbol
@@ -52,6 +56,10 @@ import qualified Data.ByteString.Char8 as Char8
5256parenthesized :: ByteString -> ByteString
5357parenthesized str = "(" <> str <> ")"
5458
59+-- | Square bracket a `ByteString`
60+bracketed :: ByteString -> ByteString
61+bracketed str = "[" <> str <> "]"
62+
5563-- | Concatenate two `ByteString`s with a space between.
5664(<+>) :: ByteString -> ByteString -> ByteString
5765infixr 7 <+>
@@ -84,6 +92,16 @@ renderCommaSeparated render
8492 . hcollapse
8593 . hmap (K . render)
8694
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+
87105-- | Comma separate the `Maybe` renderings of a heterogeneous list, dropping
88106-- `Nothing`s.
89107renderCommaSeparatedMaybe