2013-12-13 11:39:26 +01:00
|
|
|
|
2013-12-14 12:33:15 +01:00
|
|
|
> -- | The AST for SQL queries
|
2013-12-13 15:04:48 +01:00
|
|
|
> module Language.SQL.SimpleSQL.Syntax
|
2013-12-14 12:33:15 +01:00
|
|
|
> (-- * Scalar expressions
|
|
|
|
> ScalarExpr(..)
|
2013-12-13 17:50:41 +01:00
|
|
|
> ,TypeName(..)
|
2013-12-14 12:33:15 +01:00
|
|
|
> ,Duplicates(..)
|
|
|
|
> ,Direction(..)
|
2013-12-13 20:00:06 +01:00
|
|
|
> ,InThing(..)
|
2013-12-14 12:33:15 +01:00
|
|
|
> ,SubQueryExprType(..)
|
|
|
|
> -- * Query expressions
|
2013-12-13 17:50:41 +01:00
|
|
|
> ,QueryExpr(..)
|
2013-12-13 15:04:48 +01:00
|
|
|
> ,makeSelect
|
2013-12-13 22:41:12 +01:00
|
|
|
> ,CombineOp(..)
|
2013-12-13 22:49:22 +01:00
|
|
|
> ,Corresponding(..)
|
2013-12-14 12:33:15 +01:00
|
|
|
> -- ** From
|
2013-12-13 15:04:48 +01:00
|
|
|
> ,TableRef(..)
|
|
|
|
> ,JoinType(..)
|
|
|
|
> ,JoinCondition(..)
|
|
|
|
> ) where
|
2013-12-13 11:39:26 +01:00
|
|
|
|
2013-12-14 12:33:15 +01:00
|
|
|
> -- | Represents a scalar expression
|
2013-12-14 13:10:46 +01:00
|
|
|
> data ScalarExpr = -- | a numeric literal optional decimal point, e+-
|
|
|
|
> -- integral exponent, e.g
|
|
|
|
> --
|
|
|
|
> -- * 10
|
|
|
|
> --
|
|
|
|
> -- * 10.
|
|
|
|
> --
|
|
|
|
> -- * .1
|
|
|
|
> --
|
|
|
|
> -- * 10.1
|
|
|
|
> --
|
|
|
|
> -- * 1e5
|
|
|
|
> --
|
|
|
|
> -- * 12.34e-6
|
|
|
|
> NumLit String
|
|
|
|
|
|
|
|
> -- | string literal, currently only basic strings
|
|
|
|
> -- between single quotes without escapes (no
|
|
|
|
> -- single quotes in strings then)
|
2013-12-13 16:00:22 +01:00
|
|
|
> | StringLit String
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | text of interval literal, units of interval
|
|
|
|
> -- precision, e.g. interval 3 days (3)
|
|
|
|
> | IntervalLit String
|
|
|
|
> String
|
|
|
|
> (Maybe Int)
|
|
|
|
> -- | identifier without dots
|
2013-12-13 16:00:22 +01:00
|
|
|
> | Iden String
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | identifier with one dot
|
2013-12-13 16:00:22 +01:00
|
|
|
> | Iden2 String String
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | star
|
2013-12-13 11:39:26 +01:00
|
|
|
> | Star
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | star with qualifier, e.g t.*
|
2013-12-13 11:39:26 +01:00
|
|
|
> | Star2 String
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | function application (anything that looks
|
|
|
|
> -- like c style function application syntactically)
|
2013-12-13 11:39:26 +01:00
|
|
|
> | App String [ScalarExpr]
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | aggregate application, which adds distinct or
|
|
|
|
> -- all, and order by, to regular function
|
|
|
|
> -- application
|
2013-12-13 22:18:30 +01:00
|
|
|
> | AggregateApp String (Maybe Duplicates)
|
|
|
|
> [ScalarExpr]
|
|
|
|
> [(ScalarExpr,Direction)]
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | window application, which adds over
|
|
|
|
> -- (partition by a order by b) to regular function
|
|
|
|
> -- application. Explicit frames are not currently
|
|
|
|
> -- supported
|
2013-12-13 22:31:36 +01:00
|
|
|
> | WindowApp String [ScalarExpr] [ScalarExpr] [(ScalarExpr,Direction)]
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | Infix binary operators. This is used for
|
|
|
|
> -- symbol operators (a + b), keyword operators (a
|
|
|
|
> -- and b) and multiple keyword operators (a is
|
|
|
|
> -- similar to b)
|
2013-12-14 12:33:15 +01:00
|
|
|
> | BinOp ScalarExpr String ScalarExpr
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | Prefix unary operators. This is used for
|
|
|
|
> -- symbol operators, keyword operators and
|
|
|
|
> -- multiple keyword operators
|
2013-12-13 20:26:14 +01:00
|
|
|
> | PrefixOp String ScalarExpr
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | Postfix unary operators. This is used for
|
|
|
|
> -- symbol operators, keyword operators and multiple
|
|
|
|
> -- keyword operators
|
2013-12-13 20:26:14 +01:00
|
|
|
> | PostfixOp String ScalarExpr
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | Used for ternary, mixfix and other non
|
|
|
|
> -- orthodox operators, including the function
|
|
|
|
> -- looking calls which use keywords instead of
|
|
|
|
> -- commas to separate the arguments,
|
|
|
|
> -- e.g. substring(t from 1 to 5)
|
2013-12-13 20:26:14 +01:00
|
|
|
> | SpecialOp String [ScalarExpr]
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | case expression. both flavours
|
|
|
|
> -- supported. Multiple condition when branches not
|
|
|
|
> -- currently supported (case when a=4,b=5 then x
|
|
|
|
> -- end)
|
2013-12-13 11:39:26 +01:00
|
|
|
> | Case (Maybe ScalarExpr) -- test value
|
|
|
|
> [(ScalarExpr,ScalarExpr)] -- when branches
|
|
|
|
> (Maybe ScalarExpr) -- else value
|
|
|
|
> | Parens ScalarExpr
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | cast(a as typename)
|
2013-12-13 17:50:41 +01:00
|
|
|
> | Cast ScalarExpr TypeName
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | prefix 'typed literal', e.g. int '42'
|
2013-12-13 19:24:20 +01:00
|
|
|
> | CastOp TypeName String
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | exists, all, any, some subqueries
|
2013-12-13 19:43:28 +01:00
|
|
|
> | SubQueryExpr SubQueryExprType QueryExpr
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | in list literal and in subquery, if the bool
|
|
|
|
> -- is false it means not in was used ('a not in
|
|
|
|
> -- (1,2)')
|
2013-12-13 20:00:06 +01:00
|
|
|
> | In Bool -- true if in, false if not in
|
|
|
|
> ScalarExpr InThing
|
2013-12-14 15:34:57 +01:00
|
|
|
> deriving (Eq,Show,Read)
|
2013-12-13 11:39:26 +01:00
|
|
|
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | Represents a type name, used in casts.
|
2013-12-14 15:34:57 +01:00
|
|
|
> data TypeName = TypeName String deriving (Eq,Show,Read)
|
2013-12-14 12:33:15 +01:00
|
|
|
|
|
|
|
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | Used for 'expr in (scalar expression list)', and 'expr in
|
|
|
|
> -- | (subquery)' syntax
|
2013-12-13 20:00:06 +01:00
|
|
|
> data InThing = InList [ScalarExpr]
|
|
|
|
> | InQueryExpr QueryExpr
|
2013-12-14 15:34:57 +01:00
|
|
|
> deriving (Eq,Show,Read)
|
2013-12-13 17:50:41 +01:00
|
|
|
|
2013-12-14 12:33:15 +01:00
|
|
|
> -- | A subquery in a scalar expression
|
2013-12-14 13:10:46 +01:00
|
|
|
> data SubQueryExprType
|
|
|
|
> = -- | exists (query expr)
|
|
|
|
> SqExists
|
|
|
|
> -- | a scalar subquery
|
|
|
|
> | SqSq
|
|
|
|
> -- | all (query expr)
|
|
|
|
> | SqAll
|
|
|
|
> -- | some (query expr)
|
|
|
|
> | SqSome
|
|
|
|
> -- | any (query expr)
|
|
|
|
> | SqAny
|
2013-12-14 15:34:57 +01:00
|
|
|
> deriving (Eq,Show,Read)
|
2013-12-13 19:43:28 +01:00
|
|
|
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | Represents a query expression, which can be:
|
|
|
|
> --
|
|
|
|
> -- * a regular 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
|
2013-12-14 13:10:46 +01:00
|
|
|
> ,qeSelectList :: [(Maybe String,ScalarExpr)] -- ^ the column aliases and the expressions
|
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
|
2013-12-13 22:49:22 +01:00
|
|
|
> ,qeDuplicates :: Duplicates
|
|
|
|
> ,qeCorresponding :: Corresponding
|
2013-12-13 22:41:12 +01:00
|
|
|
> ,qe2 :: QueryExpr
|
|
|
|
> }
|
2013-12-13 23:58:12 +01:00
|
|
|
> | With [(String,QueryExpr)] QueryExpr
|
2013-12-14 15:34:57 +01:00
|
|
|
> deriving (Eq,Show,Read)
|
2013-12-13 11:39:26 +01:00
|
|
|
|
2013-12-14 10:41:58 +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
|
|
|
|
|
2013-12-14 12:33:15 +01:00
|
|
|
|
|
|
|
> -- | represents the Distinct or All keywords, which can be used
|
|
|
|
> -- before a select list, in an aggregate/window function
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- application, or in a query expression set operator
|
2013-12-14 15:34:57 +01:00
|
|
|
> data Duplicates = Distinct | All deriving (Eq,Show,Read)
|
2013-12-14 12:33:15 +01:00
|
|
|
|
|
|
|
> -- | The direction for a column in order by.
|
2013-12-14 15:34:57 +01:00
|
|
|
> data Direction = Asc | Desc deriving (Eq,Show,Read)
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | Query expression set operators
|
2013-12-14 15:34:57 +01:00
|
|
|
> data CombineOp = Union | Except | Intersect deriving (Eq,Show,Read)
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | Corresponding, an option for the set operators
|
2013-12-14 15:34:57 +01:00
|
|
|
> data Corresponding = Corresponding | Respectively deriving (Eq,Show,Read)
|
2013-12-13 16:08:10 +01:00
|
|
|
|
2013-12-14 12:33:15 +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
|
|
|
|
2013-12-14 12:33:15 +01:00
|
|
|
> -- | Represents a entry in the csv of tables in the from clause.
|
2013-12-14 13:10:46 +01:00
|
|
|
> data TableRef = -- | from t
|
|
|
|
> TRSimple String
|
|
|
|
> -- | from a join b
|
|
|
|
> | TRJoin TableRef JoinType TableRef (Maybe JoinCondition)
|
|
|
|
> -- | from (a)
|
|
|
|
> | TRParens TableRef
|
|
|
|
> -- | from a as b(c,d)
|
|
|
|
> | TRAlias TableRef String (Maybe [String])
|
|
|
|
> -- | from (query expr)
|
|
|
|
> | TRQueryExpr QueryExpr
|
2013-12-14 15:34:57 +01:00
|
|
|
> deriving (Eq,Show,Read)
|
2013-12-13 11:39:26 +01:00
|
|
|
|
2013-12-14 10:41:58 +01:00
|
|
|
TODO: add function table ref
|
|
|
|
|
2013-12-14 12:33:15 +01:00
|
|
|
> -- | The type of a join
|
|
|
|
> data JoinType = JInner | JLeft | JRight | JFull | JCross
|
2013-12-14 15:34:57 +01:00
|
|
|
> deriving (Eq,Show,Read)
|
2013-12-13 11:39:26 +01:00
|
|
|
|
2013-12-14 12:33:15 +01:00
|
|
|
> -- | The join condition.
|
|
|
|
> data JoinCondition = JoinOn ScalarExpr -- ^ on expr
|
|
|
|
> | JoinUsing [String] -- ^ using (column list)
|
2013-12-14 13:10:46 +01:00
|
|
|
> | JoinNatural -- ^ natural join was used
|
2013-12-14 15:34:57 +01:00
|
|
|
> deriving (Eq,Show,Read)
|