com.ibatis.db.sqlmap
Class MappedStatement

java.lang.Object
  extended bycom.ibatis.db.sqlmap.MappedStatement
Direct Known Subclasses:
DynamicMappedStatement

public class MappedStatement
extends java.lang.Object

One of the core classes in the iBATIS database framework, the MappedStatement represents an SQL statement that will have a parameter bean and/or result beans mapped to it.


Nested Class Summary
private static class MappedStatement.CacheKey
          A key for results in the cache.
private static class MappedStatement.ErrorField
          This is a value holder class for the very limited use of passing error information around.
private static class MappedStatement.ResultSetAutoMapper
          Class for automatically mapping resultsets to columns, and caching the relevant mappings.
 
Field Summary
private  CacheModel cache
           
private static java.lang.String CHECK_PARAMETER_MAP
           
private static java.lang.String CHECK_RESULT_MAP
           
private static java.lang.String CHECK_SQL_STATEMENT
           
private static java.lang.String ELEMENT_TOKEN
           
private  java.util.List executeListeners
           
private  boolean isInline
           
private static org.apache.commons.logging.Log log
           
private  java.lang.String name
           
static int NO_MAXIMUM_RESULTS
          Magic number used to set the the maximum number of rows returned to 'all'.
static int NO_SKIPPED_RESULTS
          Magic number used to set the the number of rows skipped to 'none'.
private  java.lang.String parameterClass
           
private  java.lang.Class parameterClassType
           
private  java.lang.String parameterMapName
           
private  java.lang.String resourceName
           
private  java.lang.String resultClass
           
private  java.lang.String resultMapName
           
private  MappedStatement.ResultSetAutoMapper resultSetAutoMapper
           
private  java.lang.String sql
           
private  SqlMap sqlMap
           
private  StatementFactory statementFactory
           
private  boolean storedProcedure
           
 
Constructor Summary
MappedStatement()
          Default constructor.
 
Method Summary
 void addExecuteListener(ExecuteListener listener)
          Adds an instance of a class that implements the ExecuteListener interface to a list of objects that will be notified after an execute method is run.
private  void applyParameterMap(SqlStatement localSql, java.sql.PreparedStatement ps, java.lang.Object parameterObject, MappedStatement.ErrorField errorField)
           
private  java.lang.Object applyResultMap(java.sql.Connection conn, java.sql.ResultSet rs, java.lang.Object resultObject, MappedStatement.ErrorField errorField)
           
protected  void assertParameterClass(java.lang.Object parameterObject)
           
private  void autoMapResultSet(java.sql.Connection conn, java.sql.ResultSet rs, java.lang.Object object, MappedStatement.ErrorField errorField)
           
private  void closeResultSet(java.sql.ResultSet rs)
           
private  void closeStatement(java.sql.PreparedStatement ps)
           
private  java.sql.PreparedStatement createStatement(java.sql.Connection conn, SqlStatement localSql, java.lang.Object parameterObject)
           
 java.util.List executeQueryForList(java.sql.Connection conn, java.lang.Object parameterObject)
          Executes the SQL and retuns all rows selected.
 java.util.List executeQueryForList(java.sql.Connection conn, java.lang.Object parameterObject, int skipResults, int maxResults)
          Executes the SQL and retuns a subset of the rows selected.
 java.util.Map executeQueryForMap(java.sql.Connection conn, java.lang.Object parameterObject, java.lang.String keyProperty)
          Executes the SQL and retuns all rows selected in a map that is keyed on the property named in the keyProperty parameter.
 java.util.Map executeQueryForMap(java.sql.Connection conn, java.lang.Object parameterObject, java.lang.String keyProperty, java.lang.String valueProperty)
          Executes the SQL and retuns all rows selected in a map that is keyed on the property named in the keyProperty parameter.
 java.lang.Object executeQueryForObject(java.sql.Connection conn, java.lang.Object parameterObject)
          Executes an SQL statement that returns a single row as an Object.
 java.lang.Object executeQueryForObject(java.sql.Connection conn, java.lang.Object parameterObject, java.lang.Object resultObject)
          Executes an SQL statement that returns a single row as an Object of the type of the resultObject passed in as a parameter.
 PaginatedList executeQueryForPaginatedList(java.lang.Object parameterObject, int pageSize)
          Executes the SQL and retuns a subset of the results in a dynamic PaginatedList that can be used to automatically scroll through results from a database table.
 void executeQueryWithRowHandler(java.sql.Connection conn, java.lang.Object parameterObject, RowHandler rowHandler)
          Runs a query with a custom object that gets a chance to deal with each row as it is processed.
 int executeUpdate(java.sql.Connection conn, java.lang.Object parameterObject)
          Executes an SQL statement that could change the database.
 CacheModel getCache()
          Getter for the cache object used by this object
 java.lang.Double getDataCacheHitRatio()
          Gets a percentage of successful cache hits achieved
 java.lang.String getName()
          Getter for name property
 java.lang.String getParameterClass()
          Getter for the class name of the parameter object (if any) related to this MappedStatement.
 java.lang.String getParameterMapName()
          Getter for the name of the parameter map object (if any) related to this MappedStatement.
 java.lang.String getResourceName()
          Getter for resourceName property
 java.lang.String getResultClass()
          Getter for the class name of the result object (if any) related to this MappedStatement.
 java.lang.String getResultMapName()
          Getter for the name of the result map object (if any) related to this MappedStatement.
 java.lang.String getSql(java.lang.Object parameterObject)
          Returns the SQL statement for the object
 SqlMap getSqlMap()
          Getter for the SqlMap object that contains this MappedStatement
