未验证 提交 ead87d48 编写于 作者: L Liang Zhang 提交者: GitHub

Redesign SQL parser and visitor API (#7946)

* Refactor SQLParserFactory

* Refactor SQLParserFactory

* Refactor SQLParserFactory

* Refactor SQLParserExecutor

* Move SQLParserFacadeRegistry to parser package

* Merge SQLStatementVisitorFactory and SQLFormatVisitorFactory

* Add generic on SQLVisitorFactory

* Refactor StandardSQLStatementParserEngineFactory

* Move SQLParserEngine inside core package

* Add SQLVisitorEngineFactory

* Add generic type for SQLVisitorEngineFactory

* Revert invalid changes
上级 82e5f038
......@@ -31,7 +31,7 @@ import org.antlr.v4.runtime.tree.ErrorNode;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementLexer;
import org.apache.shardingsphere.rdl.parser.sql.parser.ShardingSphereParser;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
import org.apache.shardingsphere.sql.parser.core.parser.ParseASTNode;
import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
import java.nio.CharBuffer;
......
......@@ -21,7 +21,7 @@ import org.antlr.v4.runtime.TokenStream;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
import org.apache.shardingsphere.sql.parser.core.parser.ParseASTNode;
/**
* SQL parser for RDL.
......
......@@ -21,7 +21,7 @@ import org.antlr.v4.runtime.TokenStream;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
import org.apache.shardingsphere.sql.parser.core.parser.ParseASTNode;
/**
* SQL parser for MySQL.
......
......@@ -21,7 +21,7 @@ import org.antlr.v4.runtime.TokenStream;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
import org.apache.shardingsphere.sql.parser.core.parser.ParseASTNode;
/**
* SQL parser for Oracle.
......
......@@ -21,7 +21,7 @@ import org.antlr.v4.runtime.TokenStream;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
import org.apache.shardingsphere.sql.parser.core.parser.ParseASTNode;
/**
* SQL parser for PostgreSQL.
......
......@@ -21,7 +21,7 @@ import org.antlr.v4.runtime.TokenStream;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
import org.apache.shardingsphere.sql.parser.core.parser.ParseASTNode;
/**
* SQL parser for SQL92.
......
......@@ -21,7 +21,7 @@ import org.antlr.v4.runtime.TokenStream;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
import org.apache.shardingsphere.sql.parser.core.parser.ParseASTNode;
/**
* SQL parser for SQLServer.
......
......@@ -15,12 +15,19 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sql.parser.engine;
package org.apache.shardingsphere.sql.parser.api.parser;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.shardingsphere.sql.parser.cache.SQLParsedResultCache;
import org.apache.shardingsphere.sql.parser.core.parser.SQLParserExecutor;
import org.apache.shardingsphere.sql.parser.core.parser.ParseASTNode;
import org.apache.shardingsphere.sql.parser.core.parser.SQLParserFactory;
import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
import java.util.Optional;
......@@ -30,13 +37,13 @@ import java.util.Optional;
@RequiredArgsConstructor
public final class SQLParserEngine {
private final String databaseTypeName;
private final String databaseType;
private final SQLParsedResultCache<ParseTree> cache = new SQLParsedResultCache<>();
/**
* Parse SQL.
*
* Parse.
*
* @param sql SQL to be parsed
* @param useCache whether use cache
* @return parse tree
......@@ -49,12 +56,33 @@ public final class SQLParserEngine {
if (parseTree.isPresent()) {
return parseTree.get();
}
ParseTree result = new SQLParserExecutor(databaseTypeName, sql).execute().getRootNode();
ParseTree result = parse(sql);
cache.put(sql, result);
return result;
}
private ParseTree parse(final String sql) {
return new SQLParserExecutor(databaseTypeName, sql).execute().getRootNode();
ParseASTNode result = twoPhaseParse(sql);
if (result.getRootNode() instanceof ErrorNode) {
throw new SQLParsingException(String.format("Unsupported SQL of `%s`", sql));
}
return result.getRootNode();
}
private ParseASTNode twoPhaseParse(final String sql) {
SQLParser sqlParser = SQLParserFactory.newInstance(databaseType, sql);
try {
setPredictionMode((Parser) sqlParser, PredictionMode.SLL);
return (ParseASTNode) sqlParser.parse();
} catch (final ParseCancellationException ex) {
((Parser) sqlParser).reset();
setPredictionMode((Parser) sqlParser, PredictionMode.LL);
return (ParseASTNode) sqlParser.parse();
}
}
private void setPredictionMode(final Parser sqlParser, final PredictionMode mode) {
sqlParser.setErrorHandler(new BailErrorStrategy());
sqlParser.getInterpreter().setPredictionMode(mode);
}
}
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sql.parser.engine;
package org.apache.shardingsphere.sql.parser.api.parser;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
......@@ -33,20 +33,20 @@ public final class SQLParserEngineFactory {
/**
* Get SQL parser engine.
*x
* @param databaseTypeName name of database type
*
* @param databaseType database type
* @return SQL parser engine
*/
public static SQLParserEngine getSQLParserEngine(final String databaseTypeName) {
if (ENGINES.containsKey(databaseTypeName)) {
return ENGINES.get(databaseTypeName);
public static SQLParserEngine getSQLParserEngine(final String databaseType) {
if (ENGINES.containsKey(databaseType)) {
return ENGINES.get(databaseType);
}
synchronized (ENGINES) {
if (ENGINES.containsKey(databaseTypeName)) {
return ENGINES.get(databaseTypeName);
if (ENGINES.containsKey(databaseType)) {
return ENGINES.get(databaseType);
}
SQLParserEngine result = new SQLParserEngine(databaseTypeName);
ENGINES.put(databaseTypeName, result);
SQLParserEngine result = new SQLParserEngine(databaseType);
ENGINES.put(databaseType, result);
return result;
}
}
......
/*
* 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.sql.parser.api.visitor;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLVisitorFactory;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLVisitorRule;
/**
* SQL visitor engnie.
*
* @param <T> type of return value
*/
@RequiredArgsConstructor
public final class SQLVisitorEngine<T> {
private final String databaseType;
private final String visitorType;
/**
* Visit parse tree.
*
* @param parseTree parse tree
* @return visit result
*/
public T visit(final ParseTree parseTree) {
ParseTreeVisitor<T> visitor = SQLVisitorFactory.newInstance(databaseType, visitorType, SQLVisitorRule.valueOf(parseTree.getClass()));
return parseTree.accept(visitor);
}
}
/*
* 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.sql.parser.api.visitor;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* SQL visitor engine factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SQLVisitorEngineFactory {
/**
* Get SQL visitor engine.
*
* @param databaseType database type
* @param visitorType visitor type
* @param <T> type of visitor result
* @return SQL visitor engine
*/
public static <T> SQLVisitorEngine<T> getSQLVisitorEngine(final String databaseType, final String visitorType) {
return new SQLVisitorEngine<>(databaseType, visitorType);
}
}
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sql.parser.core;
package org.apache.shardingsphere.sql.parser.core.parser;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.tree.ParseTree;
......
/*
* 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.sql.parser.core.parser;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
/**
* SQL parser executor.
*/
@RequiredArgsConstructor
public final class SQLParserExecutor {
private final String databaseTypeName;
private final String sql;
/**
* Execute to parse SQL.
*
* @return AST node
*/
public ParseASTNode execute() {
ParseASTNode result = twoPhaseParse();
if (result.getRootNode() instanceof ErrorNode) {
throw new SQLParsingException(String.format("Unsupported SQL of `%s`", sql));
}
return result;
}
private ParseASTNode twoPhaseParse() {
SQLParser sqlParser = SQLParserFactory.newInstance(databaseTypeName, sql);
try {
((Parser) sqlParser).setErrorHandler(new BailErrorStrategy());
((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.SLL);
return (ParseASTNode) sqlParser.parse();
} catch (final ParseCancellationException ex) {
((Parser) sqlParser).reset();
((Parser) sqlParser).setErrorHandler(new BailErrorStrategy());
((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.LL);
return (ParseASTNode) sqlParser.parse();
}
}
}
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sql.parser.core;
package org.apache.shardingsphere.sql.parser.core.parser;
import org.apache.shardingsphere.sql.parser.spi.SQLParserFacade;
......
......@@ -26,8 +26,8 @@ import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.TokenStream;
import org.apache.shardingsphere.sql.parser.api.lexer.SQLLexer;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.core.SQLParserFacadeRegistry;
import org.apache.shardingsphere.sql.parser.spi.SQLParserFacade;
import java.nio.CharBuffer;
......@@ -38,22 +38,31 @@ import java.nio.CharBuffer;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SQLParserFactory {
/**
/**
* New instance of SQL parser.
*
* @param databaseTypeName name of database type
* @param databaseType database type
* @param sql SQL
* @return SQL parser
*/
public static SQLParser newInstance(final String databaseTypeName, final String sql) {
return createSQLParser(sql, SQLParserFacadeRegistry.getInstance().getSQLParserFacade(databaseTypeName));
public static SQLParser newInstance(final String databaseType, final String sql) {
SQLParserFacade sqlParserFacade = SQLParserFacadeRegistry.getInstance().getSQLParserFacade(databaseType);
return createSQLParser(createTokenStream(sql, sqlParserFacade.getLexerClass()), sqlParserFacade.getParserClass());
}
@SneakyThrows(ReflectiveOperationException.class)
private static SQLParser createSQLParser(final String sql, final SQLParserFacade config) {
private static SQLParser createSQLParser(final TokenStream tokenStream, final Class<? extends SQLParser> parserClass) {
return parserClass.getConstructor(TokenStream.class).newInstance(tokenStream);
}
@SneakyThrows(ReflectiveOperationException.class)
private static TokenStream createTokenStream(final String sql, final Class<? extends SQLLexer> lexerClass) {
Lexer lexer = (Lexer) lexerClass.getConstructor(CharStream.class).newInstance(getSQLCharStream(sql));
return new CommonTokenStream(lexer);
}
private static CharStream getSQLCharStream(final String sql) {
CodePointBuffer buffer = CodePointBuffer.withChars(CharBuffer.wrap(sql.toCharArray()));
CodePointCharStream codePointCharStream = CodePointCharStream.fromBuffer(buffer);
Lexer lexer = (Lexer) config.getLexerClass().getConstructor(CharStream.class).newInstance(codePointCharStream);
return config.getParserClass().getConstructor(TokenStream.class).newInstance(new CommonTokenStream(lexer));
return CodePointCharStream.fromBuffer(buffer);
}
}
/*
* 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.sql.parser.core.visitor;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
import org.apache.shardingsphere.sql.parser.spi.SQLVisitorFacade;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatementType;
/**
* SQL format visitor factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SQLFormatVisitorFactory {
/**
* New instance of SQL format visitor.
*
* @param databaseTypeName name of database type
* @param sqlVisitorRule visitor rule
* @return parse tree visitor
*/
public static ParseTreeVisitor newInstance(final String databaseTypeName, final SQLVisitorRule sqlVisitorRule) {
SQLVisitorFacade facade = SQLVisitorFacadeRegistry.getInstance().getSQLVisitorFacade(databaseTypeName, "FORMAT");
return createParseTreeVisitor(facade, sqlVisitorRule.getType());
}
@SneakyThrows(ReflectiveOperationException.class)
private static ParseTreeVisitor createParseTreeVisitor(final SQLVisitorFacade visitorFacade, final SQLStatementType type) {
switch (type) {
case DML:
return (ParseTreeVisitor) visitorFacade.getDMLVisitorClass().getConstructor().newInstance();
case DDL:
return (ParseTreeVisitor) visitorFacade.getDDLVisitorClass().getConstructor().newInstance();
case TCL:
return (ParseTreeVisitor) visitorFacade.getTCLVisitorClass().getConstructor().newInstance();
case DCL:
return (ParseTreeVisitor) visitorFacade.getDCLVisitorClass().getConstructor().newInstance();
case DAL:
return (ParseTreeVisitor) visitorFacade.getDALVisitorClass().getConstructor().newInstance();
case RL:
return (ParseTreeVisitor) visitorFacade.getRLVisitorClass().getConstructor().newInstance();
default:
throw new SQLParsingException("Can not support SQL statement type: `%s`", type);
}
}
}
......@@ -26,25 +26,28 @@ import org.apache.shardingsphere.sql.parser.spi.SQLVisitorFacade;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatementType;
/**
* SQL statement visitor factory.
* SQL visitor factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SQLStatementVisitorFactory {
public final class SQLVisitorFactory {
/**
* New instance of SQL statement visitor.
/**
* New instance of SQL visitor.
*
* @param databaseTypeName name of database type
* @param sqlVisitorRule SQL visitor rule
* @param databaseType database type
* @param visitorType SQL visitor type
* @param visitorRule SQL visitor rule
* @param <T> type of visitor result
* @return parse tree visitor
*/
public static ParseTreeVisitor newInstance(final String databaseTypeName, final SQLVisitorRule sqlVisitorRule) {
SQLVisitorFacade facade = SQLVisitorFacadeRegistry.getInstance().getSQLVisitorFacade(databaseTypeName, "STATEMENT");
return createParseTreeVisitor(facade, sqlVisitorRule.getType());
public static <T> ParseTreeVisitor<T> newInstance(final String databaseType, final String visitorType, final SQLVisitorRule visitorRule) {
SQLVisitorFacade facade = SQLVisitorFacadeRegistry.getInstance().getSQLVisitorFacade(databaseType, visitorType);
return createParseTreeVisitor(facade, visitorRule.getType());
}
@SuppressWarnings("unchecked")
@SneakyThrows(ReflectiveOperationException.class)
private static ParseTreeVisitor createParseTreeVisitor(final SQLVisitorFacade visitorFacade, final SQLStatementType type) {
private static <T> ParseTreeVisitor<T> createParseTreeVisitor(final SQLVisitorFacade visitorFacade, final SQLStatementType type) {
switch (type) {
case DML:
return (ParseTreeVisitor) visitorFacade.getDMLVisitorClass().getConstructor().newInstance();
......
......@@ -51,47 +51,47 @@ public enum SQLVisitorRule {
ALTER_INDEX("AlterIndex", SQLStatementType.DDL),
DROP_INDEX("DropIndex", SQLStatementType.DDL),
CREATE_PROCEDURE("CreateProcedure", SQLStatementType.DDL),
ALTER_PROCEDURE("AlterProcedure", SQLStatementType.DDL),
DROP_PROCEDURE("DropProcedure", SQLStatementType.DDL),
CREATE_FUNCTION("CreateFunction", SQLStatementType.DDL),
ALTER_FUNCTION("AlterFunction", SQLStatementType.DDL),
DROP_FUNCTION("DropFunction", SQLStatementType.DDL),
CREATE_DATABASE("CreateDatabase", SQLStatementType.DDL),
ALTER_DATABASE("AlterDatabase", SQLStatementType.DDL),
DROP_DATABASE("DropDatabase", SQLStatementType.DDL),
CREATE_EVENT("CreateEvent", SQLStatementType.DDL),
ALTER_EVENT("AlterEvent", SQLStatementType.DDL),
DROP_EVENT("DropEvent", SQLStatementType.DDL),
ALTER_INSTANCE("AlterInstance", SQLStatementType.DDL),
CREATE_LOGFILE_GROUP("CreateLogfileGroup", SQLStatementType.DDL),
ALTER_LOGFILE_GROUP("AlterLogfileGroup", SQLStatementType.DDL),
DROP_LOGFILE_GROUP("DropLogfileGroup", SQLStatementType.DDL),
CREATE_SERVER("CreateServer", SQLStatementType.DDL),
ALTER_SERVER("AlterServer", SQLStatementType.DDL),
DROP_SERVER("DropServer", SQLStatementType.DDL),
CREATE_TRIGGER("CreateTrigger", SQLStatementType.DDL),
DROP_TRIGGER("DropTrigger", SQLStatementType.DDL),
CREATE_VIEW("CreateView", SQLStatementType.DDL),
......
......@@ -21,9 +21,9 @@ import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.apache.shardingsphere.sql.parser.cache.SQLParsedResultCache;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLStatementVisitorFactory;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLVisitorFactory;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLVisitorRule;
import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParserEngineFactory;
import org.apache.shardingsphere.sql.parser.hook.ParsingHookRegistry;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.statement.SQLStatementParserEngine;
......@@ -76,10 +76,9 @@ public final class StandardSQLStatementParserEngine implements SQLStatementParse
return result;
}
@SuppressWarnings("unchecked")
private SQLStatement parseSQLStatement(final String sql) {
ParseTree parseTree = SQLParserEngineFactory.getSQLParserEngine(databaseTypeName).parse(sql, false);
ParseTreeVisitor<SQLStatement> visitor = SQLStatementVisitorFactory.newInstance(databaseTypeName, SQLVisitorRule.valueOf(parseTree.getClass()));
ParseTreeVisitor<SQLStatement> visitor = SQLVisitorFactory.newInstance(databaseTypeName, "STATEMENT", SQLVisitorRule.valueOf(parseTree.getClass()));
return parseTree.accept(visitor);
}
}
......@@ -34,19 +34,19 @@ public final class StandardSQLStatementParserEngineFactory {
/**
* Get standard SQL statement parser engine.
*x
* @param databaseTypeName name of database type
* @param databaseType name of database type
* @return standard SQL statement parser engine
*/
public static StandardSQLStatementParserEngine getSQLStatementParserEngine(final String databaseTypeName) {
if (ENGINES.containsKey(databaseTypeName)) {
return ENGINES.get(databaseTypeName);
public static StandardSQLStatementParserEngine getSQLStatementParserEngine(final String databaseType) {
if (ENGINES.containsKey(databaseType)) {
return ENGINES.get(databaseType);
}
synchronized (ENGINES) {
if (ENGINES.containsKey(databaseTypeName)) {
return ENGINES.get(databaseTypeName);
if (ENGINES.containsKey(databaseType)) {
return ENGINES.get(databaseType);
}
StandardSQLStatementParserEngine result = new StandardSQLStatementParserEngine(databaseTypeName);
ENGINES.put(databaseTypeName, result);
StandardSQLStatementParserEngine result = new StandardSQLStatementParserEngine(databaseType);
ENGINES.put(databaseType, result);
return result;
}
}
......
......@@ -15,8 +15,9 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sql.parser.engine;
package org.apache.shardingsphere.sql.parser.api;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParserEngineFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册