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);
}
}
}
}
}
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"));
}
}
Aggregations