From 5e859c59817d1402a6394b14a6775c3bcd4a8a9d Mon Sep 17 00:00:00 2001 From: codefairy08 Date: Tue, 26 Mar 2019 16:32:17 +0800 Subject: [PATCH] #2081 bug fixs --- .../shardingsphere/core/constant/SQLType.java | 7 +++- .../core/parse/antlr/AntlrParsingEngine.java | 15 +++++--- .../extractor/SQLSegmentsExtractorEngine.java | 2 +- .../core/parse/antlr/parser/SQLAST.java | 9 +++-- .../parse/antlr/parser/SQLParserEngine.java | 16 ++++++--- .../parse/parser/sql/GeneralSQLStatement.java | 36 +++++++++++++++++++ 6 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/parser/sql/GeneralSQLStatement.java diff --git a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/constant/SQLType.java b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/constant/SQLType.java index 92f12a57d2eaf..65ddff5bb2bc9 100644 --- a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/constant/SQLType.java +++ b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/constant/SQLType.java @@ -60,5 +60,10 @@ public enum SQLType { /** * Database control Language. */ - DCL + DCL, + + /** + * Database general Language. + */ + GENERAL } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/AntlrParsingEngine.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/AntlrParsingEngine.java index 349ab938cc539..61d3acfb057b7 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/AntlrParsingEngine.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/AntlrParsingEngine.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.core.parse.antlr; +import java.util.Collection; + import org.apache.shardingsphere.core.constant.DatabaseType; import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData; import org.apache.shardingsphere.core.parse.antlr.extractor.SQLSegmentsExtractorEngine; @@ -28,13 +30,12 @@ import org.apache.shardingsphere.core.parse.antlr.rule.registry.ParsingRuleRegistry; import org.apache.shardingsphere.core.parse.antlr.rule.registry.ShardingParsingRuleRegistry; import org.apache.shardingsphere.core.parse.antlr.sql.segment.SQLSegment; +import org.apache.shardingsphere.core.parse.parser.sql.GeneralSQLStatement; import org.apache.shardingsphere.core.parse.parser.sql.SQLParser; import org.apache.shardingsphere.core.parse.parser.sql.SQLStatement; import org.apache.shardingsphere.core.rule.EncryptRule; import org.apache.shardingsphere.core.rule.SQLStatementFillerRule; -import java.util.Collection; - /** * SQL parsing engine. * @@ -51,8 +52,9 @@ public final class AntlrParsingEngine implements SQLParser { private final SQLStatementOptimizerEngine optimizerEngine; + private final ParsingRuleRegistry parsingRuleRegistry; + public AntlrParsingEngine(final DatabaseType databaseType, final String sql, final SQLStatementFillerRule sqlStatementFillerRule, final ShardingTableMetaData shardingTableMetaData) { - ParsingRuleRegistry parsingRuleRegistry; if (sqlStatementFillerRule instanceof EncryptRule) { parsingRuleRegistry = EncryptParsingRuleRegistry.getInstance(); } else { @@ -67,9 +69,12 @@ public AntlrParsingEngine(final DatabaseType databaseType, final String sql, fin @Override public SQLStatement parse() { SQLAST ast = parserEngine.parse(); + if (!ast.getRule().isPresent() && (parsingRuleRegistry instanceof EncryptParsingRuleRegistry)) { + return new GeneralSQLStatement(); + } Collection sqlSegments = extractorEngine.extract(ast); - SQLStatement result = fillerEngine.fill(sqlSegments, ast.getRule()); - optimizerEngine.optimize(ast.getRule(), result); + SQLStatement result = fillerEngine.fill(sqlSegments, ast.getRule().get()); + optimizerEngine.optimize(ast.getRule().get(), result); return result; } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/SQLSegmentsExtractorEngine.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/SQLSegmentsExtractorEngine.java index c5c773b749e0d..ab7da00811130 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/SQLSegmentsExtractorEngine.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/extractor/SQLSegmentsExtractorEngine.java @@ -39,7 +39,7 @@ public final class SQLSegmentsExtractorEngine { */ public Collection extract(final SQLAST ast) { Collection result = new LinkedList<>(); - for (SQLSegmentExtractor each : ast.getRule().getExtractors()) { + for (SQLSegmentExtractor each : ast.getRule().get().getExtractors()) { if (each instanceof OptionalSQLSegmentExtractor) { Optional sqlSegment = ((OptionalSQLSegmentExtractor) each).extract(ast.getParserRuleContext()); if (sqlSegment.isPresent()) { diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/parser/SQLAST.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/parser/SQLAST.java index 9e90602d79e11..f1d5a181b3886 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/parser/SQLAST.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/parser/SQLAST.java @@ -17,11 +17,14 @@ package org.apache.shardingsphere.core.parse.antlr.parser; -import lombok.Getter; -import lombok.RequiredArgsConstructor; import org.antlr.v4.runtime.ParserRuleContext; import org.apache.shardingsphere.core.parse.antlr.rule.registry.statement.SQLStatementRule; +import com.google.common.base.Optional; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + /** * Abstract syntax tree of SQL. * @@ -33,5 +36,5 @@ public final class SQLAST { private final ParserRuleContext parserRuleContext; - private final SQLStatementRule rule; + private final Optional rule; } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/parser/SQLParserEngine.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/parser/SQLParserEngine.java index 5cf2cdb878631..a15cb4461d954 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/parser/SQLParserEngine.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/antlr/parser/SQLParserEngine.java @@ -17,19 +17,22 @@ package org.apache.shardingsphere.core.parse.antlr.parser; -import com.google.common.base.Optional; -import lombok.RequiredArgsConstructor; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ErrorNode; import org.antlr.v4.runtime.tree.ParseTree; import org.apache.shardingsphere.core.constant.DatabaseType; +import org.apache.shardingsphere.core.parse.antlr.rule.registry.EncryptParsingRuleRegistry; import org.apache.shardingsphere.core.parse.antlr.rule.registry.ParsingRuleRegistry; import org.apache.shardingsphere.core.parse.antlr.rule.registry.statement.SQLStatementRule; import org.apache.shardingsphere.core.parse.parser.exception.SQLParsingUnsupportedException; +import com.google.common.base.Optional; + +import lombok.RequiredArgsConstructor; + /** * SQL parser engine. - * + * * @author zhangliang */ @RequiredArgsConstructor @@ -43,7 +46,7 @@ public final class SQLParserEngine { /** * Parse SQL to abstract syntax tree. - * + * * @return abstract syntax tree of SQL */ public SQLAST parse() { @@ -53,8 +56,11 @@ public SQLAST parse() { } Optional sqlStatementRule = parsingRuleRegistry.findSQLStatementRule(databaseType, parseTree.getClass().getSimpleName()); if (!sqlStatementRule.isPresent()) { + if (parsingRuleRegistry instanceof EncryptParsingRuleRegistry) { + return new SQLAST((ParserRuleContext) parseTree, sqlStatementRule); + } throw new SQLParsingUnsupportedException(String.format("Unsupported SQL of `%s`", sql)); } - return new SQLAST((ParserRuleContext) parseTree, sqlStatementRule.get()); + return new SQLAST((ParserRuleContext) parseTree, sqlStatementRule); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/parser/sql/GeneralSQLStatement.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/parser/sql/GeneralSQLStatement.java new file mode 100644 index 0000000000000..757002cd7591c --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/parser/sql/GeneralSQLStatement.java @@ -0,0 +1,36 @@ +/* + * 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.parser.sql; + +import org.apache.shardingsphere.core.constant.SQLType; + +/** + * General SQL statement. + * + * @author duhongjun + */ +public final class GeneralSQLStatement extends AbstractSQLStatement { + + public GeneralSQLStatement(final SQLType type) { + super(type); + } + + public GeneralSQLStatement() { + super(SQLType.GENERAL); + } +}