Search in sources :

Example 6 with TableUnit

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

Example 7 with TableUnit

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

the class SQLRewriteEngineTest method assertGenerateSQL.

@Test
public void assertGenerateSQL() {
    selectStatement.getSqlTokens().add(new TableToken(7, "table_x"));
    selectStatement.getSqlTokens().add(new TableToken(31, "table_x"));
    selectStatement.getSqlTokens().add(new TableToken(58, "table_x"));
    selectStatement.getTables().add(new Table("table_x", Optional.of("x")));
    selectStatement.getTables().add(new Table("table_y", Optional.of("y")));
    SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT table_x.id, x.name FROM table_x x, table_y y WHERE table_x.id=? AND x.name=?", DatabaseType.MySQL, selectStatement);
    SQLBuilder sqlBuilder = sqlRewriteEngine.rewrite(true);
    assertThat(sqlRewriteEngine.generateSQL(new TableUnit("db0", "table_x", "table_x"), sqlBuilder), is("SELECT table_x.id, x.name FROM table_x x, table_y y WHERE table_x.id=? AND x.name=?"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) Table(io.shardingjdbc.core.parsing.parser.context.table.Table) TableUnit(io.shardingjdbc.core.routing.type.TableUnit) Test(org.junit.Test)

Aggregations

TableUnit (io.shardingjdbc.core.routing.type.TableUnit)7 RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)3 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)2 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)2 SQLRouteResult (io.shardingjdbc.core.routing.SQLRouteResult)2 CartesianTableReference (io.shardingjdbc.core.routing.type.complex.CartesianTableReference)2 Test (org.junit.Test)2 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)1 ShardingKey (io.shardingjdbc.core.hint.ShardingKey)1 Table (io.shardingjdbc.core.parsing.parser.context.table.Table)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 DatabaseHintRoutingEngine (io.shardingjdbc.core.routing.type.hint.DatabaseHintRoutingEngine)1 BindingTableRule (io.shardingjdbc.core.rule.BindingTableRule)1 DataNode (io.shardingjdbc.core.rule.DataNode)1