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

yx9o 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 be5cfc2510a Refactor SQLParsingException (#20183)
be5cfc2510a is described below

commit be5cfc2510a4b285845949b822cc31420d943ffe
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Aug 15 12:49:17 2022 +0800

    Refactor SQLParsingException (#20183)
---
 .../FeaturedDistSQLStatementParserEngine.java      |  4 +--
 .../kernel/KernelDistSQLStatementParserEngine.java |  2 +-
 .../UtilityDistSQLStatementParserEngine.java       |  4 +--
 .../shardingsphere/error/SQLExceptionHandler.java  |  9 +++----
 .../handler/distsql/rul/sql/FormatSQLHandler.java  |  2 +-
 .../distsql/rul/sql/ParseDistSQLHandler.java       |  2 +-
 .../mysql/err/MySQLErrPacketFactoryTest.java       | 15 ++++++++---
 .../core/database/parser/SQLParserExecutor.java    |  4 +--
 .../core/database/visitor/SQLVisitorFactory.java   |  2 +-
 .../sql/parser/exception/SQLParsingException.java  | 18 ++++++++-----
 .../parser/exception/SQLParsingExceptionTest.java  | 31 ----------------------
 11 files changed, 37 insertions(+), 56 deletions(-)

diff --git 
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
 
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
index c39da7c9170..95473d245d8 100644
--- 
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
+++ 
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
@@ -52,7 +52,7 @@ public final class FeaturedDistSQLStatementParserEngine {
             } catch (final ParseCancellationException | SQLParsingException 
ignored) {
             }
         }
-        throw new SQLParsingException("You have an error in your SQL syntax.");
+        throw new SQLParsingException();
     }
     
     @SneakyThrows(ReflectiveOperationException.class)
