org.skife.jdbi.v2
Class Query<ResultType>

java.lang.Object
  extended by org.skife.jdbi.v2.SQLStatement<Query<ResultType>>
      extended by org.skife.jdbi.v2.Query<ResultType>
All Implemented Interfaces:
Iterable<ResultType>

public class Query<ResultType>
extends SQLStatement<Query<ResultType>>
implements Iterable<ResultType>

Statement prviding convenience result handling for SQL queries.


Method Summary
 Query<ResultType> fetchForward()
          Specify that the fetch order should be forward, uses the underlying Statement.setFetchDirection(int)
 Query<ResultType> fetchReverse()
          Specify that the fetch order should be reversed, uses the underlying Statement.setFetchDirection(int)
 ResultType first()
          Executes the select.
<AccumulatorType>
AccumulatorType
fold(AccumulatorType accumulator, Folder<AccumulatorType> folder)
          Deprecated. Use fold(Object, Folder2)
<AccumulatorType>
AccumulatorType
fold(AccumulatorType accumulator, Folder2<AccumulatorType> folder)
          Used to execute the query and traverse the result set with a accumulator.
 ResultIterator<ResultType> iterator()
          Obtain a forward-only result set iterator.
 List<ResultType> list()
          Executes the select

Will eagerly load all results

 List<ResultType> list(int maxRows)
          Executes the select

Will eagerly load all results up to a maximum of maxRows

<Type> Query<Type>
map(Class<Type> resultType)
          Provide basic JavaBean mapping capabilities.
<T> Query<T>
map(ResultSetMapper<T> mapper)
           
<T> Query<T>
mapTo(Class<T> resultType)
          Makes use of registered mappers to map the result set to the desired type.
 Query<ResultType> setFetchSize(int i)
          Specify the fetch size for the query.
 Query<ResultType> setMaxFieldSize(int i)
          Specify the maimum field size in the result set.
 Query<ResultType> setMaxRows(int i)
          Specify the maimum number of rows the query is to return.
 
Methods inherited from class org.skife.jdbi.v2.SQLStatement
addStatementCustomizer, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bindASCIIStream, bindASCIIStream, bindAsInt, bindAsInt, bindAsInt, bindAsInt, bindBinaryStream, bindBinaryStream, bindBySqlType, bindBySqlType, bindFromMap, bindFromProperties, bindNamedArgumentFinder, bindNull, bindNull, define, define, getConnection, getContext, getLog, getParameters, getParams, getRewriter, getSql, getStatementBuilder, getStatementCustomizers, getStatementLocator, getTimingCollector, internalExecute, setQueryTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

list

public List<ResultType> list()
Executes the select

Will eagerly load all results

Throws:
UnableToCreateStatementException - if there is an error creating the statement
UnableToExecuteStatementException - if there is an error executing the statement
ResultSetException - if there is an error dealing with the result set

list

public List<ResultType> list(int maxRows)
Executes the select

Will eagerly load all results up to a maximum of maxRows

Parameters:
maxRows - The maximum number of results to include in the result, any rows in the result set beyond this number will be ignored.
Throws:
UnableToCreateStatementException - if there is an error creating the statement
UnableToExecuteStatementException - if there is an error executing the statement
ResultSetException - if there is an error dealing with the result set

fold

public <AccumulatorType> AccumulatorType fold(AccumulatorType accumulator,
                                              Folder2<AccumulatorType> folder)
Used to execute the query and traverse the result set with a accumulator. Folding over the result involves invoking a callback for each row, passing into the callback the return value from the previous function invocation.

Parameters:
accumulator - The initial accumulator value
folder - Defines the function which will fold over the result set.
Returns:
The return value from the last invocation of Folder.fold(Object, java.sql.ResultSet)
See Also:
Folder

fold

public <AccumulatorType> AccumulatorType fold(AccumulatorType accumulator,
                                              Folder<AccumulatorType> folder)
Deprecated. Use fold(Object, Folder2)

Used to execute the query and traverse the result set with a accumulator. Folding over the result involves invoking a callback for each row, passing into the callback the return value from the previous function invocation.

Parameters:
accumulator - The initial accumulator value
folder - Defines the function which will fold over the result set.
Returns:
The return value from the last invocation of Folder.fold(Object, java.sql.ResultSet)
See Also:
Folder

iterator

public ResultIterator<ResultType> iterator()
Obtain a forward-only result set iterator. Note that you must explicitely close the iterator to close the underlying resources.

Specified by:
iterator in interface Iterable<ResultType>

first

public ResultType first()
Executes the select.

Specifies a maximum of one result on the JDBC statement, and map that one result as the return value, or return null if there is nothing in the results

Returns:
first result, mapped, or null if there is no first result

map

public <Type> Query<Type> map(Class<Type> resultType)
Provide basic JavaBean mapping capabilities. Will instantiate an instance of resultType for each row and set the JavaBean properties which match fields in the result set.

Parameters:
resultType - JavaBean class to map result set fields into the properties of, by name
Returns:
a Query which provides the bean property mapping

mapTo

public <T> Query<T> mapTo(Class<T> resultType)
Makes use of registered mappers to map the result set to the desired type.

Parameters:
resultType - the type to map the query results to
Returns:
a new query instance which will map to the desired type
See Also:
DBI.registerMapper(org.skife.jdbi.v2.tweak.ResultSetMapper), DBI.registerMapper(ResultSetMapperFactory), Handle.registerMapper(ResultSetMapperFactory), Handle.registerMapper(org.skife.jdbi.v2.tweak.ResultSetMapper)

map

public <T> Query<T> map(ResultSetMapper<T> mapper)

setFetchSize

public Query<ResultType> setFetchSize(int i)
Specify the fetch size for the query. This should cause the results to be fetched from the underlying RDBMS in groups of rows equal to the number passed. This is useful for doing chunked streaming of results when exhausting memory could be a problem.

Parameters:
i - the number of rows to fetch in a bunch
Returns:
the modified query

setMaxRows

public Query<ResultType> setMaxRows(int i)
Specify the maimum number of rows the query is to return. This uses the underlying JDBC Statement.setMaxRows(int)}.

Parameters:
i - maximum number of rows to return
Returns:
modified query

setMaxFieldSize

public Query<ResultType> setMaxFieldSize(int i)
Specify the maimum field size in the result set. This uses the underlying JDBC Statement.setMaxFieldSize(int)

Parameters:
i - maximum field size
Returns:
modified query

fetchReverse

public Query<ResultType> fetchReverse()
Specify that the fetch order should be reversed, uses the underlying Statement.setFetchDirection(int)

Returns:
the modified query

fetchForward

public Query<ResultType> fetchForward()
Specify that the fetch order should be forward, uses the underlying Statement.setFetchDirection(int)

Returns:
the modified query


Copyright © 2011. All Rights Reserved.