DMLStatement.g4 3.4 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

grammar DMLStatement;

import Symbol, Keyword, SQLServerKeyword, Literals, BaseRule;

insert
23 24 25 26 27
    : withClause_? INSERT INTO? tableName (AS? alias)? (insertDefaultValue | insertValuesClause | insertSelectClause)
    ;
    
insertDefaultValue
    : columnNames? DEFAULT VALUES
T
terrymanu 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    ;

insertValuesClause
    : columnNames? VALUES assignmentValues (COMMA_ assignmentValues)*
    ;

insertSelectClause
    : columnNames? select
    ;

update
    : UPDATE tableReferences setAssignmentsClause whereClause?
    ;

assignment
    : columnName EQ_ assignmentValue
    ;

setAssignmentsClause
    : SET assignment (COMMA_ assignment)*
    ;

assignmentValues
    : LP_ assignmentValue (COMMA_ assignmentValue)* RP_
    | LP_ RP_
    ;

assignmentValue
    : expr | DEFAULT
    ;

delete
60
    : DELETE (singleTableClause | multipleTablesClause) whereClause?
T
terrymanu 已提交
61 62
    ;

63
singleTableClause
T
terrymanu 已提交
64 65 66
    : FROM? LP_? tableName RP_? (AS? alias)?
    ;

67 68
multipleTablesClause
    : multipleTableNames FROM tableReferences | FROM multipleTableNames USING tableReferences
T
terrymanu 已提交
69 70
    ;

71
multipleTableNames
T
terrymanu 已提交
72 73 74 75
    : tableName DOT_ASTERISK_? (COMMA_ tableName DOT_ASTERISK_?)*
    ;

select 
76
    : unionClause
T
terrymanu 已提交
77 78
    ;

79
unionClause
T
terrymanu 已提交
80 81 82 83
    : selectClause (UNION (ALL | DISTINCT)? selectClause)*
    ;

selectClause
84
    : SELECT duplicateSpecification? projections fromClause? whereClause? groupByClause? havingClause? orderByClause?
T
terrymanu 已提交
85 86 87 88 89 90
    ;

duplicateSpecification
    : ALL | DISTINCT
    ;

91 92
projections
    : (unqualifiedShorthand | projection) (COMMA_ projection)*
T
terrymanu 已提交
93 94
    ;

95
projection
T
terrymanu 已提交
96 97 98 99
    : (top | columnName | expr) (AS? alias)? | qualifiedShorthand
    ;

top
100 101 102 103 104
    : TOP LP_? topNum RP_? PERCENT? (WITH TIES)? (ROW_NUMBER LP_ RP_ OVER LP_ orderByClause RP_)?
    ;

topNum
    : numberLiterals | parameterMarker
T
terrymanu 已提交
105 106 107
    ;

alias
108
    : identifier | STRING_
T
terrymanu 已提交
109 110 111 112 113 114 115
    ;

unqualifiedShorthand
    : ASTERISK_
    ;

qualifiedShorthand
116
    : identifier DOT_ASTERISK_
T
terrymanu 已提交
117 118 119 120 121 122 123 124 125 126 127
    ;

fromClause
    : FROM tableReferences
    ;

tableReferences
    : tableReference (COMMA_ tableReference)*
    ;

tableReference
128
    : tableFactor joinedTable*
T
terrymanu 已提交
129 130 131
    ;

tableFactor
132
    : tableName (AS? alias)? | subquery AS? alias columnNames? | LP_ tableReferences RP_
T
terrymanu 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    ;

joinedTable
    : NATURAL? ((INNER | CROSS)? JOIN) tableFactor joinSpecification?
    | NATURAL? (LEFT | RIGHT | FULL) OUTER? JOIN tableFactor joinSpecification?
    ;

joinSpecification
    : ON expr | USING columnNames
    ;

whereClause
    : WHERE expr
    ;

groupByClause
    : GROUP BY orderByItem (COMMA_ orderByItem)*
    ;

havingClause
    : HAVING expr
    ;

subquery
157
    : LP_ unionClause RP_
T
terrymanu 已提交
158
    ;
159 160 161 162 163 164 165 166 167
    
withClause_
    : WITH cteClause_ (COMMA_ cteClause_)*
    ;

cteClause_
    : identifier columnNames? AS subquery
    ;