Skip to content

Commit

Permalink
[DistSQL] Keyword recognition. (apache#12812)
Browse files Browse the repository at this point in the history
* Keyword recognition.

* Add null judgment.

* Add null judgment.

* Add statement.

* Add test.

* Use keywords as names.
  • Loading branch information
lanchengx authored Sep 30, 2021
1 parent 1b46490 commit 644d637
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public ASTNode visitDataSource(final DataSourceContext ctx) {
port = ctx.simpleSource().port().getText();
dbName = ctx.simpleSource().dbName().getText();
}
return new DataSourceSegment(ctx.dataSourceName().getText(), url, hostName, port, dbName,
return new DataSourceSegment(getIdentifierValue(ctx.dataSourceName()), url, hostName, port, dbName,
ctx.user().getText(), null == ctx.password() ? "" : getPassword(ctx.password()),
null == ctx.poolProperties() ? new Properties() : getPoolProperties(ctx.poolProperties()));
}
Expand Down Expand Up @@ -110,7 +110,7 @@ private Properties getPoolProperties(final PoolPropertiesContext ctx) {
@Override
public ASTNode visitDropResource(final DropResourceContext ctx) {
boolean ignoreSingleTables = null != ctx.ignoreSingleTables();
return new DropResourceStatement(ctx.IDENTIFIER().stream().map(ParseTree::getText).collect(Collectors.toList()), ignoreSingleTables);
return new DropResourceStatement(ctx.IDENTIFIER().stream().map(ParseTree::getText).map(each -> new IdentifierValue(each).getValue()).collect(Collectors.toList()), ignoreSingleTables);
}

@Override
Expand All @@ -125,12 +125,19 @@ public ASTNode visitSchemaName(final SchemaNameContext ctx) {

@Override
public ASTNode visitSetVariable(final SetVariableContext ctx) {
return new SetVariableStatement(ctx.variableName().getText(), ctx.variableValue().getText());
return new SetVariableStatement(getIdentifierValue(ctx.variableName()), getIdentifierValue(ctx.variableValue()));
}

@Override
public ASTNode visitShowVariable(final ShowVariableContext ctx) {
return new ShowVariableStatement(ctx.variableName().getText());
return new ShowVariableStatement(getIdentifierValue(ctx.variableName()));
}

private String getIdentifierValue(final ParseTree context) {
if (null == context) {
return null;
}
return new IdentifierValue(context.getText()).getValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.dbdiscovery.distsql.parser.core;

import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryRuleSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.AlterDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.CreateDatabaseDiscoveryRuleStatement;
Expand All @@ -30,7 +31,6 @@
import org.apache.shardingsphere.distsql.parser.autogen.DatabaseDiscoveryDistSQLStatementParser.CreateDatabaseDiscoveryRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.DatabaseDiscoveryDistSQLStatementParser.DatabaseDiscoveryRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.DatabaseDiscoveryDistSQLStatementParser.DropDatabaseDiscoveryRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.DatabaseDiscoveryDistSQLStatementParser.RuleNameContext;
import org.apache.shardingsphere.distsql.parser.autogen.DatabaseDiscoveryDistSQLStatementParser.SchemaNameContext;
import org.apache.shardingsphere.distsql.parser.autogen.DatabaseDiscoveryDistSQLStatementParser.ShowDatabaseDiscoveryRulesContext;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
Expand Down Expand Up @@ -60,7 +60,7 @@ public ASTNode visitAlterDatabaseDiscoveryRule(final AlterDatabaseDiscoveryRuleC

@Override
public ASTNode visitDropDatabaseDiscoveryRule(final DropDatabaseDiscoveryRuleContext ctx) {
return new DropDatabaseDiscoveryRuleStatement(ctx.ruleName().stream().map(RuleNameContext::getText).collect(Collectors.toList()));
return new DropDatabaseDiscoveryRuleStatement(ctx.ruleName().stream().map(each -> getIdentifierValue(each)).collect(Collectors.toList()));
}

@Override
Expand All @@ -71,8 +71,15 @@ public ASTNode visitShowDatabaseDiscoveryRules(final ShowDatabaseDiscoveryRulesC
@Override
public ASTNode visitDatabaseDiscoveryRuleDefinition(final DatabaseDiscoveryRuleDefinitionContext ctx) {
Collection<String> dataSources = ctx.resources().resourceName().stream().map(each -> new IdentifierValue(each.getText()).getValue()).collect(Collectors.toList());
return new DatabaseDiscoveryRuleSegment(ctx.ruleName().getText(),
dataSources, ctx.algorithmDefinition().algorithmName().getText(), getAlgorithmProperties(ctx.algorithmDefinition().algorithmProperties()));
return new DatabaseDiscoveryRuleSegment(getIdentifierValue(ctx.ruleName()),
dataSources, getIdentifierValue(ctx.algorithmDefinition().algorithmName()), getAlgorithmProperties(ctx.algorithmDefinition().algorithmProperties()));
}

private String getIdentifierValue(final ParseTree context) {
if (null == context) {
return null;
}
return new IdentifierValue(context.getText()).getValue();
}

@Override
Expand All @@ -82,7 +89,7 @@ public ASTNode visitSchemaName(final SchemaNameContext ctx) {

@Override
public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
return new AlgorithmSegment(ctx.algorithmName().getText(), null == ctx.algorithmProperties() ? new Properties() : getAlgorithmProperties(ctx.algorithmProperties()));
return new AlgorithmSegment(getIdentifierValue(ctx.algorithmName()), null == ctx.algorithmProperties() ? new Properties() : getAlgorithmProperties(ctx.algorithmProperties()));
}

private Properties getAlgorithmProperties(final AlgorithmPropertiesContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.encrypt.distsql.parser.core;

import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementBaseVisitor;
import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.AlgorithmDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.AlgorithmPropertyContext;
Expand Down Expand Up @@ -61,30 +62,37 @@ public ASTNode visitAlterEncryptRule(final AlterEncryptRuleContext ctx) {

@Override
public ASTNode visitDropEncryptRule(final DropEncryptRuleContext ctx) {
return new DropEncryptRuleStatement(ctx.tableName().stream().map(TableNameContext::getText).collect(Collectors.toList()));
return new DropEncryptRuleStatement(ctx.tableName().stream().map(each -> getIdentifierValue(each)).collect(Collectors.toList()));
}

@Override
public ASTNode visitShowEncryptRules(final ShowEncryptRulesContext ctx) {
return new ShowEncryptRulesStatement(null == ctx.tableRule() ? null : ctx.tableRule().tableName().getText(),
return new ShowEncryptRulesStatement(null == ctx.tableRule() ? null : getIdentifierValue(ctx.tableRule().tableName()),
null == ctx.schemaName() ? null : (SchemaSegment) visit(ctx.schemaName()));
}

@Override
public ASTNode visitEncryptRuleDefinition(final EncryptRuleDefinitionContext ctx) {
return new EncryptRuleSegment(ctx.tableName().getText(), ctx.columnDefinition().stream().map(each -> (EncryptColumnSegment) visit(each)).collect(Collectors.toList()));
return new EncryptRuleSegment(getIdentifierValue(ctx.tableName()), ctx.columnDefinition().stream().map(each -> (EncryptColumnSegment) visit(each)).collect(Collectors.toList()));
}

@Override
public ASTNode visitColumnDefinition(final ColumnDefinitionContext ctx) {
return new EncryptColumnSegment(ctx.columnName().getText(),
ctx.cipherColumnName().getText(), null == ctx.plainColumnName() ? null : ctx.plainColumnName().getText(),
null == ctx.assistedQueryColumnName() ? null : ctx.assistedQueryColumnName().getText(), (AlgorithmSegment) visit(ctx.algorithmDefinition()));
return new EncryptColumnSegment(getIdentifierValue(ctx.columnName()),
getIdentifierValue(ctx.cipherColumnName()), null == ctx.plainColumnName() ? null : getIdentifierValue(ctx.plainColumnName()),
null == ctx.assistedQueryColumnName() ? null : getIdentifierValue(ctx.assistedQueryColumnName()), (AlgorithmSegment) visit(ctx.algorithmDefinition()));
}

@Override
public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
return new AlgorithmSegment(ctx.algorithmName().getText(), getAlgorithmProperties(ctx));
return new AlgorithmSegment(getIdentifierValue(ctx.algorithmName()), getAlgorithmProperties(ctx));
}

private String getIdentifierValue(final ParseTree context) {
if (null == context) {
return null;
}
return new IdentifierValue(context.getText()).getValue();
}

private Properties getAlgorithmProperties(final AlgorithmDefinitionContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.readwritesplitting.distsql.parser.core;

import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementBaseVisitor;
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementParser.AlgorithmDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementParser.AlgorithmPropertyContext;
Expand All @@ -28,7 +28,6 @@
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementParser.DropReadwriteSplittingRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementParser.EnableReadDataSourceContext;
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementParser.ReadwriteSplittingRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementParser.RuleNameContext;
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementParser.SchemaNameContext;
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementParser.SetReadwriteSplittingHintSourceContext;
import org.apache.shardingsphere.distsql.parser.autogen.ReadwriteSplittingDistSQLStatementParser.ShowReadwriteSplittingHintStatusContext;
Expand Down Expand Up @@ -70,19 +69,19 @@ public ASTNode visitAlterReadwriteSplittingRule(final AlterReadwriteSplittingRul

@Override
public ASTNode visitDropReadwriteSplittingRule(final DropReadwriteSplittingRuleContext ctx) {
return new DropReadwriteSplittingRuleStatement(ctx.ruleName().stream().map(RuleNameContext::getText).collect(Collectors.toList()));
return new DropReadwriteSplittingRuleStatement(ctx.ruleName().stream().map(each -> getIdentifierValue(each)).collect(Collectors.toList()));
}

@Override
public ASTNode visitEnableReadDataSource(final EnableReadDataSourceContext ctx) {
SchemaSegment schemaSegment = Objects.nonNull(ctx.schemaName()) ? (SchemaSegment) visit(ctx.schemaName()) : null;
return new SetReadwriteSplittingStatusStatement(ctx.ENABLE().getText().toUpperCase(), new IdentifierValue(ctx.resourceName().getText()).getValue(), schemaSegment);
return new SetReadwriteSplittingStatusStatement(ctx.ENABLE().getText().toUpperCase(), getIdentifierValue(ctx.resourceName()), schemaSegment);
}

@Override
public ASTNode visitDisableReadDataSource(final DisableReadDataSourceContext ctx) {
SchemaSegment schemaSegment = Objects.nonNull(ctx.schemaName()) ? (SchemaSegment) visit(ctx.schemaName()) : null;
return new SetReadwriteSplittingStatusStatement(ctx.DISABLE().getText().toUpperCase(), new IdentifierValue(ctx.resourceName().getText()).getValue(), schemaSegment);
return new SetReadwriteSplittingStatusStatement(ctx.DISABLE().getText().toUpperCase(), getIdentifierValue(ctx.resourceName()), schemaSegment);
}

@Override
Expand All @@ -97,13 +96,14 @@ public ASTNode visitReadwriteSplittingRuleDefinition(final ReadwriteSplittingRul
ctx.algorithmDefinition().algorithmProperties().algorithmProperty().forEach(each -> props.setProperty(each.key.getText(), each.value.getText()));
}
if (null == ctx.staticReadwriteSplittingRuleDefinition()) {
return new ReadwriteSplittingRuleSegment(
ctx.ruleName().getText(), ctx.dynamicReadwriteSplittingRuleDefinition().resourceName().getText(), ctx.algorithmDefinition().algorithmName().getText(), props);
return new ReadwriteSplittingRuleSegment(getIdentifierValue(ctx.ruleName()), getIdentifierValue(ctx.dynamicReadwriteSplittingRuleDefinition().resourceName()),
getIdentifierValue(ctx.algorithmDefinition().algorithmName()), props);
}
StaticReadwriteSplittingRuleDefinitionContext staticRuleDefinitionCtx = ctx.staticReadwriteSplittingRuleDefinition();
return new ReadwriteSplittingRuleSegment(ctx.ruleName().getText(),
staticRuleDefinitionCtx.writeResourceName().getText(), staticRuleDefinitionCtx.readResourceNames().resourceName().stream().map(RuleContext::getText).collect(Collectors.toList()),
ctx.algorithmDefinition().algorithmName().getText(), props);
return new ReadwriteSplittingRuleSegment(getIdentifierValue(ctx.ruleName()),
getIdentifierValue(staticRuleDefinitionCtx.writeResourceName()),
staticRuleDefinitionCtx.readResourceNames().resourceName().stream().map(each -> getIdentifierValue(each)).collect(Collectors.toList()),
getIdentifierValue(ctx.algorithmDefinition().algorithmName()), props);
}

@Override
Expand All @@ -113,12 +113,19 @@ public ASTNode visitSchemaName(final SchemaNameContext ctx) {

@Override
public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
return new AlgorithmSegment(ctx.algorithmName().getText(), getAlgorithmProperties(ctx));
return new AlgorithmSegment(getIdentifierValue(ctx.algorithmName()), getAlgorithmProperties(ctx));
}

@Override
public ASTNode visitSetReadwriteSplittingHintSource(final SetReadwriteSplittingHintSourceContext ctx) {
return new SetReadwriteSplittingHintStatement(ctx.sourceValue().getText());
return new SetReadwriteSplittingHintStatement(getIdentifierValue(ctx.sourceValue()));
}

private String getIdentifierValue(final ParseTree context) {
if (null == context) {
return null;
}
return new IdentifierValue(context.getText()).getValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public ASTNode visitCreateShadowRule(final CreateShadowRuleContext ctx) {
@Override
public ASTNode visitShadowRuleDefinition(final ShadowRuleDefinitionContext ctx) {
Map<String, Collection<ShadowAlgorithmSegment>> shadowAlgorithms = ctx.shadowTableRule().stream()
.collect(Collectors.toMap(each -> getText(each.tableName()), each -> visitShadowAlgorithms(each.shadowAlgorithmDefinition()), (oldValue, newValue) -> newValue));
return new ShadowRuleSegment(getText(ctx.ruleName()), getText(ctx.source()), getText(ctx.shadow()), shadowAlgorithms);
.collect(Collectors.toMap(each -> getIdentifierValue(each.tableName()), each -> visitShadowAlgorithms(each.shadowAlgorithmDefinition()), (oldValue, newValue) -> newValue));
return new ShadowRuleSegment(getIdentifierValue(ctx.ruleName()), getIdentifierValue(ctx.source()), getIdentifierValue(ctx.shadow()), shadowAlgorithms);
}

@Override
Expand All @@ -81,9 +81,10 @@ public ASTNode visitShowShadowAlgorithms(final ShowShadowAlgorithmsContext ctx)

@Override
public ASTNode visitShadowAlgorithmDefinition(final ShadowAlgorithmDefinitionContext ctx) {
AlgorithmSegment segment = new AlgorithmSegment(getText(ctx.shadowAlgorithmType()), getAlgorithmProperties(ctx.algorithmProperties()));
String algorithmName = null != ctx.algorithmName() ? getText(ctx.algorithmName())
: createAlgorithmName(getText(((ShadowRuleDefinitionContext) ctx.getParent().getParent()).ruleName()), getText(((ShadowTableRuleContext) ctx.getParent()).tableName()), segment);
AlgorithmSegment segment = new AlgorithmSegment(getIdentifierValue(ctx.shadowAlgorithmType()), getAlgorithmProperties(ctx.algorithmProperties()));
String algorithmName = null != ctx.algorithmName() ? getIdentifierValue(ctx.algorithmName())
: createAlgorithmName(getIdentifierValue(((ShadowRuleDefinitionContext) ctx.getParent().getParent()).ruleName()),
getIdentifierValue(((ShadowTableRuleContext) ctx.getParent()).tableName()), segment);
return new ShadowAlgorithmSegment(algorithmName, segment);
}

Expand Down Expand Up @@ -112,12 +113,12 @@ public ASTNode visitAlterShadowAlgorithm(final AlterShadowAlgorithmContext ctx)
@Override
public ASTNode visitDropShadowAlgorithm(final DropShadowAlgorithmContext ctx) {
return new DropShadowAlgorithmStatement(null == ctx.algorithmName() ? Collections.emptyList()
: ctx.algorithmName().stream().map(ShadowDistSQLStatementVisitor::getText).collect(Collectors.toSet()));
: ctx.algorithmName().stream().map(each -> getIdentifierValue(each)).collect(Collectors.toSet()));
}

@Override
public ASTNode visitShowShadowRules(final ShowShadowRulesContext ctx) {
String ruleName = null == ctx.shadowRule() ? null : getText(ctx.shadowRule().ruleName());
String ruleName = null == ctx.shadowRule() ? null : getIdentifierValue(ctx.shadowRule().ruleName());
SchemaSegment schemaSegment = null == ctx.schemaName() ? null : (SchemaSegment) visit(ctx.schemaName());
return new ShowShadowRulesStatement(ruleName, null == ctx.schemaName() ? null : schemaSegment);
}
Expand All @@ -127,7 +128,7 @@ public ASTNode visitShowShadowTableRules(final ShowShadowTableRulesContext ctx)
return new ShowShadowTableRulesStatement(null != ctx.schemaName() ? (SchemaSegment) visit(ctx.schemaName()) : null);
}

private static String getText(final ParserRuleContext ctx) {
private String getIdentifierValue(final ParserRuleContext ctx) {
if (null == ctx || ctx.isEmpty()) {
return null;
}
Expand Down
Loading

0 comments on commit 644d637

Please sign in to comment.