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

Determine SQL parser API (#7954)

* Refactor SQLParserEngine

* Determine SQL parser API
上级 bdbec38d
......@@ -15,29 +15,56 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sql.parser.api.parser;
package org.apache.shardingsphere.sql.parser.api;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.apache.shardingsphere.sql.parser.core.parser.SQLParserExecutor;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLVisitorFactory;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLVisitorRule;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* SQL parser engine factory.
* SQL parser engine.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SQLParserEngineFactory {
public final class SQLParserEngine {
private static final Map<String, SQLParserEngine> ENGINES = new ConcurrentHashMap<>();
private static final Map<String, SQLParserExecutor> ENGINES = new ConcurrentHashMap<>();
/**
* Get SQL parser engine.
* Parse SQL.
*
* @param databaseType database type
* @return SQL parser engine
* @param sql SQL to be parsed
* @param useCache whether use cache
* @param visitorType SQL visitor type
* @param <T> type of SQL visitor result
* @return SQL visitor result
*/
public static SQLParserEngine getSQLParserEngine(final String databaseType) {
public static <T> T parse(final String databaseType, final String sql, final boolean useCache, final String visitorType) {
ParseTree parseTree = parse(databaseType, sql, useCache);
ParseTreeVisitor<T> visitor = SQLVisitorFactory.newInstance(databaseType, visitorType, SQLVisitorRule.valueOf(parseTree.getClass()));
return parseTree.accept(visitor);
}
/**
* Parse SQL.
*
* @param databaseType database type
* @param sql SQL to be parsed
* @param useCache whether use cache
* @return parse tree
*/
public static ParseTree parse(final String databaseType, final String sql, final boolean useCache) {
return getSQLParserExecutor(databaseType).parse(sql, useCache);
}
private static SQLParserExecutor getSQLParserExecutor(final String databaseType) {
if (ENGINES.containsKey(databaseType)) {
return ENGINES.get(databaseType);
}
......@@ -45,7 +72,7 @@ public final class SQLParserEngineFactory {
if (ENGINES.containsKey(databaseType)) {
return ENGINES.get(databaseType);
}
SQLParserEngine result = new SQLParserEngine(databaseType);
SQLParserExecutor result = new SQLParserExecutor(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.api.parser;
package org.apache.shardingsphere.sql.parser.core.parser;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.BailErrorStrategy;
......@@ -24,18 +24,17 @@ 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.api.parser.SQLParser;
import org.apache.shardingsphere.sql.parser.cache.SQLParsedResultCache;
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;
/**
* SQL parser engine.
* SQL parser executor.
*/
@RequiredArgsConstructor
public final class SQLParserEngine {
public final class SQLParserExecutor {
private final String databaseType;
......
......@@ -18,12 +18,8 @@
package org.apache.shardingsphere.sql.parser.statement.standard;
import lombok.RequiredArgsConstructor;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.apache.shardingsphere.sql.parser.api.SQLParserEngine;
import org.apache.shardingsphere.sql.parser.cache.SQLParsedResultCache;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLVisitorFactory;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLVisitorRule;
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;
......@@ -65,20 +61,14 @@ public final class StandardSQLStatementParserEngine implements SQLStatementParse
private SQLStatement parse0(final String sql, final boolean useCache) {
if (!useCache) {
return parseSQLStatement(sql);
return SQLParserEngine.parse(databaseTypeName, sql, false, "STATEMENT");
}
Optional<SQLStatement> statement = cache.get(sql);
if (statement.isPresent()) {
return statement.get();
}
SQLStatement result = parseSQLStatement(sql);
SQLStatement result = SQLParserEngine.parse(databaseTypeName, sql, false, "STATEMENT");
cache.put(sql, result);
return result;
}
private SQLStatement parseSQLStatement(final String sql) {
ParseTree parseTree = SQLParserEngineFactory.getSQLParserEngine(databaseTypeName).parse(sql, false);
ParseTreeVisitor<SQLStatement> visitor = SQLVisitorFactory.newInstance(databaseTypeName, "STATEMENT", 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;
import org.apache.shardingsphere.sql.parser.api.parser.SQLParserEngineFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class SQLParserEngineFactoryTest {
@Before
@After
public void reset() throws NoSuchFieldException, IllegalAccessException {
Field field = SQLParserEngineFactory.class.getDeclaredField("ENGINES");
field.setAccessible(true);
((Map) field.get(SQLParserEngineFactory.class)).clear();
}
@Test
public void assertGetSQLParserEngine() {
assertThat(SQLParserEngineFactory.getSQLParserEngine("MySQL"), is(SQLParserEngineFactory.getSQLParserEngine("MySQL")));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册