Search in sources :

Example 1 with TypeHandler

use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.

the class MapperAnnotationBuilder method applyResults.

private void applyResults(Result[] results, Class<?> resultType, List<ResultMapping> resultMappings) {
    for (Result result : results) {
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        if (result.id()) {
            flags.add(ResultFlag.ID);
        }
        @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>) ((result.typeHandler() == UnknownTypeHandler.class) ? null : result.typeHandler());
        ResultMapping resultMapping = assistant.buildResultMapping(resultType, nullOrEmpty(result.property()), nullOrEmpty(result.column()), result.javaType() == void.class ? null : result.javaType(), result.jdbcType() == JdbcType.UNDEFINED ? null : result.jdbcType(), hasNestedSelect(result) ? nestedSelectId(result) : null, null, null, null, typeHandler, flags, null, null, isLazy(result));
        resultMappings.add(resultMapping);
    }
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) ResultFlag(org.apache.ibatis.mapping.ResultFlag) TypeHandler(org.apache.ibatis.type.TypeHandler) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) Result(org.apache.ibatis.annotations.Result)

Example 2 with TypeHandler

use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.

the class MapperAnnotationBuilder method applyDiscriminator.

private Discriminator applyDiscriminator(String resultMapId, Class<?> resultType, TypeDiscriminator discriminator) {
    if (discriminator != null) {
        String column = discriminator.column();
        Class<?> javaType = discriminator.javaType() == void.class ? String.class : discriminator.javaType();
        JdbcType jdbcType = discriminator.jdbcType() == JdbcType.UNDEFINED ? null : discriminator.jdbcType();
        @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>) (discriminator.typeHandler() == UnknownTypeHandler.class ? null : discriminator.typeHandler());
        Case[] cases = discriminator.cases();
        Map<String, String> discriminatorMap = new HashMap<String, String>();
        for (Case c : cases) {
            String value = c.value();
            String caseResultMapId = resultMapId + "-" + value;
            discriminatorMap.put(value, caseResultMapId);
        }
        return assistant.buildDiscriminator(resultType, column, javaType, jdbcType, typeHandler, discriminatorMap);
    }
    return null;
}
Also used : HashMap(java.util.HashMap) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) JdbcType(org.apache.ibatis.type.JdbcType) TypeHandler(org.apache.ibatis.type.TypeHandler) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) Case(org.apache.ibatis.annotations.Case)

Example 3 with TypeHandler

use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.

the class DefaultParameterHandler method setParameters.

@Override
public void setParameters(PreparedStatement ps) {
    ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    if (parameterMappings != null) {
        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 {
                    MetaObject metaObject = configuration.newMetaObject(parameterObject);
                    value = metaObject.getValue(propertyName);
                }
                TypeHandler typeHandler = parameterMapping.getTypeHandler();
                JdbcType jdbcType = parameterMapping.getJdbcType();
                if (value == null && jdbcType == null) {
                    jdbcType = configuration.getJdbcTypeForNull();
                }
                try {
                    typeHandler.setParameter(ps, i + 1, value, jdbcType);
                } catch (TypeException e) {
                    throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
                } catch (SQLException e) {
                    throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
                }
            }
        }
    }
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) SQLException(java.sql.SQLException) JdbcType(org.apache.ibatis.type.JdbcType) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeException(org.apache.ibatis.type.TypeException) TypeHandler(org.apache.ibatis.type.TypeHandler)

Example 4 with TypeHandler

use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.

the class ResultSetWrapper method getTypeHandler.

/**
   * Gets the type handler to use when reading the result set.
   * Tries to get from the TypeHandlerRegistry by searching for the property type.
   * If not found it gets the column JDBC type and tries to get a handler for it.
   * 
   * @param propertyType
   * @param columnName
   * @return
   */