protected  SqlStatement getSqlStatement(java.lang.Object parameterObject)
           
private  java.lang.Object instantiate(java.lang.String className)
           
 boolean isDynamic()
          Getter for flag that indicates if this MappedStatement is 'dynamic'
private  boolean isEqualToNullValue(java.lang.String nullValue, java.lang.Object realValue)
           
 boolean isInline()
          Getter for inline property
private static boolean isSimpleType(java.lang.Class type)
           
 boolean isStoredProcedure()
          Getter for a flag that indicates that this MappedStatement represents a stored procedure.
private  void notifyListeners()
           
private  java.lang.String processDynamicElements(java.lang.String sql, java.lang.Object parameterObject)
           
private  java.util.List runQueryForList(SqlStatement localSql, java.sql.Connection conn, java.lang.Object parameterObject, int skipResults, int maxResults, RowHandler rowHandler)
           
private  java.util.Map runQueryForMap(java.sql.Connection conn, java.lang.Object parameterObject, java.lang.String keyProperty, java.lang.String valueProperty)
           
private  java.lang.Object runQueryForObject(SqlStatement localSql, java.sql.Connection conn, java.lang.Object parameterObject, java.lang.Object resultObject)
           
private  void setBeanProperty(ResultMapping mapping, java.lang.Object object, java.sql.Connection conn, java.sql.ResultSet rs, MappedStatement.ErrorField errorField)
           
 void setCache(CacheModel cache)
          Setter for the cache object used by this object
 void setInline(boolean inline)
          Setter for inline property
 void setName(java.lang.String name)
          Setter for name property
private  void setNullParameter(ParameterMapping mapping, int index, java.sql.PreparedStatement ps)
           
private  void setParameter(java.sql.PreparedStatement ps, int index, java.lang.Object object, ParameterMap map, MappedStatement.ErrorField errorField)
           
 void setParameterClass(java.lang.String parameterClass)
          Setter for the class name of the parameter object (if any) related to this MappedStatement.
 void setParameterMapName(java.lang.String parameterMapName)
          Setter for the name of the parameter map object (if any) related to this MappedStatement.
 void setResourceName(java.lang.String resourceName)
          Setter for resourceName property
 void setResultClass(java.lang.String resultClass)
          Setter for the class name of the result object (if any) related to this MappedStatement.
 void setResultMapName(java.lang.String resultMapName)
          Setter for the name of the result map object (if any) related to this MappedStatement.
 void setSql(java.lang.String sql)
          Sets the SQL statement for the object
 void setSqlMap(SqlMap sqlMap)
          Setter for the SqlMap object that contains this MappedStatement
 void setStoredProcedure(boolean storedProcedure)
          Setter for a flag that indicates that this MappedStatement represents a stored procedure.
 java.lang.String toString()
          Simple toString implementation
private  java.lang.Throwable unwrapProxyException(java.lang.Throwable t)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

private static final org.apache.commons.logging.Log log

NO_MAXIMUM_RESULTS

public static final int NO_MAXIMUM_RESULTS
Magic number used to set the the maximum number of rows returned to 'all'.

See Also:
Constant Field Values

NO_SKIPPED_RESULTS

public static final int NO_SKIPPED_RESULTS
Magic number used to set the the number of rows skipped to 'none'.

See Also:
Constant Field Values

CHECK_SQL_STATEMENT

private static final java.lang.String CHECK_SQL_STATEMENT
See Also:
Constant Field Values

CHECK_PARAMETER_MAP

private static final java.lang.String CHECK_PARAMETER_MAP
See Also:
Constant Field Values

CHECK_RESULT_MAP