@@ -60,7 +60,7 @@ public final class FeaturedDistSQLStatementParserEngine {
     private SQLStatement getSQLStatement(final String sql, final String 
featureType, final ParseASTNode parseASTNode) {
         SQLVisitor visitor = 
FeaturedDistSQLStatementParserFacadeFactory.getInstance(featureType).getVisitorClass().getDeclaredConstructor().newInstance();
         if (parseASTNode.getRootNode() instanceof ErrorNode) {
-            throw new SQLParsingException("Unsupported SQL of `%s`", sql);
+            throw new SQLParsingException(sql);
         }
         return (SQLStatement) ((ParseTreeVisitor) 
visitor).visit(parseASTNode.getRootNode());
     }
diff --git 
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementParserEngine.java
 
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementParserEngine.java
index 999bd5de7b7..3eedc6f2390 100644
--- 
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementParserEngine.java
+++ 
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementParserEngine.java
@@ -42,7 +42,7 @@ public final class KernelDistSQLStatementParserEngine {
     
     private SQLStatement getSQLStatement(final String sql, final ParseASTNode 
parseASTNode) {
         if (parseASTNode.getRootNode() instanceof ErrorNode) {
-            throw new SQLParsingException("Unsupported SQL of `%s`", sql);
+            throw new SQLParsingException(sql);
         }
         return (SQLStatement) (new 
KernelDistSQLStatementVisitor()).visit(parseASTNode.getRootNode());
     }
diff --git 
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/utility/UtilityDistSQLStatementParserEngine.java
 
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/utility/UtilityDistSQLStatementParserEngine.java
index 593f7a169d5..3c80b4826a8 100644
--- 
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/utility/UtilityDistSQLStatementParserEngine.java
+++ 
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/utility/UtilityDistSQLStatementParserEngine.java
@@ -45,13 +45,13 @@ public final class UtilityDistSQLStatementParserEngine {
         try {
             return SQLParserFactory.newInstance(sql, 
UtilityDistSQLLexer.class, UtilityDistSQLParser.class).parse();
         } catch (final ParseCancellationException | SQLParsingException 
ignored) {
-            throw new SQLParsingException("You have an error in your SQL 
syntax.");
+            throw new SQLParsingException();
         }
     }
     
     private SQLStatement getSQLStatement(final String sql, final ParseASTNode 
parseASTNode) {
         if (parseASTNode.getRootNode() instanceof ErrorNode) {
-            throw new SQLParsingException("Unsupported SQL of `%s`", sql);
+            throw new SQLParsingException(sql);
         }
         return (SQLStatement) (new 
UtilityDistSQLStatementVisitor()).visit(parseASTNode.getRootNode());
     }
diff --git 
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/SQLExceptionHandler.java
 
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/SQLExceptionHandler.java
index cddb8a928a9..9182477f40d 100644
--- 
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/SQLExceptionHandler.java
+++ 
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/SQLExceptionHandler.java
@@ -20,13 +20,12 @@ package org.apache.shardingsphere.error;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.error.dialect.SQLDialectException;
-import 
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
 import org.apache.shardingsphere.error.mapper.SQLDialectExceptionMapperFactory;
-import 
org.apache.shardingsphere.infra.util.exception.sql.vendor.ShardingSphereVendorError;
-import org.apache.shardingsphere.infra.util.exception.sql.vendor.VendorError;
 import 
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
 import 
org.apache.shardingsphere.infra.util.exception.ShardingSphereInsideException;
-import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
+import 
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
+import 
org.apache.shardingsphere.infra.util.exception.sql.vendor.ShardingSphereVendorError;
+import org.apache.shardingsphere.infra.util.exception.sql.vendor.VendorError;
 
 import java.sql.SQLException;
 
@@ -50,7 +49,7 @@ public final class SQLExceptionHandler {
         if (insideException instanceof ShardingSphereSQLException) {
             return ((ShardingSphereSQLException) 
insideException).toSQLException();
         }
-        if (insideException instanceof ShardingSphereConfigurationException || 
insideException instanceof SQLParsingException) {
+        if (insideException instanceof ShardingSphereConfigurationException) {
             return toSQLException(ShardingSphereVendorError.UNSUPPORTED_SQL, 
insideException);
         }
         return toSQLException(ShardingSphereVendorError.UNKNOWN_EXCEPTION, 
insideException);
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/FormatSQLHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/FormatSQLHandler.java
index 06381c9ead3..f5ef875eda7 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/FormatSQLHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/FormatSQLHandler.java
@@ -61,7 +61,7 @@ public final class FormatSQLHandler extends 
SQLRULBackendHandler<FormatStatement
         try {
             return new SQLParserEngine(databaseType, new CacheOption(1, 
1L)).parse(sql, false);
         } catch (final SQLParsingException ex) {
-            throw new SQLParsingException("You have a syntax error in your 
formatted statement");
+            throw new SQLParsingException();
         }
     }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/ParseDistSQLHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/ParseDistSQLHandler.java
index 546fcea7d90..f7c7710ecaf 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/ParseDistSQLHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/ParseDistSQLHandler.java
@@ -56,7 +56,7 @@ public final class ParseDistSQLHandler extends 
SQLRULBackendHandler<ParseStateme
         try {
             return 
sqlParserRule.getSQLParserEngine(databaseType).parse(getSqlStatement().getSql(),
 false);
         } catch (final SQLParsingException ex) {
-            throw new SQLParsingException("You have a syntax error in your 
parsed statement");
+            throw new SQLParsingException();
         }
     }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactoryTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactoryTest.java
index 2fab376f48b..cd00fe1bcd4 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactoryTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactoryTest.java
@@ -148,19 +148,26 @@ public final class MySQLErrPacketFactoryTest {
     
     @Test
     public void assertNewInstanceWithShardingSphereConfigurationException() {
-        assertCommonException(MySQLErrPacketFactory.newInstance(new 
ShardingSphereConfigurationException("No reason")));
+        
assertShardingSphereConfigurationException(MySQLErrPacketFactory.newInstance(new
 ShardingSphereConfigurationException("No reason")));
+    }
+    
+    private void assertShardingSphereConfigurationException(final 
MySQLErrPacket actual) {
+        assertThat(actual.getSequenceId(), is(1));
+        assertThat(actual.getErrorCode(), is(1235));
+        assertThat(actual.getSqlState(), is("42000"));
+        assertThat(actual.getErrorMessage(), is("Unsupported SQL: No reason"));
     }
     
     @Test
     public void assertNewInstanceWithSQLParsingException() {
-        assertCommonException(MySQLErrPacketFactory.newInstance(new 
SQLParsingException("No reason")));
+        assertSQLParsingException(MySQLErrPacketFactory.newInstance(new 
SQLParsingException()));
     }
     
-    private void assertCommonException(final MySQLErrPacket actual) {
+    private void assertSQLParsingException(final MySQLErrPacket actual) {
         assertThat(actual.getSequenceId(), is(1));
         assertThat(actual.getErrorCode(), is(1235));
         assertThat(actual.getSqlState(), is("42000"));
-        assertThat(actual.getErrorMessage(), is("Unsupported SQL: No reason"));
+        assertThat(actual.getErrorMessage(), is("You have an error in your SQL 
syntax"));
     }
     
     @Test
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/parser/SQLParserExecutor.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/parser/SQLParserExecutor.java
index ce5e4cd67c6..4e81b5c649f 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/parser/SQLParserExecutor.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/parser/SQLParserExecutor.java
@@ -45,7 +45,7 @@ public final class SQLParserExecutor {
     public ParseASTNode parse(final String sql) {
         ParseASTNode result = twoPhaseParse(sql);
         if (result.getRootNode() instanceof ErrorNode) {
-            throw new SQLParsingException("Unsupported SQL of `%s`", sql);
+            throw new SQLParsingException(sql);
         }
         return result;
     }
@@ -62,7 +62,7 @@ public final class SQLParserExecutor {
             try {
                 return (ParseASTNode) sqlParser.parse();
             } catch (final ParseCancellationException e) {
-                throw new SQLParsingException("You have an error in your SQL 
syntax");
+                throw new SQLParsingException();
             }
         }
     }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorFactory.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorFactory.java
index 6bd065cbb99..97fa8e0ad38 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorFactory.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorFactory.java
@@ -65,7 +65,7 @@ public final class SQLVisitorFactory {
             case RL:
                 return (ParseTreeVisitor<T>) 
visitorFacade.getRLVisitorClass().getConstructor(Properties.class).newInstance(props);
             default:
-                throw new SQLParsingException("Can not support SQL statement 
type: `%s`", type);
+                throw new SQLParsingException(type);
         }
     }
 }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingException.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingException.java
index bd3fde0ea8a..868215dfc96 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingException.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingException.java
@@ -17,20 +17,26 @@
 
 package org.apache.shardingsphere.sql.parser.exception;
 
-import 
org.apache.shardingsphere.infra.util.exception.ShardingSphereInsideException;
+import 
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
+import 
org.apache.shardingsphere.infra.util.exception.sql.sqlstate.XOpenSQLState;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatementType;
 
 /**
  * Throw exception when SQL parsing error.
  */
-public final class SQLParsingException extends ShardingSphereInsideException {
+public final class SQLParsingException extends ShardingSphereSQLException {
     
     private static final long serialVersionUID = -6408790652103666096L;
     
-    public SQLParsingException(final String message) {
-        super(message);
+    public SQLParsingException() {
+        super(XOpenSQLState.SYNTAX_ERROR, 1235, "You have an error in your SQL 
syntax");
     }
     
-    public SQLParsingException(final String message, final Object... args) {
-        super(String.format(message, args));
+    public SQLParsingException(final String sql) {
+        super(XOpenSQLState.SYNTAX_ERROR, 1235, "Unsupported SQL: %s", sql);
+    }
+    
+    public SQLParsingException(final SQLStatementType type) {
+        super(XOpenSQLState.SYNTAX_ERROR, 1235, "Unsupported SQL statement 
type: `%s`", type.toString());
     }
 }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingExceptionTest.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingExceptionTest.java
deleted file mode 100644
index 31b846e6e84..00000000000
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingExceptionTest.java
+++ /dev/null
@@ -1,31 +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.exception;
-
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class SQLParsingExceptionTest {
-    
-    @Test
-    public void assertException() {
-        assertThat(new SQLParsingException("Parse error: %s", 
"unsupported").getMessage(), is("Parse error: unsupported"));
-    }
-}

Reply via email to