From 1bc809c7f069165d1aa521fa00edf012f38f4df2 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 29 Oct 2020 13:16:47 +0800 Subject: [PATCH] Determine SQL parser API (#7954) * Refactor SQLParserEngine * Determine SQL parser API --- .../sql/parser/api/SQLParserEngine.java | 80 +++++++++++++++++++ .../api/parser/SQLParserEngineFactory.java | 53 ------------ .../parser/api/visitor/SQLVisitorEngine.java | 48 ----------- .../api/visitor/SQLVisitorEngineFactory.java | 40 ---------- .../parser/SQLParserExecutor.java} | 9 +-- .../StandardSQLStatementParserEngine.java | 16 +--- .../api/SQLParserEngineFactoryTest.java | 45 ----------- 7 files changed, 87 insertions(+), 204 deletions(-) create mode 100644 shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngine.java delete mode 100644 shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/parser/SQLParserEngineFactory.java delete mode 100644 shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/visitor/SQLVisitorEngine.java delete mode 100644 shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/visitor/SQLVisitorEngineFactory.java rename shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/{api/parser/SQLParserEngine.java => core/parser/SQLParserExecutor.java} (92%) delete mode 100644 shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngineFactoryTest.java diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngine.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngine.java new file mode 100644 index 0000000000..12a16475ec --- /dev/null +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngine.java @@ -0,0 +1,80 @@ +/* + * 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 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. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class SQLParserEngine { + + private static final Map ENGINES = new ConcurrentHashMap<>(); + + /** + * Parse SQL. + * + * @param databaseType database type + * @param sql SQL to be parsed + * @param useCache whether use cache + * @param visitorType SQL visitor type + * @param type of SQL visitor result + * @return SQL visitor result + */ + public static T parse(final String databaseType, final String sql, final boolean useCache, final String visitorType) { + ParseTree parseTree = parse(databaseType, sql, useCache); + ParseTreeVisitor 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); + } + synchronized (ENGINES) { + if (ENGINES.containsKey(databaseType)) { + return ENGINES.get(databaseType); + } + SQLParserExecutor result = new SQLParserExecutor(databaseType); + ENGINES.put(databaseType, result); + return result; + } + } +} diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/parser/SQLParserEngineFactory.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/parser/SQLParserEngineFactory.java deleted file mode 100644 index 70e22030eb..0000000000 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/parser/SQLParserEngineFactory.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.parser; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * SQL parser engine factory. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class SQLParserEngineFactory { - - private static final Map ENGINES = new ConcurrentHashMap<>(); - - /** - * Get SQL parser engine. - * - * @param databaseType database type - * @return SQL parser engine - */ - public static SQLParserEngine getSQLParserEngine(final String databaseType) { - if (ENGINES.containsKey(databaseType)) { - return ENGINES.get(databaseType); - } - synchronized (ENGINES) { - if (ENGINES.containsKey(databaseType)) { - return ENGINES.get(databaseType); - } - SQLParserEngine result = new SQLParserEngine(databaseType); - ENGINES.put(databaseType, result); - return result; - } - } -} diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/visitor/SQLVisitorEngine.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/visitor/SQLVisitorEngine.java deleted file mode 100644 index b488522d4f..0000000000 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/visitor/SQLVisitorEngine.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 type of return value - */ -@RequiredArgsConstructor -public final class SQLVisitorEngine { - - 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 visitor = SQLVisitorFactory.newInstance(databaseType, visitorType, SQLVisitorRule.valueOf(parseTree.getClass())); - return parseTree.accept(visitor); - } -} diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/visitor/SQLVisitorEngineFactory.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/visitor/SQLVisitorEngineFactory.java deleted file mode 100644 index c30f3dfa18..0000000000 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/visitor/SQLVisitorEngineFactory.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 type of visitor result - * @return SQL visitor engine - */ - public static SQLVisitorEngine getSQLVisitorEngine(final String databaseType, final String visitorType) { - return new SQLVisitorEngine<>(databaseType, visitorType); - } -} diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/parser/SQLParserEngine.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/parser/SQLParserExecutor.java similarity index 92% rename from shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/parser/SQLParserEngine.java rename to shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/parser/SQLParserExecutor.java index 9a159fc7f5..35f2ce7a2f 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/api/parser/SQLParserEngine.java +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/parser/SQLParserExecutor.java @@ -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; diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/statement/standard/StandardSQLStatementParserEngine.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/statement/standard/StandardSQLStatementParserEngine.java index 664ad424db..2a019c6c08 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/statement/standard/StandardSQLStatementParserEngine.java +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/statement/standard/StandardSQLStatementParserEngine.java @@ -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 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 visitor = SQLVisitorFactory.newInstance(databaseTypeName, "STATEMENT", SQLVisitorRule.valueOf(parseTree.getClass())); - return parseTree.accept(visitor); - } } diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngineFactoryTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngineFactoryTest.java deleted file mode 100644 index 1887e21216..0000000000 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngineFactoryTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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"))); - } -} -- GitLab