org.openanzo.jdbc.utils
Class ResultSetIterator<E>

java.lang.Object
  extended by org.openanzo.jdbc.utils.ResultSetIterator<E>
Type Parameters:
E - type of object that iterator will return
All Implemented Interfaces:
java.lang.Iterable<E>, java.util.Iterator<E>, ClosableIterator<E>

public class ResultSetIterator<E>
extends java.lang.Object
implements ClosableIterator<E>

Implements iterator support over query result sets, which JDBC was lacking. By default each row in a result set is transformed to a Map where key is the column name and object is the value. However, any iterator Transform may be supplied to instances of this class which will convert rows in a result set to whatever Java type is most convenient. Example transforms are included. see: toMapTransform and toArrayTransform.

Author:
Joe Betz

Field Summary
static org.openanzo.jdbc.utils.ResultSetIterator.ToArrayTransform toArrayTransform
          Transform results into an array
static org.openanzo.jdbc.utils.ResultSetIterator.ToMapTransform toMapTransform
          Transform results into a map
 
Constructor Summary
ResultSetIterator(java.sql.ResultSet rs)
          Construct a new iterator over the results in the provided ResultSet.
ResultSetIterator(java.sql.ResultSet rs, java.sql.PreparedStatement ps, PreparedStatementCache cache, Transformer<E> transformer)
          Construct a new iterator over the results in the provided ResultSet.
ResultSetIterator(java.sql.ResultSet rs, java.sql.PreparedStatement ps, Transformer<E> transformer)
          Construct a new iterator over the results in the provided ResultSet.
ResultSetIterator(java.sql.ResultSet rs, Transformer<E> transformer)
          Construct a new iterator over the results in the provided ResultSet.
 
Method Summary
 void close()
          Close the iterator
 boolean hasNext()
           
 java.util.Iterator<E> iterator()
           
 E next()
           
 void remove()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

toArrayTransform

public static final org.openanzo.jdbc.utils.ResultSetIterator.ToArrayTransform toArrayTransform
Transform results into an array


toMapTransform

public static final org.openanzo.jdbc.utils.ResultSetIterator.ToMapTransform toMapTransform
Transform results into a map

Constructor Detail

ResultSetIterator

public ResultSetIterator(java.sql.ResultSet rs)
Construct a new iterator over the results in the provided ResultSet.

Parameters:
rs - A JDBC ResultSet to iterate over.

ResultSetIterator

public ResultSetIterator(java.sql.ResultSet rs,
                         Transformer<E> transformer)
Construct a new iterator over the results in the provided ResultSet. Also, the iterator transforms the rows of the result set using the supplied transformer.

Parameters:
rs - A JDBC ResultSet to iterate over.
transformer - A Transformer for converting rows in the ResultSet to some type.

ResultSetIterator

public ResultSetIterator(java.sql.ResultSet rs,
                         java.sql.PreparedStatement ps,
                         Transformer<E> transformer)
Construct a new iterator over the results in the provided ResultSet. In addition to closing the ResultSet when this iterator is closed, it will close the provided PreparedStatement. Also, the iterator transforms the rows of the result set using the supplied transformer.

Parameters:
rs - A JDBC ResultSet to iterate over.
ps - PreparedStatement to close when iterator closes.
transformer - A Transformer for converting rows in the ResultSet to some type.

ResultSetIterator

public ResultSetIterator(java.sql.ResultSet rs,
                         java.sql.PreparedStatement ps,
                         PreparedStatementCache cache,
                         Transformer<E> transformer)
Construct a new iterator over the results in the provided ResultSet. In addition to closing the ResultSet when this iterator is closed, the provided PreparedStatement will be returned to the provided PreparedStatementCache. Also, the iterator transforms the rows of the result set using the supplied transformer.

Parameters:
rs - A JDBC ResultSet to iterate over.
ps - PreparedStatement to return to the given PreparedStatementCache when the iterator closes.
cache - A cache to return the given PreparedStatement to.
transformer - A Transformer for converting rows in the ResultSet to some type.
Method Detail

hasNext

public boolean hasNext()
Specified by:
hasNext in interface java.util.Iterator<E>
See Also:
Iterator.hasNext()

next

public E next()
Specified by:
next in interface java.util.Iterator<E>
See Also:
Iterator.next()

remove

public void remove()
Specified by:
remove in interface java.util.Iterator<E>
See Also:
Iterator.remove()

close

public void close()
Description copied from interface: ClosableIterator
Close the iterator

Specified by:
close in interface ClosableIterator<E>
See Also:
ClosableIterator.close()

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>


Copyright © 2007 Cambridge Semantics Inc.. All Rights Reserved.