1
Fork 0
simple-sql-parser/Language/SQL/SimpleSQL/Syntax.lhs

145 lines
5.5 KiB
Plaintext
Raw Normal View History

2013-12-13 11:39:26 +01:00
> -- | The AST for SQL queries
2013-12-13 15:04:48 +01:00
> module Language.SQL.SimpleSQL.Syntax
> (-- * Scalar expressions
> ScalarExpr(..)
> ,TypeName(..)
> ,Duplicates(..)
> ,Direction(..)
> ,InThing(..)
> ,SubQueryExprType(..)
> -- * Query expressions
> ,QueryExpr(..)
2013-12-13 15:04:48 +01:00
> ,makeSelect
2013-12-13 22:41:12 +01:00
> ,CombineOp(..)
> ,Corresponding(..)
> -- ** From
2013-12-13 15:04:48 +01:00
> ,TableRef(..)
> ,JoinType(..)
> ,JoinCondition(..)
> ) where
2013-12-13 11:39:26 +01:00
> -- | Represents a scalar expression
> data ScalarExpr = NumLit String
> | StringLit String
> | IntervalLit String -- text of interval
> String -- units of interval
> (Maybe Int) -- precision
> | Iden String
> | Iden2 String String
2013-12-13 11:39:26 +01:00
> | Star
> | Star2 String
> | App String [ScalarExpr]
> | AggregateApp String (Maybe Duplicates)
> [ScalarExpr]
> [(ScalarExpr,Direction)]
2013-12-13 22:31:36 +01:00
> | WindowApp String [ScalarExpr] [ScalarExpr] [(ScalarExpr,Direction)]
> -- the binop, prefixop and postfix op
> -- are used for symbol and keyword operators
> -- these are used even for the multiple keyword
> -- operators
> | BinOp ScalarExpr String ScalarExpr
> | PrefixOp String ScalarExpr
> | PostfixOp String ScalarExpr
> -- the special op is used for ternary, mixfix and other non orthodox operators
> | SpecialOp String [ScalarExpr]
2013-12-13 11:39:26 +01:00
> | Case (Maybe ScalarExpr) -- test value
> [(ScalarExpr,ScalarExpr)] -- when branches
> (Maybe ScalarExpr) -- else value
> | Parens ScalarExpr
> | Cast ScalarExpr TypeName
> | CastOp TypeName String
> | SubQueryExpr SubQueryExprType QueryExpr
> | In Bool -- true if in, false if not in
> ScalarExpr InThing
2013-12-13 11:39:26 +01:00
> deriving (Eq,Show)
> data TypeName = TypeName String deriving (Eq,Show)
> -- Represents 'expr in (scalar expression list)', and 'expr in
> -- (subquery)' syntax
> data InThing = InList [ScalarExpr]
> | InQueryExpr QueryExpr
> deriving (Eq,Show)
> -- | A subquery in a scalar expression
> data SubQueryExprType = SqExists | SqSq | SqAll | SqSome | SqAny
> deriving (Eq,Show)
> -- | Represents a query expression, which can be a select, a 'set
> -- operator' (union/except/intersect), a common table expression
> -- (with), a values expression (not yet supported) or the table
> -- syntax - 'table t', shorthand for 'select * from t' (not yet
> -- supported).
2013-12-13 11:39:26 +01:00
> data QueryExpr
> = Select
2013-12-13 16:27:02 +01:00
> {qeDuplicates :: Duplicates
> ,qeSelectList :: [(Maybe String,ScalarExpr)]
2013-12-13 11:39:26 +01:00
> ,qeFrom :: [TableRef]
> ,qeWhere :: Maybe ScalarExpr
> ,qeGroupBy :: [ScalarExpr]
> ,qeHaving :: Maybe ScalarExpr
2013-12-13 16:08:10 +01:00
> ,qeOrderBy :: [(ScalarExpr,Direction)]
2013-12-13 16:27:02 +01:00
> ,qeLimit :: Maybe ScalarExpr
> ,qeOffset :: Maybe ScalarExpr
2013-12-13 22:41:12 +01:00
> }
> | CombineQueryExpr
> {qe1 :: QueryExpr
> ,qeCombOp :: CombineOp
> ,qeDuplicates :: Duplicates
> ,qeCorresponding :: Corresponding
2013-12-13 22:41:12 +01:00
> ,qe2 :: QueryExpr
> }
> | With [(String,QueryExpr)] QueryExpr
2013-12-13 22:41:12 +01:00
> deriving (Eq,Show)
2013-12-13 11:39:26 +01:00
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
> -- | represents the Distinct or All keywords, which can be used
> -- before a select list, in an aggregate/window function
> -- application, or in a query expression 'set operator'
2013-12-13 16:27:02 +01:00
> data Duplicates = Distinct | All deriving (Eq,Show)
> -- | The direction for a column in order by.
2013-12-13 16:08:10 +01:00
> data Direction = Asc | Desc deriving (Eq,Show)
> -- | Query expression 'set operators'
2013-12-13 22:41:12 +01:00
> data CombineOp = Union | Except | Intersect deriving (Eq,Show)
> -- | Corresponding, an option for the 'set operators'
> data Corresponding = Corresponding | Respectively deriving (Eq,Show)
2013-12-13 16:08:10 +01:00
> -- | helper/'default' value for query exprs to make creating query expr values a little easier
2013-12-13 11:39:26 +01:00
> makeSelect :: QueryExpr
2013-12-13 16:27:02 +01:00
> makeSelect = Select {qeDuplicates = All
> ,qeSelectList = []
2013-12-13 11:39:26 +01:00
> ,qeFrom = []
> ,qeWhere = Nothing
> ,qeGroupBy = []
> ,qeHaving = Nothing
2013-12-13 16:27:02 +01:00
> ,qeOrderBy = []
> ,qeLimit = Nothing
> ,qeOffset = Nothing}
2013-12-13 11:39:26 +01:00
> -- | Represents a entry in the csv of tables in the from clause.
> data TableRef = SimpleTableRef String -- from t
> | JoinTableRef TableRef JoinType TableRef (Maybe JoinCondition) -- from a join b
> | JoinParens TableRef -- from (a)
> | JoinAlias TableRef String (Maybe [String]) -- from a as b(c,d)
> | JoinQueryExpr QueryExpr -- from (query expr)
2013-12-13 11:39:26 +01:00
> deriving (Eq,Show)
TODO: add function table ref
> -- | The type of a join
> data JoinType = JInner | JLeft | JRight | JFull | JCross
2013-12-13 11:39:26 +01:00
> deriving (Eq,Show)
> -- | The join condition.
> data JoinCondition = JoinOn ScalarExpr -- ^ on expr
> | JoinUsing [String] -- ^ using (column list)
> | JoinNatural -- ^ natural join was specified
2013-12-13 11:39:26 +01:00
> deriving (Eq,Show)