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 boolean applyPropertyMappings(
     ResultSetWrapper rsw,
     ResultMap resultMap,
     MetaObject metaObject,
     ResultLoaderMap lazyLoader,
     String columnPrefix)
     throws SQLException {
   final List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
   boolean foundValues = false;
   final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings();
   for (ResultMapping propertyMapping : propertyMappings) {
     final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
     if (propertyMapping.isCompositeResult()
         || (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH)))
         || propertyMapping.getResultSet() != null) {
       Object value =
           getPropertyMappingValue(
               rsw.getResultSet(), metaObject, propertyMapping, lazyLoader, columnPrefix);
       final String property = propertyMapping.getProperty(); // issue #541 make property optional
       if (value != NO_VALUE
           && property != null
           && (value != null
               || configuration.isCallSettersOnNulls())) { // issue #377, call setter on nulls
         if (value != null || !metaObject.getSetterType(property).isPrimitive()) {
           metaObject.setValue(property, value);
         }
         foundValues = true;
       }
     }
   }
   return foundValues;
 }
 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);
         }
       }
     }
   }
 }
 private void addPendingChildRelation(
     ResultSet rs, MetaObject metaResultObject, ResultMapping parentMapping) throws SQLException {
   CacheKey cacheKey =
       createKeyForMultipleResults(
           rs, parentMapping, parentMapping.getColumn(), parentMapping.getColumn());
   PendingRelation deferLoad = new PendingRelation();
   deferLoad.metaObject = metaResultObject;
   deferLoad.propertyMapping = parentMapping;
   pendingRelations.put(cacheKey, deferLoad);
   ResultMapping previous = nextResultMaps.get(parentMapping.getResultSet());
   if (previous == null) {
     nextResultMaps.put(parentMapping.getResultSet(), parentMapping);
   } else {
     if (!previous.equals(parentMapping)) {
       throw new ExecutorException("Two different properties are mapped to the same resultSet");
     }
   }
 }
 private Object prepareSimpleKeyParameter(
     ResultSet rs, ResultMapping resultMapping, Class<?> parameterType, String columnPrefix)
     throws SQLException {
   final TypeHandler<?> typeHandler;
   if (typeHandlerRegistry.hasTypeHandler(parameterType)) {
     typeHandler = typeHandlerRegistry.getTypeHandler(parameterType);
   } else {
     typeHandler = typeHandlerRegistry.getUnknownTypeHandler();
   }
   return typeHandler.getResult(rs, prependPrefix(resultMapping.getColumn(), columnPrefix));
 }
 private Object createPrimitiveResultObject(
     ResultSetWrapper rsw, ResultMap resultMap, String columnPrefix) 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 = prependPrefix(mapping.getColumn(), columnPrefix);
   } else {
     columnName = rsw.getColumnNames().get(0);
   }
   final TypeHandler<?> typeHandler = rsw.getTypeHandler(resultType, columnName);
   return typeHandler.getResult(rsw.getResultSet(), columnName);
 }
 private Object prepareCompositeKeyParameter(
     ResultSet rs, ResultMapping resultMapping, Class<?> parameterType, String columnPrefix)
     throws SQLException {
   final Object parameterObject = instantiateParameterObject(parameterType);
   final MetaObject metaObject = configuration.newMetaObject(parameterObject);
   boolean foundValues = false;
   for (ResultMapping innerResultMapping : resultMapping.getComposites()) {
     final Class<?> propType = metaObject.getSetterType(innerResultMapping.getProperty());
     final TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(propType);
     final Object propValue =
         typeHandler.getResult(rs, prependPrefix(innerResultMapping.getColumn(), columnPrefix));
     if (propValue != null) { // issue #353 & #560 do not execute nested query if key is null
       metaObject.setValue(innerResultMapping.getProperty(), propValue);
       foundValues = true;
     }
   }
   return foundValues ? parameterObject : null;
 }
 private void linkToParent(ResultSet rs, ResultMapping parentMapping, Object rowValue)
     throws SQLException {
   CacheKey parentKey =
       createKeyForMultipleResults(
           rs, parentMapping, parentMapping.getColumn(), parentMapping.getForeignColumn());
   PendingRelation parent = pendingRelations.get(parentKey);
   if (parent != null) {
     final Object collectionProperty =
         instantiateCollectionPropertyIfAppropriate(parent.propertyMapping, parent.metaObject);
     if (rowValue != null) {
       if (collectionProperty != null) {
         final MetaObject targetMetaObject = configuration.newMetaObject(collectionProperty);
         targetMetaObject.add(rowValue);
       } else {
         parent.metaObject.setValue(parent.propertyMapping.getProperty(), rowValue);
       }
     }
   }
 }
 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);
   }
 }
Ejemplo n.º 10
0
 private Object getDiscriminatorValue(
     ResultSet rs, Discriminator discriminator, String columnPrefix) throws SQLException {
   final ResultMapping resultMapping = discriminator.getResultMapping();
   final TypeHandler<?> typeHandler = resultMapping.getTypeHandler();
   return typeHandler.getResult(rs, prependPrefix(resultMapping.getColumn(), columnPrefix));
 }