DMLStatement.g4 3.2 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 23 24 25 26 27 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
/*
 * 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
    : INSERT INTO tableName (AS? alias)? (insertValuesClause | insertSelectClause)
    ;

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
56
    : DELETE (singleTableClause | multipleTablesClause) whereClause?
T
terrymanu 已提交
57 58
    ;

59
singleTableClause
T
terrymanu 已提交
60 61 62
    : FROM? LP_? tableName RP_? (AS? alias)?
    ;

63 64
multipleTablesClause
    : multipleTableNames FROM tableReferences | FROM multipleTableNames USING tableReferences
T
terrymanu 已提交
65 66
    ;

67
multipleTableNames
T
terrymanu 已提交
68 69 70 71
    : tableName DOT_ASTERISK_? (COMMA_ tableName DOT_ASTERISK_?)*
    ;

select 
72
    : unionClause
T
terrymanu 已提交
73 74
    ;

75
unionClause
T
terrymanu 已提交
76 77 78 79
    : selectClause (UNION (ALL | DISTINCT)? selectClause)*
    ;

selectClause
80
    : SELECT duplicateSpecification? projections fromClause? whereClause? groupByClause? havingClause? orderByClause?
T
terrymanu 已提交
81 82 83 84 85 86
    ;

duplicateSpecification
    : ALL | DISTINCT
    ;

87 88
projections
    : (unqualifiedShorthand | projection) (COMMA_ projection)*
T
terrymanu 已提交
89 90
    ;

91
projection
T
terrymanu 已提交
92 93 94 95
    : (top | columnName | expr) (AS? alias)? | qualifiedShorthand
    ;

top
96 97 98 99 100
    : TOP LP_? topNum RP_? PERCENT? (WITH TIES)? (ROW_NUMBER LP_ RP_ OVER LP_ orderByClause RP_)?
    ;

topNum
    : numberLiterals | parameterMarker
T
terrymanu 已提交
101 102 103
    ;

alias
104
    : identifier | STRING_
T
terrymanu 已提交
105 106 107 108 109 110 111
    ;

unqualifiedShorthand
    : ASTERISK_
    ;

qualifiedShorthand
112
    : identifier DOT_ASTERISK_
T
terrymanu 已提交
113 114 115 116 117 118 119 120 121 122 123
    ;

fromClause
    : FROM tableReferences
    ;

tableReferences
    : tableReference (COMMA_ tableReference)*
    ;

tableReference
124
    : tableFactor joinedTable*
T
terrymanu 已提交
125 126 127
    ;

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

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
153
    : LP_ unionClause RP_
T
terrymanu 已提交
154
    ;