private Object createParameterizedResultObject(
     ResultSetWrapper rsw,
     Class<?> resultType,
     List<ResultMapping> constructorMappings,
     List<Class<?>> constructorArgTypes,
     List<Object> constructorArgs,
     String columnPrefix)
     throws SQLException {
   boolean foundValues = false;
   for (ResultMapping constructorMapping : constructorMappings) {
     final Class<?> parameterType = constructorMapping.getJavaType();
     final String column = constructorMapping.getColumn();
     final Object value;
     if (constructorMapping.getNestedQueryId() != null) {
       value =
           getNestedQueryConstructorValue(rsw.getResultSet(), constructorMapping, columnPrefix);
     } else if (constructorMapping.getNestedResultMapId() != null) {
       final ResultMap resultMap =
           configuration.getResultMap(constructorMapping.getNestedResultMapId());
       value = getRowValue(rsw, resultMap);
     } else {
       final TypeHandler<?> typeHandler = constructorMapping.getTypeHandler();
       value = typeHandler.getResult(rsw.getResultSet(), prependPrefix(column, columnPrefix));
     }
     constructorArgTypes.add(parameterType);
     constructorArgs.add(value);
     foundValues = value != null || foundValues;
   }
   return foundValues
       ? objectFactory.create(resultType, constructorArgTypes, constructorArgs)
       : null;
 }
 private void createRowKeyForMappedProperties(
     ResultMap resultMap,
     ResultSetWrapper rsw,
     CacheKey cacheKey,
     List<ResultMapping> resultMappings,
     String columnPrefix)
     throws SQLException {
   for (ResultMapping resultMapping : resultMappings) {
     if (resultMapping.getNestedResultMapId() != null
         && resultMapping.getResultSet() == null) { // Issue #392
       final ResultMap nestedResultMap =
           configuration.getResultMap(resultMapping.getNestedResultMapId());
       createRowKeyForMappedProperties(
           nestedResultMap,
           rsw,
           cacheKey,
           nestedResultMap.getConstructorResultMappings(),
           prependPrefix(resultMapping.getColumnPrefix(), columnPrefix));
     } else if (resultMapping.getNestedQueryId() == null) {
       final String column = prependPrefix(resultMapping.getColumn(), columnPrefix);
       final TypeHandler<?> th = resultMapping.getTypeHandler();
       List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
       if (column != null
           && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) { // Issue #114
         final Object value = th.getResult(rsw.getResultSet(), column);
         if (value != null) {
           cacheKey.update(column);
           cacheKey.update(value);
         }
       }
     }
   }
 }
  public List<Object> handleResultSets(Statement stmt) throws SQLException {
    final List<Object> multipleResults = new ArrayList<Object>();

    int resultSetCount = 0;
    ResultSetWrapper rsw = getFirstResultSet(stmt);

    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    validateResultMapsCount(rsw, resultMapCount);
    while (rsw != null && resultMapCount > resultSetCount) {
      ResultMap resultMap = resultMaps.get(resultSetCount);
      handleResultSet(rsw, resultMap, multipleResults, null);
      rsw = getNextResultSet(stmt);
      cleanUpAfterHandlingResultSet();
      resultSetCount++;
    }

    String[] resultSets = mappedStatement.getResulSets();
    if (resultSets != null) {
      while (rsw != null && resultSetCount < resultSets.length) {
        ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);
        if (parentMapping != null) {
          String nestedResultMapId = parentMapping.getNestedResultMapId();
          ResultMap resultMap = configuration.getResultMap(nestedResultMapId);
          handleResultSet(rsw, resultMap, null, parentMapping);
        }
        rsw = getNextResultSet(stmt);
        cleanUpAfterHandlingResultSet();
        resultSetCount++;
      }
    }

    return collapseSingleResultList(multipleResults);
  }
 private boolean applyNestedResultMappings(
     ResultSetWrapper rsw,
     ResultMap resultMap,
     MetaObject metaObject,
     String parentPrefix,
     CacheKey parentRowKey,
     boolean newObject) {
   boolean foundValues = false;
   for (ResultMapping resultMapping : resultMap.getPropertyResultMappings()) {
     final String nestedResultMapId = resultMapping.getNestedResultMapId();
     if (nestedResultMapId != null && resultMapping.getResultSet() == null) {
       try {
         final String columnPrefix = getColumnPrefix(parentPrefix, resultMapping);
         final ResultMap nestedResultMap =
             getNestedResultMap(rsw.getResultSet(), nestedResultMapId, columnPrefix);
         CacheKey rowKey = null;
         Object ancestorObject = null;
         if (ancestorColumnPrefix.containsKey(nestedResultMapId)) {
           rowKey =
               createRowKey(nestedResultMap, rsw, ancestorColumnPrefix.get(nestedResultMapId));
           ancestorObject = ancestorObjects.get(rowKey);
         }
         if (ancestorObject != null) {
           if (newObject) metaObject.setValue(resultMapping.getProperty(), ancestorObject);
         } else {
           rowKey = createRowKey(nestedResultMap, rsw, columnPrefix);
           final CacheKey combinedKey = combineKeys(rowKey, parentRowKey);
           Object rowValue = nestedResultObjects.get(combinedKey);
           boolean knownValue = (rowValue != null);
           final Object collectionProperty =
               instantiateCollectionPropertyIfAppropriate(resultMapping, metaObject);
           if (anyNotNullColumnHasValue(resultMapping, columnPrefix, rsw.getResultSet())) {
             rowValue =
                 getRowValue(rsw, nestedResultMap, combinedKey, rowKey, columnPrefix, rowValue);
             if (rowValue != null && !knownValue) {
               if (collectionProperty != null) {
                 final MetaObject targetMetaObject =
                     configuration.newMetaObject(collectionProperty);
                 targetMetaObject.add(rowValue);
               } else {
                 metaObject.setValue(resultMapping.getProperty(), rowValue);
               }
               foundValues = true;
             }
           }
         }
       } catch (SQLException e) {
         throw new ExecutorException(
             "Error getting nested result map values for '"
                 + resultMapping.getProperty()
                 + "'.  Cause: "
                 + e,
             e);
       }
     }
   }
   return foundValues;
 }
 private Object getPropertyMappingValue(
     ResultSet rs,
     MetaObject metaResultObject,
     ResultMapping propertyMapping,
     ResultLoaderMap lazyLoader,
     String columnPrefix)
     throws SQLException {
   if (propertyMapping.getNestedQueryId() != null) {
     return getNestedQueryMappingValue(
         rs, metaResultObject, propertyMapping, lazyLoader, columnPrefix);
   } else if (propertyMapping.getResultSet() != null) {
     addPendingChildRelation(rs, metaResultObject, propertyMapping);
     return NO_VALUE;
   } else if (propertyMapping.getNestedResultMapId() != null) {
     // the user added a column attribute to a nested result map, ignore it
     return NO_VALUE;
   } else {
     final TypeHandler<?> typeHandler = propertyMapping.getTypeHandler();
     final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
     return typeHandler.getResult(rs, column);
   }
 }