Search in sources :

Example 1 with ResultMapException

use of org.apache.ibatis.executor.result.ResultMapException in project mybatis-3 by mybatis.

the class UnknownTypeHandlerTest method getResultWithResultSetAndColumnNameThrowsException.

@Test
public void getResultWithResultSetAndColumnNameThrowsException() throws SQLException {
    doThrow(new SQLException("invalid column")).when((UnknownTypeHandler) TYPE_HANDLER).getNullableResult(rs, "foo");
    try {
        TYPE_HANDLER.getResult(rs, "foo");
        Assert.fail("Should have thrown a ResultMapException");
    } catch (Exception e) {
        Assert.assertTrue("Expected ResultMapException", e instanceof ResultMapException);
        Assert.assertTrue("column name is not in exception", e.getMessage().contains("column 'foo'"));
    }
}
Also used : SQLException(java.sql.SQLException) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) SQLException(java.sql.SQLException) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) Test(org.junit.Test)

Example 2 with ResultMapException

use of org.apache.ibatis.executor.result.ResultMapException in project mybatis-3 by mybatis.

the class UnknownTypeHandlerTest method getResultWithCallableStatementAndColumnIndexThrowsException.

@Test
public void getResultWithCallableStatementAndColumnIndexThrowsException() throws SQLException {
    doThrow(new SQLException("invalid column")).when((UnknownTypeHandler) TYPE_HANDLER).getNullableResult(cs, 1);
    try {
        TYPE_HANDLER.getResult(cs, 1);
        Assert.fail("Should have thrown a ResultMapException");
    } catch (Exception e) {
        Assert.assertTrue("Expected ResultMapException", e instanceof ResultMapException);
        Assert.assertTrue("column index is not in exception", e.getMessage().contains("column #1"));
    }
}
Also used : SQLException(java.sql.SQLException) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) SQLException(java.sql.SQLException) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) Test(org.junit.Test)

Example 3 with ResultMapException

use of org.apache.ibatis.executor.result.ResultMapException 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 4 with ResultMapException

use of org.apache.ibatis.executor.result.ResultMapException in project mybatis-3 by mybatis.

the class UnknownTypeHandlerTest method getResultWithResultSetAndColumnIndexThrowsException.

@Test
public void getResultWithResultSetAndColumnIndexThrowsException() throws SQLException {
    doThrow(new SQLException("invalid column")).when((UnknownTypeHandler) TYPE_HANDLER).getNullableResult(rs, 1);
    try {
        TYPE_HANDLER.getResult(rs, 1);
        Assert.fail("Should have thrown a ResultMapException");
    } catch (Exception e) {
        Assert.assertTrue("Expected ResultMapException", e instanceof ResultMapException);
        Assert.assertTrue("column index is not in exception", e.getMessage().contains("column #1"));
    }
}
Also used : SQLException(java.sql.SQLException) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) SQLException(java.sql.SQLException) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)4 ResultMapException (org.apache.ibatis.executor.result.ResultMapException)4 Test (org.junit.Test)3 ExecutorException (org.apache.ibatis.executor.ExecutorException)1 ResultMap (org.apache.ibatis.mapping.ResultMap)1 ResultMapping (org.apache.ibatis.mapping.ResultMapping)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 TypeHandler (org.apache.ibatis.type.TypeHandler)1