Search in sources :

Example 26 with MetaObject

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

the class Jdbc3KeyGenerator method populateKeys.

private void populateKeys(ResultSet rs, MetaObject metaParam, String[] keyProperties, TypeHandler<?>[] typeHandlers) throws SQLException {
    for (int i = 0; i < keyProperties.length; i++) {
        String property = keyProperties[i];
        if (!metaParam.hasSetter(property)) {
            throw new ExecutorException("No setter found for the keyProperty '" + property + "' in " + metaParam.getOriginalObject().getClass().getName() + ".");
        }
        TypeHandler<?> th = typeHandlers[i];
        if (th != null) {
            Object value = th.getResult(rs, i + 1);
            metaParam.setValue(property, value);
        }
    }
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 27 with MetaObject

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

the class SelectKeyGenerator method processGeneratedKeys.

private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
    try {
        if (parameter != null && keyStatement != null && keyStatement.getKeyProperties() != null) {
            String[] keyProperties = keyStatement.getKeyProperties();
            final Configuration configuration = ms.getConfiguration();
            final MetaObject metaParam = configuration.newMetaObject(parameter);
            if (keyProperties != null) {
                // Do not close keyExecutor.
                // The transaction will be closed by parent executor.
                Executor keyExecutor = configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
                List<Object> values = keyExecutor.query(keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
                if (values.size() == 0) {
                    throw new ExecutorException("SelectKey returned no data.");
                } else if (values.size() > 1) {
                    throw new ExecutorException("SelectKey returned more than one value.");
                } else {
                    MetaObject metaResult = configuration.newMetaObject(values.get(0));
                    if (keyProperties.length == 1) {
                        if (metaResult.hasGetter(keyProperties[0])) {
                            setValue(metaParam, keyProperties[0], metaResult.getValue(keyProperties[0]));
                        } else {
                            // no getter for the property - maybe just a single value object
                            // so try that
                            setValue(metaParam, keyProperties[0], values.get(0));
                        }
                    } else {
                        handleMultipleProperties(keyProperties, metaParam, metaResult);
                    }
                }
            }
        }
    } catch (ExecutorException e) {
        throw e;
    } catch (Exception e) {
        throw new ExecutorException("Error selecting key or setting result to parameter object. Cause: " + e, e);
    }
}
Also used : Executor(org.apache.ibatis.executor.Executor) ExecutorException(org.apache.ibatis.executor.ExecutorException) Configuration(org.apache.ibatis.session.Configuration) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) ExecutorException(org.apache.ibatis.executor.ExecutorException)

Example 28 with MetaObject

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

the class ResultExtractorTest method shouldExtractSet.

@Test
public void shouldExtractSet() {
    final List list = Arrays.asList(1, 2, 3);
    final Class<Set> targetType = Set.class;
    final Set set = new HashSet();
    final MetaObject metaObject = mock(MetaObject.class);
    when(objectFactory.isCollection(targetType)).thenReturn(true);
    when(objectFactory.create(targetType)).thenReturn(set);
    when(configuration.newMetaObject(set)).thenReturn(metaObject);
    final Set result = (Set) resultExtractor.extractObjectFromList(list, targetType);
    assertThat(result, sameInstance(set));
    verify(metaObject).addAll(list);
}
Also used : MetaObject(org.apache.ibatis.reflection.MetaObject) Test(org.junit.Test)

Example 29 with MetaObject

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

the class CacheBuilder method setCacheProperties.

private void setCacheProperties(Cache cache) {
    if (properties != null) {
        MetaObject metaCache = SystemMetaObject.forObject(cache);
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            String name = (String) entry.getKey();
            String value = (String) entry.getValue();
            if (metaCache.hasSetter(name)) {
                Class<?> type = metaCache.getSetterType(name);
                if (String.class == type) {
                    metaCache.setValue(name, value);
                } else if (int.class == type || Integer.class == type) {
                    metaCache.setValue(name, Integer.valueOf(value));
                } else if (long.class == type || Long.class == type) {
                    metaCache.setValue(name, Long.valueOf(value));
                } else if (short.class == type || Short.class == type) {
                    metaCache.setValue(name, Short.valueOf(value));
                } else if (byte.class == type || Byte.class == type) {
                    metaCache.setValue(name, Byte.valueOf(value));
                } else if (float.class == type || Float.class == type) {
                    metaCache.setValue(name, Float.valueOf(value));
                } else if (boolean.class == type || Boolean.class == type) {
                    metaCache.setValue(name, Boolean.valueOf(value));
                } else if (double.class == type || Double.class == type) {
                    metaCache.setValue(name, Double.valueOf(value));
                } else {
                    throw new CacheException("Unsupported property type for cache: '" + name + "' of type " + type);
                }
            }
        }
    }
    if (InitializingObject.class.isAssignableFrom(cache.getClass())) {
        try {
            ((InitializingObject) cache).initialize();
        } catch (Exception e) {
            throw new CacheException("Failed cache initialization for '" + cache.getId() + "' on '" + cache.getClass().getName() + "'", e);
        }
    }
}
Also used : InitializingObject(org.apache.ibatis.builder.InitializingObject) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) CacheException(org.apache.ibatis.cache.CacheException) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) InitializingObject(org.apache.ibatis.builder.InitializingObject) MetaObject(org.apache.ibatis.reflection.MetaObject) Map(java.util.Map) CacheException(org.apache.ibatis.cache.CacheException)

Example 30 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis.flying by limeng32.

the class AutoMapperInterceptor method setParameters.

@SuppressWarnings("unchecked")
private 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);
                }
                TypeHandler<Object> typeHandler = (TypeHandler<Object>) parameterMapping.getTypeHandler();
                if (typeHandler == null) {
                    throw new AutoMapperException(new StringBuffer(AutoMapperExceptionEnum.noTypeHandlerSuitable.toString()).append(propertyName).append(" of statement ").append(mappedStatement.getId()).toString());
                }
                typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
            }
        }
    }
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) Configuration(org.apache.ibatis.session.Configuration) MetaObject(org.apache.ibatis.reflection.MetaObject) PropertyTokenizer(org.apache.ibatis.reflection.property.PropertyTokenizer) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) AutoMapperException(indi.mybatis.flying.exception.AutoMapperException) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler)

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