org.springframework.jdbc.object
Class SqlQuery

java.lang.Object
  extended by org.springframework.jdbc.object.RdbmsOperation
      extended by org.springframework.jdbc.object.SqlOperation
          extended by org.springframework.jdbc.object.SqlQuery
All Implemented Interfaces:
InitializingBean
Direct Known Subclasses:
MappingSqlQueryWithParameters, UpdatableSqlQuery

public abstract class SqlQuery
extends SqlOperation

Reusable object to represent a SQL query. Like all RdbsOperation objects, SqlQuery objects are threadsafe after their initialization is complete. That is, after they are constructed and configured via their setter methods, they can be used safely from multiple threads.

Subclasses must implement the newRowMapper method to provide an object that can extract the results of iterating over the ResultSet.

This class provides a number of public execute methods that are analogous to the different convenient JDO query execute methods. Subclasses can either rely on one of these inherited methods, or can add their own custom execution methods, with meaningful names and typed parameters. Each custom query method will invoke one of this class's untyped query methods.

Author:
Rod Johnson, Jean-Pierre Pawlak

Field Summary
 
Fields inherited from class org.springframework.jdbc.object.RdbmsOperation
logger
 
Constructor Summary
SqlQuery()
          Constructor to allow use as a JavaBean.
SqlQuery(DataSource ds, String sql)
          Convenient constructor with DataSource and SQL string.
 
Method Summary
 List execute()
          Convenient method to execute without parameters nor context.
 List execute(int p1)
          Convenient method to execute with a single int parameter.
 List execute(int p1, int p2)
          Convenient method to execute with two int parameters.
 List execute(int p1, int p2, Map context)
          Convenient method to execute with two int parameters and context.
 List execute(int p1, Map context)
          Convenient method to execute with a single int parameter and context.
 List execute(long p1)
          Convenient method to execute with a single long parameter.
 List execute(long p1, Map context)
          Convenient method to execute with a single long parameter and context.
 List execute(Map context)
          Convenient method to execute without parameters.
 List execute(Object[] params)
          Convenient method to execute without context.
 List execute(Object[] params, Map context)
          Central execution method.
 List execute(String p1)
          Convenient method to execute with a single String parameter.
 List execute(String p1, Map context)
          Convenient method to execute with a single String parameter and context.
 Object findObject(int p1)
          Convenient method to find a single object given a single int parameter.
 Object findObject(int p1, int p2)
          Convenient method to find a single object given two int parameters.
 Object findObject(int p1, int p2, Map context)
          Convenient method to find a single object given two int parameters and a context.
 Object findObject(int p1, Map context)
          Convenient method to find a single object given a single int parameter and a context.
 Object findObject(long p1)
          Convenient method to find a single object given a single long parameter.
 Object findObject(long p1, Map context)
          Convenient method to find a single object given a single long parameter and a context.
 Object findObject(Object[] params)
          Convenient method to find a single object without context.
 Object findObject(Object[] params, Map context)
          Generic object finder method, used by all other findObject methods.
 Object findObject(String p1)
          Convenient method to find a single object given a single String parameter.
 Object findObject(String p1, Map context)
          Convenient method to find a single object given a single String parameter and a context.
 int getRowsExpected()
          Get the number of rows expected.
protected abstract  ResultReader newResultReader(int rowsExpected, Object[] parameters, Map context)
          Subclasses must implement this method to save a List of objects returned by the execute method.
 void setRowsExpected(int rowsExpected)
          Set the number of rows expected.
 
Methods inherited from class org.springframework.jdbc.object.SqlOperation
compileInternal, newPreparedStatementCreator, newPreparedStatementSetter, onCompileInternal
 
Methods inherited from class org.springframework.jdbc.object.RdbmsOperation
afterPropertiesSet, allowsUnusedParameters, checkCompiled, compile, declareParameter, getDeclaredParameters, getGeneratedKeysColumnNames, getJdbcTemplate, getResultSetType, getSql, isCompiled, isReturnGeneratedKeys, isUpdatableResults, setDataSource, setFetchSize, setGeneratedKeysColumnNames, setJdbcTemplate, setMaxRows, setResultSetType, setReturnGeneratedKeys, setSql, setTypes, setUpdatableResults, supportsLobParameters, validateParameters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlQuery

public SqlQuery()
Constructor to allow use as a JavaBean. DataSource and SQL must be supplied before compilation and use.


SqlQuery

public SqlQuery(DataSource ds,
                String sql)
Convenient constructor with DataSource and SQL string.

Parameters:
ds - DataSource to use to get connections
sql - to execute. SQL can also be supplied at runtime by overriding the getSql() method.
Method Detail

setRowsExpected

public void setRowsExpected(int rowsExpected)
Set the number of rows expected. This can be used to ensure efficient storage of results. The default behavior is not to expect any specific number of rows.


getRowsExpected

public int getRowsExpected()
Get the number of rows expected.


execute

public List execute(Object[] params,
                    Map context)
             throws DataAccessException
Central execution method. All execution goes through this method.

