use of org.apache.ibatis.executor.ExecutorException in project mybatis-3 by mybatis.
the class DefaultResultSetHandler method handleCursorResultSets.
@Override
public <E> Cursor<E> handleCursorResultSets(Statement stmt) throws SQLException {
ErrorContext.instance().activity("handling cursor results").object(mappedStatement.getId());
ResultSetWrapper rsw = getFirstResultSet(stmt);
List<ResultMap> resultMaps = mappedStatement.getResultMaps();
int resultMapCount = resultMaps.size();
validateResultMapsCount(rsw, resultMapCount);
if (resultMapCount != 1) {
throw new ExecutorException("Cursor results cannot be mapped to multiple resultMaps");
}
ResultMap resultMap = resultMaps.get(0);
return new DefaultCursor<E>(this, resultMap, rsw, rowBounds);
}
use of org.apache.ibatis.executor.ExecutorException in project mybatis-3 by mybatis.
the class Jdbc3KeyGenerator method populateKeys.
private void populateKeys(ResultSet rs, MetaObject metaParam, String[] keyProperties, TypeHandler<?>[] typeHandlers) throws SQLException {
for (int i = 0; i < keyProperties.length; i++) {
String property = keyProperties[i];
if (!metaParam.hasSetter(property)) {
throw new ExecutorException("No setter found for the keyProperty '" + property + "' in " + metaParam.getOriginalObject().getClass().getName() + ".");
}
TypeHandler<?> th = typeHandlers[i];
if (th != null) {
Object value = th.getResult(rs, i + 1);
metaParam.setValue(property, value);
}
}
}
use of org.apache.ibatis.executor.ExecutorException in project mybatis-3 by mybatis.
the class SelectKeyGenerator method processGeneratedKeys.
private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
try {
if (parameter != null && keyStatement != null && keyStatement.getKeyProperties() != null) {
String[] keyProperties = keyStatement.getKeyProperties();
final Configuration configuration = ms.getConfiguration();
final MetaObject metaParam = configuration.newMetaObject(parameter);
if (keyProperties != null) {
// Do not close keyExecutor.
// The transaction will be closed by parent executor.
Executor keyExecutor = configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
List<Object> values = keyExecutor.query(keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
if (values.size() == 0) {
throw new ExecutorException("SelectKey returned no data.");
} else if (values.size() > 1) {
throw new ExecutorException("SelectKey returned more than one value.");
} else {
MetaObject metaResult = configuration.newMetaObject(values.get(0));
if (keyProperties.length == 1) {
if (metaResult.hasGetter(keyProperties[0])) {
setValue(metaParam, keyProperties[0], metaResult.getValue(keyProperties[0]));
} else {
// no getter for the property - maybe just a single value object
// so try that
setValue(metaParam, keyProperties[0], values.get(0));
}
} else {
handleMultipleProperties(keyProperties, metaParam, metaResult);
}
}
}
}
} catch (ExecutorException e) {
throw e;
} catch (Exception e) {
throw new ExecutorException("Error selecting key or setting result to parameter object. Cause: " + e, e);
}
}
use of org.apache.ibatis.executor.ExecutorException in project mybatis-3 by mybatis.
the class DefaultResultSetHandlerTest method shouldThrowExceptionWithColumnName.
@Test
public void shouldThrowExceptionWithColumnName() throws Exception {
final MappedStatement ms = getMappedStatement();
final RowBounds rowBounds = new RowBounds(0, 100);
final DefaultResultSetHandler defaultResultSetHandler = new DefaultResultSetHandler(null, /*executor*/
ms, null, /*parameterHandler*/
null, /*resultHandler*/
null, /*boundSql*/
rowBounds);
final ResultSetWrapper rsw = mock(ResultSetWrapper.class);
when(rsw.getResultSet()).thenReturn(mock(ResultSet.class));
final ResultMapping resultMapping = mock(ResultMapping.class);
final TypeHandler typeHandler = mock(TypeHandler.class);
when(resultMapping.getColumn()).thenReturn("column");
when(resultMapping.getTypeHandler()).thenReturn(typeHandler);
when(typeHandler.getResult(any(ResultSet.class), any(String.class))).thenThrow(new SQLException("exception"));
List<ResultMapping> constructorMappings = Collections.singletonList(resultMapping);
try {
defaultResultSetHandler.createParameterizedResultObject(rsw, null, /*resultType*/
constructorMappings, null, /*constructorArgTypes*/
null, /*constructorArgs*/
null);
Assert.fail("Should have thrown ExecutorException");
} catch (Exception e) {
Assert.assertTrue("Expected ExecutorException", e instanceof ExecutorException);
Assert.assertTrue("", e.getMessage().contains("mapping: " + resultMapping.toString()));
}
}
use of org.apache.ibatis.executor.ExecutorException in project mybatis-paginator by HuQingmiao.
the class DefaultParameterHandler method setParameters.
public void setParameters(PreparedStatement ps) throws SQLException {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings != null) {
MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
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 {
value = metaObject == null ? null : metaObject.getValue(propertyName);
}
TypeHandler typeHandler = parameterMapping.getTypeHandler();
if (typeHandler == null) {
throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
}
JdbcType jdbcType = parameterMapping.getJdbcType();
if (value == null && jdbcType == null)
jdbcType = configuration.getJdbcTypeForNull();
typeHandler.setParameter(ps, i + 1, value, jdbcType);
}
}
}
}
Aggregations