提交 c012463e 编写于 作者: T tristaZero

Merge branch 'dev' of ssh://github.com/shardingjdbc/sharding-jdbc into dev

/*
* 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.
*/
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.groupby;
import org.apache.shardingsphere.core.constant.OrderDirection;
import org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.orderby.OrderByItemExtractor;
/**
* Group by extractor for ASC null order direction.
*
* @author zhangliang
*/
public final class AscNullOrderDirectionGroupByExtractor extends GroupByExtractor {
public AscNullOrderDirectionGroupByExtractor() {
super(new OrderByItemExtractor(OrderDirection.ASC));
}
}
/*
* 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.
*/
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.groupby;
import org.apache.shardingsphere.core.constant.OrderDirection;
import org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.orderby.OrderByItemExtractor;
/**
* Group by extractor for DESC null order direction.
*
* @author zhangliang
*/
public final class DescNullOrderDirectionGroupByExtractor extends GroupByExtractor {
public DescNullOrderDirectionGroupByExtractor() {
super(new OrderByItemExtractor(OrderDirection.DESC));
}
}
......@@ -15,11 +15,13 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select;
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.groupby;
import com.google.common.base.Optional;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.shardingsphere.core.parse.antlr.extractor.api.OptionalSQLSegmentExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.orderby.OrderByItemExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.ExtractorUtils;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.RuleName;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.order.GroupBySegment;
......@@ -31,13 +33,15 @@ import java.util.Map;
*
* @author duhongjun
* @author panjuan
* @author zhangliang
*/
public final class GroupByExtractor implements OptionalSQLSegmentExtractor {
@RequiredArgsConstructor
public abstract class GroupByExtractor implements OptionalSQLSegmentExtractor {
private final OrderByItemExtractor orderByItemExtractor = new OrderByItemExtractor();
private final OrderByItemExtractor orderByItemExtractor;
@Override
public Optional<GroupBySegment> extract(final ParserRuleContext ancestorNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
public final Optional<GroupBySegment> extract(final ParserRuleContext ancestorNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
Optional<ParserRuleContext> groupByNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.GROUP_BY_CLAUSE);
return groupByNode.isPresent() ? Optional.of(new GroupBySegment(groupByNode.get().getStop().getStopIndex(), orderByItemExtractor.extract(groupByNode.get(), parameterMarkerIndexes)))
: Optional.<GroupBySegment>absent();
......
/*
* 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.
*/
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.orderby;
import org.apache.shardingsphere.core.constant.OrderDirection;
/**
* Order by extractor for ASC null order direction.
*
* @author zhangliang
*/
public final class AscNullOrderDirectionOrderByExtractor extends OrderByExtractor {
public AscNullOrderDirectionOrderByExtractor() {
super(new OrderByItemExtractor(OrderDirection.ASC));
}
}
/*
* 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.
*/
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.orderby;
import org.apache.shardingsphere.core.constant.OrderDirection;
/**
* Order by extractor for DESC null order direction.
*
* @author zhangliang
*/
public final class DescNullOrderDirectionOrderByExtractor extends OrderByExtractor {
public DescNullOrderDirectionOrderByExtractor() {
super(new OrderByItemExtractor(OrderDirection.DESC));
}
}
......@@ -15,9 +15,10 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select;
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.orderby;
import com.google.common.base.Optional;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.shardingsphere.core.parse.antlr.extractor.api.OptionalSQLSegmentExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.ExtractorUtils;
......@@ -30,13 +31,15 @@ import java.util.Map;
* Order by extractor.
*
* @author duhongjun
* @author zhangliang
*/
public final class OrderByExtractor implements OptionalSQLSegmentExtractor {
@RequiredArgsConstructor
public abstract class OrderByExtractor implements OptionalSQLSegmentExtractor {
private final OrderByItemExtractor orderByItemExtractor = new OrderByItemExtractor();
private final OrderByItemExtractor orderByItemExtractor;
@Override
public Optional<OrderBySegment> extract(final ParserRuleContext ancestorNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
public final Optional<OrderBySegment> extract(final ParserRuleContext ancestorNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
Optional<ParserRuleContext> orderByNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.ORDER_BY_CLAUSE);
return orderByNode.isPresent() ? Optional.of(new OrderBySegment(orderByItemExtractor.extract(orderByNode.get(), parameterMarkerIndexes))) : Optional.<OrderBySegment>absent();
}
......
......@@ -15,9 +15,10 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select;
package org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.orderby;
import com.google.common.base.Optional;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.shardingsphere.core.constant.OrderDirection;
import org.apache.shardingsphere.core.parse.antlr.extractor.api.CollectionSQLSegmentExtractor;
......@@ -40,8 +41,11 @@ import java.util.Map;
*
* @author zhangliang
*/
@RequiredArgsConstructor
public final class OrderByItemExtractor implements CollectionSQLSegmentExtractor {
private final OrderDirection nullOrderDirection;
private final ColumnExtractor columnExtractor = new ColumnExtractor();
@Override
......@@ -51,17 +55,17 @@ public final class OrderByItemExtractor implements CollectionSQLSegmentExtractor
OrderDirection orderDirection = 2 == each.getChildCount() && OrderDirection.DESC.name().equalsIgnoreCase(each.getChild(1).getText()) ? OrderDirection.DESC : OrderDirection.ASC;
Optional<ParserRuleContext> indexNode = ExtractorUtils.findFirstChildNode(each, RuleName.NUMBER_LITERALS);
if (indexNode.isPresent()) {
result.add(new IndexOrderByItemSegment(NumberUtil.getExactlyNumber(indexNode.get().getText(), 10).intValue(), orderDirection, OrderDirection.ASC));
result.add(new IndexOrderByItemSegment(NumberUtil.getExactlyNumber(indexNode.get().getText(), 10).intValue(), orderDirection, nullOrderDirection));
continue;
}
Optional<ParserRuleContext> expressionNode = ExtractorUtils.findFirstChildNode(each, RuleName.EXPR);
if (expressionNode.isPresent()) {
result.add(new ExpressionOrderByItemSegment(expressionNode.get().getText(), orderDirection, OrderDirection.ASC));
result.add(new ExpressionOrderByItemSegment(expressionNode.get().getText(), orderDirection, nullOrderDirection));
continue;
}
Optional<ColumnSegment> columnSegment = columnExtractor.extract(each, parameterMarkerIndexes);
if (columnSegment.isPresent()) {
result.add(new ColumnOrderByItemSegment(columnSegment.get(), orderDirection, OrderDirection.ASC));
result.add(new ColumnOrderByItemSegment(columnSegment.get(), orderDirection, nullOrderDirection));
}
}
return result;
......
......@@ -36,8 +36,6 @@
<extractor-rule id="where" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.WhereExtractor" />
<extractor-rule id="predicate" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.PredicateExtractor" />
<extractor-rule id="selectItems" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.SelectItemsExtractor" />
<extractor-rule id="groupBy" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.GroupByExtractor" />
<extractor-rule id="orderBy" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.OrderByExtractor" />
<extractor-rule id="limit" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.LimitExtractor" />
<extractor-rule id="subqueryPredicate" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.SubqueryPredicateExtractor" />
</extractor-rule-definition>
......@@ -171,6 +171,10 @@ FROM
: F R O M
;
NATURAL
: N A T U R A L
;
JOIN
: J O I N
;
......@@ -195,6 +199,10 @@ RIGHT
: R I G H T
;
CROSS
: C R O S S
;
USING
: U S I N G
;
......
......@@ -383,10 +383,6 @@ COPY
: C O P Y
;
CROSS
: C R O S S
;
DATA
: D A T A
;
......@@ -519,10 +515,6 @@ MEMORY
: M E M O R Y
;
NATURAL
: N A T U R A L
;
NONE
: N O N E
;
......
......@@ -22,6 +22,9 @@
<extractor-rule id="changeColumnDefinition" extractor-class="org.apache.shardingsphere.core.parse.extractor.ddl.MySQLChangeColumnDefinitionExtractor" />
<extractor-rule id="dropPrimaryKey" extractor-class="org.apache.shardingsphere.core.parse.extractor.ddl.MySQLDropPrimaryKeyExtractor" />
<extractor-rule id="groupBy" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.groupby.AscNullOrderDirectionGroupByExtractor" />
<extractor-rule id="orderBy" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.orderby.AscNullOrderDirectionOrderByExtractor" />
<extractor-rule id="setAutoCommit" extractor-class="org.apache.shardingsphere.core.parse.extractor.tcl.MySQLSetAutoCommitExtractor" />
<extractor-rule id="showTables" extractor-class="org.apache.shardingsphere.core.parse.extractor.dal.MySQLShowTablesExtractor" />
<extractor-rule id="showTableStatus" extractor-class="org.apache.shardingsphere.core.parse.extractor.dal.MySQLShowTableStatusExtractor" />
......
......@@ -171,6 +171,10 @@ FROM
: F R O M
;
NATURAL
: N A T U R A L
;
JOIN
: J O I N
;
......@@ -195,6 +199,10 @@ RIGHT
: R I G H T
;
CROSS
: C R O S S
;
USING
: U S I N G
;
......
......@@ -92,6 +92,7 @@ unreservedWord_
| SEQUENCE | SESSION | SHOW | SIMPLE | STATISTICS | STORAGE | TABLESPACE
| TEMP | TEMPORARY | TRIGGER | TYPE | UNBOUNDED | UNLOGGED | UPDATE
| USAGE | VALID | VALIDATE | WITHIN | WITHOUT | ZONE | GROUPS
| RECURSIVE
;
schemaName
......@@ -260,11 +261,11 @@ regularFunction_
;
regularFunctionName_
: identifier_ | IF | CURRENT_TIMESTAMP | LOCALTIME | LOCALTIMESTAMP | NOW | REPLACE | INTERVAL
: identifier_ | IF | CURRENT_TIMESTAMP | LOCALTIME | LOCALTIMESTAMP | INTERVAL
;
caseExpression_
: CASE simpleExpr? caseWhen_+ caseElse_? END
: CASE simpleExpr? caseWhen_+ caseElse_?
;
caseWhen_
......
/*
* 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, PostgreSQLKeyword, Literals, BaseRule;
insert
: INSERT INTO tableName (AS? alias)? (insertValuesClause | insertSelectClause)
;
insertValuesClause
: columnNames? VALUES assignmentValues (COMMA_ assignmentValues)*
;
insertSelectClause
: columnNames? select
;
update
: UPDATE updateSpecification_? tableReferences setAssignmentsClause whereClause?
;
updateSpecification_
: ONLY
;
assignment
: columnName EQ_ assignmentValue
;
setAssignmentsClause
: SET assignment (COMMA_ assignment)*
;
assignmentValues
: LP_ assignmentValue (COMMA_ assignmentValue)* RP_
| LP_ RP_
;
assignmentValue
: expr | DEFAULT
;
delete
: DELETE deleteSpecification_? (singleTableClause_ | multipleTablesClause_) whereClause?
;
deleteSpecification_
: ONLY
;
singleTableClause_
: FROM tableName (AS? alias)?
;
multipleTablesClause_
: multipleTableNames_ FROM tableReferences | FROM multipleTableNames_ USING tableReferences
;
multipleTableNames_
: tableName DOT_ASTERISK_? (COMMA_ tableName DOT_ASTERISK_?)*
;
select
: unionClause_
;
unionClause_
: selectClause (UNION (ALL | DISTINCT)? selectClause)*
;
selectClause
: SELECT duplicateSpecification? selectItems fromClause? whereClause? groupByClause? havingClause? orderByClause? limitClause?
;
duplicateSpecification
: ALL | DISTINCT
;
selectItems
: (unqualifiedShorthand | selectItem) (COMMA_ selectItem)*
;
selectItem
: (columnName | expr) (AS? alias)? | qualifiedShorthand
;
alias
: identifier_ | STRING_
;
unqualifiedShorthand
: ASTERISK_
;
qualifiedShorthand
: identifier_ DOT_ASTERISK_
;
fromClause
: FROM tableReferences
;
tableReferences
: tableReference (COMMA_ tableReference)*
;
tableReference
: (tableFactor joinedTable)+ | tableFactor joinedTable*
;
tableFactor
: tableName (AS? alias)? | subquery AS? alias columnNames? | LP_ tableReferences RP_
;
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
;
limitClause
: (LIMIT (ALL | limitRowCount))? (OFFSET limitOffset (ROW | ROWS)?)?
;
limitRowCount
: numberLiterals | parameterMarker
;
limitOffset
: numberLiterals | parameterMarker
;
subquery
: LP_ unionClause_ RP_
;
......@@ -171,6 +171,10 @@ FROM
: F R O M
;
NATURAL
: N A T U R A L
;
JOIN
: J O I N
;
......@@ -195,6 +199,10 @@ RIGHT
: R I G H T
;
CROSS
: C R O S S
;
USING
: U S I N G
;
......
......@@ -482,3 +482,7 @@ UESCAPE
GROUPS
: G R O U P S
;
RECURSIVE
: R E C U R S I V E
;
\ No newline at end of file
......@@ -17,10 +17,14 @@
grammar PostgreSQLStatement;
import Symbol, Comments, DDLStatement, TCLStatement, DCLStatement, DALStatement;
import Symbol, Comments, DDLStatement, DMLStatement, TCLStatement, DCLStatement, DALStatement;
execute
: (createIndex
: (select
| insert
| update
| delete
| createIndex
| alterIndex
| dropIndex
| createTable
......
......@@ -19,5 +19,9 @@
<extractor-rule-definition>
<extractor-rule id="addColumnDefinition" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.ddl.column.AddColumnDefinitionExtractor" />
<extractor-rule id="modifyColumnDefinition" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.ddl.column.ModifyColumnDefinitionExtractor" />
<extractor-rule id="groupBy" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.groupby.DescNullOrderDirectionGroupByExtractor" />
<extractor-rule id="orderBy" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.dml.select.orderby.DescNullOrderDirectionOrderByExtractor" />
<extractor-rule id="showParam" extractor-class="org.apache.shardingsphere.core.parse.extractor.ShowParamExtractor" />
</extractor-rule-definition>
......@@ -171,6 +171,10 @@ FROM
: F R O M
;
NATURAL
: N A T U R A L
;
JOIN
: J O I N
;
......@@ -195,6 +199,10 @@ RIGHT
: R I G H T
;
CROSS
: C R O S S
;
USING
: U S I N G
;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册