Search in sources :

Example 21 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class BaseExecutor method handleLocallyCachedOutputParameters.

private void handleLocallyCachedOutputParameters(MappedStatement ms, CacheKey key, Object parameter, BoundSql boundSql) {
    if (ms.getStatementType() == StatementType.CALLABLE) {
        final Object cachedParameter = localOutputParameterCache.getObject(key);
        if (cachedParameter != null && parameter != null) {
            final MetaObject metaCachedParameter = configuration.newMetaObject(cachedParameter);
            final MetaObject metaParameter = configuration.newMetaObject(parameter);
            for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) {
                if (parameterMapping.getMode() != ParameterMode.IN) {
                    final String parameterName = parameterMapping.getProperty();
                    final Object cachedValue = metaCachedParameter.getValue(parameterName);
                    metaParameter.setValue(parameterName, cachedValue);
                }
            }
        }
    }
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 22 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method getRowValue.

//
// GET VALUE FROM ROW FOR NESTED RESULT MAP
//
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey combinedKey, String columnPrefix, Object partialObject) throws SQLException {
    final String resultMapId = resultMap.getId();
    Object rowValue = partialObject;
    if (rowValue != null) {
        final MetaObject metaObject = configuration.newMetaObject(rowValue);
        putAncestor(rowValue, resultMapId, columnPrefix);
        applyNestedResultMappings(rsw, resultMap, metaObject, columnPrefix, combinedKey, false);
        ancestorObjects.remove(resultMapId);
    } else {
        final ResultLoaderMap lazyLoader = new ResultLoaderMap();
        rowValue = createResultObject(rsw, resultMap, lazyLoader, columnPrefix);
        if (rowValue != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
            final MetaObject metaObject = configuration.newMetaObject(rowValue);
            boolean foundValues = this.useConstructorMappings;
            if (shouldApplyAutomaticMappings(resultMap, true)) {
                foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, columnPrefix) || foundValues;
            }
            foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, columnPrefix) || foundValues;
            putAncestor(rowValue, resultMapId, columnPrefix);
            foundValues = applyNestedResultMappings(rsw, resultMap, metaObject, columnPrefix, combinedKey, true) || foundValues;
            ancestorObjects.remove(resultMapId);
            foundValues = lazyLoader.size() > 0 || foundValues;
            rowValue = (foundValues || configuration.isReturnInstanceForEmptyRow()) ? rowValue : null;
        }
        if (combinedKey != CacheKey.NULL_CACHE_KEY) {
            nestedResultObjects.put(combinedKey, rowValue);
        }
    }
    return rowValue;
}
Also used : MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) ResultLoaderMap(org.apache.ibatis.executor.loader.ResultLoaderMap)

Example 23 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method getNestedQueryMappingValue.

private Object getNestedQueryMappingValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping, ResultLoaderMap lazyLoader, String columnPrefix) 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, columnPrefix);
    Object value = null;
    if (nestedQueryParameterObject != null) {
        final BoundSql nestedBoundSql = nestedQuery.getBoundSql(nestedQueryParameterObject);
        final CacheKey key = executor.createCacheKey(nestedQuery, nestedQueryParameterObject, RowBounds.DEFAULT, nestedBoundSql);
        final Class<?> targetType = propertyMapping.getJavaType();
        if (executor.isCached(nestedQuery, key)) {
            executor.deferLoad(nestedQuery, metaResultObject, property, key, targetType);
            value = DEFERED;
        } else {
            final ResultLoader resultLoader = new ResultLoader(configuration, executor, nestedQuery, nestedQueryParameterObject, targetType, key, nestedBoundSql);
            if (propertyMapping.isLazy()) {
                lazyLoader.addLoader(property, metaResultObject, resultLoader);
                value = DEFERED;
            } else {
                value = resultLoader.loadResult();
            }
        }
    }
    return value;
}
Also used : ResultLoader(org.apache.ibatis.executor.loader.ResultLoader) BoundSql(org.apache.ibatis.mapping.BoundSql) MetaObject(org.apache.ibatis.reflection.MetaObject) MappedStatement(org.apache.ibatis.mapping.MappedStatement) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 24 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method instantiateCollectionPropertyIfAppropriate.

private Object instantiateCollectionPropertyIfAppropriate(ResultMapping resultMapping, MetaObject metaObject) {
    final String propertyName = resultMapping.getProperty();
    Object propertyValue = metaObject.getValue(propertyName);
    if (propertyValue == null) {
        Class<?> type = resultMapping.getJavaType();
        if (type == null) {
            type = metaObject.getSetterType(propertyName);
        }
        try {
            if (objectFactory.isCollection(type)) {
                propertyValue = objectFactory.create(type);
                metaObject.setValue(propertyName, propertyValue);
                return propertyValue;
            }
        } catch (Exception e) {
            throw new ExecutorException("Error instantiating collection property for result '" + resultMapping.getProperty() + "'.  Cause: " + e, e);
        }
    } else if (objectFactory.isCollection(propertyValue.getClass())) {
        return propertyValue;
    }
    return null;
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) MetaObject(org.apache.ibatis.reflection.MetaObject) SQLException(java.sql.SQLException) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) ExecutorException(org.apache.ibatis.executor.ExecutorException)

Example 25 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method getRowValue.

//
// GET VALUE FROM ROW FOR SIMPLE RESULT MAP
//
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {
    final ResultLoaderMap lazyLoader = new ResultLoaderMap();
    Object rowValue = createResultObject(rsw, resultMap, lazyLoader, null);
    if (rowValue != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
        final MetaObject metaObject = configuration.newMetaObject(rowValue);
        boolean foundValues = this.useConstructorMappings;
        if (shouldApplyAutomaticMappings(resultMap, false)) {
            foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;
        }
        foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
        foundValues = lazyLoader.size() > 0 || foundValues;
        rowValue = (foundValues || configuration.isReturnInstanceForEmptyRow()) ? rowValue : null;
    }
    return rowValue;
}
Also used : MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) ResultLoaderMap(org.apache.ibatis.executor.loader.ResultLoaderMap)

Aggregations

MetaObject (org.apache.ibatis.reflection.MetaObject)33 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)9 ExecutorException (org.apache.ibatis.executor.ExecutorException)6 CacheKey (org.apache.ibatis.cache.CacheKey)5 SystemMetaObject (org.apache.ibatis.reflection.SystemMetaObject)5 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)5 SQLException (java.sql.SQLException)4 Configuration (org.apache.ibatis.session.Configuration)4 TypeHandler (org.apache.ibatis.type.TypeHandler)4 BoundSql (org.apache.ibatis.mapping.BoundSql)3 MappedStatement (org.apache.ibatis.mapping.MappedStatement)3 ResultMapping (org.apache.ibatis.mapping.ResultMapping)3 AutoMapperException (indi.mybatis.flying.exception.AutoMapperException)2 Conditionable (indi.mybatis.flying.models.Conditionable)2 FlyingModel (indi.mybatis.flying.models.FlyingModel)2 ResultSet (java.sql.ResultSet)2 CacheException (org.apache.ibatis.cache.CacheException)2 Executor (org.apache.ibatis.executor.Executor)2 ResultLoaderMap (org.apache.ibatis.executor.loader.ResultLoaderMap)2 JdbcType (org.apache.ibatis.type.JdbcType)2