Search in sources :

Example 1 with StatementUnit

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

the class StatementExecutorTest method createStatementUnits.

private Collection<StatementUnit> createStatementUnits(final String sql, final Statement statement, final String dataSource) {
    Collection<StatementUnit> result = new LinkedList<>();
    SQLBuilder sqlBuilder = new SQLBuilder();
    sqlBuilder.appendLiterals(sql);
    result.add(new StatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(Collections.<String, String>emptyMap(), null)), statement));
    return result;
}
Also used : SQLBuilder(io.shardingjdbc.core.rewrite.SQLBuilder) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) StatementUnit(io.shardingjdbc.core.executor.type.statement.StatementUnit) LinkedList(java.util.LinkedList)

Example 2 with StatementUnit

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

the class ExecuteEventListenerTest method assertSQLException.

@Test(expected = SQLException.class)
public void assertSQLException() throws Exception {
    Statement statement = mock(Statement.class);
    when(statement.getConnection()).thenReturn(mock(Connection.class));
    executorEngine.executeStatement(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", "select ..."), statement)), new ExecuteCallback<Integer>() {

        @Override
        public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
            throw new SQLException();
        }
    });
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) StatementUnit(io.shardingjdbc.core.executor.type.statement.StatementUnit) BaseStatementUnit(io.shardingjdbc.core.executor.BaseStatementUnit) BaseStatementUnit(io.shardingjdbc.core.executor.BaseStatementUnit) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 3 with StatementUnit

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

the class ExecuteEventListenerTest method assertMultiStatement.

@Test
public void assertMultiStatement() throws Exception {
    List<StatementUnit> statementUnitList = new ArrayList<>(2);
    Statement stm1 = mock(Statement.class);
    when(stm1.getConnection()).thenReturn(mock(Connection.class));
    statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", "insert into ..."), stm1));
    Statement stm2 = mock(Statement.class);
    when(stm2.getConnection()).thenReturn(mock(Connection.class));
    statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", "insert into ..."), stm2));
    executorEngine.executeStatement(SQLType.DML, statementUnitList, new ExecuteCallback<Integer>() {

        @Override
        public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
            return 0;
        }
    });
    assertThat(TRACER.finishedSpans().size(), is(3));
}
Also used : Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) StatementUnit(io.shardingjdbc.core.executor.type.statement.StatementUnit) BaseStatementUnit(io.shardingjdbc.core.executor.BaseStatementUnit) BaseStatementUnit(io.shardingjdbc.core.executor.BaseStatementUnit) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 4 with StatementUnit

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

the class ExecuteEventListenerTest method assertSingleStatement.

@Test
public void assertSingleStatement() throws Exception {
    Statement statement = mock(Statement.class);
    when(statement.getConnection()).thenReturn(mock(Connection.class));
    executorEngine.executeStatement(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", "insert into ..."), statement)), new ExecuteCallback<Integer>() {

        @Override
        public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
            return 0;
        }
    });
    assertThat(TRACER.finishedSpans().size(), is(2));
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) StatementUnit(io.shardingjdbc.core.executor.type.statement.StatementUnit) BaseStatementUnit(io.shardingjdbc.core.executor.BaseStatementUnit) BaseStatementUnit(io.shardingjdbc.core.executor.BaseStatementUnit) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 5 with StatementUnit

use of io.shardingjdbc.core.executor.type.statement.StatementUnit 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

StatementUnit (io.shardingjdbc.core.executor.type.statement.StatementUnit)5 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)5 Connection (java.sql.Connection)4 Statement (java.sql.Statement)4 BaseStatementUnit (io.shardingjdbc.core.executor.BaseStatementUnit)3 SQLException (java.sql.SQLException)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 SQLType (io.shardingjdbc.core.constant.SQLType)1 StatementExecutor (io.shardingjdbc.core.executor.type.statement.StatementExecutor)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 SQLBuilder (io.shardingjdbc.core.rewrite.SQLBuilder)1 StatementRoutingEngine (io.shardingjdbc.core.routing.StatementRoutingEngine)1 ArrayList (java.util.ArrayList)1