Skip to content

Commit

Permalink
Merge pull request #8 from sharding-sphere/dev
Browse files Browse the repository at this point in the history
Synchronize origin dev branch
  • Loading branch information
KomachiSion authored Nov 16, 2018
2 parents b090356 + 73a20b5 commit 8e41926
Show file tree
Hide file tree
Showing 90 changed files with 2,101 additions and 370 deletions.
80 changes: 38 additions & 42 deletions sharding-core/src/main/antlr4/imports/BaseRule.g4
Original file line number Diff line number Diff line change
Expand Up @@ -6,135 +6,131 @@ ID:
(BQ_?[a-zA-Z_$][a-zA-Z0-9_$]* BQ_? DOT)? (BQ_?[a-zA-Z_$][a-zA-Z0-9_$]* BQ_?)
| [a-zA-Z_$0-9]+ DOT ASTERISK
;

schemaName
: ID
;

databaseName
: ID
;

domainName
: ID
;

tableName
: ID
;

columnName
: ID
;

sequenceName
: ID
;

tablespaceName
: ID
;

collationName
: STRING
| ID
;

indexName
: ID
;

alias
: ID
;

cteName
: ID
;

parserName
: ID
;

extensionName
: ID
;

rowName
: ID
;

opclass
: ID
;

fileGroup
: ID
;

groupName
: ID
;

constraintName
: ID
;

keyName
: ID
;

typeName
: ID
;

xmlSchemaCollection
: ID
;

columnSetName
: ID
;

directoryName
: ID
;

triggerName
: ID
;

routineName
: ID
;

roleName
: STRING | ID
;

partitionName
: ID
;

rewriteRuleName
: ID
;

ownerName
: ID
;

userName
: STRING | ID
;

serverName
: ID
;

databaseName
: ID
;

dataTypeLength
: LP_ (NUMBER (COMMA NUMBER)?)? RP_
Expand Down Expand Up @@ -321,15 +317,15 @@ simpleExpr
// | interval_expr
|privateExprOfDb
;

functionCall
: ID LP_ bitExprs? RP_
;

privateExprOfDb
: matchNone
;

liter
: QUESTION
| NUMBER
Expand All @@ -342,19 +338,19 @@ liter
| (DATE | TIME |TIMESTAMP) STRING
| ID? BIT_NUM collateClause?
;

subquery
: matchNone
;

collateClause
: matchNone
;

orderByClause
: ORDER BY groupByItem (COMMA groupByItem)*
;

groupByItem
: (columnName | NUMBER |expr) (ASC|DESC)?
;
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ permissionOnColumns
;

permission
: ID *?
: ID +?
;

