Search in sources :

Example 6 with PreparedStatementExecutor

use of io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor in project sharding-jdbc by shardingjdbc.

the class PreparedStatementExecutorTest method assertExecuteQueryForMultiplePreparedStatementsSuccess.

@Test
public void assertExecuteQueryForMultiplePreparedStatementsSuccess() throws SQLException {
    PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
    PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
    ResultSet resultSet1 = mock(ResultSet.class);
    ResultSet resultSet2 = mock(ResultSet.class);
    when(preparedStatement1.executeQuery()).thenReturn(resultSet1);
    when(preparedStatement2.executeQuery()).thenReturn(resultSet2);
    when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
    when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
    PreparedStatementExecutor actual = new PreparedStatementExecutor(getExecutorEngine(), SQLType.DQL, createPreparedStatementUnits(DQL_SQL, preparedStatement1, "ds_0", preparedStatement2, "ds_1"), Collections.emptyList());
    List<ResultSet> actualResultSets = actual.executeQuery();
    assertThat(actualResultSets, hasItem(resultSet1));
    assertThat(actualResultSets, hasItem(resultSet2));
    verify(preparedStatement1).executeQuery();
    verify(preparedStatement2).executeQuery();
    verify(preparedStatement1).getConnection();
    verify(preparedStatement2).getConnection();
    verify(getEventCaller(), times(2)).verifySQLType(SQLType.DQL);
    verify(getEventCaller(), times(2)).verifyDataSource("ds_0");
    verify(getEventCaller(), times(2)).verifyDataSource("ds_1");
    verify(getEventCaller(), times(4)).verifySQL(DQL_SQL);
    verify(getEventCaller(), times(4)).verifyParameters(Collections.emptyList());
    verify(getEventCaller(), times(2)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(getEventCaller(), times(2)).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(getEventCaller(), times(0)).verifyException(null);
}
Also used : PreparedStatementExecutor(io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 7 with PreparedStatementExecutor

use of io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor in project sharding-jdbc by shardingjdbc.

the class PreparedStatementExecutorTest method assertExecuteQueryForSinglePreparedStatementFailure.

@Test
public void assertExecuteQueryForSinglePreparedStatementFailure() throws SQLException {
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    SQLException exp = new SQLException();
    when(preparedStatement.executeQuery()).thenThrow(exp);
    when(preparedStatement.getConnection()).thenReturn(mock(Connection.class));
    PreparedStatementExecutor actual = new PreparedStatementExecutor(getExecutorEngine(), SQLType.DQL, createPreparedStatementUnits(DQL_SQL, preparedStatement, "ds_0"), Collections.emptyList());
    assertThat(actual.executeQuery(), is(Collections.singletonList((ResultSet) null)));
    verify(preparedStatement).executeQuery();
    verify(getEventCaller(), times(2)).verifySQLType(SQLType.DQL);
    verify(getEventCaller(), times(2)).verifyDataSource("ds_0");
    verify(getEventCaller(), times(2)).verifySQL(DQL_SQL);
    verify(getEventCaller(), times(2)).verifyParameters(Collections.emptyList());
    verify(getEventCaller()).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(getEventCaller()).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
    verify(getEventCaller()).verifyException(exp);
}
Also used : PreparedStatementExecutor(io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 8 with PreparedStatementExecutor

use of io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor in project sharding-jdbc by shardingjdbc.

the class PreparedStatementExecutorTest method assertExecuteQueryForSinglePreparedStatementSuccess.

@Test
public void assertExecuteQueryForSinglePreparedStatementSuccess() throws SQLException {
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    ResultSet resultSet = mock(ResultSet.class);
    when(preparedStatement.executeQuery()).thenReturn(resultSet);
    when(preparedStatement.getConnection()).thenReturn(mock(Connection.class));
    PreparedStatementExecutor actual = new PreparedStatementExecutor(getExecutorEngine(), SQLType.DQL, createPreparedStatementUnits(DQL_SQL, preparedStatement, "ds_0"), Collections.emptyList());
    assertThat(actual.executeQuery(), is(Collections.singletonList(resultSet)));
    verify(preparedStatement).executeQuery();
    verify(getEventCaller(), times(2)).verifySQLType(SQLType.DQL);
    verify(getEventCaller(), times(2)).verifyDataSource("ds_0");
    verify(getEventCaller(), times(2)).verifySQL(DQL_SQL);
    verify(getEventCaller(), times(2)).verifyParameters(Collections.emptyList());
    verify(getEventCaller()).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(getEventCaller()).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(getEventCaller(), times(0)).verifyException(null);
}
Also used : PreparedStatementExecutor(io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 9 with PreparedStatementExecutor

use of io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor in project sharding-jdbc by shardingjdbc.

the class ShardingPreparedStatement method executeQuery.

@Override
public ResultSet executeQuery() throws SQLException {
    ResultSet result;
    try {
        Collection<PreparedStatementUnit> preparedStatementUnits = route();
        List<ResultSet> resultSets = new PreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), preparedStatementUnits, getParameters()).executeQuery();
        List<QueryResult> queryResults = new ArrayList<>(resultSets.size());
        for (ResultSet each : resultSets) {
            queryResults.add(new JDBCQueryResult(each));
        }
        MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getShardingContext().getShardingRule(), queryResults, routeResult.getSqlStatement());
        result = new ShardingResultSet(resultSets, mergeEngine.merge(), this);
    } finally {
        clearBatch();
    }
    currentResultSet = result;
    return result;
}
Also used : BatchPreparedStatementExecutor(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor) PreparedStatementExecutor(io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor) QueryResult(io.shardingjdbc.core.merger.QueryResult) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) MergeEngine(io.shardingjdbc.core.merger.MergeEngine) ShardingResultSet(io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet) ResultSet(java.sql.ResultSet) GeneratedKeysResultSet(io.shardingjdbc.core.jdbc.core.resultset.GeneratedKeysResultSet) ShardingResultSet(io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet) ArrayList(java.util.ArrayList) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) PreparedStatementUnit(io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit) BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)