private static final java.lang.String CHECK_RESULT_MAP
See Also:
Constant Field Values

ELEMENT_TOKEN

private static final java.lang.String ELEMENT_TOKEN
See Also:
Constant Field Values

executeListeners

private java.util.List executeListeners

resourceName

private java.lang.String resourceName

name

private java.lang.String name

sql

private java.lang.String sql

parameterClass

private java.lang.String parameterClass

parameterClassType

private java.lang.Class parameterClassType

resultClass

private java.lang.String resultClass

parameterMapName

private java.lang.String parameterMapName

resultMapName

private java.lang.String resultMapName

sqlMap

private SqlMap sqlMap

isInline

private boolean isInline

storedProcedure

private boolean storedProcedure

cache

private CacheModel cache

statementFactory

private StatementFactory statementFactory

resultSetAutoMapper

private MappedStatement.ResultSetAutoMapper resultSetAutoMapper
Constructor Detail

MappedStatement

public MappedStatement()
Default constructor. Sets hasDynamicElements, storedProcedure and isInline to false.

Method Detail

getName

public java.lang.String getName()
Getter for name property

Returns:
The name of the object

setName

public void setName(java.lang.String name)
Setter for name property

Parameters:
name - The new name of the object

getResourceName

public java.lang.String getResourceName()
Getter for resourceName property

Returns:
The resourceName of the object

setResourceName

public void setResourceName(java.lang.String resourceName)
Setter for resourceName property

Parameters:
resourceName - The new resourceName of the object

isInline

public boolean isInline()
Getter for inline property

Returns:
True if statement is to be processed for inline parameters

setInline

public void setInline(boolean inline)
Setter for inline property

Parameters:
inline - The new inline setting for the object

getSql

public java.lang.String getSql(java.lang.Object parameterObject)
Returns the SQL statement for the object

Parameters:
parameterObject - The bean containing the parameters for the SQL statement
Returns:
The SQL statement

setSql

public void setSql(java.lang.String sql)
Sets the SQL statement for the object

Parameters:
sql - The new SQL statement for the object

getSqlMap

public SqlMap getSqlMap()
Getter for the SqlMap object that contains this MappedStatement

Returns:
The containing SqlMAp

setSqlMap

public void setSqlMap(SqlMap sqlMap)
Setter for the SqlMap object that contains this MappedStatement

Parameters:
sqlMap - The new SqlMap object

getParameterClass

public java.lang.String getParameterClass()
Getter for the class name of the parameter object (if any) related to this MappedStatement.

Returns:
The class name

setParameterClass

public void setParameterClass(java.lang.String parameterClass)
Setter for the class name of the parameter object (if any) related to this MappedStatement.

Parameters:
parameterClass - The new class name

getResultClass

public java.lang.String getResultClass()
Getter for the class name of the result object (if any) related to this MappedStatement.

Returns:
The class name

setResultClass

public void setResultClass(java.lang.String resultClass)
Setter for the class name of the result object (if any) related to this MappedStatement.

Parameters:
resultClass - The new class name

getParameterMapName

public java.lang.String getParameterMapName()
Getter for the name of the parameter map object (if any) related to this MappedStatement.

Returns:
The name of the parameter map

setParameterMapName

public void setParameterMapName(java.lang.String parameterMapName)
Setter for the name of the parameter map object (if any) related to this MappedStatement.

Parameters:
parameterMapName - The new name of the parameter map

getResultMapName

public java.lang.String getResultMapName()
Getter for the name of the result map object (if any) related to this MappedStatement.

Returns:
The name of the result map

setResultMapName

public void setResultMapName(java.lang.String resultMapName)
Setter for the name of the result map object (if any) related to this MappedStatement.

Parameters:
resultMapName - The new name of the result map

getCache

public CacheModel getCache()
Getter for the cache object used by this object

Returns:
The cache

setCache

public void setCache(CacheModel cache)
Setter for the cache object used by this object

Parameters:
cache - The new cache to use

isStoredProcedure

public boolean isStoredProcedure()
Getter for a flag that indicates that this MappedStatement represents a stored procedure.

Returns:
True if the MappedStatement is for a stored procedure

setStoredProcedure

public void setStoredProcedure(boolean storedProcedure)
Setter for a flag that indicates that this MappedStatement represents a stored procedure.

Parameters:
storedProcedure - True if this MappedStatement is for a stored procedure

isDynamic

public boolean isDynamic()
Getter for flag that indicates if this MappedStatement is 'dynamic'

Returns:
True if the MappedStatement is dynamic

getDataCacheHitRatio

