Search in sources :

Example 1 with SQLExecutionUnit

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

the class DatabaseHintSQLRouter method route.

@Override
public // TODO insert SQL need parse gen key
SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
    SQLRouteResult result = new SQLRouteResult(sqlStatement);
    RoutingResult routingResult = new DatabaseHintRoutingEngine(shardingRule.getDataSourceNames(), (HintShardingStrategy) shardingRule.getDefaultDatabaseShardingStrategy()).route();
    for (TableUnit each : routingResult.getTableUnits().getTableUnits()) {
        result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), logicSQL));
    }
    if (showSQL) {
        SQLLogger.logSQL(logicSQL, sqlStatement, result.getExecutionUnits(), parameters);
    }
    return result;
}
Also used : RoutingResult(io.shardingjdbc.core.routing.type.RoutingResult) DatabaseHintRoutingEngine(io.shardingjdbc.core.routing.type.hint.DatabaseHintRoutingEngine) SQLRouteResult(io.shardingjdbc.core.routing.SQLRouteResult) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) TableUnit(io.shardingjdbc.core.routing.type.TableUnit) HintShardingStrategy(io.shardingjdbc.core.routing.strategy.hint.HintShardingStrategy)

Example 2 with SQLExecutionUnit

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

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

the class ShardingPreparedStatement method route.

private Collection<PreparedStatementUnit> route() throws SQLException {
    Collection<PreparedStatementUnit> result = new LinkedList<>();
    routeResult = routingEngine.route(getParameters());
    for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
        SQLType sqlType = routeResult.getSqlStatement().getType();
        Collection<PreparedStatement> preparedStatements;
        if (SQLType.DDL == sqlType) {
            preparedStatements = generatePreparedStatementForDDL(each);
        } else {
            preparedStatements = Collections.singletonList(generatePreparedStatement(each));
        }
        routedStatements.addAll(preparedStatements);
        for (PreparedStatement preparedStatement : preparedStatements) {
            replaySetParameter(preparedStatement);
            result.add(new PreparedStatementUnit(each, preparedStatement));
        }
    }
    return result;
}
Also used : SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) PreparedStatement(java.sql.PreparedStatement) PreparedStatementUnit(io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit) BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit) SQLType(io.shardingjdbc.core.constant.SQLType) LinkedList(java.util.LinkedList)

Example 4 with SQLExecutionUnit

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

the class ShardingPreparedStatement method routeBatch.

private List<BatchPreparedStatementUnit> routeBatch() throws SQLException {
    List<BatchPreparedStatementUnit> result = new ArrayList<>();
    routeResult = routingEngine.route(getParameters());
    for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
        BatchPreparedStatementUnit batchStatementUnit = getPreparedBatchStatement(each);
        replaySetParameter(batchStatementUnit.getStatement());
        result.add(batchStatementUnit);
    }
    return result;
}
Also used : BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit) ArrayList(java.util.ArrayList) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit)

Example 5 with SQLExecutionUnit

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

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