Search in sources :

Example 1 with RoutingResult

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

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

the class CartesianRoutingEngine method getDataSourceLogicTablesMap.

private Map<String, Set<String>> getDataSourceLogicTablesMap() {
    Collection<String> intersectionDataSources = getIntersectionDataSources();
    Map<String, Set<String>> result = new HashMap<>(routingResults.size());
    for (RoutingResult each : routingResults) {
        for (Entry<String, Set<String>> entry : each.getTableUnits().getDataSourceLogicTablesMap(intersectionDataSources).entrySet()) {
            if (result.containsKey(entry.getKey())) {
                result.get(entry.getKey()).addAll(entry.getValue());
            } else {
                result.put(entry.getKey(), entry.getValue());
            }
        }
    }
    return result;
}
Also used : RoutingResult(io.shardingjdbc.core.routing.type.RoutingResult) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap)

Example 3 with RoutingResult

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

the class DatabaseHintRoutingEngine method route.

@Override
public RoutingResult route() {
    Optional<ShardingValue> shardingValue = HintManagerHolder.getDatabaseShardingValue(new ShardingKey(HintManagerHolder.DB_TABLE_NAME, HintManagerHolder.DB_COLUMN_NAME));
    Preconditions.checkState(shardingValue.isPresent());
    log.debug("Before database sharding only db:{} sharding values: {}", dataSourceNames, shardingValue.get());
    Collection<String> routingDataSources;
    routingDataSources = databaseShardingStrategy.doSharding(dataSourceNames, Collections.singletonList(shardingValue.get()));
    Preconditions.checkState(!routingDataSources.isEmpty(), "no database route info");
    log.debug("After database sharding only result: {}", routingDataSources);
    RoutingResult result = new RoutingResult();
    for (String each : routingDataSources) {
        result.getTableUnits().getTableUnits().add(new TableUnit(each, "", ""));
    }
    return result;
}
Also used : RoutingResult(io.shardingjdbc.core.routing.type.RoutingResult) ShardingKey(io.shardingjdbc.core.hint.ShardingKey) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) TableUnit(io.shardingjdbc.core.routing.type.TableUnit)

Example 4 with RoutingResult

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

RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)4 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)3 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)2 SQLRouteResult (io.shardingjdbc.core.routing.SQLRouteResult)2 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)1 ShardingKey (io.shardingjdbc.core.hint.ShardingKey)1 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 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1