This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new f4b386a  Create ShardingSphereSQLParserEngine and factory (#6553)
f4b386a is described below

commit f4b386a03f9e19d4791251a00a4308ec593d9770
Author: Juan Pan(Trista) <[email protected]>
AuthorDate: Fri Jul 31 17:12:05 2020 +0800

    Create ShardingSphereSQLParserEngine and factory (#6553)
---
 .../shardingsphere-rdl-parser-engine/pom.xml       |  2 +-
 .../engine/ShardingSphereSQLParserEngine.java      | 51 ++++++++++++++
 .../ShardingSphereSQLParserEngineFactory.java      | 41 +++++++++++
 .../parser/engine/engine/RDLSQLParserEngine.java   | 77 +++++++++++++++++++++
 .../engine/executor/RDLSQLParserExecutor.java      | 80 ++++++++++++++++++++++
 .../shardingsphere-rdl-parser-sql/pom.xml          |  5 +-
 .../parser/sql/parser/ShardingSphereParser.java    | 39 +++++++++++
 7 files changed, 291 insertions(+), 4 deletions(-)

diff --git a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/pom.xml 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/pom.xml
index b0f757c..f34ef37 100644
--- a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/pom.xml
+++ b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/pom.xml
@@ -38,7 +38,7 @@
         </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
-            <artifactId>shardingsphere-rdl-parser-statement</artifactId>
+            <artifactId>shardingsphere-rdl-parser-sql</artifactId>
             <version>${project.version}</version>
         </dependency>
     </dependencies>
diff --git 
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/ShardingSphereSQLParserEngine.java
 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/ShardingSphereSQLParserEngine.java
new file mode 100644
index 0000000..2e2fa72
--- /dev/null
+++ 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/ShardingSphereSQLParserEngine.java
@@ -0,0 +1,51 @@
+/*
+ * 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.rdl.parser.engine;
+
+import org.apache.shardingsphere.sql.parser.QuerySQLParserEngine;
+import org.apache.shardingsphere.rdl.parser.engine.engine.RDLSQLParserEngine;
+import org.apache.shardingsphere.sql.parser.SQLParserEngine;
+import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
+
+/**
+ * Sharding sphere SQL parser engine.
+ */
+public final class ShardingSphereSQLParserEngine implements 
QuerySQLParserEngine {
+    
+    private final RDLSQLParserEngine rdlsqlParserEngine;
+    
+    private final SQLParserEngine sqlParserEngine;
+    
+    public ShardingSphereSQLParserEngine(final SQLParserEngine 
sqlParserEngine) {
+        rdlsqlParserEngine = new RDLSQLParserEngine();
+        this.sqlParserEngine = sqlParserEngine;
+    }
+    
+    @Override
+    public SQLStatement parse(final String sql, final boolean useCache) {
+        SQLStatement result;
+        try {
+            result = sqlParserEngine.parse(sql, useCache);
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            result = rdlsqlParserEngine.parse(sql, useCache);
+        }
+        return result;
+    }
+}
diff --git 
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/ShardingSphereSQLParserEngineFactory.java
 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/ShardingSphereSQLParserEngineFactory.java
new file mode 100644
index 0000000..532a359
--- /dev/null
+++ 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/ShardingSphereSQLParserEngineFactory.java
@@ -0,0 +1,41 @@
+/*
+ * 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.rdl.parser.engine;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.sql.parser.SQLParserEngine;
+import org.apache.shardingsphere.sql.parser.SQLParserEngineFactory;
+
+/**
+ * SQL parser engine factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShardingSphereSQLParserEngineFactory {
+    
+    /**
+     * Get SQL parser engine.
+     *
+     * @param databaseTypeName name of database type
+     * @return SQL parser engine
+     */
+    public static ShardingSphereSQLParserEngine getSQLParserEngine(final 
String databaseTypeName) {
+        SQLParserEngine sqlParserEngine = 
SQLParserEngineFactory.getSQLParserEngine(databaseTypeName);
+        return new ShardingSphereSQLParserEngine(sqlParserEngine);
+    }
+}
diff --git 
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/engine/RDLSQLParserEngine.java
 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/engine/RDLSQLParserEngine.java
new file mode 100644
index 0000000..12dabf7
--- /dev/null
+++ 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/engine/RDLSQLParserEngine.java
@@ -0,0 +1,77 @@
+/*
+ * 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.rdl.parser.engine.engine;
+
+import lombok.RequiredArgsConstructor;
+import org.antlr.v4.runtime.tree.ParseTree;
+import 
org.apache.shardingsphere.rdl.parser.engine.executor.RDLSQLParserExecutor;
+import org.apache.shardingsphere.rdl.parser.sql.visitor.ShardingSphereVisitor;
+import org.apache.shardingsphere.sql.parser.QuerySQLParserEngine;
+import org.apache.shardingsphere.sql.parser.cache.SQLParseResultCache;
+import org.apache.shardingsphere.sql.parser.hook.ParsingHookRegistry;
+import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
+
+import java.util.Optional;
+
+/**
+ * RDL SQL parser engine.
+ */
+@RequiredArgsConstructor
+public final class RDLSQLParserEngine implements QuerySQLParserEngine {
+    
+    private final SQLParseResultCache cache = new SQLParseResultCache();
+    
+    private final ParsingHookRegistry parsingHookRegistry = 
ParsingHookRegistry.getInstance();
+    
+    // TODO check skywalking plugin
+    /*
+     * To make sure SkyWalking will be available at the next release of 
ShardingSphere,
+     * a new plugin should be provided to SkyWalking project if this API 
changed.
+     *
+     * @see <a 
href="https://github.com/apache/skywalking/blob/master/docs/en/guides/Java-Plugin-Development-Guide.md#user-content-plugin-development-guide";>Plugin
 Development Guide</a>
+     */
+    @Override
+    public SQLStatement parse(final String sql, final boolean useCache) {
+        parsingHookRegistry.start(sql);
+        try {
+            SQLStatement result = parse0(sql, useCache);
+            parsingHookRegistry.finishSuccess(result);
+            return result;
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            parsingHookRegistry.finishFailure(ex);
+            throw ex;
+        }
+    }
+    
+    private SQLStatement parse0(final String sql, final boolean useCache) {
+        if (useCache) {
+            Optional<SQLStatement> cachedSQLStatement = 
cache.getSQLStatement(sql);
+            if (cachedSQLStatement.isPresent()) {
+                return cachedSQLStatement.get();
+            }
+        }
+        ParseTree parseTree = new 
RDLSQLParserExecutor(sql).execute().getRootNode();
+        SQLStatement result = (SQLStatement) new 
ShardingSphereVisitor().visit(parseTree);
+        if (useCache) {
+            cache.put(sql, result);
+        }
+        return result;
+    }
+}
diff --git 
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/executor/RDLSQLParserExecutor.java
 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/executor/RDLSQLParserExecutor.java
new file mode 100644
index 0000000..afc6a17
--- /dev/null
+++ 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-engine/src/main/java/org/apache/shardingsphere/rdl/parser/engine/executor/RDLSQLParserExecutor.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.rdl.parser.engine.executor;
+
+import lombok.RequiredArgsConstructor;
+import org.antlr.v4.runtime.BailErrorStrategy;
+import org.antlr.v4.runtime.CodePointBuffer;
+import org.antlr.v4.runtime.CodePointCharStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.DefaultErrorStrategy;
+import org.antlr.v4.runtime.Lexer;
+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.rdl.parser.sql.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.exception.SQLParsingException;
+
+import java.nio.CharBuffer;
+
+/**
+ * RDL SQL parser executor.
+ */
+@RequiredArgsConstructor
+public final class RDLSQLParserExecutor {
+    
+    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 = createSQLParser();
+        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 DefaultErrorStrategy());
+            ((Parser) 
sqlParser).getInterpreter().setPredictionMode(PredictionMode.LL);
+            return (ParseASTNode) sqlParser.parse();
+        }
+    }
+    
+    private SQLParser createSQLParser() {
+        CodePointBuffer buffer = 
CodePointBuffer.withChars(CharBuffer.wrap(sql.toCharArray()));
+        CodePointCharStream codePointCharStream = 
CodePointCharStream.fromBuffer(buffer);
+        Lexer lexer = new ShardingSphereStatementLexer(codePointCharStream);
+        return new ShardingSphereParser(new CommonTokenStream(lexer));
+    }
+}
diff --git a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/pom.xml 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/pom.xml
index fc26b03..8f53c48 100644
--- a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/pom.xml
+++ b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/pom.xml
@@ -38,15 +38,14 @@
         </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
