You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by tu...@apache.org on 2022/12/14 06:37:00 UTC

[shardingsphere] branch master updated: Make ShardingSphereResultSet always tries to transform the target class (#22865)

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

tuichenchuxin 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 3ef93a28ced Make ShardingSphereResultSet always tries to transform the target class (#22865)
3ef93a28ced is described below

commit 3ef93a28ced8424e2c7f9ffef68dcae97e23c62e
Author: Ling Hengqian <li...@outlook.com>
AuthorDate: Wed Dec 14 14:36:52 2022 +0800

    Make ShardingSphereResultSet always tries to transform the target class (#22865)
---
 .../query/impl/driver/jdbc/type/util/ResultSetUtil.java      | 12 +++++++++++-
 .../jdbc/core/resultset/ShardingSphereResultSetTest.java     | 12 ++++++++----
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
index b01cdeb5b43..15e84aecef2 100644
--- a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
+++ b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
@@ -86,7 +86,11 @@ public final class ResultSetUtil {
         if (String.class.equals(convertType)) {
             return value.toString();
         }
-        throw new SQLFeatureNotSupportedException("getObject with type");
+        try {
+            return convertType.cast(value);
+        } catch (ClassCastException cce) {
+            throw new SQLFeatureNotSupportedException("getObject with type");
+        }
     }
     
     private static Object convertURL(final Object value) {
@@ -135,6 +139,9 @@ public final class ResultSetUtil {
         if (Timestamp.class.equals(convertType)) {
             return Timestamp.valueOf(localDateTime);
         }
+        if (String.class.equals(convertType)) {
+            return value.toString();
+        }
         return value;
     }
     
@@ -152,6 +159,9 @@ public final class ResultSetUtil {
         if (OffsetDateTime.class.equals(convertType)) {
             return timestamp.toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime();
         }
+        if (String.class.equals(convertType)) {
+            return value.toString();
+        }
         return value;
     }
     
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetTest.java
index c9a6b48bd19..04e28a7116b 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetTest.java
@@ -42,7 +42,6 @@ import java.sql.Ref;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
-import java.sql.SQLFeatureNotSupportedException;
 import java.sql.SQLXML;
 import java.sql.Time;
 import java.sql.Timestamp;
@@ -55,8 +54,8 @@ import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertFalse;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
@@ -207,7 +206,12 @@ public final class ShardingSphereResultSetTest {
     @Test
     public void assertGetStringWithColumnIndex() throws SQLException {
         when(mergeResultSet.getValue(1, String.class)).thenReturn("value");
+        LocalDateTime tempTime = LocalDateTime.of(2022, 12, 14, 0, 0);
+        when(mergeResultSet.getValue(2, String.class)).thenReturn(tempTime);
+        when(mergeResultSet.getValue(3, String.class)).thenReturn(Timestamp.valueOf(tempTime));
         assertThat(shardingSphereResultSet.getString(1), is("value"));
+        assertThat(shardingSphereResultSet.getString(2), is("2022-12-14T00:00"));
+        assertThat(shardingSphereResultSet.getString(3), is("2022-12-14 00:00:00.0"));
     }
     
     @Test
@@ -616,11 +620,11 @@ public final class ShardingSphereResultSetTest {
         assertThat(shardingSphereResultSet.getObject(1, Clob.class), is(result));
     }
     
-    @Test(expected = SQLFeatureNotSupportedException.class)
+    @Test
     public void assertGetObjectWithRef() throws SQLException {
         Ref result = mock(Ref.class);
         when(mergeResultSet.getValue(1, Ref.class)).thenReturn(result);
-        shardingSphereResultSet.getObject(1, Ref.class);
+        assertThat(shardingSphereResultSet.getObject(1, Ref.class), is(result));
     }
     
     @Test