Author: cbegin
Date: Sat Oct 10 06:23:22 2009
New Revision: 823802
URL: http://svn.apache.org/viewvc?rev=823802&view=rev
Log:
Split DefaultResultHandler in two.
Added:
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/FastResultSetHandler.java
- copied, changed from r823796,
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
Modified:
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
Modified:
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
URL:
http://svn.apache.org/viewvc/ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java?rev=823802&r1=823801&r2=823802&view=diff
==============================================================================
---
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
(original)
+++
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
Sat Oct 10 06:23:22 2009
@@ -3,86 +3,34 @@
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.ExecutorException;
-import org.apache.ibatis.executor.loader.ResultLoader;
import org.apache.ibatis.executor.loader.ResultLoaderRegistry;
-import org.apache.ibatis.executor.loader.ResultObjectProxy;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.result.DefaultResultContext;
-import org.apache.ibatis.executor.result.DefaultResultHandler;
-import org.apache.ibatis.mapping.*;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.ResultMap;
+import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.reflection.MetaObject;
-import org.apache.ibatis.reflection.factory.ObjectFactory;
-import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandler;
-import org.apache.ibatis.type.TypeHandlerRegistry;
-import java.sql.*;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
import java.util.*;
-public class DefaultResultSetHandler implements ResultSetHandler {
-
- private final Executor executor;
- private final Configuration configuration;
- private final MappedStatement mappedStatement;
- private final RowBounds rowBounds;
- private final ParameterHandler parameterHandler;
- private final ResultHandler resultHandler;
- private final BoundSql boundSql;
- private final TypeHandlerRegistry typeHandlerRegistry;
- private final ObjectFactory objectFactory;
+public class DefaultResultSetHandler extends FastResultSetHandler {
private final Map<CacheKey, Set<CacheKey>> localRowValueCaches;
private final Map<CacheKey, Object> globalRowValueCache;
public DefaultResultSetHandler(Executor executor, MappedStatement
mappedStatement, ParameterHandler parameterHandler, ResultHandler
resultHandler, BoundSql boundSql, RowBounds rowBounds) {
- this.executor = executor;
- this.configuration = mappedStatement.getConfiguration();
- this.mappedStatement = mappedStatement;
- this.rowBounds = rowBounds;
- this.parameterHandler = parameterHandler;
- this.boundSql = boundSql;
- this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
- this.objectFactory = configuration.getObjectFactory();
- this.resultHandler = resultHandler;
- this.localRowValueCaches = new HashMap<CacheKey, Set<CacheKey>>();
- this.globalRowValueCache = new HashMap<CacheKey, Object>();
- }
-
- //
- // HANDLE OUTPUT PARAMETER
- //
-
- public void handleOutputParameters(CallableStatement cs) throws SQLException
{
- final Object parameterObject = parameterHandler.getParameterObject();
- final MetaObject metaParam = MetaObject.forObject(parameterObject);
- final List<ParameterMapping> parameterMappings =
boundSql.getParameterMappings();
- for (int i = 0; i < parameterMappings.size(); i++) {
- final ParameterMapping parameterMapping = parameterMappings.get(i);
- if (parameterMapping.getMode() == ParameterMode.OUT ||
parameterMapping.getMode() == ParameterMode.INOUT) {
- if
("java.sql.ResultSet".equalsIgnoreCase(parameterMapping.getJavaType().getName()))
{
- handleRefCursorOutputParameter(cs, parameterMapping, i, metaParam);
- } else {
- metaParam.setValue(parameterMapping.getProperty(),
parameterMapping.getTypeHandler().getResult(cs, i + 1));
- }
- }
- }
- }
-
- private void handleRefCursorOutputParameter(CallableStatement cs,
ParameterMapping parameterMapping, int parameterMappingIndex, MetaObject
metaParam) throws SQLException {
- final ResultSet rs = (ResultSet) cs.getObject(parameterMappingIndex + 1);
- final String resultMapId = parameterMapping.getResultMapId();
- if (resultMapId != null) {
- final ResultMap resultMap = configuration.getResultMap(resultMapId);
- final DefaultResultHandler resultHandler = new DefaultResultHandler();
- handleRowValues(rs, resultMap, resultHandler, new RowBounds());
- metaParam.setValue(parameterMapping.getProperty(),
resultHandler.getResultList());
- } else {
- throw new ExecutorException("Parameter requires ResultMap for output
types of java.sql.ResultSet");
- }
- rs.close();
+ super(executor, mappedStatement, parameterHandler, resultHandler,
boundSql, rowBounds);
+ localRowValueCaches = new HashMap<CacheKey, Set<CacheKey>>();
+ globalRowValueCache = new HashMap<CacheKey, Object>();
}
//
@@ -104,29 +52,11 @@
return collapseSingleResultList(multipleResults);
}
- private void handleResultSet(ResultSet rs, ResultMap resultMap, List
multipleResults) throws SQLException {
- if (resultHandler == null) {
- DefaultResultHandler defaultResultHandler = new DefaultResultHandler();
- handleRowValues(rs, resultMap, defaultResultHandler, rowBounds);
- multipleResults.add(defaultResultHandler.getResultList());
- } else {
- handleRowValues(rs, resultMap, resultHandler, rowBounds);
- }
- }
-
- private List collapseSingleResultList(List multipleResults) {
- if (multipleResults.size() == 1) {
- return (List) multipleResults.get(0);
- } else {
- return multipleResults;
- }
- }
-
//
// HANDLE ROWS
//
- private void handleRowValues(ResultSet rs, ResultMap resultMap,
ResultHandler resultHandler, RowBounds rowBounds) throws SQLException {
+ protected void handleRowValues(ResultSet rs, ResultMap resultMap,
ResultHandler resultHandler, RowBounds rowBounds) throws SQLException {
final DefaultResultContext resultContext = new DefaultResultContext();
skipRows(rs, rowBounds);
while (shouldProcessMoreRows(rs, resultContext.getResultCount(),
rowBounds)) {
@@ -141,38 +71,11 @@
}
}
- private boolean shouldProcessMoreRows(ResultSet rs, int count, RowBounds
rowBounds) throws SQLException {
- return rs.next() && count < rowBounds.getLimit();
- }
-
- private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException
{
- if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
- rs.absolute(rowBounds.getOffset());
- } else {
- for (int i = 0; i < rowBounds.getOffset(); i++) rs.next();
- }
- }
-
- private ResultSet getNextResultSet(Statement stmt) throws SQLException {
- // Making this method tolerant of bad JDBC drivers
- try {
- if (stmt.getConnection().getMetaData().supportsMultipleResultSets()) {
- // Crazy Standard JDBC way of determining if there are more results
- if (!((!stmt.getMoreResults()) && (stmt.getUpdateCount() == -1))) {
- return stmt.getResultSet();
- }
- }
- } catch (Exception e) {
- // Intentionally ignored.
- }
- return null;
- }
-
//
// GET VALUE FROM ROW
//
- private Object getRowValue(ResultSet rs, ResultMap resultMap, CacheKey
rowKey) throws SQLException {
+ protected Object getRowValue(ResultSet rs, ResultMap resultMap, CacheKey
rowKey) throws SQLException {
if (globalRowValueCache.containsKey(rowKey)) {
final Object resultObject = globalRowValueCache.get(rowKey);
final MetaObject metaObject = MetaObject.forObject(resultObject);
@@ -199,229 +102,6 @@
}
}
- private ResultLoaderRegistry instantiateResultLoaderRegistry() {
- if (configuration.isLazyLoadingEnabled()) {
- return new ResultLoaderRegistry();
- } else {
- return null;
- }
- }
-
- //
- // PROPERTY MAPPINGS
- //
-
- private boolean applyPropertyMappings(ResultSet rs, ResultMap resultMap,
List<String> mappedColumnNames, MetaObject metaObject, ResultLoaderRegistry
lazyLoader) throws SQLException {
- boolean foundValues = false;
- final List<ResultMapping> propertyMappings =
resultMap.getPropertyResultMappings();
- for (ResultMapping propertyMapping : propertyMappings) {
- final String column = propertyMapping.getColumn();
- if (propertyMapping.isCompositeResult() || (column != null &&
mappedColumnNames.contains(column.toUpperCase()))) {
- Object value = getPropertyMappingValue(rs, metaObject,
propertyMapping, lazyLoader);
- if (value != null) {
- final String property = propertyMapping.getProperty();
- metaObject.setValue(property, value);
- foundValues = true;
- }
- }
- }
- return foundValues;
- }
-
- private Object getPropertyMappingValue(ResultSet rs, MetaObject
metaResultObject, ResultMapping propertyMapping, ResultLoaderRegistry
lazyLoader) throws SQLException {
- final TypeHandler typeHandler = propertyMapping.getTypeHandler();
- if (propertyMapping.getNestedQueryId() != null) {
- return getNestedQueryMappingValue(rs, metaResultObject, propertyMapping,
lazyLoader);
- } else if (typeHandler != null) {
- final String column = propertyMapping.getColumn();
- return typeHandler.getResult(rs, column);
- }
- return null;
- }
-
- private boolean applyAutomaticMappings(ResultSet rs, List<String>
unmappedColumnNames, MetaObject metaObject) throws SQLException {
- boolean foundValues = false;
- for (String columnName : unmappedColumnNames) {
- final String property = metaObject.findProperty(columnName);
- if (property != null) {
- final Class propertyType = metaObject.getSetterType(property);
- if (typeHandlerRegistry.hasTypeHandler(propertyType)) {
- final TypeHandler typeHandler =
typeHandlerRegistry.getTypeHandler(propertyType);
- final Object value = typeHandler.getResult(rs, columnName);
- if (value != null) {
- metaObject.setValue(property, value);
- foundValues = true;
- }
- }
- }
- }
- return foundValues;
- }
-
- private void loadMappedAndUnmappedColumnNames(ResultSet rs, ResultMap
resultMap, List<String> mappedColumnNames, List<String> unmappedColumnNames)
throws SQLException {
- mappedColumnNames.clear();
- unmappedColumnNames.clear();
- final ResultSetMetaData rsmd = rs.getMetaData();
- final int columnCount = rsmd.getColumnCount();
- final Set<String> mappedColumns = resultMap.getMappedColumns();
- for (int i = 1; i <= columnCount; i++) {
- final String columnName = configuration.isUseColumnLabel() ?
rsmd.getColumnLabel(i) : rsmd.getColumnName(i);
- final String upperColumnName = columnName.toUpperCase();
- if (mappedColumns.contains(upperColumnName)) {
- mappedColumnNames.add(upperColumnName);
- mappedColumnNames.add(columnName);
- } else {
- unmappedColumnNames.add(upperColumnName);
- unmappedColumnNames.add(columnName);
- }
- }
- }
-
- //
- // INSTANTIATION & CONSTRUCTOR MAPPING
- //
-
- private Object createResultObject(ResultSet rs, ResultMap resultMap,
ResultLoaderRegistry lazyLoader) throws SQLException {
- final Object resultObject = createResultObject(rs, resultMap);
- if (resultObject != null && configuration.isLazyLoadingEnabled()) {
- return ResultObjectProxy.createProxy(resultMap.getType(), resultObject,
lazyLoader);
- }
- return resultObject;
- }
-
- private Object createResultObject(ResultSet rs, ResultMap resultMap) throws
SQLException {
- final Class resultType = resultMap.getType();
- final List<ResultMapping> constructorMappings =
resultMap.getConstructorResultMappings();
- if (typeHandlerRegistry.hasTypeHandler(resultType)) {
- return createPrimitiveResultObject(rs, resultMap);
- } else if (constructorMappings.size() > 0) {
- return createParameterizedResultObject(rs, resultType,
constructorMappings);
- } else {
- return objectFactory.create(resultType);
- }
- }
-
- private Object createParameterizedResultObject(ResultSet rs, Class
resultType, List<ResultMapping> constructorMappings) throws SQLException {
- boolean foundValues = false;
- final List<Class> parameterTypes = new ArrayList<Class>();
- final List<Object> parameterValues = new ArrayList<Object>();
- for (ResultMapping constructorMapping : constructorMappings) {
- final Class parameterType = constructorMapping.getJavaType();
- final TypeHandler typeHandler = constructorMapping.getTypeHandler();
- final String column = constructorMapping.getColumn();
- final Object value = typeHandler.getResult(rs, column);
- parameterTypes.add(parameterType);
- parameterValues.add(value);
- foundValues = value != null || foundValues;
- }
- return foundValues ? objectFactory.create(resultType, parameterTypes,
parameterValues) : null;
- }
-
- private Object createPrimitiveResultObject(ResultSet rs, ResultMap
resultMap) throws SQLException {
- final Class resultType = resultMap.getType();
- final String columnName;
- if (resultMap.getResultMappings().size() > 0) {
- final List<ResultMapping> resultMappingList =
resultMap.getResultMappings();
- final ResultMapping mapping = resultMappingList.get(0);
- columnName = mapping.getColumn();
- } else {
- final ResultSetMetaData rsmd = rs.getMetaData();
- columnName = configuration.isUseColumnLabel() ? rsmd.getColumnLabel(1) :
rsmd.getColumnName(1);
- }
- final TypeHandler typeHandler =
typeHandlerRegistry.getTypeHandler(resultType);
- return typeHandler.getResult(rs, columnName);
- }
-
- //
- // NESTED QUERY
- //
-
- private Object getNestedQueryMappingValue(ResultSet rs, MetaObject
metaResultObject, ResultMapping propertyMapping, ResultLoaderRegistry
lazyLoader) throws SQLException {
- final String nestedQueryId = propertyMapping.getNestedQueryId();
- final String property = propertyMapping.getProperty();
- final MappedStatement nestedQuery =
configuration.getMappedStatement(nestedQueryId);
- final Class nestedQueryParameterType =
nestedQuery.getParameterMap().getType();
- final Object nestedQueryParameterObject =
prepareParameterForNestedQuery(rs, propertyMapping, nestedQueryParameterType);
- Object value = null;
- if (nestedQueryParameterObject != null) {
- final CacheKey key = executor.createCacheKey(nestedQuery,
nestedQueryParameterObject, RowBounds.DEFAULT);
- if (executor.isCached(nestedQuery, key)) {
- executor.deferLoad(nestedQuery, metaResultObject, property, key);
- } else {
- final ResultLoader resultLoader = new ResultLoader(configuration,
executor, nestedQuery, nestedQueryParameterObject,
propertyMapping.getJavaType());
- if (configuration.isLazyLoadingEnabled()) {
- lazyLoader.registerLoader(property, metaResultObject, resultLoader);
- } else {
- value = resultLoader.loadResult();
- }
- }
- }
- return value;
- }
-
- private Object prepareParameterForNestedQuery(ResultSet rs, ResultMapping
resultMapping, Class parameterType) throws SQLException {
- if (resultMapping.isCompositeResult()) {
- return prepareCompositeKeyParameter(rs, resultMapping, parameterType);
- } else {
- return prepareSimpleKeyParameter(rs, resultMapping, parameterType);
- }
- }
-
- private Object prepareSimpleKeyParameter(ResultSet rs, ResultMapping
resultMapping, Class parameterType) throws SQLException {
- final TypeHandler typeHandler;
- if (typeHandlerRegistry.hasTypeHandler(parameterType)) {
- typeHandler = typeHandlerRegistry.getTypeHandler(parameterType);
- } else {
- typeHandler = typeHandlerRegistry.getUnkownTypeHandler();
- }
- return typeHandler.getResult(rs, resultMapping.getColumn());
- }
-
- private Object prepareCompositeKeyParameter(ResultSet rs, ResultMapping
resultMapping, Class parameterType) throws SQLException {
- final Object parameterObject = instantiateParameterObject(parameterType);
- final MetaObject metaObject = MetaObject.forObject(parameterObject);
- for (ResultMapping innerResultMapping : resultMapping.getComposites()) {
- final Class propType =
metaObject.getSetterType(innerResultMapping.getProperty());
- final TypeHandler typeHandler =
typeHandlerRegistry.getTypeHandler(propType);
- final Object propValue = typeHandler.getResult(rs,
innerResultMapping.getColumn());
- metaObject.setValue(innerResultMapping.getProperty(), propValue);
- }
- return parameterObject;
- }
-
- private Object instantiateParameterObject(Class parameterType) {
- if (parameterType == null) {
- return new HashMap();
- } else {
- return objectFactory.create(parameterType);
- }
- }
-
- //
- // DISCRIMINATOR
- //
-
- public ResultMap resolveDiscriminatedResultMap(ResultSet rs, ResultMap
resultMap) throws SQLException {
- final Discriminator discriminator = resultMap.getDiscriminator();
- if (discriminator != null) {
- final Object value = getDiscriminatorValue(rs, discriminator);
- final String discriminatedMapId =
discriminator.getMapIdFor(String.valueOf(value));
- if (configuration.hasResultMap(discriminatedMapId)) {
- return configuration.getResultMap(discriminatedMapId);
- }
- }
- return resultMap;
- }
-
- private Object getDiscriminatorValue(ResultSet rs, Discriminator
discriminator) throws SQLException {
- final ResultMapping resultMapping = discriminator.getResultMapping();
- final TypeHandler typeHandler = resultMapping.getTypeHandler();
- if (typeHandler != null) {
- return typeHandler.getResult(rs, resultMapping.getColumn());
- } else {
- throw new ExecutorException("No type handler could be found to map the
property '" + resultMapping.getProperty() + "' to the column '" +
resultMapping.getColumn() + "'. One or both of the types, or the combination
of types is not supported.");
- }
- }
//
// NESTED RESULT MAP (JOIN MAPPING)
Copied:
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/FastResultSetHandler.java
(from r823796,
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java)
URL:
http://svn.apache.org/viewvc/ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/FastResultSetHandler.java?p2=ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/FastResultSetHandler.java&p1=ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java&r1=823796&r2=823802&rev=823802&view=diff
==============================================================================
---
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
(original)
+++
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/FastResultSetHandler.java
Sat Oct 10 06:23:22 2009
@@ -10,7 +10,6 @@
import org.apache.ibatis.executor.result.DefaultResultContext;
import org.apache.ibatis.executor.result.DefaultResultHandler;
import org.apache.ibatis.mapping.*;
-import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.session.Configuration;
@@ -20,24 +19,24 @@
import org.apache.ibatis.type.TypeHandlerRegistry;
import java.sql.*;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Set;
+
+public abstract class FastResultSetHandler implements ResultSetHandler {
+
+ protected final Executor executor;
+ protected final Configuration configuration;
+ protected final MappedStatement mappedStatement;
+ protected final RowBounds rowBounds;
+ protected final ParameterHandler parameterHandler;
+ protected final ResultHandler resultHandler;
+ protected final BoundSql boundSql;
+ protected final TypeHandlerRegistry typeHandlerRegistry;
+ protected final ObjectFactory objectFactory;
-public class DefaultResultSetHandler implements ResultSetHandler {
-
- private final Executor executor;
- private final Configuration configuration;
- private final MappedStatement mappedStatement;
- private final RowBounds rowBounds;
- private final ParameterHandler parameterHandler;
- private final ResultHandler resultHandler;
- private final BoundSql boundSql;
- private final TypeHandlerRegistry typeHandlerRegistry;
- private final ObjectFactory objectFactory;
-
- private final Map<CacheKey, Set<CacheKey>> localRowValueCaches;
- private final Map<CacheKey, Object> globalRowValueCache;
-
- public DefaultResultSetHandler(Executor executor, MappedStatement
mappedStatement, ParameterHandler parameterHandler, ResultHandler
resultHandler, BoundSql boundSql, RowBounds rowBounds) {
+ public FastResultSetHandler(Executor executor, MappedStatement
mappedStatement, ParameterHandler parameterHandler, ResultHandler
resultHandler, BoundSql boundSql, RowBounds rowBounds) {
this.executor = executor;
this.configuration = mappedStatement.getConfiguration();
this.mappedStatement = mappedStatement;
@@ -47,8 +46,6 @@
this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
this.objectFactory = configuration.getObjectFactory();
this.resultHandler = resultHandler;
- this.localRowValueCaches = new HashMap<CacheKey, Set<CacheKey>>();
- this.globalRowValueCache = new HashMap<CacheKey, Object>();
}
//
@@ -71,7 +68,7 @@
}
}
- private void handleRefCursorOutputParameter(CallableStatement cs,
ParameterMapping parameterMapping, int parameterMappingIndex, MetaObject
metaParam) throws SQLException {
+ protected void handleRefCursorOutputParameter(CallableStatement cs,
ParameterMapping parameterMapping, int parameterMappingIndex, MetaObject
metaParam) throws SQLException {
final ResultSet rs = (ResultSet) cs.getObject(parameterMappingIndex + 1);
final String resultMapId = parameterMapping.getResultMapId();
if (resultMapId != null) {
@@ -99,12 +96,11 @@
handleResultSet(rs, resultMap, multipleResults);
rs = getNextResultSet(stmt);
count++;
- globalRowValueCache.clear();
}
return collapseSingleResultList(multipleResults);
}
- private void handleResultSet(ResultSet rs, ResultMap resultMap, List
multipleResults) throws SQLException {
+ protected void handleResultSet(ResultSet rs, ResultMap resultMap, List
multipleResults) throws SQLException {
if (resultHandler == null) {
DefaultResultHandler defaultResultHandler = new DefaultResultHandler();
handleRowValues(rs, resultMap, defaultResultHandler, rowBounds);
@@ -114,7 +110,7 @@
}
}
- private List collapseSingleResultList(List multipleResults) {
+ protected List collapseSingleResultList(List multipleResults) {
if (multipleResults.size() == 1) {
return (List) multipleResults.get(0);
} else {
@@ -126,26 +122,22 @@
// HANDLE ROWS
//
- private void handleRowValues(ResultSet rs, ResultMap resultMap,
ResultHandler resultHandler, RowBounds rowBounds) throws SQLException {
+ protected void handleRowValues(ResultSet rs, ResultMap resultMap,
ResultHandler resultHandler, RowBounds rowBounds) throws SQLException {
final DefaultResultContext resultContext = new DefaultResultContext();
skipRows(rs, rowBounds);
while (shouldProcessMoreRows(rs, resultContext.getResultCount(),
rowBounds)) {
final ResultMap discriminatedResultMap =
resolveDiscriminatedResultMap(rs, resultMap);
- final CacheKey rowKey = createRowKey(discriminatedResultMap, rs);
- final boolean knownValue = globalRowValueCache.containsKey(rowKey);
- Object rowValue = getRowValue(rs, discriminatedResultMap, rowKey);
- if (!knownValue) {
- resultContext.nextResultObject(rowValue);
- resultHandler.handleResult(resultContext);
- }
+ Object rowValue = getRowValue(rs, discriminatedResultMap, null);
+ resultContext.nextResultObject(rowValue);
+ resultHandler.handleResult(resultContext);
}
}
- private boolean shouldProcessMoreRows(ResultSet rs, int count, RowBounds
rowBounds) throws SQLException {
+ protected boolean shouldProcessMoreRows(ResultSet rs, int count, RowBounds
rowBounds) throws SQLException {
return rs.next() && count < rowBounds.getLimit();
}
- private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException
{
+ protected void skipRows(ResultSet rs, RowBounds rowBounds) throws
SQLException {
if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
rs.absolute(rowBounds.getOffset());
} else {
@@ -153,7 +145,7 @@
}
}
- private ResultSet getNextResultSet(Statement stmt) throws SQLException {
+ protected ResultSet getNextResultSet(Statement stmt) throws SQLException {
// Making this method tolerant of bad JDBC drivers
try {
if (stmt.getConnection().getMetaData().supportsMultipleResultSets()) {
@@ -172,34 +164,24 @@
// GET VALUE FROM ROW
//
- private Object getRowValue(ResultSet rs, ResultMap resultMap, CacheKey
rowKey) throws SQLException {
- if (globalRowValueCache.containsKey(rowKey)) {
- final Object resultObject = globalRowValueCache.get(rowKey);
+ protected Object getRowValue(ResultSet rs, ResultMap resultMap, CacheKey
rowKey) throws SQLException {
+ final List<String> mappedColumnNames = new ArrayList<String>();
+ final List<String> unmappedColumnNames = new ArrayList<String>();
+ final ResultLoaderRegistry lazyLoader = instantiateResultLoaderRegistry();
+ Object resultObject = createResultObject(rs, resultMap, lazyLoader);
+ if (resultObject != null &&
!typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {
final MetaObject metaObject = MetaObject.forObject(resultObject);
- applyNestedResultMappings(rs, resultMap, metaObject);
- return resultObject;
- } else {
- final List<String> mappedColumnNames = new ArrayList<String>();
- final List<String> unmappedColumnNames = new ArrayList<String>();
- final ResultLoaderRegistry lazyLoader =
instantiateResultLoaderRegistry();
- Object resultObject = createResultObject(rs, resultMap, lazyLoader);
- if (resultObject != null &&
!typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {
- final MetaObject metaObject = MetaObject.forObject(resultObject);
- loadMappedAndUnmappedColumnNames(rs, resultMap, mappedColumnNames,
unmappedColumnNames);
- boolean foundValues = resultMap.getConstructorResultMappings().size()
> 0;
- foundValues = applyPropertyMappings(rs, resultMap, mappedColumnNames,
metaObject, lazyLoader) || foundValues;
- foundValues = applyAutomaticMappings(rs, unmappedColumnNames,
metaObject) || foundValues;
- foundValues = applyNestedResultMappings(rs, resultMap, metaObject) ||
foundValues;
- resultObject = foundValues ? resultObject : null;
- }
- if (rowKey != CacheKey.NULL_CACHE_KEY) {
- globalRowValueCache.put(rowKey, resultObject);
- }
+ loadMappedAndUnmappedColumnNames(rs, resultMap, mappedColumnNames,
unmappedColumnNames);
+ boolean foundValues = resultMap.getConstructorResultMappings().size() >
0;
+ foundValues = applyPropertyMappings(rs, resultMap, mappedColumnNames,
metaObject, lazyLoader) || foundValues;
+ foundValues = applyAutomaticMappings(rs, unmappedColumnNames,
metaObject) || foundValues;
+ resultObject = foundValues ? resultObject : null;
return resultObject;
}
+ return null;
}
- private ResultLoaderRegistry instantiateResultLoaderRegistry() {
+ protected ResultLoaderRegistry instantiateResultLoaderRegistry() {
if (configuration.isLazyLoadingEnabled()) {
return new ResultLoaderRegistry();
} else {
@@ -211,7 +193,7 @@
// PROPERTY MAPPINGS
//
- private boolean applyPropertyMappings(ResultSet rs, ResultMap resultMap,
List<String> mappedColumnNames, MetaObject metaObject, ResultLoaderRegistry
lazyLoader) throws SQLException {
+ protected boolean applyPropertyMappings(ResultSet rs, ResultMap resultMap,
List<String> mappedColumnNames, MetaObject metaObject, ResultLoaderRegistry
lazyLoader) throws SQLException {
boolean foundValues = false;
final List<ResultMapping> propertyMappings =
resultMap.getPropertyResultMappings();
for (ResultMapping propertyMapping : propertyMappings) {
@@ -228,7 +210,7 @@
return foundValues;
}
- private Object getPropertyMappingValue(ResultSet rs, MetaObject
metaResultObject, ResultMapping propertyMapping, ResultLoaderRegistry
lazyLoader) throws SQLException {
+ protected Object getPropertyMappingValue(ResultSet rs, MetaObject
metaResultObject, ResultMapping propertyMapping, ResultLoaderRegistry
lazyLoader) throws SQLException {
final TypeHandler typeHandler = propertyMapping.getTypeHandler();
if (propertyMapping.getNestedQueryId() != null) {
return getNestedQueryMappingValue(rs, metaResultObject, propertyMapping,
lazyLoader);
@@ -239,7 +221,7 @@
return null;
}
- private boolean applyAutomaticMappings(ResultSet rs, List<String>
unmappedColumnNames, MetaObject metaObject) throws SQLException {
+ protected boolean applyAutomaticMappings(ResultSet rs, List<String>
unmappedColumnNames, MetaObject metaObject) throws SQLException {
boolean foundValues = false;
for (String columnName : unmappedColumnNames) {
final String property = metaObject.findProperty(columnName);
@@ -258,7 +240,7 @@
return foundValues;
}
- private void loadMappedAndUnmappedColumnNames(ResultSet rs, ResultMap
resultMap, List<String> mappedColumnNames, List<String> unmappedColumnNames)
throws SQLException {
+ protected void loadMappedAndUnmappedColumnNames(ResultSet rs, ResultMap
resultMap, List<String> mappedColumnNames, List<String> unmappedColumnNames)
throws SQLException {
mappedColumnNames.clear();
unmappedColumnNames.clear();
final ResultSetMetaData rsmd = rs.getMetaData();
@@ -281,7 +263,7 @@
// INSTANTIATION & CONSTRUCTOR MAPPING
//
- private Object createResultObject(ResultSet rs, ResultMap resultMap,
ResultLoaderRegistry lazyLoader) throws SQLException {
+ protected Object createResultObject(ResultSet rs, ResultMap resultMap,
ResultLoaderRegistry lazyLoader) throws SQLException {
final Object resultObject = createResultObject(rs, resultMap);
if (resultObject != null && configuration.isLazyLoadingEnabled()) {
return ResultObjectProxy.createProxy(resultMap.getType(), resultObject,
lazyLoader);
@@ -289,7 +271,7 @@
return resultObject;
}
- private Object createResultObject(ResultSet rs, ResultMap resultMap) throws
SQLException {
+ protected Object createResultObject(ResultSet rs, ResultMap resultMap)
throws SQLException {
final Class resultType = resultMap.getType();
final List<ResultMapping> constructorMappings =
resultMap.getConstructorResultMappings();
if (typeHandlerRegistry.hasTypeHandler(resultType)) {
@@ -301,7 +283,7 @@
}
}
- private Object createParameterizedResultObject(ResultSet rs, Class
resultType, List<ResultMapping> constructorMappings) throws SQLException {
+ protected Object createParameterizedResultObject(ResultSet rs, Class
resultType, List<ResultMapping> constructorMappings) throws SQLException {
boolean foundValues = false;
final List<Class> parameterTypes = new ArrayList<Class>();
final List<Object> parameterValues = new ArrayList<Object>();
@@ -317,7 +299,7 @@
return foundValues ? objectFactory.create(resultType, parameterTypes,
parameterValues) : null;
}
- private Object createPrimitiveResultObject(ResultSet rs, ResultMap
resultMap) throws SQLException {
+ protected Object createPrimitiveResultObject(ResultSet rs, ResultMap
resultMap) throws SQLException {
final Class resultType = resultMap.getType();
final String columnName;
if (resultMap.getResultMappings().size() > 0) {
@@ -336,7 +318,7 @@
// NESTED QUERY
//
- private Object getNestedQueryMappingValue(ResultSet rs, MetaObject
metaResultObject, ResultMapping propertyMapping, ResultLoaderRegistry
lazyLoader) throws SQLException {
+ protected Object getNestedQueryMappingValue(ResultSet rs, MetaObject
metaResultObject, ResultMapping propertyMapping, ResultLoaderRegistry
lazyLoader) throws SQLException {
final String nestedQueryId = propertyMapping.getNestedQueryId();
final String property = propertyMapping.getProperty();
final MappedStatement nestedQuery =
configuration.getMappedStatement(nestedQueryId);
@@ -359,7 +341,7 @@
return value;
}
- private Object prepareParameterForNestedQuery(ResultSet rs, ResultMapping
resultMapping, Class parameterType) throws SQLException {
+ protected Object prepareParameterForNestedQuery(ResultSet rs, ResultMapping
resultMapping, Class parameterType) throws SQLException {
if (resultMapping.isCompositeResult()) {
return prepareCompositeKeyParameter(rs, resultMapping, parameterType);
} else {
@@ -367,7 +349,7 @@
}
}
- private Object prepareSimpleKeyParameter(ResultSet rs, ResultMapping
resultMapping, Class parameterType) throws SQLException {
+ protected Object prepareSimpleKeyParameter(ResultSet rs, ResultMapping
resultMapping, Class parameterType) throws SQLException {
final TypeHandler typeHandler;
if (typeHandlerRegistry.hasTypeHandler(parameterType)) {
typeHandler = typeHandlerRegistry.getTypeHandler(parameterType);
@@ -377,7 +359,7 @@
return typeHandler.getResult(rs, resultMapping.getColumn());
}
- private Object prepareCompositeKeyParameter(ResultSet rs, ResultMapping
resultMapping, Class parameterType) throws SQLException {
+ protected Object prepareCompositeKeyParameter(ResultSet rs, ResultMapping
resultMapping, Class parameterType) throws SQLException {
final Object parameterObject = instantiateParameterObject(parameterType);
final MetaObject metaObject = MetaObject.forObject(parameterObject);
for (ResultMapping innerResultMapping : resultMapping.getComposites()) {
@@ -389,7 +371,7 @@
return parameterObject;
}
- private Object instantiateParameterObject(Class parameterType) {
+ protected Object instantiateParameterObject(Class parameterType) {
if (parameterType == null) {
return new HashMap();
} else {
@@ -413,7 +395,7 @@
return resultMap;
}
- private Object getDiscriminatorValue(ResultSet rs, Discriminator
discriminator) throws SQLException {
+ protected Object getDiscriminatorValue(ResultSet rs, Discriminator
discriminator) throws SQLException {
final ResultMapping resultMapping = discriminator.getResultMapping();
final TypeHandler typeHandler = resultMapping.getTypeHandler();
if (typeHandler != null) {
@@ -423,158 +405,4 @@
}
}
- //
- // NESTED RESULT MAP (JOIN MAPPING)
- //
-
- private boolean applyNestedResultMappings(ResultSet rs, ResultMap resultMap,
MetaObject metaObject) {
- boolean foundValues = false;
- for (ResultMapping resultMapping : resultMap.getPropertyResultMappings()) {
- final String nestedResultMapId = resultMapping.getNestedResultMapId();
- if (nestedResultMapId != null) {
- try {
- final ResultMap nestedResultMap = getNestedResultMap(rs,
nestedResultMapId);
- final Object collectionProperty =
instantiateCollectionPropertyIfAppropriate(resultMapping, metaObject);
-
- final CacheKey parentRowKey = createRowKey(resultMap, rs);
- final CacheKey rowKey = createRowKey(nestedResultMap, rs);
- final Set<CacheKey> localRowValueCache =
getRowValueCache(parentRowKey);
- final boolean knownValue = localRowValueCache.contains(rowKey);
- localRowValueCache.add(rowKey);
- Object rowValue = getRowValue(rs, nestedResultMap, rowKey);
-
- if (rowValue != null) {
- if (collectionProperty != null && collectionProperty instanceof
Collection) {
- if (!knownValue) {
- ((Collection) collectionProperty).add(rowValue);
- }
- } else {
- metaObject.setValue(resultMapping.getProperty(), rowValue);
- }
- foundValues = true;
- }
-
- } catch (Exception e) {
- throw new ExecutorException("Error getting nested result map values
for '" + resultMapping.getProperty() + "'. Cause: " + e, e);
- }
- }
- }
- return foundValues;
- }
-
- private Set<CacheKey> getRowValueCache(CacheKey rowKey) {
- Set<CacheKey> cache = localRowValueCaches.get(rowKey);
- if (cache == null) {
- cache = new HashSet<CacheKey>();
- localRowValueCaches.put(rowKey, cache);
- }
- return cache;
- }
-
- private Object instantiateCollectionPropertyIfAppropriate(ResultMapping
resultMapping, MetaObject metaObject) {
- final String propertyName = resultMapping.getProperty();
- Class type = resultMapping.getJavaType();
- Object propertyValue = metaObject.getValue(propertyName);
- if (propertyValue == null) {
- if (type == null) {
- type = metaObject.getSetterType(propertyName);
- }
- try {
- if (Collection.class.isAssignableFrom(type)) {
- propertyValue = objectFactory.create(type);
- metaObject.setValue(propertyName, propertyValue);
- }
- } catch (Exception e) {
- throw new ExecutorException("Error instantiating collection property
for result '" + resultMapping.getProperty() + "'. Cause: " + e, e);
- }
- }
- return propertyValue;
- }
-
- private ResultMap getNestedResultMap(ResultSet rs, String nestedResultMapId)
throws SQLException {
- ResultMap nestedResultMap = configuration.getResultMap(nestedResultMapId);
- nestedResultMap = resolveDiscriminatedResultMap(rs, nestedResultMap);
- return nestedResultMap;
- }
-
- //
- // UNIQUE RESULT KEY
- //
-
- private CacheKey createRowKey(ResultMap resultMap, ResultSet rs) throws
SQLException {
- final CacheKey cacheKey = new CacheKey();
- List<ResultMapping> resultMappings = getResultMappingsForRowKey(resultMap);
- cacheKey.update(resultMap.getId());
- if (resultMappings.size() == 0) {
- if (Map.class.isAssignableFrom(resultMap.getType())) {
- createRowKeyForMap(rs, cacheKey);
- } else {
- createRowKeyForUnmappedProperties(resultMap, rs, cacheKey);
- }
- } else {
- createRowKeyForMappedProperties(rs, cacheKey, resultMappings);
- }
- if (cacheKey.getUpdateCount() < 2) {
- return CacheKey.NULL_CACHE_KEY;
- }
- return cacheKey;
- }
-
- private List<ResultMapping> getResultMappingsForRowKey(ResultMap resultMap) {
- List<ResultMapping> resultMappings = resultMap.getIdResultMappings();
- if (resultMappings.size() == 0) {
- resultMappings = resultMap.getPropertyResultMappings();
- }
- return resultMappings;
- }
-
- private void createRowKeyForMappedProperties(ResultSet rs, CacheKey
cacheKey, List<ResultMapping> resultMappings) {
- for (ResultMapping resultMapping : resultMappings) {
- if (resultMapping.getNestedQueryId() == null &&
resultMapping.getNestedResultMapId() == null) {
- final String column = resultMapping.getColumn();
- final TypeHandler th = resultMapping.getTypeHandler();
- if (column != null) {
- try {
- final Object value = th.getResult(rs, column);
- if (value != null) {
- cacheKey.update(column);
- cacheKey.update(value);
- }
- } catch (Exception e) {
- //ignore
- }
- }
- }
- }
- }
-
- private void createRowKeyForUnmappedProperties(ResultMap resultMap,
ResultSet rs, CacheKey cacheKey) throws SQLException {
- final MetaClass metaType = MetaClass.forClass(resultMap.getType());
- final List<String> mappedColumnNames = new ArrayList<String>();
- final List<String> unmappedColumnNames = new ArrayList<String>();
- loadMappedAndUnmappedColumnNames(rs, resultMap, mappedColumnNames,
unmappedColumnNames);
- for (String column : unmappedColumnNames) {
- if (metaType.findProperty(column) != null) {
- String value = rs.getString(column);
- if (value != null) {
- cacheKey.update(column);
- cacheKey.update(value);
- }
- }
- }
- }
-
- private void createRowKeyForMap(ResultSet rs, CacheKey cacheKey) throws
SQLException {
- final ResultSetMetaData rsmd = rs.getMetaData();
- final int columnCount = rsmd.getColumnCount();
- for (int i = 1; i <= columnCount; i++) {
- final String columnName = configuration.isUseColumnLabel() ?
rsmd.getColumnLabel(i) : rsmd.getColumnName(i);
- final String value = rs.getString(columnName);
- if (value != null) {
- cacheKey.update(columnName);
- cacheKey.update(value);
- }
- }
- }
-
}
\ No newline at end of file