public java.lang.Double getDataCacheHitRatio()
Gets a percentage of successful cache hits achieved

Returns:
The percentage of hits (0-1), or null if cache is disabled.

addExecuteListener

public void addExecuteListener(ExecuteListener listener)
Adds an instance of a class that implements the ExecuteListener interface to a list of objects that will be notified after an execute method is run.

Parameters:
listener - The object to add to the list

toString

public java.lang.String toString()
Simple toString implementation

Returns:
A string that describes the MappedStatement

notifyListeners

private void notifyListeners()

processDynamicElements

private java.lang.String processDynamicElements(java.lang.String sql,
                                                java.lang.Object parameterObject)

createStatement

private java.sql.PreparedStatement createStatement(java.sql.Connection conn,
                                                   SqlStatement localSql,
                                                   java.lang.Object parameterObject)
                                            throws java.sql.SQLException
Throws:
java.sql.SQLException

assertParameterClass

protected void assertParameterClass(java.lang.Object parameterObject)
                             throws java.sql.SQLException
Throws:
java.sql.SQLException

executeUpdate

public int executeUpdate(java.sql.Connection conn,
                         java.lang.Object parameterObject)
                  throws java.sql.SQLException
Executes an SQL statement that could change the database. Listeners are notified at the end of this method.

Parameters:
conn - The connection to use to execute the update
parameterObject - The parameter object that will be mapped to the SQL
Returns:
The number of rows changed
Throws:
java.sql.SQLException - If an exception occurs in the database

executeQueryWithRowHandler

public void executeQueryWithRowHandler(java.sql.Connection conn,
                                       java.lang.Object parameterObject,
                                       RowHandler rowHandler)
                                throws java.sql.SQLException
Runs a query with a custom object that gets a chance to deal with each row as it is processed.

Parameters:
conn - The connection used to execute the query
parameterObject - The parameter object mapped the the statement
rowHandler - The custom row handler object
Throws:
java.sql.SQLException - If an exception occurs in the database

executeQueryForMap

public java.util.Map executeQueryForMap(java.sql.Connection conn,
                                        java.lang.Object parameterObject,
                                        java.lang.String keyProperty)
                                 throws java.sql.SQLException
Executes the SQL and retuns all rows selected in a map that is keyed on the property named in the keyProperty parameter. The value at each key will be the entire result object.

Parameters:
conn - The connection used to execute the statement
parameterObject - The object used to set the parameters in the SQL
keyProperty - The property of the result object to be used as the key
Returns:
A Map of beans containing the rows keyed by keyProperty
Throws:
java.sql.SQLException - If an exception occurs in the database

executeQueryForMap

public java.util.Map executeQueryForMap(java.sql.Connection conn,
                                        java.lang.Object parameterObject,
                                        java.lang.String keyProperty,
                                        java.lang.String valueProperty)
                                 throws java.sql.SQLException
Executes the SQL and retuns all rows selected in a map that is keyed on the property named in the keyProperty parameter. The value at each key will be the value of the property specified in the valueProperty parameter. If valueProperty is null, the entire result object will be entered.

Parameters:
conn - The connection used to execute the statement
parameterObject - The object used to set the parameters in the SQL
keyProperty - The property of the result object to be used as the key
valueProperty - The property of the result object to be used as the value (or null)
Returns:
A Map of beans containing the rows keyed by keyProperty
Throws:
java.sql.SQLException - If an exception occurs in the database

runQueryForMap

private java.util.Map runQueryForMap(java.sql.Connection conn,
                                     java.lang.Object parameterObject,
                                     java.lang.String keyProperty,
                                     java.lang.String valueProperty)
                              throws java.sql.SQLException
Throws:
java.sql.SQLException

executeQueryForPaginatedList

public PaginatedList executeQueryForPaginatedList(java.lang.Object parameterObject,
                                                  int pageSize)
                                           throws java.sql.SQLException
Executes the SQL and retuns a subset of the results in a dynamic PaginatedList that can be used to automatically scroll through results from a database table.

Parameters:
parameterObject - The object used to set the parameters in the SQL
pageSize - The maximum number of objects to store in each page
Returns:
A PaginatedList of beans containing the rows
Throws:
java.sql.SQLException - If an exception occurs in the database

executeQueryForList

public java.util.List executeQueryForList(java.sql.Connection conn,
                                          java.lang.Object parameterObject)
                                   throws java.sql.SQLException
Executes the SQL and retuns all rows selected. This is exactly the same as calling executeQueryForList(conn, parameterObject, NO_SKIPPED_RESULTS, NO_MAXIMUM_RESULTS) because that is what it does.

