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 15:58:35 +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)
|
|
|
|
> | StringLit String
|
|
|
|
> -- | text of interval literal, units of interval precision,
|
|
|
|
> -- e.g. interval 3 days (3)
|
|
|
|
> | IntervalLit String String (Maybe Int)
|
|
|
|
> -- | identifier without dots
|
|
|
|
> | Iden String
|
|
|
|
> -- | identifier with one dot
|
|
|
|
> | Iden2 String String
|
|
|
|
> -- | star
|
|
|
|
> | Star
|
|
|
|
> -- | star with qualifier, e.g t.*
|
|
|
|
> | Star2 String
|
|
|
|
> -- | function application (anything that looks like c style
|
|
|
|
> -- function application syntactically)
|
|
|
|
> | App String [ScalarExpr]
|
|
|
|
> -- | aggregate application, which adds distinct or all, and
|
|
|
|
> -- order by, to regular function application
|
|
|
|
> | AggregateApp String (Maybe Duplicates)
|
|
|
|
> [ScalarExpr]
|
|
|
|
> [(ScalarExpr,Direction)]
|
|
|
|
> -- | window application, which adds over (partition by a order
|
|
|
|
> -- by b) to regular function application. Explicit frames are
|
|
|
|
> -- not currently supported
|
|
|
|
> | WindowApp String [ScalarExpr] [ScalarExpr] [(ScalarExpr,Direction)]
|
|
|
|
> -- | 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)
|
|
|
|
> | BinOp ScalarExpr String ScalarExpr
|
|
|
|
> -- | Prefix unary operators. This is used for symbol
|
|
|
|
> -- operators, keyword operators and multiple keyword operators
|
|
|
|
> | PrefixOp String ScalarExpr
|
|
|
|
> -- | Postfix unary operators. This is used for symbol
|
|
|
|
> -- operators, keyword operators and multiple keyword operators
|
|
|
|
> | PostfixOp String ScalarExpr
|
|
|
|
> -- | 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)
|
|
|
|
> | SpecialOp String [ScalarExpr]
|
|
|
|
> -- | case expression. both flavours supported. Multiple
|
|
|
|
> -- condition when branches not currently supported (case when
|
|
|
|
> -- a=4,b=5 then x end)
|
|
|
|
> | Case (Maybe ScalarExpr) -- test value
|
|
|
|
> [(ScalarExpr,ScalarExpr)] -- when branches
|
|
|
|
> (Maybe ScalarExpr) -- else value
|
|
|
|
> | Parens ScalarExpr
|
|
|
|
> -- | cast(a as typename)
|
|
|
|
> | Cast ScalarExpr TypeName
|
|
|
|
> -- | prefix 'typed literal', e.g. int '42'
|
|
|
|
> | CastOp TypeName String
|
|
|
|
> -- | exists, all, any, some subqueries
|
|
|
|
> | SubQueryExpr SubQueryExprType QueryExpr
|
|
|
|
> -- | in list literal and in subquery, if the bool is false it
|
|
|
|
> -- means not in was used ('a not in (1,2)')
|
|
|
|
> | In Bool ScalarExpr InThing
|
|
|
|
> 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
|
2013-12-14 15:58:35 +01:00
|
|
|
> -- (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 15:58:35 +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
|
2013-12-14 15:58:35 +01:00
|
|
|
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 15:58:35 +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)
|