Search in sources :

Example 1 with TypeException

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

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

the class DefaultParameterHandlerTest method setParametersThrowsProperException.

@Test
public void setParametersThrowsProperException() throws SQLException {
    final MappedStatement mappedStatement = getMappedStatement();
    final Object parameterObject = null;
    final BoundSql boundSql = mock(BoundSql.class);
    TypeHandler<Object> typeHandler = mock(TypeHandler.class);
    doThrow(new SQLException("foo")).when(typeHandler).setParameter(any(PreparedStatement.class), anyInt(), any(), any(JdbcType.class));
    ParameterMapping parameterMapping = new ParameterMapping.Builder(mappedStatement.getConfiguration(), "prop", typeHandler).build();
    List<ParameterMapping> parameterMappings = Collections.singletonList(parameterMapping);
    when(boundSql.getParameterMappings()).thenReturn(parameterMappings);
    DefaultParameterHandler defaultParameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
    PreparedStatement ps = mock(PreparedStatement.class);
    try {
        defaultParameterHandler.setParameters(ps);
        Assert.fail("Should have thrown TypeException");
    } catch (Exception e) {
        Assert.assertTrue("expected TypeException", e instanceof TypeException);
        Assert.assertTrue("", e.getMessage().contains("mapping: ParameterMapping"));
    }
}
Also used : SQLException(java.sql.SQLException) JdbcType(org.apache.ibatis.type.JdbcType) PreparedStatement(java.sql.PreparedStatement) TypeException(org.apache.ibatis.type.TypeException) SQLException(java.sql.SQLException) TypeException(org.apache.ibatis.type.TypeException) ArgumentMatchers.anyObject(org.mockito.ArgumentMatchers.anyObject) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)2 JdbcType (org.apache.ibatis.type.JdbcType)2 TypeException (org.apache.ibatis.type.TypeException)2 PreparedStatement (java.sql.PreparedStatement)1 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 TypeHandler (org.apache.ibatis.type.TypeHandler)1 Test (org.junit.Test)1 ArgumentMatchers.anyObject (org.mockito.ArgumentMatchers.anyObject)1