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

526 lines
18 KiB
Plaintext
Raw Normal View History

2013-12-13 11:39:26 +01:00
> -- | The AST for SQL.
2014-04-10 17:53:11 +02:00
> {-# LANGUAGE DeriveDataTypeable #-}
2013-12-13 15:04:48 +01:00
> module Language.SQL.SimpleSQL.Syntax
> (-- * Value expressions
> ValueExpr(..)
2013-12-17 12:21:36 +01:00
> ,Name(..)
> ,TypeName(..)
> ,IntervalTypeField(..)
> ,PrecMultiplier(..)
> ,PrecUnits(..)
> ,SetQuantifier(..)
> ,SortSpec(..)
> ,Direction(..)
2013-12-17 17:28:31 +01:00
> ,NullsOrder(..)
> ,InPredValue(..)
> ,SubQueryExprType(..)
> ,CompPredQuantifier(..)
> ,Frame(..)
> ,FrameRows(..)
> ,FramePos(..)
> -- * Query expressions
> ,QueryExpr(..)
2013-12-13 15:04:48 +01:00
> ,makeSelect
2013-12-13 22:41:12 +01:00
> ,CombineOp(..)
> ,Corresponding(..)
> ,Alias(..)
> ,GroupingExpr(..)
> -- ** From
2013-12-13 15:04:48 +01:00
> ,TableRef(..)
> ,JoinType(..)
> ,JoinCondition(..)
> -- * Statements
> ,Statement(..)
> ,DropBehaviour(..)
> ,IdentityRestart(..)
> ,InsertSource(..)
> ,SetClause(..)
2015-08-01 22:16:26 +02:00
> ,TableElement(..)
> -- * Dialect
2014-06-27 11:19:15 +02:00
> ,Dialect(..)
> -- * Comment
> ,Comment(..)
2013-12-13 15:04:48 +01:00
> ) where
2013-12-13 11:39:26 +01:00
2014-04-10 17:53:11 +02:00
> import Data.Data
2013-12-19 11:15:05 +01:00
> -- | Represents a value expression. This is used for the expressions
> -- in select lists. It is also used for expressions in where, group
> -- by, having, order by and so on.
> data ValueExpr
2013-12-14 15:58:35 +01:00
> = -- | 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 with a single quote escaped using ''
2013-12-14 15:58:35 +01:00
> | StringLit String
> -- | text of interval literal, units of interval precision,
> -- e.g. interval 3 days (3)
> | IntervalLit
2014-04-18 20:38:24 +02:00
> {ilSign :: Maybe Bool -- ^ true if + used, false if - used
> ,ilLiteral :: String -- ^ literal text
> ,ilFrom :: IntervalTypeField
> ,ilTo :: Maybe IntervalTypeField
> }
> -- | identifier with parts separated by dots
> | 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)
> | App [Name] [ValueExpr]
2013-12-14 15:58:35 +01:00
> -- | aggregate application, which adds distinct or all, and
> -- order by, to regular function application
> | AggregateApp
> {aggName :: [Name] -- ^ aggregate function name
> ,aggDistinct :: SetQuantifier -- ^ distinct
> ,aggArgs :: [ValueExpr]-- ^ args
> ,aggOrderBy :: [SortSpec] -- ^ order by
2014-04-19 17:01:49 +02:00
> ,aggFilter :: Maybe ValueExpr -- ^ filter
> }
> -- | aggregates with within group
> | AggregateAppGroup
> {aggName :: [Name] -- ^ aggregate function name
> ,aggArgs :: [ValueExpr] -- ^ args
> ,aggGroup :: [SortSpec] -- ^ within group
> }
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
> | WindowApp
> {wnName :: [Name] -- ^ window function name
> ,wnArgs :: [ValueExpr] -- ^ args
> ,wnPartition :: [ValueExpr] -- ^ partition by
> ,wnOrderBy :: [SortSpec] -- ^ 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)
> | BinOp ValueExpr [Name] ValueExpr
2013-12-14 15:58:35 +01:00
> -- | Prefix unary operators. This is used for symbol
> -- operators, keyword operators and multiple keyword operators.
> | PrefixOp [Name] ValueExpr
2013-12-14 15:58:35 +01:00
> -- | Postfix unary operators. This is used for symbol
> -- operators, keyword operators and multiple keyword operators.
> | PostfixOp [Name] ValueExpr
2013-12-14 15:58:35 +01:00
> -- | Used for ternary, mixfix and other non orthodox
> -- operators. Currently used for row constructors, and for
> -- between.
> | SpecialOp [Name] [ValueExpr]
> -- | Used for the operators which look like functions
> -- except the arguments are separated by keywords instead
> -- of commas. The maybe is for the first unnamed argument
> -- if it is present, and the list is for the keyword argument
> -- pairs.
> | SpecialOpK [Name] (Maybe ValueExpr) [(String,ValueExpr)]
> -- | case expression. both flavours supported
> | Case
> {caseTest :: Maybe ValueExpr -- ^ test value
> ,caseWhens :: [([ValueExpr],ValueExpr)] -- ^ when branches
> ,caseElse :: Maybe ValueExpr -- ^ else value
> }
> | Parens ValueExpr
2013-12-14 15:58:35 +01:00
> -- | cast(a as typename)
> | Cast ValueExpr TypeName
2013-12-14 15:58:35 +01:00
> -- | 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 ValueExpr InPredValue
> | Parameter -- ^ Represents a ? in a parameterized query
2014-04-17 18:27:18 +02:00
> | HostParameter String (Maybe String) -- ^ represents a host
> -- parameter, e.g. :a. The
> -- Maybe String is for the
> -- indicator, e.g. :var
> -- indicator :nl
> | QuantifiedComparison
> ValueExpr
> [Name] -- operator
> CompPredQuantifier
> QueryExpr
> | Match ValueExpr Bool -- true if unique
> QueryExpr
> | Array ValueExpr [ValueExpr] -- ^ represents an array
> -- access expression, or an array ctor
> -- e.g. a[3]. The first
> -- valueExpr is the array, the
> -- second is the subscripts/ctor args
> | ArrayCtor QueryExpr -- ^ this is used for the query expression version of array constructors, e.g. array(select * from t)
> | CSStringLit String String
> | Escape ValueExpr Char
> | UEscape ValueExpr Char
> | Collate ValueExpr [Name]
2014-04-18 19:50:24 +02:00
> | MultisetBinOp ValueExpr CombineOp SetQuantifier ValueExpr
> | MultisetCtor [ValueExpr]
> | MultisetQueryCtor QueryExpr
> | NextValueFor [Name]
> | VEComment [Comment] ValueExpr
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
2013-12-13 11:39:26 +01:00
2013-12-17 21:15:19 +01:00
> -- | Represents an identifier name, which can be quoted or unquoted.
2013-12-17 12:21:36 +01:00
> data Name = Name String
> | QName String
> | UQName String
2014-06-27 11:19:15 +02:00
> | DQName String String String
> -- ^ dialect quoted name, the fields are start quote, end quote and the string itself, e.g. `something` is parsed to DQName "`" "`" "something, and $a$ test $a$ is parsed to DQName "$a$" "$a" " test "
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 12:21:36 +01:00
2013-12-14 13:10:46 +01:00
> -- | Represents a type name, used in casts.
> data TypeName
> = TypeName [Name]
> | PrecTypeName [Name] Integer
> | PrecScaleTypeName [Name] Integer Integer
> | PrecLengthTypeName [Name] Integer (Maybe PrecMultiplier) (Maybe PrecUnits)
> -- precision, characterset, collate
> | CharTypeName [Name] (Maybe Integer) [Name] [Name]
> | TimeTypeName [Name] (Maybe Integer) Bool -- true == with time zone
> | RowTypeName [(Name,TypeName)]
> | IntervalTypeName IntervalTypeField (Maybe IntervalTypeField)
2014-04-18 18:49:00 +02:00
> | ArrayTypeName TypeName (Maybe Integer)
> | MultisetTypeName TypeName
> deriving (Eq,Show,Read,Data,Typeable)
2014-04-18 18:49:00 +02:00
> data IntervalTypeField = Itf String (Maybe (Integer, Maybe Integer))
> deriving (Eq,Show,Read,Data,Typeable)
> data PrecMultiplier = PrecK | PrecM | PrecG | PrecT | PrecP
> deriving (Eq,Show,Read,Data,Typeable)
> data PrecUnits = PrecCharacters
> | PrecOctets
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
> -- | Used for 'expr in (value expression list)', and 'expr in
2013-12-17 21:15:19 +01:00
> -- (subquery)' syntax.
> data InPredValue = InList [ValueExpr]
> | InQueryExpr QueryExpr
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
not sure if scalar subquery, exists and unique should be represented like this
> -- | A subquery in a value expression.
2013-12-14 13:10:46 +01:00
> data SubQueryExprType
> = -- | exists (query expr)
> SqExists
> -- | unique (query expr)
> | SqUnique
2013-12-14 13:10:46 +01:00
> -- | a scalar subquery
> | SqSq
> deriving (Eq,Show,Read,Data,Typeable)
> data CompPredQuantifier
> = CPAny
> | CPSome
> | CPAll
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 21:15:19 +01:00
> -- | Represents one field in an order by list.
> data SortSpec = SortSpec ValueExpr Direction NullsOrder
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 17:28:31 +01:00
2013-12-17 21:15:19 +01:00
> -- | Represents 'nulls first' or 'nulls last' in an order by clause.
2013-12-17 17:28:31 +01:00
> data NullsOrder = NullsOrderDefault
> | NullsFirst
> | NullsLast
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 17:28:31 +01:00
2013-12-17 21:15:19 +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
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 21:15:19 +01:00
> -- | Represents whether a window frame clause is over rows or ranges.
> data FrameRows = FrameRows | FrameRange
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
> -- | represents the start or end of a frame
> data FramePos = UnboundedPreceding
> | Preceding ValueExpr
> | Current
> | Following ValueExpr
> | UnboundedFollowing
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
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);
> --
2013-12-19 11:15:05 +01:00
> -- * a table value constructor (values (1,2),(3,4)); or
2013-12-14 13:10:46 +01:00
> --
2013-12-19 11:15:05 +01:00
> -- * an explicit table (table t).
2013-12-13 11:39:26 +01:00
> data QueryExpr
> = Select
> {qeSetQuantifier :: SetQuantifier
> ,qeSelectList :: [(ValueExpr,Maybe Name)]
> -- ^ the expressions and the column aliases
TODO: consider breaking this up. The SQL grammar has
queryexpr = select <select list> [<table expression>]
table expression = <from> [where] [groupby] [having] ...
This would make some things a bit cleaner?
2013-12-13 11:39:26 +01:00
> ,qeFrom :: [TableRef]
> ,qeWhere :: Maybe ValueExpr
> ,qeGroupBy :: [GroupingExpr]
> ,qeHaving :: Maybe ValueExpr
> ,qeOrderBy :: [SortSpec]
> ,qeOffset :: Maybe ValueExpr
2013-12-19 16:50:25 +01:00
> ,qeFetchFirst :: Maybe ValueExpr
2013-12-13 22:41:12 +01:00
> }
> | CombineQueryExpr
> {qe0 :: QueryExpr
2013-12-13 22:41:12 +01:00
> ,qeCombOp :: CombineOp
> ,qeSetQuantifier :: SetQuantifier
> ,qeCorresponding :: Corresponding
> ,qe1 :: QueryExpr
2013-12-13 22:41:12 +01:00
> }
> | With
> {qeWithRecursive :: Bool
> ,qeViews :: [(Alias,QueryExpr)]
> ,qeQueryExpression :: QueryExpr}
> | Values [[ValueExpr]]
> | Table [Name]
> | QEComment [Comment] QueryExpr
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
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.
2013-12-17 21:15:19 +01:00
> -- | Helper/'default' value for query exprs to make creating query
2013-12-19 11:15:05 +01:00
> -- expr values a little easier. It is defined like this:
> --
> -- > makeSelect :: QueryExpr
> -- > makeSelect = Select {qeSetQuantifier = SQDefault
2013-12-19 11:15:05 +01:00
> -- > ,qeSelectList = []
> -- > ,qeFrom = []
> -- > ,qeWhere = Nothing
> -- > ,qeGroupBy = []
> -- > ,qeHaving = Nothing
> -- > ,qeOrderBy = []
> -- > ,qeOffset = Nothing
2013-12-19 16:50:25 +01:00
> -- > ,qeFetchFirst = Nothing}
2013-12-19 11:15:05 +01:00
2013-12-13 11:39:26 +01:00
> makeSelect :: QueryExpr
> makeSelect = Select {qeSetQuantifier = SQDefault
2013-12-13 16:27:02 +01:00
> ,qeSelectList = []
2013-12-13 11:39:26 +01:00
> ,qeFrom = []
> ,qeWhere = Nothing
> ,qeGroupBy = []
> ,qeHaving = Nothing
2013-12-13 16:27:02 +01:00
> ,qeOrderBy = []
> ,qeOffset = Nothing
> ,qeFetchFirst = Nothing}
2013-12-17 12:58:44 +01:00
2013-12-17 21:15:19 +01:00
> -- | Represents the Distinct or All keywords, which can be used
2013-12-17 12:58:44 +01:00
> -- before a select list, in an aggregate/window function
2013-12-17 21:15:19 +01:00
> -- application, or in a query expression set operator.
> data SetQuantifier = SQDefault | Distinct | All deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 12:58:44 +01:00
> -- | The direction for a column in order by.
> data Direction = DirDefault | Asc | Desc deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 21:15:19 +01:00
> -- | Query expression set operators.
2014-04-10 17:53:11 +02:00
> data CombineOp = Union | Except | Intersect deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 21:15:19 +01:00
> -- | Corresponding, an option for the set operators.
2014-04-10 17:53:11 +02:00
> data Corresponding = Corresponding | Respectively deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 12:58:44 +01:00
2013-12-17 21:15:19 +01:00
> -- | Represents an item in a group by clause.
> data GroupingExpr
> = GroupingParens [GroupingExpr]
> | Cube [GroupingExpr]
> | Rollup [GroupingExpr]
> | GroupingSets [GroupingExpr]
> | SimpleGroup ValueExpr
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
> -- | Represents a entry in the csv of tables in the from clause.
> data TableRef = -- | from t / from s.t
> TRSimple [Name]
> -- | from a join b, the bool is true if natural was used
> | TRJoin TableRef Bool JoinType TableRef (Maybe JoinCondition)
2013-12-14 13:10:46 +01:00
> -- | from (a)
> | TRParens TableRef
> -- | from a as b(c,d)
> | 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)
> | TRFunction [Name] [ValueExpr]
2013-12-17 11:45:32 +01:00
> -- | from lateral t
> | TRLateral TableRef
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
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),
2013-12-17 21:15:19 +01:00
> -- with a(c) as select 1, select * from a.
> data Alias = Alias Name (Maybe [Name])
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
2013-12-17 21:15:19 +01:00
> -- | The type of a join.
> data JoinType = JInner | JLeft | JRight | JFull | JCross
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
2013-12-13 11:39:26 +01:00
> -- | The join condition.
> data JoinCondition = JoinOn ValueExpr -- ^ on expr
2013-12-17 12:21:36 +01:00
> | JoinUsing [Name] -- ^ using (column list)
2014-04-10 17:53:11 +02:00
> deriving (Eq,Show,Read,Data,Typeable)
2014-06-27 11:19:15 +02:00
---------------------------
> data Statement =
> -- ddl
> CreateSchema [Name] -- XXX
> | DropSchema [Name] DropBehaviour -- XXX
2015-08-01 22:16:26 +02:00
> | CreateTable [Name] [TableElement]
> {- | AlterTable -- XXX
> | DropTable -- XXX
> | CreateView -- XXX
> | DropView -- XXX
> | CreateDomain -- XXX
> | AlterDomain
> | DropDomain -- XXX
> | CreateCharacterSet
> | DropCharacterSet
> | CreateCollation
> | DropCollation
> | CreateTranslation
> | DropTranslation
> | CreateAssertion
> | DropAssertion
> | CreateTrigger
> | DropTrigger
> | CreateType
> | AlterType
> | DropType
> -- routine stuff?
> | CreateCast
> | DropCast
> | CreateOrdering
> | DropOrdering
> -- transforms
> | CreateSequence -- XXX
> | AlterSequence -- XXX
> | DropSequence -- XXX -}
> -- dml
> | SelectStatement QueryExpr
> {- | DeclareCursor
> | OpenCursor
> | FetchCursor
> | CloseCursor
> | SelectInto -}
> -- | DeletePositioned
> | Delete [Name] (Maybe Name) (Maybe ValueExpr)
> | Truncate [Name] IdentityRestart
> | Insert [Name] (Maybe [Name]) InsertSource
> -- | Merge
> | Update [Name] (Maybe Name) [SetClause] (Maybe ValueExpr)
> {- | TemporaryTable
> | FreeLocator
> | HoldLocator -}
> -- access control
> {- | GrantPrivilege
> | GrantRole
> | CreateRole
> | DropRole
> | RevokePrivilege
> | RevokeRole -}
> -- transaction management
> {- | StartTransaction
> | SetTransaction
> | SetContraints
> | SavePoint
> | ReleaseSavePoint
> | Rollback -}
> -- session
> {- | SetSessionCharacteristics
> | SetSessionAuthorization
> | SetRole
> | SetTimeZone
> | SetCatalog
> | SetSchema
> | SetNames
> | SetTransform
> | SetCollation -}
> deriving (Eq,Show,Read,Data,Typeable)
> data DropBehaviour =
> Restrict
> | Cascade
> | DefaultDropBehaviour
> deriving (Eq,Show,Read,Data,Typeable)
> data IdentityRestart =
> ContinueIdentity
> | RestartIdentity
> | DefaultIdentityRestart
> deriving (Eq,Show,Read,Data,Typeable)
> data InsertSource =
> InsertQuery QueryExpr
> | DefaultInsertValues
> deriving (Eq,Show,Read,Data,Typeable)
> data SetClause =
> Set [Name] ValueExpr
> | SetMultiple [[Name]] [ValueExpr]
> deriving (Eq,Show,Read,Data,Typeable)
2015-08-01 22:16:26 +02:00
> data TableElement =
> ColumnDef Name TypeName
> (Maybe DefaultClause)
> -- (Maybe ColumnConstraintDef)
> -- (Maybe CollateClause)
> -- | TableConstraintDef
> deriving (Eq,Show,Read,Data,Typeable)
> {-data TableConstraintDef
> deriving (Eq,Show,Read,Data,Typeable) -}
> data DefaultClause =
> DefaultClause ValueExpr
> | IdentityColumnSpec
> | GenerationClause
> {-data ColumnConstraintDef =
> | NotNullConstraint
> | UniqueConstraint
> | ReferencesConstraint
> | CheckConstraint-}
--------------------------
2014-06-27 11:19:15 +02:00
> -- | Used to set the dialect used for parsing and pretty printing,
> -- very unfinished at the moment.
> data Dialect = SQL2011
> | MySQL
> deriving (Eq,Show,Read,Data,Typeable)
> -- | Comment. Useful when generating SQL code programmatically. The
> -- parser doesn't produce these.
2015-03-14 15:15:37 +01:00
> data Comment = BlockComment String
> deriving (Eq,Show,Read,Data,Typeable)