{-# LANGUAGE
DataKinds
, OverloadedStrings
, RankNTypes
, TypeOperators
#-}
module Squeal.PostgreSQL.Expression.Subquery
(
exists
, in_
, notIn
, subAll
, subAny
) where
import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Expression.Logic
import Squeal.PostgreSQL.Type.List
import Squeal.PostgreSQL.Query
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
exists
:: Query (Join lat from) with db params row
-> Expression grp lat with db params from (null 'PGbool)
exists :: forall (lat :: [(Symbol, RowType)]) (from :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (row :: RowType) (grp :: Grouping)
(null :: PGType -> NullType).
Query (Join lat from) with db params row
-> Expression grp lat with db params from (null 'PGbool)
exists Query (Join lat from) with db params row
query = forall (grp :: Grouping) (lat :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (from :: [(Symbol, RowType)])
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression forall a b. (a -> b) -> a -> b
$ ByteString
"EXISTS" ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query (Join lat from) with db params row
query)
subAll
:: Expression grp lat with db params from ty1
-> Operator ty1 ty2 ('Null 'PGbool)
-> Query (Join lat from) with db params '[col ::: ty2]
-> Condition grp lat with db params from
subAll :: forall (grp :: Grouping) (lat :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (from :: [(Symbol, RowType)])
(ty1 :: NullType) (ty2 :: NullType) (col :: Symbol).
Expression grp lat with db params from ty1
-> Operator ty1 ty2 ('Null 'PGbool)
-> Query (Join lat from) with db params '[col ::: ty2]
-> Condition grp lat with db params from
subAll Expression grp lat with db params from ty1
expr Operator ty1 ty2 ('Null 'PGbool)
(?) Query (Join lat from) with db params '[col ::: ty2]
qry = Expression grp lat with db params from ty1
expr Operator ty1 ty2 ('Null 'PGbool)
?
(forall (grp :: Grouping) (lat :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (from :: [(Symbol, RowType)])
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression forall a b. (a -> b) -> a -> b
$ ByteString
"ALL" ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query (Join lat from) with db params '[col ::: ty2]
qry))
subAny
:: Expression grp lat with db params from ty1
-> Operator ty1 ty2 ('Null 'PGbool)
-> Query (Join lat from) with db params '[col ::: ty2]
-> Condition grp lat with db params from
subAny :: forall (grp :: Grouping) (lat :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (from :: [(Symbol, RowType)])
(ty1 :: NullType) (ty2 :: NullType) (col :: Symbol).
Expression grp lat with db params from ty1
-> Operator ty1 ty2 ('Null 'PGbool)
-> Query (Join lat from) with db params '[col ::: ty2]
-> Condition grp lat with db params from
subAny Expression grp lat with db params from ty1
expr Operator ty1 ty2 ('Null 'PGbool)
(?) Query (Join lat from) with db params '[col ::: ty2]
qry = Expression grp lat with db params from ty1
expr Operator ty1 ty2 ('Null 'PGbool)
?
(forall (grp :: Grouping) (lat :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (from :: [(Symbol, RowType)])
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression forall a b. (a -> b) -> a -> b
$ ByteString
"ANY" ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query (Join lat from) with db params '[col ::: ty2]
qry))
in_
:: Expression grp lat with db params from ty
-> [Expression grp lat with db params from ty]
-> Expression grp lat with db params from ('Null 'PGbool)
Expression grp lat with db params from ty
_ in_ :: forall (grp :: Grouping) (lat :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (from :: [(Symbol, RowType)])
(ty :: NullType).
Expression grp lat with db params from ty
-> [Expression grp lat with db params from ty]
-> Expression grp lat with db params from ('Null 'PGbool)
`in_` [] = forall (null :: PGType -> NullType). Expr (null 'PGbool)
false
Expression grp lat with db params from ty
expr `in_` [Expression grp lat with db params from ty]
exprs = forall (grp :: Grouping) (lat :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (from :: [(Symbol, RowType)])
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression forall a b. (a -> b) -> a -> b
$ forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ty
expr ByteString -> ByteString -> ByteString
<+> ByteString
"IN"
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized ([ByteString] -> ByteString
commaSeparated (forall sql. RenderSQL sql => sql -> ByteString
renderSQL forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Expression grp lat with db params from ty]
exprs))
notIn
:: Expression grp lat with db params from ty
-> [Expression grp lat with db params from ty]
-> Expression grp lat with db params from ('Null 'PGbool)
Expression grp lat with db params from ty
_ notIn :: forall (grp :: Grouping) (lat :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (from :: [(Symbol, RowType)])
(ty :: NullType).
Expression grp lat with db params from ty
-> [Expression grp lat with db params from ty]
-> Expression grp lat with db params from ('Null 'PGbool)
`notIn` [] = forall (null :: PGType -> NullType). Expr (null 'PGbool)
true
Expression grp lat with db params from ty
expr `notIn` [Expression grp lat with db params from ty]
exprs = forall (grp :: Grouping) (lat :: [(Symbol, RowType)])
(with :: [(Symbol, RowType)]) (db :: SchemasType)
(params :: [NullType]) (from :: [(Symbol, RowType)])
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression forall a b. (a -> b) -> a -> b
$ forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ty
expr ByteString -> ByteString -> ByteString
<+> ByteString
"NOT IN"
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized ([ByteString] -> ByteString
commaSeparated (forall sql. RenderSQL sql => sql -> ByteString
renderSQL forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Expression grp lat with db params from ty]
exprs))