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 f998702d507 Add SQLExceptionTransformEngine (#20624)
f998702d507 is described below

commit f998702d5072b3e6e7d68e153f545838401445db
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Aug 29 10:23:06 2022 +0800

    Add SQLExceptionTransformEngine (#20624)
    
    * Add ExceptionTransformEngine
    
    * Add ExceptionTransformEngine
---
 .../dialect/SQLExceptionTransformEngine.java       | 35 ++++++++--------------
 .../frontend/mysql/err/MySQLErrPacketFactory.java  | 18 ++---------
 2 files changed, 15 insertions(+), 38 deletions(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
 
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/SQLExceptionTransformEngine.java
similarity index 51%
copy from 
shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
copy to 
shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/SQLExceptionTransformEngine.java
index 50bff2fad9a..823b5e5b3bf 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
+++ 
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/SQLExceptionTransformEngine.java
@@ -15,51 +15,40 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.proxy.frontend.mysql.err;
+package org.apache.shardingsphere.dialect;
 
-import com.google.common.base.Strings;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import 
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
 import org.apache.shardingsphere.dialect.exception.SQLDialectException;
 import 
org.apache.shardingsphere.dialect.mapper.SQLDialectExceptionMapperFactory;
-import org.apache.shardingsphere.dialect.mysql.vendor.MySQLVendorError;
 import 
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
 import org.apache.shardingsphere.infra.util.exception.sql.UnknownSQLException;
 
 import java.sql.SQLException;
 
 /**
- * ERR packet factory for MySQL.
+ * SQL Exception transform engine.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class MySQLErrPacketFactory {
+public final class SQLExceptionTransformEngine {
     
     /**
-     * Create new instance of MySQL ERR packet.
-     *
+     * To SQL exception.
+     * 
      * @param cause cause
-     * @return created instance
+     * @param databaseType database type
+     * @return SQL exception
      */
-    public static MySQLErrPacket newInstance(final Exception cause) {
+    public static SQLException toSQLException(final Exception cause, final 
String databaseType) {
         if (cause instanceof SQLException) {
-            SQLException sqlException = (SQLException) cause;
-            return null == sqlException.getSQLState() ? new MySQLErrPacket(1, 
MySQLVendorError.ER_INTERNAL_ERROR, getErrorMessage(sqlException)) : 
createErrPacket(sqlException);
+            return (SQLException) cause;
         }
         if (cause instanceof ShardingSphereSQLException) {
-            return createErrPacket(((ShardingSphereSQLException) 
cause).toSQLException());
+            return ((ShardingSphereSQLException) cause).toSQLException();
         }
         if (cause instanceof SQLDialectException) {
-            return 
createErrPacket(SQLDialectExceptionMapperFactory.getInstance("MySQL").convert((SQLDialectException)
 cause));
+            return 
SQLDialectExceptionMapperFactory.getInstance(databaseType).convert((SQLDialectException)
 cause);
         }
-        return createErrPacket(new 
UnknownSQLException(cause).toSQLException());
-    }
-    
-    private static String getErrorMessage(final SQLException cause) {
-        return null == cause.getNextException() || 
!Strings.isNullOrEmpty(cause.getMessage()) ? cause.getMessage() : 
cause.getNextException().getMessage();
-    }
-    
-    private static MySQLErrPacket createErrPacket(final SQLException cause) {
-        return new MySQLErrPacket(1, cause.getErrorCode(), 
cause.getSQLState(), cause.getMessage());
+        return new UnknownSQLException(cause).toSQLException();
     }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
index 50bff2fad9a..c70af18c859 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
@@ -21,11 +21,8 @@ import com.google.common.base.Strings;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import 
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
-import org.apache.shardingsphere.dialect.exception.SQLDialectException;
-import 
org.apache.shardingsphere.dialect.mapper.SQLDialectExceptionMapperFactory;
+import org.apache.shardingsphere.dialect.SQLExceptionTransformEngine;
 import org.apache.shardingsphere.dialect.mysql.vendor.MySQLVendorError;
-import 
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
-import org.apache.shardingsphere.infra.util.exception.sql.UnknownSQLException;
 
 import java.sql.SQLException;
 
@@ -42,17 +39,8 @@ public final class MySQLErrPacketFactory {
      * @return created instance
      */
     public static MySQLErrPacket newInstance(final Exception cause) {
-        if (cause instanceof SQLException) {
-            SQLException sqlException = (SQLException) cause;
-            return null == sqlException.getSQLState() ? new MySQLErrPacket(1, 
MySQLVendorError.ER_INTERNAL_ERROR, getErrorMessage(sqlException)) : 
createErrPacket(sqlException);
-        }
-        if (cause instanceof ShardingSphereSQLException) {
-            return createErrPacket(((ShardingSphereSQLException) 
cause).toSQLException());
-        }
-        if (cause instanceof SQLDialectException) {
-            return 
createErrPacket(SQLDialectExceptionMapperFactory.getInstance("MySQL").convert((SQLDialectException)
 cause));
-        }
-        return createErrPacket(new 
UnknownSQLException(cause).toSQLException());
+        SQLException sqlException = 
SQLExceptionTransformEngine.toSQLException(cause, "MySQL");
+        return null == sqlException.getSQLState() ? new MySQLErrPacket(1, 
MySQLVendorError.ER_INTERNAL_ERROR, getErrorMessage(sqlException)) : 
createErrPacket(sqlException);
     }
     
     private static String getErrorMessage(final SQLException cause) {

Reply via email to