{-# LANGUAGE
AllowAmbiguousTypes
, DataKinds
, FlexibleContexts
, FlexibleInstances
, MultiParamTypeClasses
, OverloadedLabels
, OverloadedStrings
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, TypeFamilies
, TypeOperators
, UndecidableInstances
#-}
module Squeal.PostgreSQL.Expression.Collection
( array
, array1
, array2
, cardinality
, index
, unnest
, row
, field
) where
import Data.String
import Data.Word (Word64)
import qualified Generics.SOP as SOP
import Squeal.PostgreSQL.Alias
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Expression.SetOf
import Squeal.PostgreSQL.List
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Schema
array
:: [Expression outer commons grp schemas params from ty]
-> Expression outer commons grp schemas params from (null ('PGvararray ty))
array xs = UnsafeExpression $ "ARRAY" <>
bracketed (commaSeparated (renderSQL <$> xs))
array1
:: (n ~ Length tys, SOP.All ((~) ty) tys)
=> NP (Expression outer commons grp schemas params from) tys
-> Expression outer commons grp schemas params from (null ('PGfixarray '[n] ty))
array1 xs = UnsafeExpression $ "ARRAY" <>
bracketed (renderCommaSeparated renderSQL xs)
array2
:: ( SOP.All ((~) tys) tyss
, SOP.All SOP.SListI tyss
, Length tyss ~ n1
, SOP.All ((~) ty) tys
, Length tys ~ n2 )
=> NP (NP (Expression outer commons grp schemas params from)) tyss
-> Expression outer commons grp schemas params from (null ('PGfixarray '[n1,n2] ty))
array2 xss = UnsafeExpression $ "ARRAY" <>
bracketed (renderCommaSeparatedConstraint @SOP.SListI (bracketed . renderCommaSeparated renderSQL) xss)
cardinality :: null ('PGvararray ty) :--> null 'PGint8
cardinality = unsafeFunction "cardinality"
index
:: Word64
-> null ('PGvararray ty) :--> NullifyType ty
index n expr = UnsafeExpression $
parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
unnest :: SetOfFunction "unnest" (null ('PGvararray ty)) '["unnest" ::: ty]
unnest = unsafeSetOfFunction
row
:: SOP.SListI row
=> NP (Aliased (Expression outer commons grp schemas params from)) row
-> Expression outer commons grp schemas params from (null ('PGcomposite row))
row exprs = UnsafeExpression $ "ROW" <> parenthesized
(renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
field
:: ( Has sch schemas schema
, Has tydef schema ('Typedef ('PGcomposite row))
, Has field row ty)
=> QualifiedAlias sch tydef
-> Alias field
-> Expression outer commons grp schemas params from ('NotNull ('PGcomposite row))
-> Expression outer commons grp schemas params from ty
field td fld expr = UnsafeExpression $
parenthesized (renderSQL expr <> "::" <> renderSQL td)
<> "." <> renderSQL fld