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

223 lines
7.6 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(..)
2013-12-17 12:21:36 +01:00
> ,Name(..)
> ,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
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
2013-12-17 12:21:36 +01:00
> | Iden Name
2013-12-14 15:58:35 +01:00
> -- | identifier with one dot
2013-12-17 12:21:36 +01:00
> | Iden2 Name Name
2013-12-14 15:58:35 +01:00
> -- | star
> | Star
> -- | star with qualifier, e.g t.*
2013-12-17 12:21:36 +01:00
> | Star2 Name
2013-12-14 15:58:35 +01:00
> -- | 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 12:21:36 +01:00
> | AggregateApp Name (Maybe Duplicates)
2013-12-14 15:58:35 +01:00
> [ScalarExpr]
> [(ScalarExpr,Direction)]
> -- | window application, which adds over (partition by a order
> -- by b) to regular function application. Explicit frames are
> -- not currently supported
2013-12-17 12:21:36 +01:00
> | WindowApp Name [ScalarExpr] [ScalarExpr] [(ScalarExpr,Direction)]
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)
> | 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'
> | 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.
> data TypeName = TypeName String deriving (Eq,Show,Read)
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
> data InThing = InList [ScalarExpr]
> | InQueryExpr QueryExpr
> deriving (Eq,Show,Read)
> -- | 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
> 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
> ,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
> }
2013-12-17 12:21:36 +01:00
> | With [(Name,QueryExpr)] QueryExpr
2013-12-17 12:27:16 +01:00
> | Values [[ScalarExpr]]
> deriving (Eq,Show,Read)
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
2013-12-14 15:58:35 +01:00
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
2013-12-14 13:10:46 +01:00
> -- 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)
2013-12-14 13:10:46 +01:00
> -- | Query expression set operators
> data CombineOp = Union | Except | Intersect deriving (Eq,Show,Read)
2013-12-14 13:10:46 +01:00
> -- | Corresponding, an option for the set operators
> 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
> -- | 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:21:36 +01:00
> | TRAlias TableRef Name (Maybe [Name])
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
> deriving (Eq,Show,Read)
2013-12-13 11:39:26 +01:00
TODO: add function table ref
> -- | The type of a join
> data JoinType = JInner | JLeft | JRight | JFull | JCross
> deriving (Eq,Show,Read)
2013-12-13 11:39:26 +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
> deriving (Eq,Show,Read)