Parameters:
conn - The connection used to execute the statement
parameterObject - The object used to set the parameters in the SQL
Returns:
A List of beans containing the rows
Throws:
java.sql.SQLException - If an exception occurs in the database

executeQueryForList

public java.util.List executeQueryForList(java.sql.Connection conn,
                                          java.lang.Object parameterObject,
                                          int skipResults,
                                          int maxResults)
                                   throws java.sql.SQLException
Executes the SQL and retuns a subset of the rows selected.

Parameters:
conn - The connection used to execute the statement
parameterObject - The object used to set the parameters in the SQL
skipResults - The number of rows to skip
maxResults - The number of rows to return
Returns:
A List of beans containing the rows
Throws:
java.sql.SQLException - If an exception occurs in the database

runQueryForList

private java.util.List runQueryForList(SqlStatement localSql,
                                       java.sql.Connection conn,
                                       java.lang.Object parameterObject,
                                       int skipResults,
                                       int maxResults,
                                       RowHandler rowHandler)
                                throws java.sql.SQLException
Throws:
java.sql.SQLException

executeQueryForObject

public java.lang.Object executeQueryForObject(java.sql.Connection conn,
                                              java.lang.Object parameterObject)
                                       throws java.sql.SQLException
Executes an SQL statement that returns a single row as an Object.

Parameters:
conn - The connection used to execute the statement
parameterObject - The object used to set the parameters in the SQL
Returns:
The object
Throws:
java.sql.SQLException - If an exception occurs in the database

executeQueryForObject

public java.lang.Object executeQueryForObject(java.sql.Connection conn,
                                              java.lang.Object parameterObject,
                                              java.lang.Object resultObject)
                                       throws java.sql.SQLException
Executes an SQL statement that returns a single row as an Object of the type of the resultObject passed in as a parameter. NOTE: The object returned may or may not be the same instance of the object passed in.

Parameters:
conn - The connection used to execute the statement
parameterObject - The object used to set the parameters in the SQL
resultObject - The
Returns:
The object
Throws:
java.sql.SQLException - If an exception occurs in the database

runQueryForObject

private java.lang.Object runQueryForObject(SqlStatement localSql,
                                           java.sql.Connection conn,
                                           java.lang.Object parameterObject,
                                           java.lang.Object resultObject)
                                    throws java.sql.SQLException
Throws:
java.sql.SQLException

unwrapProxyException

private java.lang.Throwable unwrapProxyException(java.lang.Throwable t)

applyParameterMap

private void applyParameterMap(SqlStatement localSql,
                               java.sql.PreparedStatement ps,
                               java.lang.Object parameterObject,
                               MappedStatement.ErrorField errorField)
                        throws java.sql.SQLException
Throws:
java.sql.SQLException

getSqlStatement

protected SqlStatement getSqlStatement(java.lang.Object parameterObject)
                                throws java.sql.SQLException
Throws:
java.sql.SQLException

isEqualToNullValue

private boolean isEqualToNullValue(java.lang.String nullValue,
                                   java.lang.Object realValue)

isSimpleType

private static boolean isSimpleType(java.lang.Class type)

setParameter

private void setParameter(java.sql.PreparedStatement ps,
                          int index,
                          java.lang.Object object,
                          ParameterMap map,
                          MappedStatement.ErrorField errorField)
                   throws java.sql.SQLException
Throws:
java.sql.SQLException

setNullParameter

private void setNullParameter(ParameterMapping mapping,
                              int index,
                              java.sql.PreparedStatement ps)
                       throws java.sql.SQLException
Throws:
java.sql.SQLException

applyResultMap

private java.lang.Object applyResultMap(java.sql.Connection conn,
                                        java.sql.ResultSet rs,
                                        java.lang.Object resultObject,
                                        MappedStatement.ErrorField errorField)
                                 throws java.sql.SQLException
Throws:
java.sql.SQLException

autoMapResultSet

private void autoMapResultSet(java.sql.Connection conn,
                              java.sql.ResultSet rs,
                              java.lang.Object object,
                              MappedStatement.ErrorField errorField)
                       throws java.sql.SQLException
Throws:
java.sql.SQLException

setBeanProperty

private void setBeanProperty(ResultMapping mapping,
                             java.lang.Object object,
                             java.sql.Connection conn,
                             java.sql.ResultSet rs,
                             MappedStatement.ErrorField errorField)
                      throws java.sql.SQLException
Throws:
java.sql.SQLException

closeStatement

private void closeStatement(java.sql.PreparedStatement ps)

closeResultSet

private void closeResultSet(java.sql.ResultSet rs)

instantiate

private java.lang.Object instantiate(java.lang.String className)