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-17 12:21:36 +01:00
|
|
|
> ,Name(..)
|
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(..)
|
2013-12-17 16:29:49 +01:00
|
|
|
> ,Frame(..)
|
|
|
|
> ,FrameRows(..)
|
|
|
|
> ,FramePos(..)
|
2013-12-14 12:33:15 +01:00
|
|
|
> -- * 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-17 12:41:06 +01:00
|
|
|
> ,Alias(..)
|
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)
|
2013-12-17 16:29:49 +01:00
|
|
|
> | IntervalLit
|
|
|
|
> {ilLiteral :: String -- ^ literal text
|
|
|
|
> ,ilUnits :: String -- ^ units
|
|
|
|
> ,ilPrecision :: Maybe Int -- ^ precision
|
|
|
|
> }
|
2013-12-14 15:58:35 +01:00
|
|
|
> -- | identifier without dots
|
2013-12-17 12:21:36 +01:00
|
|
|
> | Iden Name
|
2013-12-17 14:21:43 +01:00
|
|
|
> -- | star, as in select *, t.*, count(*)
|
2013-12-14 15:58:35 +01:00
|
|
|
> | Star
|
|
|
|
> -- | function application (anything that looks like c style
|
|
|
|
> -- function application syntactically)
|
2013-12-17 12:21:36 +01:00
|
|
|
> | App Name [ScalarExpr]
|
2013-12-14 15:58:35 +01:00
|
|
|
> -- | aggregate application, which adds distinct or all, and
|
|
|
|
> -- order by, to regular function application
|
2013-12-17 16:29:49 +01:00
|
|
|
> | AggregateApp
|
|
|
|
> {aggName :: Name -- ^ aggregate function name
|
|
|
|
> ,aggDistinct :: (Maybe Duplicates)-- ^ distinct
|
|
|
|
> ,aggArgs :: [ScalarExpr]-- ^ args
|
|
|
|
> ,aggOrderBy :: [(ScalarExpr,Direction)] -- ^ order by
|
|
|
|
> }
|
2013-12-14 15:58:35 +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-17 16:29:49 +01:00
|
|
|
> | WindowApp
|
|
|
|
> {wnName :: Name -- ^ window function name
|
|
|
|
> ,wnArgs :: [ScalarExpr] -- ^ args
|
|
|
|
> ,wnPartition :: [ScalarExpr] -- ^ partition by
|
|
|
|
> ,wnOrderBy :: [(ScalarExpr,Direction)] -- ^ order by
|
|
|
|
> ,wnFrame :: Maybe Frame -- ^ frame clause
|
|
|
|
> }
|
2013-12-14 15:58:35 +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-17 12:21:36 +01:00
|
|
|
> | BinOp ScalarExpr Name ScalarExpr
|
2013-12-14 15:58:35 +01:00
|
|
|
> -- | Prefix unary operators. This is used for symbol
|
|
|
|
> -- operators, keyword operators and multiple keyword operators
|
2013-12-17 12:21:36 +01:00
|
|
|
> | PrefixOp Name ScalarExpr
|
2013-12-14 15:58:35 +01:00
|
|
|
> -- | Postfix unary operators. This is used for symbol
|
|
|
|
> -- operators, keyword operators and multiple keyword operators
|
2013-12-17 12:21:36 +01:00
|
|
|
> | PostfixOp Name ScalarExpr
|
2013-12-14 15:58:35 +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-17 12:21:36 +01:00
|
|
|
> | SpecialOp Name [ScalarExpr]
|
2013-12-14 15:58:35 +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-17 16:29:49 +01:00
|
|
|
> | Case
|
|
|
|
> {caseTest :: Maybe ScalarExpr -- ^ test value
|
|
|
|
> ,caseWhens :: [(ScalarExpr,ScalarExpr)] -- ^ when branches
|
|
|
|
> ,caseElse :: Maybe ScalarExpr -- ^ else value
|
|
|
|
> }
|
2013-12-14 15:58:35 +01:00
|
|
|
> | Parens ScalarExpr
|
|
|
|
> -- | cast(a as typename)
|
|
|
|
> | Cast ScalarExpr TypeName
|
|
|
|
> -- | prefix 'typed literal', e.g. int '42'
|
2013-12-17 11:51:14 +01:00
|
|
|
> | TypedLit TypeName String
|
2013-12-14 15:58:35 +01:00
|
|
|
> -- | 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-17 12:21:36 +01:00
|
|
|
> -- | Represents an identifier name, which can be quoted or unquoted
|
|
|
|
> data Name = Name String
|
|
|
|
> | QName String
|
|
|
|
> deriving (Eq,Show,Read)
|
|
|
|
|
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-17 16:29:49 +01:00
|
|
|
> -- | Represents the frame clause of a window
|
|
|
|
> -- this can be [range | rows] frame_start
|
|
|
|
> -- or [range | rows] between frame_start and frame_end
|
|
|
|
> data Frame = FrameFrom FrameRows FramePos
|
|
|
|
> | FrameBetween FrameRows FramePos FramePos
|
|
|
|
> deriving (Eq,Show,Read)
|
|
|
|
|
|
|
|
> -- | Represents whether a window frame clause is over rows or ranges
|
|
|
|
> data FrameRows = FrameRows | FrameRange
|
|
|
|
> deriving (Eq,Show,Read)
|
|
|
|
|
|
|
|
> -- | represents the start or end of a frame
|
|
|
|
> data FramePos = UnboundedPreceding
|
|
|
|
> | Preceding ScalarExpr
|
|
|
|
> | Current
|
|
|
|
> | Following ScalarExpr
|
|
|
|
> | UnboundedFollowing
|
|
|
|
> deriving (Eq,Show,Read)
|
|
|
|
|
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-17 12:21:36 +01:00
|
|
|
> ,qeSelectList :: [(Maybe Name,ScalarExpr)]
|
2013-12-14 15:58:35 +01:00
|
|
|
> -- ^ 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
|
|
|
> ,qeOffset :: Maybe ScalarExpr
|
2013-12-17 15:00:17 +01:00
|
|
|
> ,qeFetch :: Maybe ScalarExpr
|
2013-12-13 22:41:12 +01:00
|
|
|
> }
|
|
|
|
> | CombineQueryExpr
|
2013-12-17 12:41:06 +01:00
|
|
|
> {qe0 :: QueryExpr
|
2013-12-13 22:41:12 +01:00
|
|
|
> ,qeCombOp :: CombineOp
|
2013-12-13 22:49:22 +01:00
|
|
|
> ,qeDuplicates :: Duplicates
|
|
|
|
> ,qeCorresponding :: Corresponding
|
2013-12-17 12:41:06 +01:00
|
|
|
> ,qe1 :: QueryExpr
|
2013-12-13 22:41:12 +01:00
|
|
|
> }
|
2013-12-17 12:41:06 +01:00
|
|
|
> | With
|
|
|
|
> {qeWithRecursive :: Bool
|
|
|
|
> ,qeViews :: [(Alias,QueryExpr)]
|
|
|
|
> ,qeQueryExpression :: QueryExpr}
|
2013-12-17 12:27:16 +01:00
|
|
|
> | Values [[ScalarExpr]]
|
2013-12-17 12:58:44 +01:00
|
|
|
> | Table Name
|
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
|
|
|
|
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 = []
|
2013-12-17 15:00:17 +01:00
|
|
|
> ,qeOffset = Nothing
|
|
|
|
> ,qeFetch = Nothing}
|
2013-12-13 11:39:26 +01:00
|
|
|
|
2013-12-17 12:58:44 +01:00
|
|
|
|
|
|
|
> -- | 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
|
|
|
|
> data Duplicates = Distinct | All deriving (Eq,Show,Read)
|
|
|
|
|
|
|
|
> -- | The direction for a column in order by.
|
|
|
|
> data Direction = Asc | Desc deriving (Eq,Show,Read)
|
|
|
|
> -- | Query expression set operators
|
|
|
|
> data CombineOp = Union | Except | Intersect deriving (Eq,Show,Read)
|
|
|
|
> -- | Corresponding, an option for the set operators
|
|
|
|
> data Corresponding = Corresponding | Respectively deriving (Eq,Show,Read)
|
|
|
|
|
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
|
2013-12-17 12:21:36 +01:00
|
|
|
> TRSimple Name
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | from a join b
|
|
|
|
> | TRJoin TableRef JoinType TableRef (Maybe JoinCondition)
|
|
|
|
> -- | from (a)
|
|
|
|
> | TRParens TableRef
|
|
|
|
> -- | from a as b(c,d)
|
2013-12-17 12:41:06 +01:00
|
|
|
> | TRAlias TableRef Alias
|
2013-12-14 13:10:46 +01:00
|
|
|
> -- | from (query expr)
|
|
|
|
> | TRQueryExpr QueryExpr
|
2013-12-17 11:33:33 +01:00
|
|
|
> -- | from function(args)
|
2013-12-17 12:21:36 +01:00
|
|
|
> | TRFunction Name [ScalarExpr]
|
2013-12-17 11:45:32 +01:00
|
|
|
> -- | from lateral t
|
|
|
|
> | TRLateral TableRef
|
2013-12-14 15:34:57 +01:00
|
|
|
> deriving (Eq,Show,Read)
|
2013-12-13 11:39:26 +01:00
|
|
|
|
2013-12-17 12:58:44 +01:00
|
|
|
> -- | Represents an alias for a table valued expression, used in with
|
|
|
|
> -- queries and in from alias, e.g. select a from t u, select a from t u(b),
|
|
|
|
> -- with a(c) as select 1, select * from a;
|
2013-12-17 12:41:06 +01:00
|
|
|
> data Alias = Alias Name (Maybe [Name])
|
|
|
|
> deriving (Eq,Show,Read)
|
|
|
|
|
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
|
2013-12-17 12:21:36 +01:00
|
|
|
> | JoinUsing [Name] -- ^ 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)
|