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 0000000000000000000000000000000000000000..12a16475ecb91f9465edce6cb9d503ea975eecef --- /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 70e22030ebd1c1c47249b69cca8a253fcf86a8b7..0000000000000000000000000000000000000000 --- 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 b488522d4f8d025694ad3c5398a877e66f705718..0000000000000000000000000000000000000000 --- 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 c30f3dfa186b30c70fc721cc76b8abac7e81f4cd..0000000000000000000000000000000000000000 --- 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 9a159fc7f50a4f653e223b85cec8ba95927c1973..35f2ce7a2ff92c5b4ae3f461c8534d5a45f8d5c7 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 664ad424dbe2fdee93a6194c25e3396adc008b13..2a019c6c08dc5424aefb72df9a2613157798d7cc 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 1887e21216c02bd39b63d7d4a458e2622be9cef2..0000000000000000000000000000000000000000 --- 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"))); - } -}