-            <artifactId>shardingsphere-sql-parser-spi</artifactId>
+            <artifactId>shardingsphere-rdl-parser-statement</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
-            <artifactId>shardingsphere-rdl-parser-statement</artifactId>
+            <artifactId>shardingsphere-sql-parser-engine</artifactId>
             <version>${project.version}</version>
         </dependency>
-
     </dependencies>
 
     <build>
diff --git 
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/src/main/java/org/apache/shardingsphere/rdl/parser/sql/parser/ShardingSphereParser.java
 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/src/main/java/org/apache/shardingsphere/rdl/parser/sql/parser/ShardingSphereParser.java
new file mode 100644
index 0000000..a499295
--- /dev/null
+++ 
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/src/main/java/org/apache/shardingsphere/rdl/parser/sql/parser/ShardingSphereParser.java
@@ -0,0 +1,39 @@
+/*
+ * 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.rdl.parser.sql.parser;
+
+import org.antlr.v4.runtime.TokenStream;
+import 
org.apache.shardingsphere.rdl.parser.sql.autogen.ShardingSphereStatementParser;
+import org.apache.shardingsphere.sql.parser.api.ASTNode;
+import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
+import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
+
+/**
+ * SQL parser for MySQL.
+ */
+public final class ShardingSphereParser extends ShardingSphereStatementParser 
implements SQLParser {
+    
+    public ShardingSphereParser(final TokenStream input) {
+        super(input);
+    }
+    
+    @Override
+    public ASTNode parse() {
+        return new ParseASTNode(execute());
+    }
+}

Reply via email to