{-# LANGUAGE
AllowAmbiguousTypes
, DataKinds
, FlexibleContexts
, FlexibleInstances
, MultiParamTypeClasses
, OverloadedLabels
, OverloadedStrings
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, TypeFamilies
, TypeOperators
, UndecidableInstances
#-}
module Squeal.PostgreSQL.Expression.Array
(
array
, array0
, array1
, array2
, cardinality
, index
, unnest
) where
import Data.String
import Data.Word (Word64)
import qualified Generics.SOP as SOP
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Expression.Type
import Squeal.PostgreSQL.Query.From.Set
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Type.List
import Squeal.PostgreSQL.Type.Schema
array
:: [Expression grp lat with db params from ty]
-> Expression grp lat with db params from (null ('PGvararray ty))
array xs = UnsafeExpression $ "ARRAY" <>
bracketed (commaSeparated (renderSQL <$> xs))
array0
:: TypeExpression db ty
-> Expression grp lat with db params from (null ('PGvararray ty))
array0 ty = array [] & astype (vararray ty)
array1
:: (n ~ Length tys, SOP.All ((~) ty) tys)
=> NP (Expression grp lat with db params from) tys
-> Expression grp lat with db 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 grp lat with db params from)) tyss
-> Expression grp lat with db 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 :: null ('PGvararray ty) -|-> ("unnest" ::: '["unnest" ::: ty])
unnest = unsafeSetFunction "unnest"