Search in sources :

Example 11 with StatementExecutor

use of io.shardingjdbc.core.executor.type.statement.StatementExecutor in project sharding-jdbc by shardingjdbc.

the class StatementExecutorTest method assertExecuteQueryForSingleStatementFailure.

@Test
public void assertExecuteQueryForSingleStatementFailure() throws SQLException {
    Statement statement = mock(Statement.class);
    SQLException exp = new SQLException();
    when(statement.executeQuery(DQL_SQL)).thenThrow(exp);
    when(statement.getConnection()).thenReturn(mock(Connection.class));
    StatementExecutor actual = new StatementExecutor(getExecutorEngine(), SQLType.DQL, createStatementUnits(DQL_SQL, statement, "ds_0"));
    assertThat(actual.executeQuery(), is(Collections.singletonList((ResultSet) null)));
    verify(statement).executeQuery(DQL_SQL);
    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 : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) StatementExecutor(io.shardingjdbc.core.executor.type.statement.StatementExecutor) Test(org.junit.Test)

Example 12 with StatementExecutor

use of io.shardingjdbc.core.executor.type.statement.StatementExecutor in project sharding-jdbc by shardingjdbc.

the class StatementExecutorTest method assertExecuteForSingleStatementSuccessWithDML.

@Test
public void assertExecuteForSingleStatementSuccessWithDML() throws SQLException {
    Statement statement = mock(Statement.class);
    when(statement.execute(DML_SQL)).thenReturn(false);
    when(statement.getConnection()).thenReturn(mock(Connection.class));
    StatementExecutor actual = new StatementExecutor(getExecutorEngine(), SQLType.DML, createStatementUnits(DML_SQL, statement, "ds_0"));
    assertFalse(actual.execute());
    verify(statement).execute(DML_SQL);
    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 : Statement(java.sql.Statement) Connection(java.sql.Connection) StatementExecutor(io.shardingjdbc.core.executor.type.statement.StatementExecutor) Test(org.junit.Test)

Example 13 with StatementExecutor

use of io.shardingjdbc.core.executor.type.statement.StatementExecutor in project sharding-jdbc by shardingjdbc.

the class StatementExecutorTest method assertExecuteUpdateForMultipleStatementsSuccess.

@Test
public void assertExecuteUpdateForMultipleStatementsSuccess() throws SQLException {
    Statement statement1 = mock(Statement.class);
    Statement statement2 = mock(Statement.class);
    when(statement1.executeUpdate(DML_SQL)).thenReturn(10);
    when(statement2.executeUpdate(DML_SQL)).thenReturn(20);
    when(statement1.getConnection()).thenReturn(mock(Connection.class));
    when(statement2.getConnection()).thenReturn(mock(Connection.class));
    StatementExecutor actual = new StatementExecutor(getExecutorEngine(), SQLType.DML, createStatementUnits(DML_SQL, statement1, "ds_0", statement2, "ds_1"));
    assertThat(actual.executeUpdate(), is(30));
    verify(statement1).executeUpdate(DML_SQL);
    verify(statement2).executeUpdate(DML_SQL);
    verify(statement1).getConnection();
    verify(statement2).getConnection();
    verify(getEventCaller(), times(2)).verifySQLType(SQLType.DML);
    verify(getEventCaller(), times(2)).verifyDataSource("ds_0");
    verify(getEventCaller(), times(2)).verifyDataSource("ds_1");
    verify(getEventCaller(), times(4)).verifySQL(DML_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 : Statement(java.sql.Statement) Connection(java.sql.Connection) StatementExecutor(io.shardingjdbc.core.executor.type.statement.StatementExecutor) Test(org.junit.Test)

Example 14 with StatementExecutor

use of io.shardingjdbc.core.executor.type.statement.StatementExecutor in project sharding-jdbc by shardingjdbc.

the class StatementExecutorTest method assertExecuteForMultipleStatementsFailureWithDML.

@Test
public void assertExecuteForMultipleStatementsFailureWithDML() throws SQLException {
    Statement statement1 = mock(Statement.class);
    Statement statement2 = mock(Statement.class);
    SQLException exp = new SQLException();
    when(statement1.execute(DML_SQL)).thenThrow(exp);
    when(statement2.execute(DML_SQL)).thenThrow(exp);
    when(statement1.getConnection()).thenReturn(mock(Connection.class));
    when(statement2.getConnection()).thenReturn(mock(Connection.class));
    StatementExecutor actual = new StatementExecutor(getExecutorEngine(), SQLType.DML, createStatementUnits(DML_SQL, statement1, "ds_0", statement2, "ds_1"));
    assertFalse(actual.execute());
    verify(statement1).execute(DML_SQL);
    verify(statement2).execute(DML_SQL);
    verify(statement1).getConnection();
    verify(statement2).getConnection();
    verify(getEventCaller(), times(2)).verifySQLType(SQLType.DML);
    verify(getEventCaller(), times(2)).verifyDataSource("ds_0");
    verify(getEventCaller(), times(2)).verifyDataSource("ds_1");
    verify(getEventCaller(), times(4)).verifySQL(DML_SQL);
    verify(getEventCaller(), times(4)).verifyParameters(Collections.emptyList());
    verify(getEventCaller(), times(2)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(getEventCaller(), times(2)).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
    verify(getEventCaller(), times(2)).verifyException(exp);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) StatementExecutor(io.shardingjdbc.core.executor.type.statement.StatementExecutor) Test(org.junit.Test)

Example 15 with StatementExecutor

use of io.shardingjdbc.core.executor.type.statement.StatementExecutor in project sharding-jdbc by shardingjdbc.

the class StatementExecutorTest method assertExecuteQueryForMultipleStatementsFailure.

@Test
public void assertExecuteQueryForMultipleStatementsFailure() throws SQLException {
    Statement statement1 = mock(Statement.class);
    Statement statement2 = mock(Statement.class);
    SQLException exp = new SQLException();
    when(statement1.executeQuery(DQL_SQL)).thenThrow(exp);
    when(statement2.executeQuery(DQL_SQL)).thenThrow(exp);
    when(statement1.getConnection()).thenReturn(mock(Connection.class));
    when(statement2.getConnection()).thenReturn(mock(Connection.class));
    StatementExecutor actual = new StatementExecutor(getExecutorEngine(), SQLType.DQL, createStatementUnits(DQL_SQL, statement1, "ds_0", statement2, "ds_1"));
    List<ResultSet> actualResultSets = actual.executeQuery();
    assertThat(actualResultSets, is(Arrays.asList((ResultSet) null, null)));
    verify(statement1).executeQuery(DQL_SQL);
    verify(statement2).executeQuery(DQL_SQL);
    verify(statement1).getConnection();
    verify(statement2).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_FAILURE);
    verify(getEventCaller(), times(2)).verifyException(exp);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) StatementExecutor(io.shardingjdbc.core.executor.type.statement.StatementExecutor) Test(org.junit.Test)

Aggregations

StatementExecutor (io.shardingjdbc.core.executor.type.statement.StatementExecutor)23 Connection (java.sql.Connection)22 Statement (java.sql.Statement)22 Test (org.junit.Test)22 SQLException (java.sql.SQLException)7 ResultSet (java.sql.ResultSet)3 SQLType (io.shardingjdbc.core.constant.SQLType)1 StatementUnit (io.shardingjdbc.core.executor.type.statement.StatementUnit)1 ShardingContext (io.shardingjdbc.core.jdbc.core.ShardingContext)1 ShardingConnection (io.shardingjdbc.core.jdbc.core.connection.ShardingConnection)1 DALStatement (io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement)1 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)1 DQLStatement (io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement)1 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)1 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)1 StatementRoutingEngine (io.shardingjdbc.core.routing.StatementRoutingEngine)1 LinkedList (java.util.LinkedList)1