Search in sources :

Example 6 with SQLExecutionUnit

use of io.shardingjdbc.core.routing.SQLExecutionUnit 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 7 with SQLExecutionUnit

use of io.shardingjdbc.core.routing.SQLExecutionUnit 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 8 with SQLExecutionUnit

use of io.shardingjdbc.core.routing.SQLExecutionUnit in project sharding-jdbc by shardingjdbc.

the class SQLExecuteBackendHandler method execute.

@Override
public List<DatabaseProtocolPacket> execute() {
    SQLRouteResult routeResult = routingEngine.route(sql);
    if (routeResult.getExecutionUnits().isEmpty()) {
        return Collections.<DatabaseProtocolPacket>singletonList(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
    }
    List<List<DatabaseProtocolPacket>> result = new LinkedList<>();
    for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
        // TODO multiple threads
        result.add(execute(routeResult.getSqlStatement(), each));
    }
    return merge(routeResult.getSqlStatement(), result);
}
Also used : SQLRouteResult(io.shardingjdbc.core.routing.SQLRouteResult) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) OKPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket) LinkedList(java.util.LinkedList)

Example 9 with SQLExecutionUnit

use of io.shardingjdbc.core.routing.SQLExecutionUnit in project sharding-jdbc by shardingjdbc.

the class SQLLogger method logSQL.

/**
 * Print SQL log.
 *
 * @param logicSQL logic SQL
 * @param sqlStatement SQL statement
 * @param sqlExecutionUnits SQL execution units
 * @param parameters parameters for SQL placeholder
 */
public static void logSQL(final String logicSQL, final SQLStatement sqlStatement, final Collection<SQLExecutionUnit> sqlExecutionUnits, final List<Object> parameters) {
    log("Logic SQL: {}", logicSQL);
    log("SQLStatement: {}", sqlStatement);
    for (SQLExecutionUnit each : sqlExecutionUnits) {
        if (parameters.isEmpty()) {
            log("Actual SQL: {} ::: {}", each.getDataSource(), each.getSql());
        } else {
            log("Actual SQL: {} ::: {} ::: {}", each.getDataSource(), each.getSql(), parameters);
        }
    }
}
Also used : SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit)

Example 10 with SQLExecutionUnit

use of io.shardingjdbc.core.routing.SQLExecutionUnit in project sharding-jdbc by shardingjdbc.

the class ParsingSQLRouter method route.

@Override
public SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
    SQLRouteResult result = new SQLRouteResult(sqlStatement);
    if (sqlStatement instanceof InsertStatement && null != ((InsertStatement) sqlStatement).getGeneratedKey()) {
        processGeneratedKey(parameters, (InsertStatement) sqlStatement, result);
    }
    RoutingResult routingResult = route(parameters, sqlStatement);
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, logicSQL, databaseType, sqlStatement);
    boolean isSingleRouting = routingResult.isSingleRouting();
    if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getLimit()) {
        processLimit(parameters, (SelectStatement) sqlStatement, isSingleRouting);
    }
    SQLBuilder sqlBuilder = rewriteEngine.rewrite(!isSingleRouting);
    if (routingResult instanceof CartesianRoutingResult) {
        for (CartesianDataSource cartesianDataSource : ((CartesianRoutingResult) routingResult).getRoutingDataSources()) {
            for (CartesianTableReference cartesianTableReference : cartesianDataSource.getRoutingTableReferences()) {
                result.getExecutionUnits().add(new SQLExecutionUnit(cartesianDataSource.getDataSource(), rewriteEngine.generateSQL(cartesianTableReference, sqlBuilder)));
            }
        }
    } else {
        for (TableUnit each : routingResult.getTableUnits().getTableUnits()) {
            result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), rewriteEngine.generateSQL(each, sqlBuilder)));
        }
    }
    if (showSQL) {
        SQLLogger.logSQL(logicSQL, sqlStatement, result.getExecutionUnits(), parameters);
    }
    return result;
}
Also used : RoutingResult(io.shardingjdbc.core.routing.type.RoutingResult) CartesianRoutingResult(io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult) SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) SQLBuilder(io.shardingjdbc.core.rewrite.SQLBuilder) SQLRouteResult(io.shardingjdbc.core.routing.SQLRouteResult) CartesianRoutingResult(io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult) CartesianDataSource(io.shardingjdbc.core.routing.type.complex.CartesianDataSource) CartesianTableReference(io.shardingjdbc.core.routing.type.complex.CartesianTableReference) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) SQLRewriteEngine(io.shardingjdbc.core.rewrite.SQLRewriteEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) TableUnit(io.shardingjdbc.core.routing.type.TableUnit)

Aggregations

SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)13 LinkedList (java.util.LinkedList)6 StatementUnit (io.shardingjdbc.core.executor.type.statement.StatementUnit)5 SQLBuilder (io.shardingjdbc.core.rewrite.SQLBuilder)4 Connection (java.sql.Connection)4 Statement (java.sql.Statement)4 BaseStatementUnit (io.shardingjdbc.core.executor.BaseStatementUnit)3 BatchPreparedStatementUnit (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)3 SQLRouteResult (io.shardingjdbc.core.routing.SQLRouteResult)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 SQLType (io.shardingjdbc.core.constant.SQLType)2 PreparedStatementUnit (io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit)2 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)2 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)2 RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)2 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)2 StatementExecutor (io.shardingjdbc.core.executor.type.statement.StatementExecutor)1 ShardingContext (io.shardingjdbc.core.jdbc.core.ShardingContext)1