Search in sources :

Example 6 with TableToken

use of io.shardingjdbc.core.parsing.parser.token.TableToken in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertRewriteForRowNum.

@Test
public void assertRewriteForRowNum() {
    selectStatement.setLimit(new Limit(DatabaseType.Oracle));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
    selectStatement.getSqlTokens().add(new TableToken(68, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(119, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(98, 4));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=4) t WHERE t.rownum_>0"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue) Test(org.junit.Test)

Example 7 with TableToken

use of io.shardingjdbc.core.parsing.parser.token.TableToken in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertGenerateSQLForCartesian.

@Test
public void assertGenerateSQLForCartesian() {
    selectStatement.getSqlTokens().add(new TableToken(7, "table_x"));
    selectStatement.getSqlTokens().add(new TableToken(31, "table_x"));
    selectStatement.getSqlTokens().add(new TableToken(47, "table_x"));
    SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT table_x.id, x.name FROM table_x x WHERE table_x.id=? AND x.name=?", DatabaseType.MySQL, selectStatement);
    SQLBuilder sqlBuilder = sqlRewriteEngine.rewrite(true);
    CartesianTableReference cartesianTableReference = new CartesianTableReference(Collections.singletonList(new TableUnit("db0", "table_x", "table_x")));
    assertThat(sqlRewriteEngine.generateSQL(cartesianTableReference, sqlBuilder), is("SELECT table_x.id, x.name FROM table_x x WHERE table_x.id=? AND x.name=?"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) CartesianTableReference(io.shardingjdbc.core.routing.type.complex.CartesianTableReference) TableUnit(io.shardingjdbc.core.routing.type.TableUnit) Test(org.junit.Test)

Example 8 with TableToken

use of io.shardingjdbc.core.parsing.parser.token.TableToken in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertRewriteForLimitForNotRewriteLimit.

@Test
public void assertRewriteForLimitForNotRewriteLimit() {
    selectStatement.setLimit(new Limit(DatabaseType.MySQL));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(2, -1, false));
    selectStatement.getSqlTokens().add(new TableToken(17, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(33, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(36, 2));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id FROM table_x x LIMIT 2, 2", DatabaseType.MySQL, selectStatement);
    assertThat(rewriteEngine.rewrite(false).toSQL(tableTokens, null), is("SELECT x.id FROM table_1 x LIMIT 2, 2"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue) Test(org.junit.Test)

Example 9 with TableToken

use of io.shardingjdbc.core.parsing.parser.token.TableToken in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertRewriteForLimit.

@Test
public void assertRewriteForLimit() {
    selectStatement.setLimit(new Limit(DatabaseType.MySQL));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(2, -1, false));
    selectStatement.getSqlTokens().add(new TableToken(17, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(33, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(36, 2));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id FROM table_x x LIMIT 2, 2", DatabaseType.MySQL, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT x.id FROM table_1 x LIMIT 0, 4"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue) Test(org.junit.Test)

Example 10 with TableToken

use of io.shardingjdbc.core.parsing.parser.token.TableToken in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method assertRewriteForTopAndRowNumber.

@Test
public void assertRewriteForTopAndRowNumber() {
    selectStatement.setLimit(new Limit(DatabaseType.SQLServer));
    selectStatement.getLimit().setOffset(new LimitValue(2, -1, true));
    selectStatement.getLimit().setRowCount(new LimitValue(4, -1, false));
    selectStatement.getSqlTokens().add(new TableToken(85, "table_x"));
    selectStatement.getSqlTokens().add(new OffsetToken(123, 2));
    selectStatement.getSqlTokens().add(new RowCountToken(26, 4));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>0"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue) Test(org.junit.Test)

Aggregations

TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)21 Test (org.junit.Test)16 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)10 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)10 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)9 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)9 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)4 ItemsToken (io.shardingjdbc.core.parsing.parser.token.ItemsToken)4 Table (io.shardingjdbc.core.parsing.parser.context.table.Table)3 OrderByToken (io.shardingjdbc.core.parsing.parser.token.OrderByToken)2 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)2 ExpectedTableToken (io.shardingjdbc.core.parsing.integrate.jaxb.token.ExpectedTableToken)1 IndexToken (io.shardingjdbc.core.parsing.parser.token.IndexToken)1 SQLToken (io.shardingjdbc.core.parsing.parser.token.SQLToken)1 SchemaToken (io.shardingjdbc.core.parsing.parser.token.SchemaToken)1 CartesianTableReference (io.shardingjdbc.core.routing.type.complex.CartesianTableReference)1