Search in sources :

Example 1 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteWithAutoGeneratedKeys.

@Test
public void assertExecuteWithAutoGeneratedKeys() throws SQLException {
    Statement statement = mock(Statement.class);
    StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDML(statement, "ds_0");
    when(statement.execute(DELETE_FROM_DUAL, Statement.NO_GENERATED_KEYS)).thenReturn(false);
    StatementExecutor actual = new StatementExecutor(executorEngine);
    actual.addStatement(wrapper);
    assertFalse(actual.execute(Statement.NO_GENERATED_KEYS));
    verify(statement).execute(DELETE_FROM_DUAL, Statement.NO_GENERATED_KEYS);
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL(DELETE_FROM_DUAL);
    verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(eventCaller, times(0)).verifyException(null);
}
Also used : Statement(java.sql.Statement) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Example 2 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutor method executeUpdate.

private int executeUpdate(final Updater updater) {
    Context context = MetricsContext.start("ShardingStatement-executeUpdate");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    try {
        if (1 == statementExecutorWrappers.size()) {
            return executeUpdateInternal(updater, statementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap);
        }
        return executorEngine.execute(statementExecutorWrappers, new ExecuteUnit<StatementExecutorWrapper, Integer>() {

            @Override
            public Integer execute(final StatementExecutorWrapper input) throws Exception {
                synchronized (input.getStatement().getConnection()) {
                    return executeUpdateInternal(updater, input, isExceptionThrown, dataMap);
                }
            }
        }, new MergeUnit<Integer, Integer>() {

            @Override
            public Integer merge(final List<Integer> results) {
                if (null == results) {
                    return 0;
                }
                int result = 0;
                for (int each : results) {
                    result += each;
                }
                return result;
            }
        });
    } finally {
        MetricsContext.stop(context);
    }
}
Also used : MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) Context(com.codahale.metrics.Timer.Context) SQLException(java.sql.SQLException) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)

Example 3 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteQueryForSingleStatementFailure.

@Test
public void assertExecuteQueryForSingleStatementFailure() throws SQLException {
    Statement statement = mock(Statement.class);
    SQLException exp = new SQLException();
    StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDQL(statement, "ds_0");
    when(statement.executeQuery(SELECT_FROM_DUAL)).thenThrow(exp);
    StatementExecutor actual = new StatementExecutor(executorEngine);
    actual.addStatement(wrapper);
    assertThat(actual.executeQuery(), is(Collections.singletonList((ResultSet) null)));
    verify(statement).executeQuery(SELECT_FROM_DUAL);
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL(SELECT_FROM_DUAL);
    verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
    verify(eventCaller).verifyException(exp);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Example 4 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteUpdateForSingleStatementFailure.

@Test
public void assertExecuteUpdateForSingleStatementFailure() throws SQLException {
    Statement statement = mock(Statement.class);
    StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDML(statement, "ds_0");
    SQLException exp = new SQLException();
    when(statement.executeUpdate(DELETE_FROM_DUAL)).thenThrow(exp);
    StatementExecutor actual = new StatementExecutor(executorEngine);
    actual.addStatement(wrapper);
    assertThat(actual.executeUpdate(), is(0));
    verify(statement).executeUpdate(DELETE_FROM_DUAL);
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL(DELETE_FROM_DUAL);
    verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
    verify(eventCaller).verifyException(exp);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Example 5 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteUpdateWithColumnIndexes.

@Test
public void assertExecuteUpdateWithColumnIndexes() throws SQLException {
    Statement statement = mock(Statement.class);
    StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDML(statement, "ds_0");
    when(statement.executeUpdate(DELETE_FROM_DUAL, new int[] { 1 })).thenReturn(10);
    StatementExecutor actual = new StatementExecutor(executorEngine);
    actual.addStatement(wrapper);
    assertThat(actual.executeUpdate(new int[] { 1 }), is(10));
    verify(statement).executeUpdate(DELETE_FROM_DUAL, new int[] { 1 });
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL(DELETE_FROM_DUAL);
    verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(eventCaller, times(0)).verifyException(null);
}
Also used : Statement(java.sql.Statement) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Aggregations

StatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)24 Statement (java.sql.Statement)21 Test (org.junit.Test)20 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)4 Context (com.codahale.metrics.Timer.Context)3 MetricsContext (com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext)3 StatementExecutor (com.dangdang.ddframe.rdb.sharding.executor.StatementExecutor)1 SQLExecutionUnit (com.dangdang.ddframe.rdb.sharding.router.SQLExecutionUnit)1 SQLRouteResult (com.dangdang.ddframe.rdb.sharding.router.SQLRouteResult)1