Example 10 with PreparedStatementExecutor

use of io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor in project sharding-jdbc by shardingjdbc.

the class PreparedStatementExecutorTest method assertExecuteForSinglePreparedStatementSuccessWithDML.

@Test
public void assertExecuteForSinglePreparedStatementSuccessWithDML() throws SQLException {
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    when(preparedStatement.execute()).thenReturn(false);
    when(preparedStatement.getConnection()).thenReturn(mock(Connection.class));
    PreparedStatementExecutor actual = new PreparedStatementExecutor(getExecutorEngine(), SQLType.DML, createPreparedStatementUnits(DML_SQL, preparedStatement, "ds_0"), Collections.emptyList());
    assertFalse(actual.execute());
    verify(preparedStatement).execute();
    verify(getEventCaller(), times(2)).verifySQLType(SQLType.DML);
    verify(getEventCaller(), times(2)).verifyDataSource("ds_0");
    verify(getEventCaller(), times(2)).verifySQL(DML_SQL);
    verify(getEventCaller(), times(2)).verifyParameters(Collections.emptyList());
    verify(getEventCaller()).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(getEventCaller()).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(getEventCaller(), times(0)).verifyException(null);
}
Also used : PreparedStatementExecutor(io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Aggregations

PreparedStatementExecutor (io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor)16 Test (org.junit.Test)15 Connection (java.sql.Connection)14 PreparedStatement (java.sql.PreparedStatement)14 SQLException (java.sql.SQLException)6 ResultSet (java.sql.ResultSet)4 BatchPreparedStatementExecutor (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor)1 BatchPreparedStatementUnit (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)1 PreparedStatementUnit (io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit)1 GeneratedKeysResultSet (io.shardingjdbc.core.jdbc.core.resultset.GeneratedKeysResultSet)1 ShardingResultSet (io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet)1 JDBCQueryResult (io.shardingjdbc.core.merger.JDBCQueryResult)1 MergeEngine (io.shardingjdbc.core.merger.MergeEngine)1 QueryResult (io.shardingjdbc.core.merger.QueryResult)1 ArrayList (java.util.ArrayList)1