Search in sources :

Example 11 with ExecutorException

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);
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ExecutorException(org.apache.ibatis.executor.ExecutorException) DefaultCursor(org.apache.ibatis.cursor.defaults.DefaultCursor)

Example 12 with ExecutorException

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);
        }
    }
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 13 with ExecutorException

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);
    }
}
Also used : Executor(org.apache.ibatis.executor.Executor) ExecutorException(org.apache.ibatis.executor.ExecutorException) Configuration(org.apache.ibatis.session.Configuration) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) ExecutorException(org.apache.ibatis.executor.ExecutorException)

Example 14 with ExecutorException

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()));
    }
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ResultSet(java.sql.ResultSet) RowBounds(org.apache.ibatis.session.RowBounds) MappedStatement(org.apache.ibatis.mapping.MappedStatement) TypeHandler(org.apache.ibatis.type.TypeHandler) SQLException(java.sql.SQLException) ExecutorException(org.apache.ibatis.executor.ExecutorException) Test(org.junit.Test)

Example 15 with ExecutorException

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);
            }
        }
    }
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) JdbcType(org.apache.ibatis.type.JdbcType) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler)

Aggregations

ExecutorException (org.apache.ibatis.executor.ExecutorException)15 MetaObject (org.apache.ibatis.reflection.MetaObject)7 SQLException (java.sql.SQLException)6 ResultMapping (org.apache.ibatis.mapping.ResultMapping)5 TypeHandler (org.apache.ibatis.type.TypeHandler)4 CacheKey (org.apache.ibatis.cache.CacheKey)3 ResultMap (org.apache.ibatis.mapping.ResultMap)3 ResultSet (java.sql.ResultSet)2 ResultMapException (org.apache.ibatis.executor.result.ResultMapException)2 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 Configuration (org.apache.ibatis.session.Configuration)2 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 Proxy (javassist.util.proxy.Proxy)1 ProxyFactory (javassist.util.proxy.ProxyFactory)1 DataSource (javax.sql.DataSource)1 DefaultCursor (org.apache.ibatis.cursor.defaults.DefaultCursor)1 Executor (org.apache.ibatis.executor.Executor)1 AbstractEnhancedDeserializationProxy (org.apache.ibatis.executor.loader.AbstractEnhancedDeserializationProxy)1 WriteReplaceInterface (org.apache.ibatis.executor.loader.WriteReplaceInterface)1