{-# 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
, index1
, index2
, unnest
, arrAny
, arrAll
) where
import Data.String
import Data.Word (Word64)
import GHC.TypeNats
import qualified Generics.SOP as SOP
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Expression.Logic
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 :: [Expression grp lat with db params from ty]
-> Expression grp lat with db params from (null ('PGvararray ty))
array [Expression grp lat with db params from ty]
xs = ByteString
-> Expression grp lat with db params from (null ('PGvararray ty))
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression (ByteString
-> Expression grp lat with db params from (null ('PGvararray ty)))
-> ByteString
-> Expression grp lat with db params from (null ('PGvararray ty))
forall a b. (a -> b) -> a -> b
$ ByteString
"ARRAY" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<>
ByteString -> ByteString
bracketed ([ByteString] -> ByteString
commaSeparated (Expression grp lat with db params from ty -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL (Expression grp lat with db params from ty -> ByteString)
-> [Expression grp lat with db params from ty] -> [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Expression grp lat with db params from ty]
xs))
array0
:: TypeExpression db ty
-> Expression grp lat with db params from (null ('PGvararray ty))
array0 :: TypeExpression db ty
-> Expression grp lat with db params from (null ('PGvararray ty))
array0 TypeExpression db ty
ty = [Expression grp lat with db params from ty]
-> Expression grp lat with db params from (null ('PGvararray ty))
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType) (null :: PGType -> NullType).
[Expression grp lat with db params from ty]
-> Expression grp lat with db params from (null ('PGvararray ty))
array [] Expression grp lat with db params from (null ('PGvararray ty))
-> (Expression grp lat with db params from (null ('PGvararray ty))
-> Expression grp lat with db params from (null ('PGvararray ty)))
-> Expression grp lat with db params from (null ('PGvararray ty))
forall a b. a -> (a -> b) -> b
& TypeExpression db (null ('PGvararray ty))
-> Expression grp lat with db params from (null ('PGvararray ty))
-> Expression grp lat with db params from (null ('PGvararray ty))
forall (db :: SchemasType) (ty :: NullType) (grp :: Grouping)
(lat :: FromType) (with :: FromType) (params :: [NullType])
(from :: FromType).
TypeExpression db ty
-> Expression grp lat with db params from ty
-> Expression grp lat with db params from ty
astype (TypeExpression db ty -> TypeExpression db (null ('PGvararray ty))
forall (db :: SchemasType) (pg :: NullType)
(null :: PGType -> NullType).
TypeExpression db pg -> TypeExpression db (null ('PGvararray pg))
vararray TypeExpression db ty
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 :: NP (Expression grp lat with db params from) tys
-> Expression
grp lat with db params from (null ('PGfixarray '[n] ty))
array1 NP (Expression grp lat with db params from) tys
xs = ByteString
-> Expression
grp lat with db params from (null ('PGfixarray '[n] ty))
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression (ByteString
-> Expression
grp lat with db params from (null ('PGfixarray '[n] ty)))
-> ByteString
-> Expression
grp lat with db params from (null ('PGfixarray '[n] ty))
forall a b. (a -> b) -> a -> b
$ ByteString
"ARRAY" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<>
ByteString -> ByteString
bracketed ((forall (x :: NullType).
Expression grp lat with db params from x -> ByteString)
-> NP (Expression grp lat with db params from) tys -> ByteString
forall k (xs :: [k]) (expression :: k -> *).
SListI xs =>
(forall (x :: k). expression x -> ByteString)
-> NP expression xs -> ByteString
renderCommaSeparated forall sql. RenderSQL sql => sql -> ByteString
forall (x :: NullType).
Expression grp lat with db params from x -> ByteString
renderSQL NP (Expression grp lat with db params from) tys
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 :: NP (NP (Expression grp lat with db params from)) tyss
-> Expression
grp lat with db params from (null ('PGfixarray '[n1, n2] ty))
array2 NP (NP (Expression grp lat with db params from)) tyss
xss = ByteString
-> Expression
grp lat with db params from (null ('PGfixarray '[n1, n2] ty))
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression (ByteString
-> Expression
grp lat with db params from (null ('PGfixarray '[n1, n2] ty)))
-> ByteString
-> Expression
grp lat with db params from (null ('PGfixarray '[n1, n2] ty))
forall a b. (a -> b) -> a -> b
$ ByteString
"ARRAY" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<>
ByteString -> ByteString
bracketed ((forall (x :: [NullType]).
SListI x =>
NP (Expression grp lat with db params from) x -> ByteString)
-> NP (NP (Expression grp lat with db params from)) tyss
-> ByteString
forall k (c :: k -> Constraint) (xs :: [k]) (expression :: k -> *).
(All c xs, SListI xs) =>
(forall (x :: k). c x => expression x -> ByteString)
-> NP expression xs -> ByteString
renderCommaSeparatedConstraint @SOP.SListI (ByteString -> ByteString
bracketed (ByteString -> ByteString)
-> (NP (Expression grp lat with db params from) x -> ByteString)
-> NP (Expression grp lat with db params from) x
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (x :: NullType).
Expression grp lat with db params from x -> ByteString)
-> NP (Expression grp lat with db params from) x -> ByteString
forall k (xs :: [k]) (expression :: k -> *).
SListI xs =>
(forall (x :: k). expression x -> ByteString)
-> NP expression xs -> ByteString
renderCommaSeparated forall sql. RenderSQL sql => sql -> ByteString
forall (x :: NullType).
Expression grp lat with db params from x -> ByteString
renderSQL) NP (NP (Expression grp lat with db params from)) tyss
xss)
cardinality :: null ('PGvararray ty) --> null 'PGint8
cardinality :: Expression grp lat with db params from (null ('PGvararray ty))
-> Expression grp lat with db params from (null 'PGint8)
cardinality = ByteString -> null ('PGvararray ty) --> null 'PGint8
forall (x :: NullType) (y :: NullType). ByteString -> x --> y
unsafeFunction ByteString
"cardinality"
index
:: Word64
-> null ('PGvararray ty) --> NullifyType ty
index :: Word64 -> null ('PGvararray ty) --> NullifyType ty
index Word64
i Expression grp lat with db params from (null ('PGvararray ty))
arr = ByteString
-> Expression grp lat with db params from (NullifyType ty)
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression (ByteString
-> Expression grp lat with db params from (NullifyType ty))
-> ByteString
-> Expression grp lat with db params from (NullifyType ty)
forall a b. (a -> b) -> a -> b
$
ByteString -> ByteString
parenthesized (Expression grp lat with db params from (null ('PGvararray ty))
-> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from (null ('PGvararray ty))
arr) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"[" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> String -> ByteString
forall a. IsString a => String -> a
fromString (Word64 -> String
forall a. Show a => a -> String
show Word64
i) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"]"
index1
:: forall i n ty
. (1 <= i, i <= n, KnownNat i)
=> 'NotNull ('PGfixarray '[n] ty) --> ty
index1 :: 'NotNull ('PGfixarray '[n] ty) --> ty
index1 Expression
grp lat with db params from ('NotNull ('PGfixarray '[n] ty))
arr = ByteString -> Expression grp lat with db params from ty
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression (ByteString -> Expression grp lat with db params from ty)
-> ByteString -> Expression grp lat with db params from ty
forall a b. (a -> b) -> a -> b
$
ByteString -> ByteString
parenthesized (Expression
grp lat with db params from ('NotNull ('PGfixarray '[n] ty))
-> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression
grp lat with db params from ('NotNull ('PGfixarray '[n] ty))
arr)
ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"[" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> String -> ByteString
forall a. IsString a => String -> a
fromString (Natural -> String
forall a. Show a => a -> String
show (Proxy i -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natVal (Proxy i
forall k (t :: k). Proxy t
SOP.Proxy @i))) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"]"
index2
:: forall i j m n ty
. ( 1 <= i, i <= m, KnownNat i
, 1 <= j, j <= n, KnownNat j
)
=> 'NotNull ('PGfixarray '[m,n] ty) --> ty
index2 :: 'NotNull ('PGfixarray '[m, n] ty) --> ty
index2 Expression
grp lat with db params from ('NotNull ('PGfixarray '[m, n] ty))
arr = ByteString -> Expression grp lat with db params from ty
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression (ByteString -> Expression grp lat with db params from ty)
-> ByteString -> Expression grp lat with db params from ty
forall a b. (a -> b) -> a -> b
$
ByteString -> ByteString
parenthesized (Expression
grp lat with db params from ('NotNull ('PGfixarray '[m, n] ty))
-> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression
grp lat with db params from ('NotNull ('PGfixarray '[m, n] ty))
arr)
ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"[" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> String -> ByteString
forall a. IsString a => String -> a
fromString (Natural -> String
forall a. Show a => a -> String
show (Proxy i -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natVal (Proxy i
forall k (t :: k). Proxy t
SOP.Proxy @i))) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"]"
ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"[" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> String -> ByteString
forall a. IsString a => String -> a
fromString (Natural -> String
forall a. Show a => a -> String
show (Proxy j -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natVal (Proxy j
forall k (t :: k). Proxy t
SOP.Proxy @j))) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"]"
unnest :: null ('PGvararray ty) -|-> ("unnest" ::: '["unnest" ::: ty])
unnest :: Expression
'Ungrouped lat with db params '[] (null ('PGvararray ty))
-> FromClause lat with db params '["unnest" ::: '["unnest" ::: ty]]
unnest = ByteString
-> null ('PGvararray ty) -|-> ("unnest" ::: '["unnest" ::: ty])
forall (fun :: Symbol) (ty :: NullType) (row :: RowType).
KnownSymbol fun =>
ByteString -> ty -|-> (fun ::: row)
unsafeSetFunction ByteString
"unnest"
arrAll
:: Expression grp lat with db params from ty1
-> Operator ty1 ty2 ('Null 'PGbool)
-> Expression grp lat with db params from (null ('PGvararray ty2))
-> Condition grp lat with db params from
arrAll :: Expression grp lat with db params from ty1
-> Operator ty1 ty2 ('Null 'PGbool)
-> Expression grp lat with db params from (null ('PGvararray ty2))
-> Condition grp lat with db params from
arrAll Expression grp lat with db params from ty1
x Operator ty1 ty2 ('Null 'PGbool)
(?) Expression grp lat with db params from (null ('PGvararray ty2))
xs = Expression grp lat with db params from ty1
x Expression grp lat with db params from ty1
-> Expression grp lat with db params from ty2
-> Condition grp lat with db params from
Operator ty1 ty2 ('Null 'PGbool)
? (ByteString -> Expression grp lat with db params from ty2
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression (ByteString -> Expression grp lat with db params from ty2)
-> ByteString -> Expression grp lat with db params from ty2
forall a b. (a -> b) -> a -> b
$ ByteString
"ALL" ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (Expression grp lat with db params from (null ('PGvararray ty2))
-> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from (null ('PGvararray ty2))
xs))
arrAny
:: Expression grp lat with db params from ty1
-> Operator ty1 ty2 ('Null 'PGbool)
-> Expression grp lat with db params from (null ('PGvararray ty2))
-> Condition grp lat with db params from
arrAny :: Expression grp lat with db params from ty1
-> Operator ty1 ty2 ('Null 'PGbool)
-> Expression grp lat with db params from (null ('PGvararray ty2))
-> Condition grp lat with db params from
arrAny Expression grp lat with db params from ty1
x Operator ty1 ty2 ('Null 'PGbool)
(?) Expression grp lat with db params from (null ('PGvararray ty2))
xs = Expression grp lat with db params from ty1
x Expression grp lat with db params from ty1
-> Expression grp lat with db params from ty2
-> Condition grp lat with db params from
Operator ty1 ty2 ('Null 'PGbool)
? (ByteString -> Expression grp lat with db params from ty2
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression (ByteString -> Expression grp lat with db params from ty2)
-> ByteString -> Expression grp lat with db params from ty2
forall a b. (a -> b) -> a -> b
$ ByteString
"ANY" ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (Expression grp lat with db params from (null ('PGvararray ty2))
-> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from (null ('PGvararray ty2))
xs))