private boolean applyAutomaticMappings(
     ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix)
     throws SQLException {
   final List<String> unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix);
   boolean foundValues = false;
   for (String columnName : unmappedColumnNames) {
     String propertyName = columnName;
     if (columnPrefix != null && columnPrefix.length() > 0) {
       // When columnPrefix is specified,
       // ignore columns without the prefix.
       if (columnName.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) {
         propertyName = columnName.substring(columnPrefix.length());
       } else {
         continue;
       }
     }
     final String property =
         metaObject.findProperty(propertyName, configuration.isMapUnderscoreToCamelCase());
     if (property != null && metaObject.hasSetter(property)) {
       final Class<?> propertyType = metaObject.getSetterType(property);
       if (typeHandlerRegistry.hasTypeHandler(propertyType)) {
         final TypeHandler<?> typeHandler = rsw.getTypeHandler(propertyType, columnName);
         final Object value = typeHandler.getResult(rsw.getResultSet(), columnName);
         if (value != null
             || configuration.isCallSettersOnNulls()) { // issue #377, call setter on nulls
           if (value != null || !propertyType.isPrimitive()) {
             metaObject.setValue(property, value);
           }
           foundValues = true;
         }
       }
     }
   }
   return foundValues;
 }
 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);
         }
       }
     }
   }
 }
 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));
 }
Exemplo n.º 5
0
 /**
  * 对SQL参数(?)设值, 参考org.apache.ibatis.executor.parameter.DefaultParameterHandler。
  *
  * @param ps 表示预编译的 SQL 语句的对象。
  * @param mappedStatement MappedStatement
  * @param boundSql SQL
  * @param parameterObject 参数对象
  * @throws java.sql.SQLException 数据库异常
  */
 @SuppressWarnings("unchecked")
 public static void setParameters(
     PreparedStatement ps,
     MappedStatement mappedStatement,
     BoundSql boundSql,
     Object parameterObject)
     throws SQLException {
   ErrorContext.instance()
       .activity("setting parameters")
       .object(mappedStatement.getParameterMap().getId());
   List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
   if (parameterMappings != null) {
     Configuration configuration = mappedStatement.getConfiguration();
     TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
     MetaObject metaObject =
         parameterObject == null ? null : configuration.newMetaObject(parameterObject);
     for (int i = 0; i < parameterMappings.size(); i++) {
       ParameterMapping parameterMapping = parameterMappings.get(i);
       if (parameterMapping.getMode() != ParameterMode.OUT) {
         Object value;
         String propertyName = parameterMapping.getProperty();
         PropertyTokenizer prop = new PropertyTokenizer(propertyName);
         if (parameterObject == null) {
           value = null;
         } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
           value = parameterObject;
         } else if (boundSql.hasAdditionalParameter(propertyName)) {
           value = boundSql.getAdditionalParameter(propertyName);
         } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
             && boundSql.hasAdditionalParameter(prop.getName())) {
           value = boundSql.getAdditionalParameter(prop.getName());
           if (value != null) {
             value =
                 configuration
                     .newMetaObject(value)
                     .getValue(propertyName.substring(prop.getName().length()));
           }
         } else {
           value = metaObject == null ? null : metaObject.getValue(propertyName);
         }
         @SuppressWarnings("rawtypes")
         TypeHandler typeHandler = parameterMapping.getTypeHandler();
         if (typeHandler == null) {
           throw new ExecutorException(
               "There was no TypeHandler found for parameter "
                   + propertyName
                   + " of statement "
                   + mappedStatement.getId());
         }
         typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
       }
     }
   }
 }
 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;
 }
 public void handleOutputParameters(CallableStatement cs) throws SQLException {
   final Object parameterObject = parameterHandler.getParameterObject();
   final MetaObject metaParam = configuration.newMetaObject(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 (ResultSet.class.equals(parameterMapping.getJavaType())) {
         handleRefCursorOutputParameter(
             (ResultSet) cs.getObject(i + 1), parameterMapping, metaParam);
       } else {
         final TypeHandler<?> typeHandler = parameterMapping.getTypeHandler();
         metaParam.setValue(parameterMapping.getProperty(), typeHandler.getResult(cs, i + 1));
       }
     }
   }
 }
 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);
   }
 }
 public void setParameters(PreparedStatement ps) throws SQLException {
   ErrorContext.instance()
       .activity("setting parameters")
       .object(mappedStatement.getParameterMap().getId());
   List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
   if (parameterMappings != null) {
     MetaObject metaObject =
         parameterObject == null ? null : configuration.newMetaObject(parameterObject);
     for (int i = 0; i < parameterMappings.size(); i++) {
       ParameterMapping parameterMapping = parameterMappings.get(i);
       if (parameterMapping.getMode() != ParameterMode.OUT) {
         Object value;
         String propertyName = parameterMapping.getProperty();
         if (boundSql.hasAdditionalParameter(
             propertyName)) { // issue #448 ask first for additional params
           value = boundSql.getAdditionalParameter(propertyName);
         } else if (parameterObject == null) {
           value = null;
         } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
           value = parameterObject;
         } else {
           value = metaObject == null ? null : metaObject.getValue(propertyName);
         }
         TypeHandler typeHandler = parameterMapping.getTypeHandler();
         if (typeHandler == null) {
           throw new ExecutorException(
               "There was no TypeHandler found for parameter "
                   + propertyName
                   + " of statement "
                   + mappedStatement.getId());
         }
         JdbcType jdbcType = parameterMapping.getJdbcType();
         if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull();
         typeHandler.setParameter(ps, i + 1, value, jdbcType);
       }
     }
   }
 }
Exemplo n.º 11
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));
 }