Author: ekoneil
Date: Mon Mar 21 08:11:11 2005
New Revision: 158478

URL: http://svn.apache.org/viewcvs?view=rev&rev=158478
Log:
Commit this change to the beta source line.  

This is the fix for JIRA 435 and fixes the ResultSetIterator problem with Derby.

BB: self
DRT: Beehive pass


Modified:
    
incubator/beehive/branches/v1/beta/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java

Modified: 
incubator/beehive/branches/v1/beta/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java
URL: 
http://svn.apache.org/viewcvs/incubator/beehive/branches/v1/beta/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java?view=diff&r1=158477&r2=158478
==============================================================================
--- 
incubator/beehive/branches/v1/beta/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java
 (original)
+++ 
incubator/beehive/branches/v1/beta/netui/src/util/org/apache/beehive/netui/util/iterator/ResultSetIterator.java
 Mon Mar 21 08:11:11 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