Search in sources :

Example 6 with TypeHandler

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

the class DefaultResultSetHandler method createParameterizedResultObject.

Object createParameterizedResultObject(ResultSetWrapper rsw, Class<?> resultType, List<ResultMapping> constructorMappings, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix) {
    boolean foundValues = false;
    for (ResultMapping constructorMapping : constructorMappings) {
        final Class<?> parameterType = constructorMapping.getJavaType();
        final String column = constructorMapping.getColumn();
        final Object value;
        try {
            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));
            }
        } catch (ResultMapException e) {
            throw new ExecutorException("Could not process result for mapping: " + constructorMapping, e);
        } catch (SQLException e) {
            throw new ExecutorException("Could not process result for mapping: " + constructorMapping, e);
        }
        constructorArgTypes.add(parameterType);
        constructorArgs.add(value);
        foundValues = value != null || foundValues;
    }
    return foundValues ? objectFactory.create(resultType, constructorArgTypes, constructorArgs) : null;
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler)

Example 7 with TypeHandler

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

the class SqlRunner method getResults.

private List<Map<String, Object>> getResults(ResultSet rs) throws SQLException {
    try {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<String> columns = new ArrayList<String>();
        List<TypeHandler<?>> typeHandlers = new ArrayList<TypeHandler<?>>();
        ResultSetMetaData rsmd = rs.getMetaData();
        for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
            columns.add(rsmd.getColumnLabel(i + 1));
            try {
                Class<?> type = Resources.classForName(rsmd.getColumnClassName(i + 1));
                TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(type);
                if (typeHandler == null) {
                    typeHandler = typeHandlerRegistry.getTypeHandler(Object.class);
                }
                typeHandlers.add(typeHandler);
            } catch (Exception e) {
                typeHandlers.add(typeHandlerRegistry.getTypeHandler(Object.class));
            }
        }
        while (rs.next()) {
            Map<String, Object> row = new HashMap<String, Object>();
            for (int i = 0, n = columns.size(); i < n; i++) {
                String name = columns.get(i);
                TypeHandler<?> handler = typeHandlers.get(i);
                row.put(name.toUpperCase(Locale.ENGLISH), handler.getResult(rs, name));
            }
            list.add(row);
        }
        return list;
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
            // ignore
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) ResultSetMetaData(java.sql.ResultSetMetaData) TypeHandler(org.apache.ibatis.type.TypeHandler) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with TypeHandler

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

the class MapperAnnotationBuilder method applyConstructorArgs.

private void applyConstructorArgs(Arg[] args, Class<?> resultType, List<ResultMapping> resultMappings) {
    for (Arg arg : args) {
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        flags.add(ResultFlag.CONSTRUCTOR);
        if (arg.id()) {
            flags.add(ResultFlag.ID);
        }
        @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>) (arg.typeHandler() == UnknownTypeHandler.class ? null : arg.typeHandler());
        ResultMapping resultMapping = assistant.buildResultMapping(resultType, nullOrEmpty(arg.name()), nullOrEmpty(arg.column()), arg.javaType() == void.class ? null : arg.javaType(), arg.jdbcType() == JdbcType.UNDEFINED ? null : arg.jdbcType(), nullOrEmpty(arg.select()), nullOrEmpty(arg.resultMap()), null, null, typeHandler, flags, null, null, false);
        resultMappings.add(resultMapping);
    }
}
Also used : UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) ResultMapping(org.apache.ibatis.mapping.ResultMapping) Arg(org.apache.ibatis.annotations.Arg) ArrayList(java.util.ArrayList) ResultFlag(org.apache.ibatis.mapping.ResultFlag) TypeHandler(org.apache.ibatis.type.TypeHandler) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler)

Example 9 with TypeHandler

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

use of org.apache.ibatis.type.TypeHandler 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

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