{-# LANGUAGE
ConstraintKinds
, DeriveGeneric
, DerivingStrategies
, FlexibleContexts
, FlexibleInstances
, GADTs
, GeneralizedNewtypeDeriving
, LambdaCase
, MultiParamTypeClasses
, OverloadedLabels
, OverloadedStrings
, QuantifiedConstraints
, ScopedTypeVariables
, StandaloneDeriving
, TypeApplications
, TypeFamilies
, TypeInType
, TypeOperators
, RankNTypes
, UndecidableInstances
#-}
module Squeal.PostgreSQL.Query
(
Query (..)
, Query_
, union
, unionAll
, intersect
, intersectAll
, except
, exceptAll
) where
import Control.DeepSeq
import Data.ByteString (ByteString)
import Data.Kind (Type)
import qualified GHC.Generics as GHC
import Squeal.PostgreSQL.Type.PG
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
newtype Query
(lat :: FromType)
(with :: FromType)
(db :: SchemasType)
(params :: [NullType])
(row :: RowType)
= UnsafeQuery { Query lat with db params row -> ByteString
renderQuery :: ByteString }
deriving stock ((forall x.
Query lat with db params row
-> Rep (Query lat with db params row) x)
-> (forall x.
Rep (Query lat with db params row) x
-> Query lat with db params row)
-> Generic (Query lat with db params row)
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType) x.
Rep (Query lat with db params row) x
-> Query lat with db params row
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType) x.
Query lat with db params row
-> Rep (Query lat with db params row) x
forall x.
Rep (Query lat with db params row) x
-> Query lat with db params row
forall x.
Query lat with db params row
-> Rep (Query lat with db params row) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType) x.
Rep (Query lat with db params row) x
-> Query lat with db params row
$cfrom :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType) x.
Query lat with db params row
-> Rep (Query lat with db params row) x
GHC.Generic,Int -> Query lat with db params row -> ShowS
[Query lat with db params row] -> ShowS
Query lat with db params row -> String
(Int -> Query lat with db params row -> ShowS)
-> (Query lat with db params row -> String)
-> ([Query lat with db params row] -> ShowS)
-> Show (Query lat with db params row)
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Int -> Query lat with db params row -> ShowS
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
[Query lat with db params row] -> ShowS
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Query lat with db params row] -> ShowS
$cshowList :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
[Query lat with db params row] -> ShowS
show :: Query lat with db params row -> String
$cshow :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row -> String
showsPrec :: Int -> Query lat with db params row -> ShowS
$cshowsPrec :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Int -> Query lat with db params row -> ShowS
Show,Query lat with db params row
-> Query lat with db params row -> Bool
(Query lat with db params row
-> Query lat with db params row -> Bool)
-> (Query lat with db params row
-> Query lat with db params row -> Bool)
-> Eq (Query lat with db params row)
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Query lat with db params row
-> Query lat with db params row -> Bool
$c/= :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Bool
== :: Query lat with db params row
-> Query lat with db params row -> Bool
$c== :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Bool
Eq,Eq (Query lat with db params row)
Eq (Query lat with db params row)
-> (Query lat with db params row
-> Query lat with db params row -> Ordering)
-> (Query lat with db params row
-> Query lat with db params row -> Bool)
-> (Query lat with db params row
-> Query lat with db params row -> Bool)
-> (Query lat with db params row
-> Query lat with db params row -> Bool)
-> (Query lat with db params row
-> Query lat with db params row -> Bool)
-> (Query lat with db params row
-> Query lat with db params row -> Query lat with db params row)
-> (Query lat with db params row
-> Query lat with db params row -> Query lat with db params row)
-> Ord (Query lat with db params row)
Query lat with db params row
-> Query lat with db params row -> Bool
Query lat with db params row
-> Query lat with db params row -> Ordering
Query lat with db params row
-> Query lat with db params row -> Query lat with db params row
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Eq (Query lat with db params row)
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Bool
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Ordering
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Query lat with db params row
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Query lat with db params row
-> Query lat with db params row -> Query lat with db params row
$cmin :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Query lat with db params row
max :: Query lat with db params row
-> Query lat with db params row -> Query lat with db params row
$cmax :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Query lat with db params row
>= :: Query lat with db params row
-> Query lat with db params row -> Bool
$c>= :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Bool
> :: Query lat with db params row
-> Query lat with db params row -> Bool
$c> :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Bool
<= :: Query lat with db params row
-> Query lat with db params row -> Bool
$c<= :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Bool
< :: Query lat with db params row
-> Query lat with db params row -> Bool
$c< :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Bool
compare :: Query lat with db params row
-> Query lat with db params row -> Ordering
$ccompare :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row
-> Query lat with db params row -> Ordering
$cp1Ord :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Eq (Query lat with db params row)
Ord)
deriving newtype (Query lat with db params row -> ()
(Query lat with db params row -> ())
-> NFData (Query lat with db params row)
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row -> ()
forall a. (a -> ()) -> NFData a
rnf :: Query lat with db params row -> ()
$crnf :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row -> ()
NFData)
instance RenderSQL (Query lat with db params row) where renderSQL :: Query lat with db params row -> ByteString
renderSQL = Query lat with db params row -> ByteString
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
Query lat with db params row -> ByteString
renderQuery
type family Query_
(db :: SchemasType)
(params :: Type)
(row :: Type) where
Query_ db params row =
Query '[] '[] db (TuplePG params) (RowPG row)
union
:: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
Query lat with db params columns
q1 union :: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
`union` Query lat with db params columns
q2 = ByteString -> Query lat with db params columns
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
ByteString -> Query lat with db params row
UnsafeQuery (ByteString -> Query lat with db params columns)
-> ByteString -> Query lat with db params columns
forall a b. (a -> b) -> a -> b
$
ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q1)
ByteString -> ByteString -> ByteString
<+> ByteString
"UNION"
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q2)
unionAll
:: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
Query lat with db params columns
q1 unionAll :: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
`unionAll` Query lat with db params columns
q2 = ByteString -> Query lat with db params columns
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
ByteString -> Query lat with db params row
UnsafeQuery (ByteString -> Query lat with db params columns)
-> ByteString -> Query lat with db params columns
forall a b. (a -> b) -> a -> b
$
ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q1)
ByteString -> ByteString -> ByteString
<+> ByteString
"UNION" ByteString -> ByteString -> ByteString
<+> ByteString
"ALL"
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q2)
intersect
:: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
Query lat with db params columns
q1 intersect :: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
`intersect` Query lat with db params columns
q2 = ByteString -> Query lat with db params columns
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
ByteString -> Query lat with db params row
UnsafeQuery (ByteString -> Query lat with db params columns)
-> ByteString -> Query lat with db params columns
forall a b. (a -> b) -> a -> b
$
ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q1)
ByteString -> ByteString -> ByteString
<+> ByteString
"INTERSECT"
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q2)
intersectAll
:: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
Query lat with db params columns
q1 intersectAll :: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
`intersectAll` Query lat with db params columns
q2 = ByteString -> Query lat with db params columns
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
ByteString -> Query lat with db params row
UnsafeQuery (ByteString -> Query lat with db params columns)
-> ByteString -> Query lat with db params columns
forall a b. (a -> b) -> a -> b
$
ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q1)
ByteString -> ByteString -> ByteString
<+> ByteString
"INTERSECT" ByteString -> ByteString -> ByteString
<+> ByteString
"ALL"
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q2)
except
:: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
Query lat with db params columns
q1 except :: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
`except` Query lat with db params columns
q2 = ByteString -> Query lat with db params columns
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
ByteString -> Query lat with db params row
UnsafeQuery (ByteString -> Query lat with db params columns)
-> ByteString -> Query lat with db params columns
forall a b. (a -> b) -> a -> b
$
ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q1)
ByteString -> ByteString -> ByteString
<+> ByteString
"EXCEPT"
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q2)
exceptAll
:: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
Query lat with db params columns
q1 exceptAll :: Query lat with db params columns
-> Query lat with db params columns
-> Query lat with db params columns
`exceptAll` Query lat with db params columns
q2 = ByteString -> Query lat with db params columns
forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (row :: RowType).
ByteString -> Query lat with db params row
UnsafeQuery (ByteString -> Query lat with db params columns)
-> ByteString -> Query lat with db params columns
forall a b. (a -> b) -> a -> b
$
ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q1)
ByteString -> ByteString -> ByteString
<+> ByteString
"EXCEPT" ByteString -> ByteString -> ByteString
<+> ByteString
"ALL"
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (Query lat with db params columns -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL Query lat with db params columns
q2)