Skip to content

Commit

Permalink
apache#2081 bug fixs
Browse files Browse the repository at this point in the history
  • Loading branch information
codefairy08 committed Mar 26, 2019
1 parent 1199424 commit 5e859c5
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,10 @@ public enum SQLType {
/**
* Database control Language.
*/
DCL
DCL,

/**
* Database general Language.
*/
GENERAL
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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 {
Expand All @@ -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<SQLSegment> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class SQLSegmentsExtractorEngine {
*/
public Collection<SQLSegment> extract(final SQLAST ast) {
Collection<SQLSegment> result = new LinkedList<>();
for (SQLSegmentExtractor each : ast.getRule().getExtractors()) {
for (SQLSegmentExtractor each : ast.getRule().get().getExtractors()) {
if (each instanceof OptionalSQLSegmentExtractor) {
Optional<? extends SQLSegment> sqlSegment = ((OptionalSQLSegmentExtractor) each).extract(ast.getParserRuleContext());
if (sqlSegment.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -33,5 +36,5 @@ public final class SQLAST {

private final ParserRuleContext parserRuleContext;

private final SQLStatementRule rule;
private final Optional<SQLStatementRule> rule;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,7 +46,7 @@ public final class SQLParserEngine {

/**
* Parse SQL to abstract syntax tree.
*
*
* @return abstract syntax tree of SQL
*/
public SQLAST parse() {
Expand All @@ -53,8 +56,11 @@ public SQLAST parse() {
}
Optional<SQLStatementRule> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 5e859c5

Please sign in to comment.