de.mathema.pride
Class ResultIterator

java.lang.Object
  extended by de.mathema.pride.ResultIterator

public class ResultIterator
extends java.lang.Object

Helper class to iterate thru a list of results from a query. ResultIterators are initialized be the Database class and provide their results step-by-step in the same Entity object. A typical usage may look like this:

    try {
       Person p = new Person();
       ResultIterator iter = p.queryAll();
       do {
          System.println(p.getName());
       } while(iter.next());
    }
    catch(NoResultsException nrx) {}
 

Author:
Jan Lessner

Field Summary
static int COLUMN_STARTINDEX
           
protected  java.sql.Connection connection
           
protected  Database db
           
protected  java.lang.Object obj
           
protected  RecordDescriptor red
           
protected  java.sql.ResultSet results
           
static java.lang.String REVISION_ID
           
protected  java.sql.Statement statement
           
 
Constructor Summary
ResultIterator(java.sql.Statement statement, java.sql.ResultSet rs, Database db, java.sql.Connection con)
           
ResultIterator(java.sql.Statement statement, java.sql.ResultSet rs, java.lang.Object obj, RecordDescriptor red, Database db, java.sql.Connection con)
          Creates a new ResultIterator from an query.
 
Method Summary
 void close()
          Closes the iteration by closing the result set, the statement and the database connection passed in the constructor.
 java.math.BigDecimal getBigDecimal(int index)
           
 java.math.BigDecimal getBigDecimal(java.lang.String column)
           
 java.sql.Date getDate(int index)
           
 int getFetchSize()
          Returns the current fetch size for the underlying ResultSet
 int getInt(int index)
           
 java.lang.Object getObject()
          Returns the object, the ResultIterator writes its data to
 java.sql.ResultSet getResultSet()
          Returns the result set, the iterator is operating on.
 boolean isClosed()
          Returns true if this result iterator has been closed.
 boolean next()
          Fetches the next record from the result set and stores it in the entity object passed in the constructor.
 void setFetchSize(int size)
          Sets the fetch size for the underlying ResultSet
 java.lang.Object[] toArray()
          Like the function above but returns all data without limitation.
 java.lang.Object[] toArray(long maxResults)
          Like toArrayList(long) but returns the data as an array.
 java.util.ArrayList toArrayList()
          Like the function above but returns all data without size limitation.
 java.util.ArrayList toArrayList(long maxResults)
          Iterates through the complete result set and returns the data in an ArrayList.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COLUMN_STARTINDEX

public static final int COLUMN_STARTINDEX
See Also:
Constant Field Values

statement

protected java.sql.Statement statement

connection

protected java.sql.Connection connection

results

protected java.sql.ResultSet results

obj

protected java.lang.Object obj

red

protected RecordDescriptor red

db

protected Database db

REVISION_ID

public static final java.lang.String REVISION_ID
See Also:
Constant Field Values
Constructor Detail

ResultIterator

public ResultIterator(java.sql.Statement statement,
                      java.sql.ResultSet rs,
                      java.lang.Object obj,
                      RecordDescriptor red,
                      Database db,
                      java.sql.Connection con)
Creates a new ResultIterator from an query.


ResultIterator

public ResultIterator(java.sql.Statement statement,
                      java.sql.ResultSet rs,
                      Database db,
                      java.sql.Connection con)
Method Detail

getResultSet

public java.sql.ResultSet getResultSet()
Returns the result set, the iterator is operating on. This may be required to pass query results to standard reporting engines etc.


close

public void close()
           throws java.sql.SQLException
Closes the iteration by closing the result set, the statement and the database connection passed in the constructor. This function is only required when aborting an iteration. If the complete result set is expected, the iterator is closed automatically.

Throws:
java.sql.SQLException

isClosed

public boolean isClosed()
Returns true if this result iterator has been closed.


next

public boolean next()
             throws java.sql.SQLException
Fetches the next record from the result set and stores it in the entity object passed in the constructor. If there is no further data available, the iterator is closed.

Returns:
false, if there is no more data available.
Throws:
java.sql.SQLException - if accessing the query results fails
{@link - NoResultsException} if the iterator is already closed

getObject

public java.lang.Object getObject()
Returns the object, the ResultIterator writes its data to


toArrayList

public java.util.ArrayList toArrayList(long maxResults)
                                throws java.sql.SQLException
Iterates through the complete result set and returns the data in an ArrayList. The list contains clones of the iterator's operation object for each iteration step.

Parameters:
maxResults - The maximum number of results to collect. If maxResults is -1, the function returns all data from the underlying result set
Returns:
The assembled ArrayList
Throws:
java.lang.CloneNotSupportedException - if the operation object has no public clone method available
java.sql.SQLException - if there occurs a databaes error during iteration

toArrayList

public java.util.ArrayList toArrayList()
                                throws java.sql.SQLException
Like the function above but returns all data without size limitation. This function must be used with great care since the number of elements in a result set can be very large.

Throws:
java.sql.SQLException

toArray

public java.lang.Object[] toArray(long maxResults)
                           throws java.sql.SQLException
Like toArrayList(long) but returns the data as an array. The runtime type of the array's elements is the one of the iterator's operation object.

Throws:
java.sql.SQLException

toArray

public java.lang.Object[] toArray()
                           throws java.sql.SQLException
Like the function above but returns all data without limitation. This function must be used with great care since the number of elements in a result set can be very large.

Throws:
java.sql.SQLException

getFetchSize

public int getFetchSize()
                 throws java.sql.SQLException
Returns the current fetch size for the underlying ResultSet

Throws:
java.sql.SQLException

setFetchSize

public void setFetchSize(int size)
                  throws java.sql.SQLException
Sets the fetch size for the underlying ResultSet

Throws:
java.sql.SQLException

getInt

public int getInt(int index)
           throws java.sql.SQLException
Throws:
java.sql.SQLException

getBigDecimal

public java.math.BigDecimal getBigDecimal(int index)
                                   throws java.sql.SQLException
Throws:
java.sql.SQLException

getBigDecimal

public java.math.BigDecimal getBigDecimal(java.lang.String column)
                                   throws java.sql.SQLException
Throws:
java.sql.SQLException

getDate

public java.sql.Date getDate(int index)
                      throws java.sql.SQLException
Throws:
java.sql.SQLException