You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by yx...@apache.org on 2022/09/09 15:48:07 UTC

[shardingsphere] branch master updated: Refactor SQLTranslationException (#20907)

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 61724fe2371 Refactor SQLTranslationException (#20907)
61724fe2371 is described below

commit 61724fe237112207e56d9571e472e18e585be095
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Sep 9 23:47:56 2022 +0800

    Refactor SQLTranslationException (#20907)
---
 .../content/user-manual/error-code/sql-error-code.cn.md      |  4 ++--
 .../content/user-manual/error-code/sql-error-code.en.md      |  4 ++--
 .../dbdiscovery/mysql/exception/DBDiscoverySQLException.java |  4 ++--
 .../encrypt/exception/EncryptSQLException.java               |  4 ++--
 .../shardingsphere/shadow/exception/ShadowSQLException.java  |  4 ++--
 .../sharding/exception/ShardingSQLException.java             |  4 ++--
 .../sqltranslator/exception/SQLTranslationException.java     | 12 +++++++-----
 .../exception/UnsupportedTranslatedDatabaseException.java    |  3 ++-
 .../exception/UnsupportedTranslatedSQLException.java         |  4 +++-
 .../sqltranslator/jooq/JooQDialectRegistry.java              |  5 ++---
 10 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index 9b405e7895a..3025ddcfde1 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -30,8 +30,6 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | 42000     | 11002       | Resource does not exist |
 | 42000     | 11003       | Rule does not exist |
 | HY000     | 11004       | File access failed, reason is: %s |
-| 42000     | 11200       | Can not support database \`%s\` in SQL translation |
-| 42000     | 11201       | Translation error, SQL is: %s |
 | 25000     | 11320       | Switch transaction type failed, please terminate the current transaction |
 | 25000     | 11321       | JDBC does not support operations across multiple logical databases in transaction |
 | 25000     | 11322       | Failed to create \`%s\` XA data source |
@@ -85,6 +83,8 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | 44000     | 23001       | MGR is not in single primary mode in database \`%s\` |
 | 44000     | 23002       | \`%s\` is not in MGR replication group member in database \`%s\` |
 | 44000     | 23003       | Group name in MGR is not same with configured one \`%s\` in database \`%s\` |
+| 42000     | 24000       | Can not support database \`%s\` in SQL translation |
+| 42000     | 24001       | Translation error, SQL is: %s |
 | HY004     | 27000       | Encrypt algorithm \`%s\` initialize failed, reason is: %s |
 | 0A000     | 27001       | The SQL clause \`%s\` is unsupported in encrypt rule |
 | 44000     | 27002       | Altered column \`%s\` must use same encrypt algorithm with previous column \`%s\` in table \`%s\` |
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index cded275a1c6..4dafe07cac7 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -30,8 +30,6 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
 | 42000     | 11002       | Resource does not exist |
 | 42000     | 11003       | Rule does not exist |
 | HY000     | 11004       | File access failed, reason is: %s |
-| 42000     | 11200       | Can not support database \`%s\` in SQL translation |
-| 42000     | 11201       | Translation error, SQL is: %s |
 | 25000     | 11320       | Switch transaction type failed, please terminate the current transaction |
 | 25000     | 11321       | JDBC does not support operations across multiple logical databases in transaction |
 | 25000     | 11322       | Failed to create \`%s\` XA data source |
@@ -85,6 +83,8 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
 | 44000     | 23001       | MGR is not in single primary mode in database \`%s\` |
 | 44000     | 23002       | \`%s\` is not in MGR replication group member in database \`%s\` |
 | 44000     | 23003       | Group name in MGR is not same with configured one \`%s\` in database \`%s\` |
+| 42000     | 24000       | Can not support database \`%s\` in SQL translation |
+| 42000     | 24001       | Translation error, SQL is: %s |
 | HY004     | 27000       | Encrypt algorithm \`%s\` initialize failed, reason is: %s |
 | 0A000     | 27001       | The SQL clause \`%s\` is unsupported in encrypt rule |
 | 44000     | 27002       | Altered column \`%s\` must use same encrypt algorithm with previous column \`%s\` in table \`%s\` |
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/exception/DBDiscoverySQLException.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/exception/DBDiscoverySQLException.java
index b780aada773..c2075e74522 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/exception/DBDiscoverySQLException.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/exception/DBDiscoverySQLException.java
@@ -29,7 +29,7 @@ public abstract class DBDiscoverySQLException extends FeatureSQLException {
     
     private static final int FEATURE_CODE = 3;
     
-    public DBDiscoverySQLException(final SQLState sqlState, final int vendorCode, final String reason, final Object... messageArguments) {
-        super(sqlState, FEATURE_CODE, vendorCode, reason, messageArguments);
+    public DBDiscoverySQLException(final SQLState sqlState, final int errorCode, final String reason, final Object... messageArguments) {
+        super(sqlState, FEATURE_CODE, errorCode, reason, messageArguments);
     }
 }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
index 9ee0b562923..dd825919406 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
@@ -29,7 +29,7 @@ public abstract class EncryptSQLException extends FeatureSQLException {
     
     private static final int FEATURE_CODE = 7;
     
-    public EncryptSQLException(final SQLState sqlState, final int vendorCode, final String reason, final Object... messageArguments) {
-        super(sqlState, FEATURE_CODE, vendorCode, reason, messageArguments);
+    public EncryptSQLException(final SQLState sqlState, final int errorCode, final String reason, final Object... messageArguments) {
+        super(sqlState, FEATURE_CODE, errorCode, reason, messageArguments);
     }
 }
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/exception/ShadowSQLException.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/exception/ShadowSQLException.java
index 731be30f0ff..93445c58c90 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/exception/ShadowSQLException.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/exception/ShadowSQLException.java
@@ -29,7 +29,7 @@ public abstract class ShadowSQLException extends FeatureSQLException {
     
     private static final int FEATURE_CODE = 8;
     
-    public ShadowSQLException(final SQLState sqlState, final int vendorCode, final String reason, final Object... messageArguments) {
-        super(sqlState, FEATURE_CODE, vendorCode, reason, messageArguments);
+    public ShadowSQLException(final SQLState sqlState, final int errorCode, final String reason, final Object... messageArguments) {
+        super(sqlState, FEATURE_CODE, errorCode, reason, messageArguments);
     }
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/ShardingSQLException.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/ShardingSQLException.java
index 9ab5e199c0b..72e076c21da 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/ShardingSQLException.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/ShardingSQLException.java
@@ -29,7 +29,7 @@ public abstract class ShardingSQLException extends FeatureSQLException {
     
     private static final int FEATURE_CODE = 0;
     
-    public ShardingSQLException(final SQLState sqlState, final int vendorCode, final String reason, final Object... messageArguments) {
-        super(sqlState, FEATURE_CODE, vendorCode, reason, messageArguments);
+    public ShardingSQLException(final SQLState sqlState, final int errorCode, final String reason, final Object... messageArguments) {
+        super(sqlState, FEATURE_CODE, errorCode, reason, messageArguments);
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/SQLTranslationException.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/SQLTranslationException.java
index f50a838c22c..f822b362811 100644
--- a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/SQLTranslationException.java
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/SQLTranslationException.java
@@ -17,17 +17,19 @@
 
 package org.apache.shardingsphere.sqltranslator.exception;
 
-import org.apache.shardingsphere.infra.util.exception.external.sql.type.kernel.KernelSQLException;
-import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.SQLState;
+import org.apache.shardingsphere.infra.util.exception.external.sql.type.feature.FeatureSQLException;
 
 /**
  * SQL translation exception.
  */
-public abstract class SQLTranslationException extends KernelSQLException {
+public abstract class SQLTranslationException extends FeatureSQLException {
     
     private static final long serialVersionUID = -7227697280221442049L;
     
-    public SQLTranslationException(final int vendorCode, final String reason, final Object... messageArguments) {
-        super(XOpenSQLState.SYNTAX_ERROR, vendorCode, reason, messageArguments);
+    private static final int FEATURE_CODE = 4;
+    
+    public SQLTranslationException(final SQLState sqlState, final int errorCode, final String reason, final Object... messageArguments) {
+        super(sqlState, FEATURE_CODE, errorCode, reason, messageArguments);
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedDatabaseException.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedDatabaseException.java
index 79dd8e7d892..d37d026a3f5 100644
--- a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedDatabaseException.java
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedDatabaseException.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.sqltranslator.exception;
 
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
 
 /**
  * Unsupported translated database exception.
@@ -27,6 +28,6 @@ public final class UnsupportedTranslatedDatabaseException extends SQLTranslation
     private static final long serialVersionUID = -8311552562051028033L;
     
     public UnsupportedTranslatedDatabaseException(final DatabaseType databaseType) {
-        super(1200, "Can not support database `%s` in SQL translation", databaseType.getType());
+        super(XOpenSQLState.SYNTAX_ERROR, 0, "Can not support database `%s` in SQL translation", databaseType.getType());
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedSQLException.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedSQLException.java
index 224a39b30da..f37cf702dc1 100644
--- a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedSQLException.java
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedSQLException.java
@@ -17,6 +17,8 @@
 
 package org.apache.shardingsphere.sqltranslator.exception;
 
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+
 /**
  * Unsupported translated SQL exception.
  */
@@ -25,6 +27,6 @@ public final class UnsupportedTranslatedSQLException extends SQLTranslationExcep
     private static final long serialVersionUID = -1419778194546662319L;
     
     public UnsupportedTranslatedSQLException(final String sql) {
-        super(1201, "Translation error, SQL is: %s", sql);
+        super(XOpenSQLState.SYNTAX_ERROR, 1, "Translation error, SQL is: %s", sql);
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQDialectRegistry.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQDialectRegistry.java
index 4569114059a..9fa51d25aab 100644
--- a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQDialectRegistry.java
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQDialectRegistry.java
@@ -21,6 +21,7 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeFactory;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.sqltranslator.exception.SQLTranslationException;
 import org.apache.shardingsphere.sqltranslator.exception.UnsupportedTranslatedDatabaseException;
 import org.jooq.SQLDialect;
@@ -54,9 +55,7 @@ public final class JooQDialectRegistry {
      */
     public static SQLDialect getSQLDialect(final DatabaseType databaseType) throws SQLTranslationException {
         SQLDialect result = DATABASE_DIALECT_MAP.get(databaseType);
-        if (null == result) {
-            throw new UnsupportedTranslatedDatabaseException(databaseType);
-        }
+        ShardingSpherePreconditions.checkState(null != result, new UnsupportedTranslatedDatabaseException(databaseType));
         return result;
     }
 }