Search in sources :

Example 21 with StatementExecutor

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

the class StatementExecutorTest method assertExecuteWithColumnNames.

@Test
public void assertExecuteWithColumnNames() throws SQLException {
    Statement statement = mock(Statement.class);
    when(statement.execute(DML_SQL, new String[] { "col" })).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(new String[] { "col" }));
    verify(statement).execute(DML_SQL, new String[] { "col" });
    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 22 with StatementExecutor

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

the class StatementExecutorTest method assertExecuteForSingleStatementFailureWithDML.

@Test
public void assertExecuteForSingleStatementFailureWithDML() throws SQLException {
    Statement statement = mock(Statement.class);
    SQLException exp = new SQLException();
    when(statement.execute(DML_SQL)).thenThrow(exp);
    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_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 23 with StatementExecutor

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

the class ShardingStatement method generateExecutor.

private StatementExecutor generateExecutor(final String sql) throws SQLException {
    clearPrevious();
    ShardingContext shardingContext = connection.getShardingContext();
    routeResult = new StatementRoutingEngine(shardingContext.getShardingRule(), shardingContext.getDatabaseType(), shardingContext.isShowSQL()).route(sql);
    Collection<StatementUnit> statementUnits = new LinkedList<>();
    for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
        Collection<Connection> connections;
        SQLType sqlType = routeResult.getSqlStatement().getType();
        if (SQLType.DDL == sqlType) {
            connections = connection.getConnectionsForDDL(each.getDataSource());
        } else {
            connections = Collections.singletonList(connection.getConnection(each.getDataSource(), routeResult.getSqlStatement().getType()));
        }
        for (Connection connection : connections) {
            Statement statement = connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
            replayMethodsInvocation(statement);
            statementUnits.add(new StatementUnit(each, statement));
            routedStatements.add(statement);
        }
    }
    return new StatementExecutor(connection.getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), statementUnits);
}
Also used : DQLStatement(io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement) SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) DALStatement(io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ShardingConnection(io.shardingjdbc.core.jdbc.core.connection.ShardingConnection) StatementRoutingEngine(io.shardingjdbc.core.routing.StatementRoutingEngine) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) SQLType(io.shardingjdbc.core.constant.SQLType) StatementExecutor(io.shardingjdbc.core.executor.type.statement.StatementExecutor) StatementUnit(io.shardingjdbc.core.executor.type.statement.StatementUnit) LinkedList(java.util.LinkedList) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext)

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