Search in sources :

Example 1 with SQLRouteResult

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

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

use of io.shardingjdbc.core.routing.SQLRouteResult 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)3 SQLRouteResult (io.shardingjdbc.core.routing.SQLRouteResult)3 RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)2 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)2 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)1 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)1 SQLBuilder (io.shardingjdbc.core.rewrite.SQLBuilder)1 SQLRewriteEngine (io.shardingjdbc.core.rewrite.SQLRewriteEngine)1 HintShardingStrategy (io.shardingjdbc.core.routing.strategy.hint.HintShardingStrategy)1 CartesianDataSource (io.shardingjdbc.core.routing.type.complex.CartesianDataSource)1 CartesianRoutingResult (io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult)1 CartesianTableReference (io.shardingjdbc.core.routing.type.complex.CartesianTableReference)1 DatabaseHintRoutingEngine (io.shardingjdbc.core.routing.type.hint.DatabaseHintRoutingEngine)1 DatabaseProtocolPacket (io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket)1 OKPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1