You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/11/24 02:16:07 UTC

[GitHub] [shardingsphere] sandynz commented on pull request #21992: Improve AbstractMemoryQueryResult.wasNull

sandynz commented on PR #21992:
URL: https://github.com/apache/shardingsphere/pull/21992#issuecomment-1325861721

   > When call `next` method,
   > 
   > ```java
   > public final boolean next() {
   >     if (rows.hasNext()) {
   >         currentRow = rows.next();
   >         rowCount--;
   >         return true;
   >     }
   >    // when rows.hasNext() is false, this line will set currentRow = null and `wasNull()` method will return true before,but return false now.
   >     currentRow = null;
   >     return false;
   > }
   > ```
   > 
   > when rows.hasNext() is false, this line will set currentRow = null and `wasNull()` method will return true before,but return false now.
   > 
   > wasNull method before:
   > 
   > ```java
   >   public final boolean wasNull() {
   >      // return true;
   >       return null == currentRow;
   >   }
   > ```
   > 
   > wasNull method now:
   > 
   > ```java
   >   public final boolean wasNull() {
   >           // return false;
   >       return wasNull;
   >   }
   > ```
   
   Hi @FlyingZC , some investigation as following.
   
   The definition in JDBC specification of `wasNull`:
   ```
       /**
        * Reports whether
        * the last column read had a value of SQL <code>NULL</code>.
        * Note that you must first call one of the getter methods
        * on a column to try to read its value and then call
        * the method <code>wasNull</code> to see if the value read was
        * SQL <code>NULL</code>.
        *
        * @return <code>true</code> if the last column value read was SQL
        *         <code>NULL</code> and <code>false</code> otherwise
        * @exception SQLException if a database access error occurs or this method is
        *            called on a closed result set
        */
       boolean wasNull() throws SQLException;
   ```
   
   MySQL JDBC implementation of `wasNull` (mysql-connector-java:5.1.47):
   ResultSetImpl.java
   ```
       /** Did the previous value retrieval find a NULL? */
       protected boolean wasNullFlag = false;
   
       public boolean wasNull() throws SQLException {
           return this.wasNullFlag;
       }
   ```
   The default value of `wasNullFlag` is `false`.
   
   PostgreSQL JDBC implementation of `wasNull` (postgresql:42.4.1):
   PgResultSet.java
   ```
     /**
      * True if the last obtained column value was SQL NULL as specified by {@link #wasNull}. The value
      * is always updated by the {@link #getRawValue} method.
      */
     protected boolean wasNullFlag = false;
   
     public boolean wasNull() throws SQLException {
       checkClosed();
       return wasNullFlag;
     }
   ```
   The default value of `wasNullFlag` is `false`.
   
   So when `ResultSet.hasNext()` return `false` (Invoke ResultSet.getXxx() method will throw exception, `wasNullFlag` won't be set with value), `wasNull` should not be invoked, else it might return old value of `wasNullFlag` or default value `false` (if there's no any record in ResultSet).
   
   But I didn't do real test on JDBC driver.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org