public TypeHandler<?> getTypeHandler(Class<?> propertyType, String columnName) {
    TypeHandler<?> handler = null;
    Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
    if (columnHandlers == null) {
        columnHandlers = new HashMap<Class<?>, TypeHandler<?>>();
        typeHandlerMap.put(columnName, columnHandlers);
    } else {
        handler = columnHandlers.get(propertyType);
    }
    if (handler == null) {
        JdbcType jdbcType = getJdbcType(columnName);
        handler = typeHandlerRegistry.getTypeHandler(propertyType, jdbcType);
        // See issue #59 comment 10
        if (handler == null || handler instanceof UnknownTypeHandler) {
            final int index = columnNames.indexOf(columnName);
            final Class<?> javaType = resolveClass(classNames.get(index));
            if (javaType != null && jdbcType != null) {
                handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType);
            } else if (javaType != null) {
                handler = typeHandlerRegistry.getTypeHandler(javaType);
            } else if (jdbcType != null) {
                handler = typeHandlerRegistry.getTypeHandler(jdbcType);
            }
        }
        if (handler == null || handler instanceof UnknownTypeHandler) {
            handler = new ObjectTypeHandler();
        }
        columnHandlers.put(propertyType, handler);
    }
    return handler;
}
Also used : ObjectTypeHandler(org.apache.ibatis.type.ObjectTypeHandler) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) JdbcType(org.apache.ibatis.type.JdbcType) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) TypeHandler(org.apache.ibatis.type.TypeHandler) ObjectTypeHandler(org.apache.ibatis.type.ObjectTypeHandler)

Example 5 with TypeHandler

use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.

the class Jdbc3KeyGenerator method processBatch.

public void processBatch(MappedStatement ms, Statement stmt, Collection<Object> parameters) {
    ResultSet rs = null;
    try {
        rs = stmt.getGeneratedKeys();
        final Configuration configuration = ms.getConfiguration();
        final TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        final String[] keyProperties = ms.getKeyProperties();
        final ResultSetMetaData rsmd = rs.getMetaData();
        TypeHandler<?>[] typeHandlers = null;
        if (keyProperties != null && rsmd.getColumnCount() >= keyProperties.length) {
            for (Object parameter : parameters) {
                // there should be one row for each statement (also one for each parameter)
                if (!rs.next()) {
                    break;
                }
                final MetaObject metaParam = configuration.newMetaObject(parameter);
                if (typeHandlers == null) {
                    typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties, rsmd);
                }
                populateKeys(rs, metaParam, keyProperties, typeHandlers);
            }
        }
    } catch (Exception e) {
        throw new ExecutorException("Error getting generated key or setting result to parameter object. Cause: " + e, e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
            // ignore
            }
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ExecutorException(org.apache.ibatis.executor.ExecutorException) Configuration(org.apache.ibatis.session.Configuration) MetaObject(org.apache.ibatis.reflection.MetaObject) ResultSet(java.sql.ResultSet) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler) ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException)

Aggregations

TypeHandler (org.apache.ibatis.type.TypeHandler)14 JdbcType (org.apache.ibatis.type.JdbcType)7 ResultMapping (org.apache.ibatis.mapping.ResultMapping)6 SQLException (java.sql.SQLException)5 ArrayList (java.util.ArrayList)5 MetaObject (org.apache.ibatis.reflection.MetaObject)5 ExecutorException (org.apache.ibatis.executor.ExecutorException)4 UnknownTypeHandler (org.apache.ibatis.type.UnknownTypeHandler)4 HashMap (java.util.HashMap)3 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)3 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 ResultFlag (org.apache.ibatis.mapping.ResultFlag)2 ResultMap (org.apache.ibatis.mapping.ResultMap)2 XNode (org.apache.ibatis.parsing.XNode)2 List (java.util.List)1 Map (java.util.Map)1 Arg (org.apache.ibatis.annotations.Arg)1 Case (org.apache.ibatis.annotations.Case)1 Result (org.apache.ibatis.annotations.Result)1