This is an automated email from the ASF dual-hosted git repository.
soulasuna 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 5a16456896c Add UnsupportedDataTypeConversionException (#20441)
5a16456896c is described below
commit 5a16456896c97273eb5f8230e9c8f9803e71331b
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Aug 23 12:53:07 2022 +0800
Add UnsupportedDataTypeConversionException (#20441)
* Add UnsupportedDataTypeConversionException
* Fix test case
---
.../user-manual/error-code/sql-error-code.cn.md | 1 +
.../user-manual/error-code/sql-error-code.en.md | 1 +
.../impl/driver/jdbc/type/util/ResultSetUtil.java | 12 ++++----
.../UnsupportedDataTypeConversionException.java | 35 ++++++++++++++++++++++
.../driver/jdbc/type/util/ResultSetUtilTest.java | 13 ++++----
5 files changed, 48 insertions(+), 14 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 60b667a72fc..cc91cae9d1e 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
@@ -12,6 +12,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 08000 | 10001 | The URL \`%s\` is not recognized, please refer to
the pattern \`%s\` |
| 42000 | 10002 | Can not support 3-tier structure for actual data
node \`%s\` with JDBC \`%s\` |
| 42000 | 10003 | Unsupported SQL node conversion for SQL statement
\`%s\` |
+| HY004 | 10004 | Unsupported conversion data type \`%s\` for value
\`%s\` |
| 42000 | 11000 | You have an error in your SQL syntax: %s |
| 42000 | 11001 | configuration error |
| 42000 | 11002 | Resource does not exist |
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 bfcb7340732..04faecbdeed 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
@@ -12,6 +12,7 @@ SQL error codes provide by standard `SQL State`, `Vendor
Code` and `Reason`, whi
| 08000 | 10001 | The URL \`%s\` is not recognized, please refer to
the pattern \`%s\` |
| 42000 | 10002 | Can not support 3-tier structure for actual data
node \`%s\` with JDBC \`%s\` |
| 42000 | 10003 | Unsupported SQL node conversion for SQL statement
\`%s\` |
+| HY004 | 10004 | Unsupported conversion data type \`%s\` for value
\`%s\` |
| 42000 | 11000 | You have an error in your SQL syntax: %s |
| 42000 | 11001 | configuration error |
| 42000 | 11002 | Resource does not exist |
diff --git
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
index e983e0cb911..39708380ecd 100644
---
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
+++
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
@@ -22,7 +22,6 @@ import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import java.math.BigDecimal;
import java.math.RoundingMode;
@@ -90,11 +89,10 @@ public final class ResultSetUtil {
}
private static Object convertURL(final Object value) {
- String val = value.toString();
try {
- return new URL(val);
+ return new URL(value.toString());
} catch (final MalformedURLException ex) {
- throw new ShardingSphereException("Unsupported data type: URL for
value %s", val);
+ throw new UnsupportedDataTypeConversionException(URL.class, value);
}
}
@@ -117,7 +115,7 @@ public final class ResultSetUtil {
BigDecimal bigDecimal = new BigDecimal(value.toString());
return adjustBigDecimalResult(bigDecimal, needScale, scale);
}
- throw new ShardingSphereException("Unsupported data type: BigDecimal
for value %s", value);
+ throw new UnsupportedDataTypeConversionException(BigDecimal.class,
value);
}
private static BigDecimal adjustBigDecimalResult(final BigDecimal value,
final boolean needScale, final int scale) {
@@ -210,7 +208,7 @@ public final class ResultSetUtil {
case "java.lang.String":
return value.toString();
default:
- throw new ShardingSphereException("Unsupported data type: %s
for value %s", convertType, value);
+ throw new UnsupportedDataTypeConversionException(convertType,
value);
}
}
@@ -226,7 +224,7 @@ public final class ResultSetUtil {
case "java.lang.String":
return date.toString();
default:
- throw new ShardingSphereException("Unsupported date type: %s
for value %s", convertType, value);
+ throw new UnsupportedDataTypeConversionException(convertType,
value);
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/UnsupportedDataTypeConversionException.java
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/UnsupportedDataTypeConversionException.java
new file mode 100644
index 00000000000..ab2dffcbe29
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/UnsupportedDataTypeConversionException.java
@@ -0,0 +1,35 @@
+/*
+ * 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.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.util;
+
+import
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
+import
org.apache.shardingsphere.infra.util.exception.sql.sqlstate.XOpenSQLState;
+
+import java.util.Objects;
+
+/**
+ * Unsupported data type conversion exception.
+ */
+public final class UnsupportedDataTypeConversionException extends
ShardingSphereSQLException {
+
+ private static final long serialVersionUID = 4808672149254705863L;
+
+ public UnsupportedDataTypeConversionException(final Class<?> convertType,
final Object value) {
+ super(XOpenSQLState.INVALID_DATA_TYPE, 10004, "Unsupported conversion
data type `%s` for value `%s`", convertType.getName(), Objects.toString(value));
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtilTest.java
b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtilTest.java
index 2f2b2b24bcf..806b9fd8291 100644
---
a/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtilTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtilTest.java
@@ -21,7 +21,6 @@ import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;
import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.junit.Test;
import java.math.BigDecimal;
@@ -93,7 +92,7 @@ public final class ResultSetUtilTest {
assertThat(ResultSetUtil.convertValue(1, Float.class),
is(Float.valueOf("1")));
}
- @Test(expected = ShardingSphereException.class)
+ @Test(expected = UnsupportedDataTypeConversionException.class)
public void assertConvertNumberValueError() throws SQLException {
ResultSetUtil.convertValue(1, Date.class);
}
@@ -144,14 +143,14 @@ public final class ResultSetUtilTest {
@SneakyThrows(MalformedURLException.class)
@Test
public void assertConvertURLValue() throws SQLException {
- String urlString = "http://apache.org";
+ String urlString = "https://shardingsphere.apache.org/";
URL url = (URL) ResultSetUtil.convertValue(urlString, URL.class);
assertThat(url, is(new URL(urlString)));
}
- @Test(expected = ShardingSphereException.class)
+ @Test(expected = UnsupportedDataTypeConversionException.class)
public void assertConvertURLValueError() throws SQLException {
- String urlString = "no-exist:apache.org";
+ String urlString = "no-exist:shardingsphere.apache.org/";
ResultSetUtil.convertValue(urlString, URL.class);
}
@@ -173,12 +172,12 @@ public final class ResultSetUtilTest {
assertThat(bigDecimal, is(BigDecimal.valueOf(12.24)));
}
- @Test(expected = ShardingSphereException.class)
+ @Test(expected = UnsupportedDataTypeConversionException.class)
public void assertConvertBigDecimalValueError() {
ResultSetUtil.convertBigDecimalValue(new Date(), true, 2);
}
- @Test(expected = ShardingSphereException.class)
+ @Test(expected = UnsupportedDataTypeConversionException.class)
public void assertConvertDateValueError() throws SQLException {
ResultSetUtil.convertValue(new Date(), int.class);
}