grantDW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Sharding rule configuration.
*
* @author zhangliang
* @author maxiaoguang
*/
@Getter
@Setter
Expand All @@ -40,6 +41,8 @@ public final class ShardingRuleConfiguration implements RuleConfiguration {

private Collection<String> bindingTableGroups = new LinkedList<>();

private Collection<String> broadcastTables = new LinkedList<>();

private ShardingStrategyConfiguration defaultDatabaseShardingStrategyConfig;

private ShardingStrategyConfiguration defaultTableShardingStrategyConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package io.shardingsphere.core.parsing.antlr.extractor.statement.handler;

import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import org.antlr.v4.runtime.ParserRuleContext;

import com.google.common.base.Optional;

import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ExtractResult;

/**
* AST extract handler.
*
Expand All @@ -30,8 +33,8 @@ public interface ASTExtractHandler {
/**
* Extract AST.
*
* @param ancestorNode ancestor node of ast
* @param statement SQL statement
* @param ancestorNode ancestor node of AST
* @return extract result
*/
void extract(ParserRuleContext ancestorNode, SQLStatement statement);
Optional<ExtractResult> extract(ParserRuleContext ancestorNode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,58 @@

package io.shardingsphere.core.parsing.antlr.extractor.statement.handler;

import java.util.Collection;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;

import com.google.common.base.Optional;

import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ColumnDefinitionExtractResult;
import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ExtractResult;
import io.shardingsphere.core.parsing.antlr.extractor.statement.phrase.ColumnDefinitionPhraseExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.statement.util.ASTUtils;
import io.shardingsphere.core.parsing.antlr.sql.ddl.AlterTableStatement;
import io.shardingsphere.core.parsing.antlr.sql.ddl.ColumnDefinition;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;

/**
* Add column extract handler.
*
*
* @author duhongjun
*/
public class AddColumnExtractHandler implements ASTExtractHandler {

private final ColumnDefinitionPhraseExtractor columnDefinitionPhraseExtractor = new ColumnDefinitionPhraseExtractor();

/**‘
* Extract add column result.
*
* @param ancestorNode ancestor node of ast
* @return column definition
*/
@Override
public final void extract(final ParserRuleContext ancestorNode, final SQLStatement statement) {
for (ParserRuleContext each : ASTUtils.getAllDescendantNodes(ancestorNode, RuleName.ADD_COLUMN)) {
extractAddColumn(each, (AlterTableStatement) statement);
public Optional<ExtractResult> extract(final ParserRuleContext ancestorNode) {
Collection<ParserRuleContext> addColumnNodes = ASTUtils.getAllDescendantNodes(ancestorNode, RuleName.ADD_COLUMN);
if (addColumnNodes.isEmpty()) {
return Optional.absent();
}
ColumnDefinitionExtractResult result = new ColumnDefinitionExtractResult();
for (ParserRuleContext each : addColumnNodes) {
extractAddColumn(each, result);
}
return Optional.<ExtractResult>of(result);
}

private void extractAddColumn(final ParserRuleContext addColumnNode, final AlterTableStatement alterTableStatement) {
private void extractAddColumn(final ParserRuleContext addColumnNode, final ColumnDefinitionExtractResult result) {
for (ParserRuleContext each : ASTUtils.getAllDescendantNodes(addColumnNode, RuleName.COLUMN_DEFINITION)) {
Optional<ColumnDefinition> columnDefinition = columnDefinitionPhraseExtractor.extract(each);
if (columnDefinition.isPresent()) {
setColumnDefinition(addColumnNode, alterTableStatement, columnDefinition.get());
columnDefinition.get().setAdd(true);
postExtractColumnDefinition(addColumnNode, columnDefinition.get());
result.getColumnDefintions().add(columnDefinition.get());
}
}
}

private void setColumnDefinition(final ParserRuleContext addColumnNode, final AlterTableStatement alterTableStatement, final ColumnDefinition columnDefinition) {
if (!alterTableStatement.findColumnDefinition(columnDefinition.getName()).isPresent()) {
alterTableStatement.getAddColumns().add(columnDefinition);
postVisitColumnDefinition(addColumnNode, alterTableStatement, columnDefinition.getName());
}
}

protected void postVisitColumnDefinition(final ParseTree ancestorNode, final SQLStatement statement, final String columnName) {
protected void postExtractColumnDefinition(final ParseTree ancestorNode, final ColumnDefinition columnDefinition) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@

package io.shardingsphere.core.parsing.antlr.extractor.statement.handler;

import java.util.Collection;

import org.antlr.v4.runtime.ParserRuleContext;

import com.google.common.base.Optional;

import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ExtractResult;
import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.PrimaryKeyExtractResult;
import io.shardingsphere.core.parsing.antlr.extractor.statement.util.ASTUtils;
import io.shardingsphere.core.parsing.antlr.sql.ddl.AlterTableStatement;
import io.shardingsphere.core.parsing.antlr.sql.ddl.ColumnDefinition;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.ParserRuleContext;

/**
* Add primary key extract handler.
*
*
* @author duhongjun
*/
@RequiredArgsConstructor
Expand All @@ -36,23 +39,23 @@ public final class AddPrimaryKeyExtractHandler implements ASTExtractHandler {
private final RuleName ruleName;

@Override
public void extract(final ParserRuleContext ancestorNode, final SQLStatement statement) {
AlterTableStatement alterStatement = (AlterTableStatement) statement;
public Optional<ExtractResult> extract(final ParserRuleContext ancestorNode) {
Optional<ParserRuleContext> modifyColumnNode = ASTUtils.findFirstChildNode(ancestorNode, ruleName);
if (!modifyColumnNode.isPresent()) {
return;
return Optional.absent();
}
Optional<ParserRuleContext> primaryKeyNode = ASTUtils.findFirstChildNode(modifyColumnNode.get(), RuleName.PRIMARY_KEY);
if (!primaryKeyNode.isPresent()) {
return;
return Optional.absent();
}
Collection<ParserRuleContext> columnNameNodes = ASTUtils.getAllDescendantNodes(modifyColumnNode.get(), RuleName.COLUMN_NAME);
if (null == columnNameNodes) {
return Optional.absent();
}
for (ParserRuleContext each : ASTUtils.getAllDescendantNodes(modifyColumnNode.get(), RuleName.COLUMN_NAME)) {
String columnName = each.getText();
Optional<ColumnDefinition> updateColumn = alterStatement.getColumnDefinitionByName(columnName);
if (updateColumn.isPresent()) {
updateColumn.get().setPrimaryKey(true);
alterStatement.getUpdateColumns().put(columnName, updateColumn.get());
}
PrimaryKeyExtractResult result = new PrimaryKeyExtractResult();
for (ParserRuleContext each : columnNameNodes) {
result.getPrimaryKeyColumnNames().add(each.getText());
}
return Optional.<ExtractResult>of(result);
}
}
Loading

0 comments on commit 8e41926

Please sign in to comment.