This is an automated email from the ASF dual-hosted git repository.
wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8ffcfe5 Use SPI to introduce DistSQL visitor (#10798)
8ffcfe5 is described below
commit 8ffcfe51dfc84875f3bb65d4e1b803b370ca8e8c
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jun 13 00:43:50 2021 +0800
Use SPI to introduce DistSQL visitor (#10798)
* Refactor DistSQLStatementParserEngine
* Add FeatureTypedParseASTNode
* Add FeatureTypedSQLVisitorFacade
---
.../parser/api/DistSQLStatementParserEngine.java | 48 +++++++++++-----
.../FeatureTypedParseASTNode.java} | 23 +++-----
.../parser/core/{ => standard}/DistSQLParser.java | 2 +-
.../core/{ => standard}/DistSQLParserFactory.java | 2 +-
.../parser/core/{ => standard}/DistSQLVisitor.java | 67 +++++++++++-----------
.../FeatureTypedSQLVisitorFacade.java} | 31 +++++-----
6 files changed, 93 insertions(+), 80 deletions(-)
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngine.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngine.java
index 6c2f8fd..1b8bae4 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngine.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngine.java
@@ -17,19 +17,26 @@
package org.apache.shardingsphere.distsql.parser.api;
+import lombok.SneakyThrows;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.tree.ErrorNode;
-import org.apache.shardingsphere.distsql.parser.core.DistSQLParserFactory;
-import org.apache.shardingsphere.distsql.parser.core.DistSQLVisitor;
+import org.antlr.v4.runtime.tree.ParseTreeVisitor;
+import
org.apache.shardingsphere.distsql.parser.core.feature.FeatureTypedParseASTNode;
+import
org.apache.shardingsphere.distsql.parser.core.standard.DistSQLParserFactory;
+import org.apache.shardingsphere.distsql.parser.core.standard.DistSQLVisitor;
import
org.apache.shardingsphere.distsql.parser.spi.FeatureTypedSQLParserFacade;
+import
org.apache.shardingsphere.distsql.parser.spi.FeatureTypedSQLVisitorFacade;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
+import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
import org.apache.shardingsphere.sql.parser.core.SQLParserFactory;
import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Collection;
+import java.util.HashMap;
import java.util.LinkedList;
+import java.util.Map;
import java.util.ServiceLoader;
/**
@@ -37,11 +44,16 @@ import java.util.ServiceLoader;
*/
public final class DistSQLStatementParserEngine {
- private static final Collection<FeatureTypedSQLParserFacade>
RULE_PARSER_FACADES = new LinkedList<>();
+ private static final Collection<FeatureTypedSQLParserFacade>
FEATURE_TYPED_PARSER_FACADES = new LinkedList<>();
+
+ private static final Map<String, FeatureTypedSQLVisitorFacade>
FEATURE_TYPED_VISITOR_FACADES = new HashMap<>();
static {
for (FeatureTypedSQLParserFacade each :
ServiceLoader.load(FeatureTypedSQLParserFacade.class)) {
- RULE_PARSER_FACADES.add(each);
+ FEATURE_TYPED_PARSER_FACADES.add(each);
+ }
+ for (FeatureTypedSQLVisitorFacade each :
ServiceLoader.load(FeatureTypedSQLVisitorFacade.class)) {
+ FEATURE_TYPED_VISITOR_FACADES.put(each.getFeatureType(), each);
}
}
@@ -51,17 +63,16 @@ public final class DistSQLStatementParserEngine {
* @param sql SQL to be parsed
* @return AST node
*/
+ @SneakyThrows(ReflectiveOperationException.class)
public SQLStatement parse(final String sql) {
- ParseASTNode parseASTNode;
try {
- parseASTNode = parseFromStandardParser(sql);
+ ParseASTNode parseASTNode = parseFromStandardParser(sql);
+ return getSQLStatement(sql, parseASTNode, new DistSQLVisitor());
} catch (final ParseCancellationException ex) {
- parseASTNode = parseFromRuleParsers(sql);
- }
- if (parseASTNode.getRootNode() instanceof ErrorNode) {
- throw new SQLParsingException("Unsupported SQL of `%s`", sql);
+ FeatureTypedParseASTNode featureTypedParseASTNode =
parseFromFeatureTypedParsers(sql);
+ return getSQLStatement(sql,
+ featureTypedParseASTNode.getParseASTNode(),
FEATURE_TYPED_VISITOR_FACADES.get(featureTypedParseASTNode.getFeatureType()).getVisitorClass().newInstance());
}
- return (SQLStatement) new
DistSQLVisitor().visit(parseASTNode.getRootNode());
}
private ParseASTNode parseFromStandardParser(final String sql) {
@@ -73,13 +84,22 @@ public final class DistSQLStatementParserEngine {
}
}
- private ParseASTNode parseFromRuleParsers(final String sql) {
- for (FeatureTypedSQLParserFacade each : RULE_PARSER_FACADES) {
+ private FeatureTypedParseASTNode parseFromFeatureTypedParsers(final String
sql) {
+ for (FeatureTypedSQLParserFacade each : FEATURE_TYPED_PARSER_FACADES) {
try {
- return (ParseASTNode) SQLParserFactory.newInstance(sql,
each.getLexerClass(), each.getParserClass()).parse();
+ ParseASTNode parseASTNode = (ParseASTNode)
SQLParserFactory.newInstance(sql, each.getLexerClass(),
each.getParserClass()).parse();
+ return new FeatureTypedParseASTNode(each.getFeatureType(),
parseASTNode);
} catch (final ParseCancellationException ignored) {
}
}
throw new SQLParsingException("You have an error in your SQL syntax.");
}
+
+ @SuppressWarnings("rawtypes")
+ private SQLStatement getSQLStatement(final String sql, final ParseASTNode
parseASTNode, final SQLVisitor visitor) {
+ if (parseASTNode.getRootNode() instanceof ErrorNode) {
+ throw new SQLParsingException("Unsupported SQL of `%s`", sql);
+ }
+ return (SQLStatement) ((ParseTreeVisitor)
visitor).visit(parseASTNode.getRootNode());
+ }
}
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParser.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/feature/FeatureTypedParseASTNode.java
similarity index 59%
copy from
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParser.java
copy to
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/feature/FeatureTypedParseASTNode.java
index afbca33..4515bee 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParser.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/feature/FeatureTypedParseASTNode.java
@@ -15,25 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.core;
+package org.apache.shardingsphere.distsql.parser.core.feature;
-import org.antlr.v4.runtime.TokenStream;
-import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser;
-import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
-import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
/**
- * SQL parser for Dist SQL.
+ * Feature type based parse AST node.
*/
-public final class DistSQLParser extends DistSQLStatementParser implements
SQLParser {
+@RequiredArgsConstructor
+@Getter
+public final class FeatureTypedParseASTNode {
- public DistSQLParser(final TokenStream input) {
- super(input);
- }
+ private final String featureType;
- @Override
- public ASTNode parse() {
- return new ParseASTNode(execute());
- }
+ private final ParseASTNode parseASTNode;
}
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParser.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/standard/DistSQLParser.java
similarity index 95%
copy from
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParser.java
copy to
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/standard/DistSQLParser.java
index afbca33..106f8d4 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParser.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/standard/DistSQLParser.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.core;
+package org.apache.shardingsphere.distsql.parser.core.standard;
import org.antlr.v4.runtime.TokenStream;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser;
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParserFactory.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/standard/DistSQLParserFactory.java
similarity index 97%
rename from
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParserFactory.java
rename to
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/standard/DistSQLParserFactory.java
index 3a59173..903fdd6 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParserFactory.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/standard/DistSQLParserFactory.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.core;
+package org.apache.shardingsphere.distsql.parser.core.standard;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/standard/DistSQLVisitor.java
similarity index 97%
rename from
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
rename to
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/standard/DistSQLVisitor.java
index de7623d..eeb10e7 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/standard/DistSQLVisitor.java
@@ -15,23 +15,45 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.core;
+package org.apache.shardingsphere.distsql.parser.core.standard;
import com.google.common.base.Joiner;
+import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementBaseVisitor;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AddResourceContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlgorithmPropertiesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlgorithmPropertyContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterDatabaseDiscoveryRuleContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterEncryptRuleContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterReadwriteSplittingRuleContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterShardingBindingTableRulesContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterShardingBroadcastTableRulesContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterShardingTableRuleContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.BindTableRulesDefinitionContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CheckScalingJobContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ColumnDefinitionContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateDatabaseDiscoveryRuleContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateEncryptRuleContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateReadwriteSplittingRuleContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateShardingBindingTableRulesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateShardingBroadcastTableRulesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateShardingTableRuleContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DataSourceContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DatabaseDiscoveryRuleDefinitionContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropDatabaseDiscoveryRuleContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropEncryptRuleContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropReadwriteSplittingRuleContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropResourceContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropScalingJobContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropShardingBindingTableRulesContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropShardingBroadcastTableRulesContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropShardingTableRuleContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DynamicReadwriteSplittingRuleDefinitionContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.EncryptRuleDefinitionContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.FunctionDefinitionContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ReadwriteSplittingRuleDefinitionContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ResetScalingJobContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.SchemaNameContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShardingTableRuleDefinitionContext;
@@ -41,34 +63,13 @@ import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.S
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowResourcesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowScalingJobListContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowScalingJobStatusContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowShardingBindingTableRulesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowShardingBroadcastTableRulesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowShardingTableRulesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StartScalingJobContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StaticReadwriteSplittingRuleDefinitionContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StopScalingJobContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.TableNameContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterShardingTableRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterShardingBindingTableRulesContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterShardingBroadcastTableRulesContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateReadwriteSplittingRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ReadwriteSplittingRuleDefinitionContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StaticReadwriteSplittingRuleDefinitionContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DynamicReadwriteSplittingRuleDefinitionContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterReadwriteSplittingRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropShardingTableRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropShardingBindingTableRulesContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropShardingBroadcastTableRulesContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateDatabaseDiscoveryRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DatabaseDiscoveryRuleDefinitionContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterDatabaseDiscoveryRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropDatabaseDiscoveryRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateEncryptRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.EncryptRuleDefinitionContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ColumnDefinitionContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlterEncryptRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropEncryptRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropReadwriteSplittingRuleContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowShardingBindingTableRulesContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.AlgorithmPropertiesContext;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
import org.apache.shardingsphere.distsql.parser.segment.FunctionSegment;
import org.apache.shardingsphere.distsql.parser.segment.TableRuleSegment;
@@ -97,21 +98,22 @@ import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.Create
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBindingTableRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBroadcastTableRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropResourceStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropDatabaseDiscoveryRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropEncryptRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReadwriteSplittingRuleStatement;
-import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropResourceStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBindingTableRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBroadcastTableRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingTableRuleStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.impl.ShowDatabaseDiscoveryRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.impl.ShowEncryptRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.impl.ShowReadwriteSplittingRulesStatement;
-import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.impl.ShowShardingBindingTableRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.impl.ShowShardingBroadcastTableRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.impl.ShowShardingTableRulesStatement;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
+import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
@@ -125,15 +127,11 @@ import java.util.stream.Collectors;
/**
* Dist SQL visitor.
*/
-public final class DistSQLVisitor extends DistSQLStatementBaseVisitor<ASTNode>
{
+public final class DistSQLVisitor extends DistSQLStatementBaseVisitor<ASTNode>
implements SQLVisitor {
@Override
public ASTNode visitAddResource(final AddResourceContext ctx) {
- Collection<DataSourceSegment> connectionInfos = new LinkedList<>();
- for (DataSourceContext each : ctx.dataSource()) {
- connectionInfos.add((DataSourceSegment) visit(each));
- }
- return new AddResourceStatement(connectionInfos);
+ return new AddResourceStatement(ctx.dataSource().stream().map(each ->
(DataSourceSegment) visit(each)).collect(Collectors.toList()));
}
@Override
@@ -224,7 +222,7 @@ public final class DistSQLVisitor extends
DistSQLStatementBaseVisitor<ASTNode> {
public ASTNode visitStaticReadwriteSplittingRuleDefinition(final
StaticReadwriteSplittingRuleDefinitionContext ctx) {
ReadwriteSplittingRuleSegment result = new
ReadwriteSplittingRuleSegment();
result.setWriteDataSource(ctx.writeResourceName().getText());
- result.setReadDataSources(ctx.resourceName().stream().map(each ->
each.getText()).collect(Collectors.toList()));
+
result.setReadDataSources(ctx.resourceName().stream().map(RuleContext::getText).collect(Collectors.toList()));
return result;
}
@@ -237,8 +235,7 @@ public final class DistSQLVisitor extends
DistSQLStatementBaseVisitor<ASTNode> {
@Override
public ASTNode visitAlterReadwriteSplittingRule(final
AlterReadwriteSplittingRuleContext ctx) {
- return new
AlterReadwriteSplittingRuleStatement(ctx.readwriteSplittingRuleDefinition()
- .stream().map(each -> (ReadwriteSplittingRuleSegment)
visit(each)).collect(Collectors.toList()));
+ return new
AlterReadwriteSplittingRuleStatement(ctx.readwriteSplittingRuleDefinition().stream().map(each
-> (ReadwriteSplittingRuleSegment) visit(each)).collect(Collectors.toList()));
}
@Override
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParser.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/spi/FeatureTypedSQLVisitorFacade.java
similarity index 55%
rename from
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParser.java
rename to
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/spi/FeatureTypedSQLVisitorFacade.java
index afbca33..3c9dc39 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLParser.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/spi/FeatureTypedSQLVisitorFacade.java
@@ -15,25 +15,26 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.core;
+package org.apache.shardingsphere.distsql.parser.spi;
-import org.antlr.v4.runtime.TokenStream;
-import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser;
-import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
-import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
-import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
+import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
/**
- * SQL parser for Dist SQL.
+ * Feature type based SQL visitor facade.
*/
-public final class DistSQLParser extends DistSQLStatementParser implements
SQLParser {
+public interface FeatureTypedSQLVisitorFacade {
- public DistSQLParser(final TokenStream input) {
- super(input);
- }
+ /**
+ * Get visitor class.
+ *
+ * @return visitor class
+ */
+ Class<? extends SQLVisitor> getVisitorClass();
- @Override
- public ASTNode parse() {
- return new ParseASTNode(execute());
- }
+ /**
+ * Get feature type.
+ *
+ * @return feature type
+ */
+ String getFeatureType();
}