>
> {-# LANGUAGE DeriveDataTypeable #-}
> module Language.SQL.SimpleSQL.Syntax
> (
> ValueExpr(..)
> ,Name(..)
> ,TypeName(..)
> ,IntervalTypeField(..)
> ,PrecMultiplier(..)
> ,PrecUnits(..)
> ,SetQuantifier(..)
> ,SortSpec(..)
> ,Direction(..)
> ,NullsOrder(..)
> ,InPredValue(..)
> ,SubQueryExprType(..)
> ,CompPredQuantifier(..)
> ,Frame(..)
> ,FrameRows(..)
> ,FramePos(..)
>
> ,QueryExpr(..)
> ,makeSelect
> ,CombineOp(..)
> ,Corresponding(..)
> ,Alias(..)
> ,GroupingExpr(..)
>
> ,TableRef(..)
> ,JoinType(..)
> ,JoinCondition(..)
>
> ,Dialect(..)
>
> ,Comment(..)
> ) where
> import Data.Data
>
>
>
> data ValueExpr
> =
>
>
>
>
>
>
>
>
>
>
>
>
>
> NumLit String
>
>
> | StringLit String
>
>
> | IntervalLit
> {ilSign :: Maybe Bool
> ,ilLiteral :: String
> ,ilFrom :: IntervalTypeField
> ,ilTo :: Maybe IntervalTypeField
> }
>
> | Iden [Name]
>
> | Star
>
>
> | App [Name] [ValueExpr]
>
>
> | AggregateApp
> {aggName :: [Name]
> ,aggDistinct :: SetQuantifier
> ,aggArgs :: [ValueExpr]
> ,aggOrderBy :: [SortSpec]
> ,aggFilter :: Maybe ValueExpr
> }
>
> | AggregateAppGroup
> {aggName :: [Name]
> ,aggArgs :: [ValueExpr]
> ,aggGroup :: [SortSpec]
> }
>
>
>
> | WindowApp
> {wnName :: [Name]
> ,wnArgs :: [ValueExpr]
> ,wnPartition :: [ValueExpr]
> ,wnOrderBy :: [SortSpec]
> ,wnFrame :: Maybe Frame
> }
>
>
>
> | BinOp ValueExpr [Name] ValueExpr
>
>
> | PrefixOp [Name] ValueExpr
>
>
> | PostfixOp [Name] ValueExpr
>
>
>
> | SpecialOp [Name] [ValueExpr]
>
>
>
>
>
> | SpecialOpK [Name] (Maybe ValueExpr) [(String,ValueExpr)]
>
> | Case
> {caseTest :: Maybe ValueExpr
> ,caseWhens :: [([ValueExpr],ValueExpr)]
> ,caseElse :: Maybe ValueExpr
> }
> | Parens ValueExpr
>
> | Cast ValueExpr TypeName
>
> | TypedLit TypeName String
>
> | SubQueryExpr SubQueryExprType QueryExpr
>
>
> | In Bool ValueExpr InPredValue
> | Parameter
> | HostParameter String (Maybe String)
>
>
>
>
> | QuantifiedComparison
> ValueExpr
> [Name]
> CompPredQuantifier
> QueryExpr
> | Match ValueExpr Bool
> QueryExpr
> | Array ValueExpr [ValueExpr]
>
>
>
>
> | ArrayCtor QueryExpr
> | CSStringLit String String
> | Escape ValueExpr Char
> | UEscape ValueExpr Char
> | Collate ValueExpr [Name]
> | MultisetBinOp ValueExpr CombineOp SetQuantifier ValueExpr
> | MultisetCtor [ValueExpr]
> | MultisetQueryCtor QueryExpr
> | NextValueFor [Name]
> | VEComment [Comment] ValueExpr
> deriving (Eq,Show,Read,Data,Typeable)
>
> data Name = Name String
> | QName String
> | UQName String
> | DQName String String String
>
> deriving (Eq,Show,Read,Data,Typeable)
>
> data TypeName
> = TypeName [Name]
> | PrecTypeName [Name] Integer
> | PrecScaleTypeName [Name] Integer Integer
> | PrecLengthTypeName [Name] Integer (Maybe PrecMultiplier) (Maybe PrecUnits)
>
> | CharTypeName [Name] (Maybe Integer) [Name] [Name]
> | TimeTypeName [Name] (Maybe Integer) Bool
> | RowTypeName [(Name,TypeName)]
> | IntervalTypeName IntervalTypeField (Maybe IntervalTypeField)
> | ArrayTypeName TypeName (Maybe Integer)
> | MultisetTypeName TypeName
> deriving (Eq,Show,Read,Data,Typeable)
> data IntervalTypeField = Itf String (Maybe (Integer, Maybe Integer))
> deriving (Eq,Show,Read,Data,Typeable)
> data PrecMultiplier = PrecK | PrecM | PrecG | PrecT | PrecP
> deriving (Eq,Show,Read,Data,Typeable)
> data PrecUnits = PrecCharacters
> | PrecOctets
> deriving (Eq,Show,Read,Data,Typeable)
>
>
> data InPredValue = InList [ValueExpr]
> | InQueryExpr QueryExpr
> deriving (Eq,Show,Read,Data,Typeable)
not sure if scalar subquery, exists and unique should be represented like this
>
> data SubQueryExprType
> =
> SqExists
>
> | SqUnique
>
> | SqSq
> deriving (Eq,Show,Read,Data,Typeable)
> data CompPredQuantifier
> = CPAny
> | CPSome
> | CPAll
> deriving (Eq,Show,Read,Data,Typeable)
>
> data SortSpec = SortSpec ValueExpr Direction NullsOrder
> deriving (Eq,Show,Read,Data,Typeable)
>
> data NullsOrder = NullsOrderDefault
> | NullsFirst
> | NullsLast
> deriving (Eq,Show,Read,Data,Typeable)
>
>
>
> data Frame = FrameFrom FrameRows FramePos
> | FrameBetween FrameRows FramePos FramePos
> deriving (Eq,Show,Read,Data,Typeable)
>
> data FrameRows = FrameRows | FrameRange
> deriving (Eq,Show,Read,Data,Typeable)
>
> data FramePos = UnboundedPreceding
> | Preceding ValueExpr
> | Current
> | Following ValueExpr
> | UnboundedFollowing
> deriving (Eq,Show,Read,Data,Typeable)
>
>
>
>
>
>
>
>
>
>
>
> data QueryExpr
> = Select
> {qeSetQuantifier :: SetQuantifier
> ,qeSelectList :: [(ValueExpr,Maybe Name)]
>
TODO: consider breaking this up. The SQL grammar has
queryexpr = select <select list> [<table expression>]
table expression = <from> [where] [groupby] [having] ...
This would make some things a bit cleaner?
> ,qeFrom :: [TableRef]
> ,qeWhere :: Maybe ValueExpr
> ,qeGroupBy :: [GroupingExpr]
> ,qeHaving :: Maybe ValueExpr
> ,qeOrderBy :: [SortSpec]
> ,qeOffset :: Maybe ValueExpr
> ,qeFetchFirst :: Maybe ValueExpr
> }
> | CombineQueryExpr
> {qe0 :: QueryExpr
> ,qeCombOp :: CombineOp
> ,qeSetQuantifier :: SetQuantifier
> ,qeCorresponding :: Corresponding
> ,qe1 :: QueryExpr
> }
> | With
> {qeWithRecursive :: Bool
> ,qeViews :: [(Alias,QueryExpr)]
> ,qeQueryExpression :: QueryExpr}
> | Values [[ValueExpr]]
> | Table [Name]
> | QEComment [Comment] QueryExpr
> deriving (Eq,Show,Read,Data,Typeable)
TODO: add queryexpr parens to deal with e.g.
(select 1 union select 2) union select 3
I'm not sure if this is valid syntax or not.
>
>
>
>
>
>
>
>
>
>
>
>
>
> makeSelect :: QueryExpr
> makeSelect = Select {qeSetQuantifier = SQDefault
> ,qeSelectList = []
> ,qeFrom = []
> ,qeWhere = Nothing
> ,qeGroupBy = []
> ,qeHaving = Nothing
> ,qeOrderBy = []
> ,qeOffset = Nothing
> ,qeFetchFirst = Nothing}
>
>
>
> data SetQuantifier = SQDefault | Distinct | All deriving (Eq,Show,Read,Data,Typeable)
>
> data Direction = DirDefault | Asc | Desc deriving (Eq,Show,Read,Data,Typeable)
>
> data CombineOp = Union | Except | Intersect deriving (Eq,Show,Read,Data,Typeable)
>
> data Corresponding = Corresponding | Respectively deriving (Eq,Show,Read,Data,Typeable)
>
> data GroupingExpr
> = GroupingParens [GroupingExpr]
> | Cube [GroupingExpr]
> | Rollup [GroupingExpr]
> | GroupingSets [GroupingExpr]
> | SimpleGroup ValueExpr
> deriving (Eq,Show,Read,Data,Typeable)
>
> data TableRef =
> TRSimple [Name]
>
> | TRJoin TableRef Bool JoinType TableRef (Maybe JoinCondition)
>
> | TRParens TableRef
>
> | TRAlias TableRef Alias
>
> | TRQueryExpr QueryExpr
>
> | TRFunction [Name] [ValueExpr]
>
> | TRLateral TableRef
> deriving (Eq,Show,Read,Data,Typeable)
>
>
>
> data Alias = Alias Name (Maybe [Name])
> deriving (Eq,Show,Read,Data,Typeable)
>
> data JoinType = JInner | JLeft | JRight | JFull | JCross
> deriving (Eq,Show,Read,Data,Typeable)
>
> data JoinCondition = JoinOn ValueExpr
> | JoinUsing [Name]
> deriving (Eq,Show,Read,Data,Typeable)
>
>
> data Dialect = SQL2011
> | MySQL
> deriving (Eq,Show,Read,Data,Typeable)
>
> data Comment = BlockComment String
> deriving (Eq,Show,Read,Data,Typeable)