Class ResultIterator
- java.lang.Object
-
- com.feedzai.commons.sql.abstraction.dml.result.ResultIterator
-
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
DB2ResultIterator,H2ResultIterator,MySqlResultIterator,OracleResultIterator,PostgreSqlResultIterator,SqlServerResultIterator
public abstract class ResultIterator extends Object implements AutoCloseable
The abstract result iterator. Extending classes will create the specificResultColumnimplementation given the engine in place.- Since:
- 2.0.0
- Author:
- Rui Vilao (rui.vilao@feedzai.com)
-
-
Constructor Summary
Constructors Constructor Description ResultIterator(PreparedStatement statement)Creates a new instance ofResultIteratorforPreparedStatement.ResultIterator(Statement statement, String sql)Creates a new instance ofResultIteratorfor regularStatement.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleancancel()Attempts to cancel the current query.voidclose()abstract ResultColumncreateResultColumn(String name, Object value)Creates aResultColumnfor the given engine in place.List<String>getColumnNames()Retrieves the column names of the iterator.intgetCurrentRowCount()Retrieves the number of rows processed by the iterator so far.protected QueryExceptionHandlergetQueryExceptionHandler()Gets the instance ofQueryExceptionHandlerto be used in disambiguating SQL exceptions.booleanisClosed()Checks if this result iterator is closed.Map<String,ResultColumn>next()Retrieves the next row in the result set.ResultColumn[]nextResult()Retrieves the values of the next row in the result set as an array of objects.
-
-
-
Constructor Detail
-
ResultIterator
public ResultIterator(Statement statement, String sql) throws DatabaseEngineException
Creates a new instance ofResultIteratorfor regularStatement.- Parameters:
statement- The statement.sql- The SQL sentence.- Throws:
DatabaseEngineException- If a database access error occurs.
-
ResultIterator
public ResultIterator(PreparedStatement statement) throws DatabaseEngineException
Creates a new instance ofResultIteratorforPreparedStatement.- Parameters:
statement- The prepared statement.- Throws:
DatabaseEngineException- If a database access error occurs.
-
-
Method Detail
-
getCurrentRowCount
public int getCurrentRowCount()
Retrieves the number of rows processed by the iterator so far. If the iteration hasn't started, this method returns 0.- Returns:
- The number of rows processed by the iterator so far or 0 if the iteration hasn't started.
-
next
public Map<String,ResultColumn> next() throws DatabaseEngineException
Retrieves the next row in the result set.This method also closes the result set upon the last call on the result set.
If the statement in place is not a
PreparedStatementit also closes the statement.If an exception is thrown the calling thread is responsible for repeating the action in place.
- Returns:
- The result row.
- Throws:
DatabaseEngineException- If a database access error occurs.
-
nextResult
public ResultColumn[] nextResult() throws DatabaseEngineException
Retrieves the values of the next row in the result set as an array of objects.This method provides an optimized version of the
next()method with less overhead.This method also closes the result set upon the last call on the result set.
If the statement in place is not a
PreparedStatementit also closes the statement.If an exception is thrown the calling thread is responsible for repeating the action in place.
- Returns:
- The result row.
- Throws:
DatabaseEngineException- If a database access error occurs.- See Also:
for the names of the columns of this method return.
-
isClosed
public boolean isClosed()
Checks if this result iterator is closed.- Returns:
trueif the result set is closed,falseotherwise.
-
getColumnNames
public List<String> getColumnNames()
Retrieves the column names of the iterator.- Returns:
- the column names of the iterator.
-
cancel
public boolean cancel()
Attempts to cancel the current query. This relies on the JDBC driver supportingStatement.cancel(), which is not guaranteed on all drivers. A possible use case for this method is to implement a timeout; If that's the case, see alsoAbstractDatabaseEngine.iterator(String, int, int)for an alternative way to accomplish this. This method is expected to be invoked from a thread distinct of the one that is reading from the result set.- Returns:
trueif the query was cancelled,falseotherwise.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
createResultColumn
public abstract ResultColumn createResultColumn(String name, Object value)
Creates aResultColumnfor the given engine in place.- Parameters:
name- The name of the column.value- The value on the column.- Returns:
- A specific result column given the implementation.
-
getQueryExceptionHandler
protected QueryExceptionHandler getQueryExceptionHandler()
Gets the instance ofQueryExceptionHandlerto be used in disambiguating SQL exceptions.- Returns:
- the
QueryExceptionHandler. - Since:
- 2.5.1
-
-