StatementExecutor extends AbstractStatementExecutor { @Override public List<QueryResult> executeQuery(final Collection<InputGroup<StatementExecuteUnit>> inputGroups) throws SQLException { boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecutorCallback<QueryResult> sqlExecutorCallback = new DefaultSQLExecutorCallback<QueryResult>(getSchemaContexts().getDatabaseType(), isExceptionThrown) { @Override protected QueryResult executeSQL(final String sql, final Statement statement, final ConnectionMode connectionMode) throws SQLException { return createQueryResult(sql, statement, connectionMode); } private QueryResult createQueryResult(final String sql, final Statement statement, final ConnectionMode connectionMode) throws SQLException { ResultSet resultSet = statement.executeQuery(sql); return ConnectionMode.MEMORY_STRICTLY == connectionMode ? new StreamQueryResult(resultSet) : new MemoryQueryResult(resultSet); } }; return getSqlExecutor().execute(inputGroups, sqlExecutorCallback); } StatementExecutor(final Map<String, DataSource> dataSourceMap, final SchemaContexts schemaContexts, final SQLExecutor sqlExecutor); @Override List<QueryResult> executeQuery(final Collection<InputGroup<StatementExecuteUnit>> inputGroups); @Override int executeUpdate(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext); int executeUpdate(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext, final int autoGeneratedKeys); int executeUpdate(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext, final int[] columnIndexes); int executeUpdate(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext, final String[] columnNames); @Override boolean execute(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext); boolean execute(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext, final int autoGeneratedKeys); boolean execute(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext, final int[] columnIndexes); boolean execute(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext, final String[] columnNames);  }
@Test public void assertExecuteQueryForSingleStatementSuccess() throws SQLException { Statement statement = getStatement(); ResultSet resultSet = mock(ResultSet.class); ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); when(resultSetMetaData.getColumnName(1)).thenReturn("column"); when(resultSetMetaData.getColumnLabel(1)).thenReturn("column"); when(resultSetMetaData.getTableName(1)).thenReturn("table_x"); when(resultSetMetaData.getColumnCount()).thenReturn(1); when(resultSetMetaData.getColumnType(1)).thenReturn(Types.VARCHAR); when(resultSet.getString(1)).thenReturn("value"); when(resultSet.getMetaData()).thenReturn(resultSetMetaData); when(statement.executeQuery(DQL_SQL)).thenReturn(resultSet); assertThat(actual.executeQuery(getExecuteGroups(Collections.singletonList(statement), true)).iterator().next().getValue(1, String.class), is("value")); verify(statement).executeQuery(DQL_SQL); }
@Test public void assertExecuteQueryForMultipleStatementsSuccess() throws SQLException { Statement statement1 = getStatement(); Statement statement2 = getStatement(); ResultSet resultSet1 = mock(ResultSet.class); ResultSet resultSet2 = mock(ResultSet.class); ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); when(resultSetMetaData.getColumnName(1)).thenReturn("column"); when(resultSetMetaData.getColumnLabel(1)).thenReturn("column"); when(resultSetMetaData.getTableName(1)).thenReturn("table_x"); when(resultSetMetaData.getColumnCount()).thenReturn(1); when(resultSetMetaData.getColumnType(1)).thenReturn(Types.INTEGER); when(resultSet1.getMetaData()).thenReturn(resultSetMetaData); when(resultSet2.getMetaData()).thenReturn(resultSetMetaData); when(resultSet1.getInt(1)).thenReturn(1); when(resultSet2.getInt(1)).thenReturn(2); when(statement1.executeQuery(DQL_SQL)).thenReturn(resultSet1); when(statement2.executeQuery(DQL_SQL)).thenReturn(resultSet2); List<QueryResult> result = actual.executeQuery(getExecuteGroups(Arrays.asList(statement1, statement2), true)); assertThat(String.valueOf(result.get(0).getValue(1, int.class)), is("1")); assertThat(String.valueOf(result.get(1).getValue(1, int.class)), is("2")); verify(statement1).executeQuery(DQL_SQL); verify(statement2).executeQuery(DQL_SQL); }
@Test public void assertExecuteQueryForSingleStatementFailure() throws SQLException { Statement statement = getStatement(); SQLException ex = new SQLException(""); when(statement.executeQuery(DQL_SQL)).thenThrow(ex); assertThat(actual.executeQuery(getExecuteGroups(Collections.singletonList(statement), true)), is(Collections.singletonList((QueryResult) null))); verify(statement).executeQuery(DQL_SQL); }
@Test public void assertExecuteQueryForMultipleStatementsFailure() throws SQLException { Statement statement1 = getStatement(); Statement statement2 = getStatement(); SQLException ex = new SQLException(""); when(statement1.executeQuery(DQL_SQL)).thenThrow(ex); when(statement2.executeQuery(DQL_SQL)).thenThrow(ex); List<QueryResult> actualResultSets = actual.executeQuery(getExecuteGroups(Arrays.asList(statement1, statement2), true)); assertThat(actualResultSets, is(Arrays.asList((QueryResult) null, null))); verify(statement1).executeQuery(DQL_SQL); verify(statement2).executeQuery(DQL_SQL); }