{-# LANGUAGE DeriveFunctor, DeriveFoldable, DeriveTraversable #-}
module Database.Relational.SqlSyntax.Types (
SubQuery (..),
Duplication (..), SetOp (..), BinOp (..),
Qualifier (..), Qualified (..), qualifier, unQualify, qualify,
Order (..), Nulls (..), OrderColumn, OrderingTerm,
AggregateColumnRef,
AggregateBitKey (..), AggregateSet (..), AggregateElem (..),
AggregateKey (..),
NodeAttr (..), ProductTree (..),
Node (..), nodeAttr, nodeTree,
JoinProduct,
CaseClause (..), WhenClauses(..),
Column (..), Tuple, tupleWidth,
Record, untypeRecord, record, PI,
recordWidth,
typeFromRawColumns,
typeFromScalarSubQuery,
Predicate,
) where
import Prelude hiding (and, product)
import Data.Foldable (Foldable)
import Data.Traversable (Traversable)
import Database.Relational.Internal.Config (Config)
import Database.Relational.Internal.ContextType (Flat, Aggregated)
import Database.Relational.Internal.String (StringSQL)
import Database.Relational.Internal.UntypedTable (Untyped)
data Duplication = All | Distinct deriving Int -> Duplication -> ShowS
[Duplication] -> ShowS
Duplication -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Duplication] -> ShowS
$cshowList :: [Duplication] -> ShowS
show :: Duplication -> String
$cshow :: Duplication -> String
showsPrec :: Int -> Duplication -> ShowS
$cshowsPrec :: Int -> Duplication -> ShowS
Show
data SetOp = Union | Except | Intersect deriving Int -> SetOp -> ShowS
[SetOp] -> ShowS
SetOp -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SetOp] -> ShowS
$cshowList :: [SetOp] -> ShowS
show :: SetOp -> String
$cshow :: SetOp -> String
showsPrec :: Int -> SetOp -> ShowS
$cshowsPrec :: Int -> SetOp -> ShowS
Show
newtype BinOp = BinOp (SetOp, Duplication) deriving Int -> BinOp -> ShowS
[BinOp] -> ShowS
BinOp -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BinOp] -> ShowS
$cshowList :: [BinOp] -> ShowS
show :: BinOp -> String
$cshow :: BinOp -> String
showsPrec :: Int -> BinOp -> ShowS
$cshowsPrec :: Int -> BinOp -> ShowS
Show
data Order = Asc | Desc deriving Int -> Order -> ShowS
[Order] -> ShowS
Order -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Order] -> ShowS
$cshowList :: [Order] -> ShowS
show :: Order -> String
$cshow :: Order -> String
showsPrec :: Int -> Order -> ShowS
$cshowsPrec :: Int -> Order -> ShowS
Show
data Nulls = NullsFirst | NullsLast deriving Int -> Nulls -> ShowS
[Nulls] -> ShowS
Nulls -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Nulls] -> ShowS
$cshowList :: [Nulls] -> ShowS
show :: Nulls -> String
$cshow :: Nulls -> String
showsPrec :: Int -> Nulls -> ShowS
$cshowsPrec :: Int -> Nulls -> ShowS
Show
type OrderColumn = Column
type OrderingTerm = ((Order, Maybe Nulls), OrderColumn)
type AggregateColumnRef = Column
newtype AggregateBitKey = AggregateBitKey [AggregateColumnRef] deriving Int -> AggregateBitKey -> ShowS
[AggregateBitKey] -> ShowS
AggregateBitKey -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AggregateBitKey] -> ShowS
$cshowList :: [AggregateBitKey] -> ShowS
show :: AggregateBitKey -> String
$cshow :: AggregateBitKey -> String
showsPrec :: Int -> AggregateBitKey -> ShowS
$cshowsPrec :: Int -> AggregateBitKey -> ShowS
Show
newtype AggregateSet = AggregateSet [AggregateElem] deriving Int -> AggregateSet -> ShowS
[AggregateSet] -> ShowS
AggregateSet -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AggregateSet] -> ShowS
$cshowList :: [AggregateSet] -> ShowS
show :: AggregateSet -> String
$cshow :: AggregateSet -> String
showsPrec :: Int -> AggregateSet -> ShowS
$cshowsPrec :: Int -> AggregateSet -> ShowS
Show
data AggregateElem = ColumnRef AggregateColumnRef
| Rollup [AggregateBitKey]
| Cube [AggregateBitKey]
| GroupingSets [AggregateSet]
deriving Int -> AggregateElem -> ShowS
[AggregateElem] -> ShowS
AggregateElem -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AggregateElem] -> ShowS
$cshowList :: [AggregateElem] -> ShowS
show :: AggregateElem -> String
$cshow :: AggregateElem -> String
showsPrec :: Int -> AggregateElem -> ShowS
$cshowsPrec :: Int -> AggregateElem -> ShowS
Show
newtype AggregateKey a = AggregateKey (a, AggregateElem)
data SubQuery = Table Untyped
| Flat Config
Tuple Duplication JoinProduct [Predicate Flat]
[OrderingTerm]
| Aggregated Config
Tuple Duplication JoinProduct [Predicate Flat]
[AggregateElem] [Predicate Aggregated] [OrderingTerm]
| Bin BinOp SubQuery SubQuery
deriving Int -> SubQuery -> ShowS
[SubQuery] -> ShowS
SubQuery -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SubQuery] -> ShowS
$cshowList :: [SubQuery] -> ShowS
show :: SubQuery -> String
$cshow :: SubQuery -> String
showsPrec :: Int -> SubQuery -> ShowS
$cshowsPrec :: Int -> SubQuery -> ShowS
Show
newtype Qualifier = Qualifier Int deriving Int -> Qualifier -> ShowS
[Qualifier] -> ShowS
Qualifier -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Qualifier] -> ShowS
$cshowList :: [Qualifier] -> ShowS
show :: Qualifier -> String
$cshow :: Qualifier -> String
showsPrec :: Int -> Qualifier -> ShowS
$cshowsPrec :: Int -> Qualifier -> ShowS
Show
data Qualified a =
Qualified Qualifier a
deriving (Int -> Qualified a -> ShowS
forall a. Show a => Int -> Qualified a -> ShowS
forall a. Show a => [Qualified a] -> ShowS
forall a. Show a => Qualified a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Qualified a] -> ShowS
$cshowList :: forall a. Show a => [Qualified a] -> ShowS
show :: Qualified a -> String
$cshow :: forall a. Show a => Qualified a -> String
showsPrec :: Int -> Qualified a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Qualified a -> ShowS
Show, forall a b. a -> Qualified b -> Qualified a
forall a b. (a -> b) -> Qualified a -> Qualified b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Qualified b -> Qualified a
$c<$ :: forall a b. a -> Qualified b -> Qualified a
fmap :: forall a b. (a -> b) -> Qualified a -> Qualified b
$cfmap :: forall a b. (a -> b) -> Qualified a -> Qualified b
Functor, forall a. Eq a => a -> Qualified a -> Bool
forall a. Num a => Qualified a -> a
forall a. Ord a => Qualified a -> a
forall m. Monoid m => Qualified m -> m
forall a. Qualified a -> Bool
forall a. Qualified a -> Int
forall a. Qualified a -> [a]
forall a. (a -> a -> a) -> Qualified a -> a
forall m a. Monoid m => (a -> m) -> Qualified a -> m
forall b a. (b -> a -> b) -> b -> Qualified a -> b
forall a b. (a -> b -> b) -> b -> Qualified a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Qualified a -> a
$cproduct :: forall a. Num a => Qualified a -> a
sum :: forall a. Num a => Qualified a -> a
$csum :: forall a. Num a => Qualified a -> a
minimum :: forall a. Ord a => Qualified a -> a
$cminimum :: forall a. Ord a => Qualified a -> a
maximum :: forall a. Ord a => Qualified a -> a
$cmaximum :: forall a. Ord a => Qualified a -> a
elem :: forall a. Eq a => a -> Qualified a -> Bool
$celem :: forall a. Eq a => a -> Qualified a -> Bool
length :: forall a. Qualified a -> Int
$clength :: forall a. Qualified a -> Int
null :: forall a. Qualified a -> Bool
$cnull :: forall a. Qualified a -> Bool
toList :: forall a. Qualified a -> [a]
$ctoList :: forall a. Qualified a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Qualified a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Qualified a -> a
foldr1 :: forall a. (a -> a -> a) -> Qualified a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Qualified a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Qualified a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Qualified a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Qualified a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Qualified a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Qualified a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Qualified a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Qualified a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Qualified a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Qualified a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Qualified a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Qualified a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Qualified a -> m
fold :: forall m. Monoid m => Qualified m -> m
$cfold :: forall m. Monoid m => Qualified m -> m
Foldable, Functor Qualified
Foldable Qualified
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
Qualified (m a) -> m (Qualified a)
forall (f :: * -> *) a.
Applicative f =>
Qualified (f a) -> f (Qualified a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Qualified a -> m (Qualified b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Qualified a -> f (Qualified b)
sequence :: forall (m :: * -> *) a.
Monad m =>
Qualified (m a) -> m (Qualified a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
Qualified (m a) -> m (Qualified a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Qualified a -> m (Qualified b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Qualified a -> m (Qualified b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Qualified (f a) -> f (Qualified a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Qualified (f a) -> f (Qualified a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Qualified a -> f (Qualified b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Qualified a -> f (Qualified b)
Traversable)
qualifier :: Qualified a -> Qualifier
qualifier :: forall a. Qualified a -> Qualifier
qualifier (Qualified Qualifier
q a
_) = Qualifier
q
unQualify :: Qualified a -> a
unQualify :: forall a. Qualified a -> a
unQualify (Qualified Qualifier
_ a
a) = a
a
qualify :: Qualifier -> a -> Qualified a
qualify :: forall a. Qualifier -> a -> Qualified a
qualify = forall a. Qualifier -> a -> Qualified a
Qualified
data NodeAttr = Just' | Maybe deriving Int -> NodeAttr -> ShowS
[NodeAttr] -> ShowS
NodeAttr -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NodeAttr] -> ShowS
$cshowList :: [NodeAttr] -> ShowS
show :: NodeAttr -> String
$cshow :: NodeAttr -> String
showsPrec :: Int -> NodeAttr -> ShowS
$cshowsPrec :: Int -> NodeAttr -> ShowS
Show
data ProductTree rs
= Leaf (Bool, Qualified SubQuery)
| Join !(Node rs) !(Node rs) !rs
deriving (Int -> ProductTree rs -> ShowS
forall rs. Show rs => Int -> ProductTree rs -> ShowS
forall rs. Show rs => [ProductTree rs] -> ShowS
forall rs. Show rs => ProductTree rs -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ProductTree rs] -> ShowS
$cshowList :: forall rs. Show rs => [ProductTree rs] -> ShowS
show :: ProductTree rs -> String
$cshow :: forall rs. Show rs => ProductTree rs -> String
showsPrec :: Int -> ProductTree rs -> ShowS
$cshowsPrec :: forall rs. Show rs => Int -> ProductTree rs -> ShowS
Show, forall a b. a -> ProductTree b -> ProductTree a
forall a b. (a -> b) -> ProductTree a -> ProductTree b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> ProductTree b -> ProductTree a
$c<$ :: forall a b. a -> ProductTree b -> ProductTree a
fmap :: forall a b. (a -> b) -> ProductTree a -> ProductTree b
$cfmap :: forall a b. (a -> b) -> ProductTree a -> ProductTree b
Functor)
data Node rs = Node !NodeAttr !(ProductTree rs) deriving (Int -> Node rs -> ShowS
forall rs. Show rs => Int -> Node rs -> ShowS
forall rs. Show rs => [Node rs] -> ShowS
forall rs. Show rs => Node rs -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Node rs] -> ShowS
$cshowList :: forall rs. Show rs => [Node rs] -> ShowS
show :: Node rs -> String
$cshow :: forall rs. Show rs => Node rs -> String
showsPrec :: Int -> Node rs -> ShowS
$cshowsPrec :: forall rs. Show rs => Int -> Node rs -> ShowS
Show, forall a b. a -> Node b -> Node a
forall a b. (a -> b) -> Node a -> Node b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Node b -> Node a
$c<$ :: forall a b. a -> Node b -> Node a
fmap :: forall a b. (a -> b) -> Node a -> Node b
$cfmap :: forall a b. (a -> b) -> Node a -> Node b
Functor)
nodeAttr :: Node rs -> NodeAttr
nodeAttr :: forall rs. Node rs -> NodeAttr
nodeAttr (Node NodeAttr
a ProductTree rs
_) = NodeAttr
a where
nodeTree :: Node rs -> ProductTree rs
nodeTree :: forall rs. Node rs -> ProductTree rs
nodeTree (Node NodeAttr
_ ProductTree rs
t) = ProductTree rs
t
type JoinProduct = Maybe (ProductTree [Predicate Flat])
data WhenClauses =
WhenClauses [(Tuple, Tuple)] Tuple
deriving Int -> WhenClauses -> ShowS
[WhenClauses] -> ShowS
WhenClauses -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WhenClauses] -> ShowS
$cshowList :: [WhenClauses] -> ShowS
show :: WhenClauses -> String
$cshow :: WhenClauses -> String
showsPrec :: Int -> WhenClauses -> ShowS
$cshowsPrec :: Int -> WhenClauses -> ShowS
Show
data CaseClause
= CaseSearch WhenClauses
| CaseSimple Tuple WhenClauses
deriving Int -> CaseClause -> ShowS
[CaseClause] -> ShowS
CaseClause -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CaseClause] -> ShowS
$cshowList :: [CaseClause] -> ShowS
show :: CaseClause -> String
$cshow :: CaseClause -> String
showsPrec :: Int -> CaseClause -> ShowS
$cshowsPrec :: Int -> CaseClause -> ShowS
Show
data Column
= RawColumn StringSQL
| SubQueryRef (Qualified Int)
| Scalar SubQuery
| Case CaseClause Int
deriving Int -> Column -> ShowS
[Column] -> ShowS
Column -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Column] -> ShowS
$cshowList :: [Column] -> ShowS
show :: Column -> String
$cshow :: Column -> String
showsPrec :: Int -> Column -> ShowS
$cshowsPrec :: Int -> Column -> ShowS
Show
type Tuple = [Column]
tupleWidth :: Tuple -> Int
tupleWidth :: [Column] -> Int
tupleWidth = forall (t :: * -> *) a. Foldable t => t a -> Int
length
newtype Record c t =
Record
{ forall c t. Record c t -> [Column]
untypeRecord :: Tuple } deriving Int -> Record c t -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall c t. Int -> Record c t -> ShowS
forall c t. [Record c t] -> ShowS
forall c t. Record c t -> String
showList :: [Record c t] -> ShowS
$cshowList :: forall c t. [Record c t] -> ShowS
show :: Record c t -> String
$cshow :: forall c t. Record c t -> String
showsPrec :: Int -> Record c t -> ShowS
$cshowsPrec :: forall c t. Int -> Record c t -> ShowS
Show
type Predicate c = Record c (Maybe Bool)
type PI c a b = Record c a -> Record c b
record :: Tuple -> Record c t
record :: forall c t. [Column] -> Record c t
record = forall c t. [Column] -> Record c t
Record
recordWidth :: Record c r -> Int
recordWidth :: forall c r. Record c r -> Int
recordWidth = forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c t. Record c t -> [Column]
untypeRecord
typeFromRawColumns :: [StringSQL]
-> Record c r
typeFromRawColumns :: forall c r. [StringSQL] -> Record c r
typeFromRawColumns = forall c t. [Column] -> Record c t
record forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map StringSQL -> Column
RawColumn
typeFromScalarSubQuery :: SubQuery -> Record c t
typeFromScalarSubQuery :: forall c t. SubQuery -> Record c t
typeFromScalarSubQuery = forall c t. [Column] -> Record c t
record forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[]) forall b c a. (b -> c) -> (a -> b) -> a -> c
. SubQuery -> Column
Scalar