Search in sources :

Example 1 with JdbcType

use of org.apache.ibatis.type.JdbcType 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 2 with JdbcType

use of org.apache.ibatis.type.JdbcType 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 3 with JdbcType

use of org.apache.ibatis.type.JdbcType 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 4 with JdbcType

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

the class XMLMapperBuilder method buildResultMappingFromContext.

private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resultType, List<ResultFlag> flags) throws Exception {
    String property;
    if (flags.contains(ResultFlag.CONSTRUCTOR)) {
        property = context.getStringAttribute("name");
    } else {
        property = context.getStringAttribute("property");
    }
    String column = context.getStringAttribute("column");
    String javaType = context.getStringAttribute("javaType");
    String jdbcType = context.getStringAttribute("jdbcType");
    String nestedSelect = context.getStringAttribute("select");
    String nestedResultMap = context.getStringAttribute("resultMap", processNestedResultMappings(context, Collections.<ResultMapping>emptyList()));
    String notNullColumn = context.getStringAttribute("notNullColumn");
    String columnPrefix = context.getStringAttribute("columnPrefix");
    String typeHandler = context.getStringAttribute("typeHandler");
    String resultSet = context.getStringAttribute("resultSet");
    String foreignColumn = context.getStringAttribute("foreignColumn");
    boolean lazy = "lazy".equals(context.getStringAttribute("fetchType", configuration.isLazyLoadingEnabled() ? "lazy" : "eager"));
    Class<?> javaTypeClass = resolveClass(javaType);
    @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
    return builderAssistant.buildResultMapping(resultType, property, column, javaTypeClass, jdbcTypeEnum, nestedSelect, nestedResultMap, notNullColumn, columnPrefix, typeHandlerClass, flags, resultSet, foreignColumn, lazy);
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) JdbcType(org.apache.ibatis.type.JdbcType) TypeHandler(org.apache.ibatis.type.TypeHandler)

Example 5 with JdbcType

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

the class XMLMapperBuilder method processDiscriminatorElement.

private Discriminator processDiscriminatorElement(XNode context, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception {
    String column = context.getStringAttribute("column");
    String javaType = context.getStringAttribute("javaType");
    String jdbcType = context.getStringAttribute("jdbcType");
    String typeHandler = context.getStringAttribute("typeHandler");
    Class<?> javaTypeClass = resolveClass(javaType);
    @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
    Map<String, String> discriminatorMap = new HashMap<String, String>();
    for (XNode caseChild : context.getChildren()) {
        String value = caseChild.getStringAttribute("value");
        String resultMap = caseChild.getStringAttribute("resultMap", processNestedResultMappings(caseChild, resultMappings));
        discriminatorMap.put(value, resultMap);
    }
    return builderAssistant.buildDiscriminator(resultType, column, javaTypeClass, jdbcTypeEnum, typeHandlerClass, discriminatorMap);
}
Also used : HashMap(java.util.HashMap) XNode(org.apache.ibatis.parsing.XNode) JdbcType(org.apache.ibatis.type.JdbcType) TypeHandler(org.apache.ibatis.type.TypeHandler)

Aggregations

JdbcType (org.apache.ibatis.type.JdbcType)8 TypeHandler (org.apache.ibatis.type.TypeHandler)7 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)3 XNode (org.apache.ibatis.parsing.XNode)3 HashMap (java.util.HashMap)2 MetaObject (org.apache.ibatis.reflection.MetaObject)2 UnknownTypeHandler (org.apache.ibatis.type.UnknownTypeHandler)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Case (org.apache.ibatis.annotations.Case)1 ExecutorException (org.apache.ibatis.executor.ExecutorException)1 ParameterMode (org.apache.ibatis.mapping.ParameterMode)1 ResultMapping (org.apache.ibatis.mapping.ResultMapping)1 ObjectTypeHandler (org.apache.ibatis.type.ObjectTypeHandler)1 TypeException (org.apache.ibatis.type.TypeException)1