You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/11/07 13:49:36 UTC

[shardingsphere] branch master updated: Improve AbstractMemoryQueryResult.wasNull (#21992)

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

zhangliang 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 281aa7d9494 Improve AbstractMemoryQueryResult.wasNull (#21992)
281aa7d9494 is described below

commit 281aa7d9494969d6ff3672b01ddc310e911f1854
Author: Hongsheng Zhong <zh...@apache.org>
AuthorDate: Mon Nov 7 21:49:26 2022 +0800

    Improve AbstractMemoryQueryResult.wasNull (#21992)
---
 .../type/memory/AbstractMemoryQueryResult.java     | 16 +++++++++++----
 .../type/memory/JDBCMemoryQueryResultTest.java     | 23 ++++++++++++++++++----
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/type/memory/AbstractMemoryQueryResult.java b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/type/memory/AbstractMemoryQueryResult.java
index 500c1ba5846..64d84fe9789 100644
--- a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/type/memory/AbstractMemoryQueryResult.java
+++ b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/type/memory/AbstractMemoryQueryResult.java
@@ -47,6 +47,8 @@ public abstract class AbstractMemoryQueryResult implements QueryResult {
     @Getter
     private long rowCount;
     
+    private boolean wasNull;
+    
     protected AbstractMemoryQueryResult(final QueryResultMetaData metaData, final Collection<MemoryQueryResultDataRow> rows) {
         this.metaData = metaData;
         this.rows = rows.iterator();
@@ -66,17 +68,23 @@ public abstract class AbstractMemoryQueryResult implements QueryResult {
     
     @Override
     public final Object getValue(final int columnIndex, final Class<?> type) {
-        return currentRow.getValue().get(columnIndex - 1);
+        Object result = currentRow.getValue().get(columnIndex - 1);
+        wasNull = null == result;
+        return result;
     }
     
     @Override
     public final Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) {
-        return currentRow.getValue().get(columnIndex - 1);
+        Object result = currentRow.getValue().get(columnIndex - 1);
+        wasNull = null == result;
+        return result;
     }
     
     @Override
     public final InputStream getInputStream(final int columnIndex, final String type) {
-        return getInputStream(currentRow.getValue().get(columnIndex - 1));
+        Object value = currentRow.getValue().get(columnIndex - 1);
+        wasNull = null == value;
+        return getInputStream(value);
     }
     
     @SneakyThrows(IOException.class)
@@ -91,7 +99,7 @@ public abstract class AbstractMemoryQueryResult implements QueryResult {
     
     @Override
     public final boolean wasNull() {
-        return null == currentRow;
+        return wasNull;
     }
     
     @Override
diff --git a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/memory/JDBCMemoryQueryResultTest.java b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/memory/JDBCMemoryQueryResultTest.java
index 5b278e7edb6..0cc248233d9 100644
--- a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/memory/JDBCMemoryQueryResultTest.java
+++ b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/memory/JDBCMemoryQueryResultTest.java
@@ -356,14 +356,29 @@ public final class JDBCMemoryQueryResultTest {
     }
     
     @Test
-    public void assertWasNull() throws SQLException {
-        JDBCMemoryQueryResult queryResult = new JDBCMemoryQueryResult(mockResultSet(), databaseType);
-        queryResult.next();
-        assertFalse(queryResult.wasNull());
+    public void assertWasNullTrue() throws SQLException {
+        JDBCMemoryQueryResult queryResult = new JDBCMemoryQueryResult(mockResultSetForWasNull(true), databaseType);
         queryResult.next();
+        queryResult.getValue(1, int.class);
         assertTrue(queryResult.wasNull());
     }
     
+    @Test
+    public void assertWasNullFalse() throws SQLException {
+        JDBCMemoryQueryResult queryResult = new JDBCMemoryQueryResult(mockResultSetForWasNull(false), databaseType);
+        queryResult.next();
+        queryResult.getValue(1, int.class);
+        assertFalse(queryResult.wasNull());
+    }
+    
+    private ResultSet mockResultSetForWasNull(final boolean wasNull) throws SQLException {
+        ResultSet result = getMockedResultSet(Types.INTEGER);
+        when(result.getInt(1)).thenReturn(0);
+        when(result.getMetaData().isSigned(1)).thenReturn(true);
+        when(result.wasNull()).thenReturn(wasNull);
+        return result;
+    }
+    
     @Test
     public void assertGetRowCount() throws SQLException {
         JDBCMemoryQueryResult queryResult = new JDBCMemoryQueryResult(mockResultSet(), databaseType);