Parameters:
params - parameters, similar to JDO query parameters. Primitive parameters must be represented by their Object wrapper type. The ordering of parameters is significant.
context - contextual information passed to the callback mapRow method. The JDBC operation itself doesn't rely on this parameter, but can be useful for creating the objects of the result list.
Returns:
a List of objects, one per row of the ResultSet. Normally all these will be of the same class, although it is possible to use different types.
Throws:
DataAccessException

execute

public List execute(Object[] params)
             throws DataAccessException
Convenient method to execute without context.

Parameters:
params - parameters for the query. Primitive parameters must be represented by their Object wrapper type. The ordering of parameters is significant.
Throws:
DataAccessException

execute

public List execute(Map context)
             throws DataAccessException
Convenient method to execute without parameters.

Parameters:
context - the contextual information for object creation
Throws:
DataAccessException

execute

public List execute()
             throws DataAccessException
Convenient method to execute without parameters nor context.

Throws:
DataAccessException

execute

public List execute(int p1,
                    Map context)
             throws DataAccessException
Convenient method to execute with a single int parameter and context.

Parameters:
p1 - single int parameter
context - the contextual information for object creation
Throws:
DataAccessException

execute

public List execute(int p1)
             throws DataAccessException
Convenient method to execute with a single int parameter.

Parameters:
p1 - single int parameter
Throws:
DataAccessException

execute

public List execute(int p1,
                    int p2,
                    Map context)
             throws DataAccessException
Convenient method to execute with two int parameters and context.

Parameters:
p1 - first int parameter
p2 - second int parameter
context - the contextual information for object creation
Throws:
DataAccessException

execute

public List execute(int p1,
                    int p2)
             throws DataAccessException
Convenient method to execute with two int parameters.

Parameters:
p1 - first int parameter
p2 - second int parameter
Throws:
DataAccessException

execute

public List execute(long p1,
                    Map context)
             throws DataAccessException
Convenient method to execute with a single long parameter and context.

Parameters:
p1 - single long parameter
context - the contextual information for object creation
Throws:
DataAccessException

execute

public List execute(long p1)
             throws DataAccessException
Convenient method to execute with a single long parameter.

Parameters:
p1 - single long parameter
Throws:
DataAccessException

execute

public List execute(String p1,
                    Map context)
             throws DataAccessException
Convenient method to execute with a single String parameter and context.

Parameters:
p1 - single String parameter
context - the contextual information for object creation
Throws:
DataAccessException

execute

public List execute(String p1)
             throws DataAccessException
Convenient method to execute with a single String parameter.

Parameters:
p1 - single String parameter
Throws:
DataAccessException

findObject

public Object findObject(Object[] params,
                         Map context)
                  throws DataAccessException
Generic object finder method, used by all other findObject methods. Object finder methods are like EJB entity bean finders, in that it is considered an error if they return more than one result.

Parameters:
params - parameters, similar to JDO query parameters. Primitive parameters must be represented by their Object wrapper type. The ordering of parameters is significant.
context - contextual information passed to the callback mapRow method. The JDBC operation itself doesn't rely on this parameter, but can be useful for creating the objects of the result list.
Returns:
the result object, or null if not found. Subclasses may choose to treat this as an error and throw an exception.
Throws:
DataAccessException
See Also:
DataAccessUtils.uniqueResult(java.util.Collection)

findObject

public Object findObject(Object[] params)
                  throws DataAccessException
Convenient method to find a single object without context.

Throws:
DataAccessException

findObject

public Object findObject(int p1,
                         Map context)
                  throws DataAccessException
Convenient method to find a single object given a single int parameter and a context.

Throws:
DataAccessException

findObject

public Object findObject(int p1)
                  throws DataAccessException
Convenient method to find a single object given a single int parameter.

Throws:
DataAccessException

findObject

public Object findObject(int p1,
                         int p2,
                         Map context)
                  throws DataAccessException
Convenient method to find a single object given two int parameters and a context.

Throws:
DataAccessException

findObject

public Object findObject(int p1,
                         int p2)
                  throws DataAccessException
Convenient method to find a single object given two int parameters.

Throws:
DataAccessException

findObject

public Object findObject(long p1,
                         Map context)
                  throws DataAccessException
Convenient method to find a single object given a single long parameter and a context.

Throws:
DataAccessException

findObject

public Object findObject(long p1)
                  throws DataAccessException
Convenient method to find a single object given a single long parameter.

Throws:
DataAccessException

findObject

public Object findObject(String p1,
                         Map context)
                  throws DataAccessException
Convenient method to find a single object given a single String parameter and a context.

Throws:
DataAccessException

findObject

public Object findObject(String p1)
                  throws DataAccessException
Convenient method to find a single object given a single String parameter.

Throws:
DataAccessException

newResultReader

protected abstract ResultReader newResultReader(int rowsExpected,
                                                Object[] parameters,
                                                Map context)
Subclasses must implement this method to save a List of objects returned by the execute method.

Parameters:
rowsExpected - If 0, we don't know how many rows to expect. This parameter can be ignored, but may help some implementations choose the most efficient Collection type: e.g. ArrayList instead of LinkedList for large result sets.
parameters - parameters to the execute() method, in case subclass is interested. May be null if there were no parameters.
See Also:
execute(java.lang.Object[], java.util.Map)


Copyright (c) 2002-2006 The Spring Framework Project.