Author: ekoneil
Date: Tue Mar 15 12:10:55 2005
New Revision: 157584

URL: http://svn.apache.org/viewcvs?view=rev&rev=157584
Log:
JIRA bug fix

BEEHIVE-435      ResultSetIterator isLast method throws exception on Derby

The problem here is that isLast can't be called with all ResultSet types.  
Ironically, this is the same call that commons-dbutil uses, but I've switched 
off of it here and use "next" exclusively to determine if there's another row 
in the ResultSet.

BB: self
BVT: NetUI pass


Modified:
    
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/api/DataGridConfig.java
    
incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/api/DataGridConfig.java
URL: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/api/DataGridConfig.java?view=diff&r1=157583&r2=157584
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/api/DataGridConfig.java
 (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/api/DataGridConfig.java
 Tue Mar 15 12:10:55 2005
@@ -18,7 +18,6 @@
 package org.apache.beehive.netui.databinding.datagrid.api;
 
 import java.util.List;
-
 import javax.servlet.ServletRequest;
 
 import org.apache.beehive.netui.databinding.datagrid.api.sort.Sort;
@@ -28,7 +27,6 @@
 import org.apache.beehive.netui.databinding.datagrid.api.pager.PagerModel;
 import 
org.apache.beehive.netui.databinding.datagrid.api.rendering.PagerRenderer;
 import org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel;
-import org.apache.beehive.netui.databinding.datagrid.api.DataGridState;
 
 /**
  *

Modified: 
incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java
URL: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java?view=diff&r1=157583&r2=157584
==============================================================================
--- 
incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java
 (original)
+++ 
incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java
 Tue Mar 15 12:10:55 2005
@@ -20,7 +20,6 @@
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 import java.util.SortedMap;
@@ -37,6 +36,7 @@
 
     private static final Logger LOGGER = 
Logger.getInstance(ResultSetIterator.class);
 
+    private boolean _primed = false;
     private String[] _columnNames = null;
     private ResultSet _rs = null;
 
@@ -58,9 +58,9 @@
                 LOGGER.trace("column[" + i + "]: " + _columnNames[i-1]);
             }
         } catch(SQLException sql) {
-            LOGGER.error("An exception occurred reading ResultSetMetaData from 
a ResultSet.  Cause: " + sql, sql);
-
-            throw new IllegalStateException("An exception occurred reading 
ResultSetMetaData from a ResultSet.  Cause: " + sql, sql);
+            String msg = "An exception occurred reading ResultSetMetaData from 
a ResultSet.  Cause: " + sql;
+            LOGGER.error(msg, sql);
+            throw new IllegalStateException(msg, sql);
         }
     }
 
@@ -71,12 +71,17 @@
         if(_rs == null)
             return false;
 
+        if(_primed)
+            return true;
+
         try {
-            return !_rs.isLast();
+            _primed = _rs.next();
+            return _primed;
         }
         catch(SQLException sql) {
-            LOGGER.error("An exception occurred reading from the Iterator.  
Cause: " + sql, sql);
-            throw new RuntimeException("An exception occurred reading from the 
Iterator.  Cause: " + sql, sql);
+            String msg = "An exception occurred reading from the Iterator.  
Cause: " + sql;
+            LOGGER.error(msg, sql);
+            throw new IllegalStateException(msg, sql);
         }
     }
 
@@ -88,29 +93,37 @@
             throw new 
NoSuchElementException(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
 
         try {
-            if(_rs.isLast())
-                throw new 
NoSuchElementException(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
-
-            /* not currently on the last row, should be safe to advance */
-            boolean hasNext = _rs.next();
-            assert hasNext : "Ran off the end of a ResultSet";
-            SortedMap map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
-            for(int i = 0; i < _columnNames.length; i++) {
-                Object value = _rs.getObject(i+1);
-                if(_rs.wasNull())
-                    value = null;
-                map.put(_columnNames[i], value);
+            if(!_primed) {
+                _primed = _rs.next();
+                if(!_primed) {
+                    throw new 
NoSuchElementException(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
+                }
             }
 
-            return map;
+            _primed = false;
+            return convertRow(_rs, _columnNames);
         }
         catch(SQLException sql) {
-            throw new RuntimeException("An exception occurred reading from the 
Iterator.  Cause: " + sql, sql);
+            String msg = "An exception occurred reading from the Iterator.  
Cause: " + sql;
+            LOGGER.error(msg, sql);
+            throw new IllegalStateException(msg, sql);
         }
     }
 
     public void remove() {
         throw new 
UnsupportedOperationException(Bundle.getErrorString("IteratorFactory_Iterator_removeUnsupported",
                                                 new 
Object[]{this.getClass().getName()}));
+    }
+
+    private static final Object convertRow(final ResultSet resultSet, final 
String[] columnNames)
+        throws SQLException {
+        SortedMap map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
+        for(int i = 0; i < columnNames.length; i++) {
+            Object value = resultSet.getObject(i+1);
+            if(resultSet.wasNull())
+                value = null;
+            map.put(columnNames[i], value);
+        }
+        return map;
     }
 }


Reply via email to