Search in sources :

Example 1 with TableToken

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

the class TableReferencesClauseParser method parseSingleTableWithoutAlias.

/**
 * Parse single table without alias.
 *
 * @param sqlStatement SQL statement
 */
public final void parseSingleTableWithoutAlias(final SQLStatement sqlStatement) {
    int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
    sqlStatement.getSqlTokens().add(new TableToken(beginPosition, lexerEngine.getCurrentToken().getLiterals()));
    sqlStatement.getTables().add(new Table(lexerEngine.getCurrentToken().getLiterals(), Optional.<String>absent()));
    lexerEngine.nextToken();
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) Table(io.shardingjdbc.core.parsing.parser.context.table.Table)

Example 2 with TableToken

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

the class UpdateSetItemsClauseParser method parseSetColumn.

private void parseSetColumn(final DMLStatement updateStatement) {
    if (lexerEngine.equalAny(Symbol.LEFT_PAREN)) {
        lexerEngine.skipParentheses(updateStatement);
        return;
    }
    int beginPosition = lexerEngine.getCurrentToken().getEndPosition();
    String literals = lexerEngine.getCurrentToken().getLiterals();
    lexerEngine.nextToken();
    if (lexerEngine.skipIfEqual(Symbol.DOT)) {
        if (updateStatement.getTables().getSingleTableName().equalsIgnoreCase(SQLUtil.getExactlyValue(literals))) {
            updateStatement.getSqlTokens().add(new TableToken(beginPosition - literals.length(), literals));
        }
        lexerEngine.nextToken();
    }
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken)

Example 3 with TableToken

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

the class SQLRewriteEngineTest method assertRewriteForAutoGeneratedKeyColumn.

@Test
public void assertRewriteForAutoGeneratedKeyColumn() {
    selectStatement.getSqlTokens().add(new TableToken(12, "table_x"));
    ItemsToken itemsToken1 = new ItemsToken(30);
    itemsToken1.getItems().add("id");
    ItemsToken itemsToken2 = new ItemsToken(44);
    itemsToken2.getItems().add("?");
    selectStatement.getSqlTokens().addAll(Arrays.asList(itemsToken1, itemsToken2));
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "INSERT INTO table_x (name, age) VALUES (?, ?)", DatabaseType.MySQL, selectStatement);
    assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null), is("INSERT INTO table_1 (name, age, id) VALUES (?, ?, ?)"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken) Test(org.junit.Test)

Example 4 with TableToken

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

the class SQLRewriteEngineTest method assertRewriteForTopAndRowNumberForNotRewriteLimit.

@Test
public void assertRewriteForTopAndRowNumberForNotRewriteLimit() {
    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(false).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_>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 5 with TableToken

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

the class SQLRewriteEngineTest method assertRewriteForRowNumForMemoryGroupBy.

@Test
public void assertRewriteForRowNumForMemoryGroupBy() {
    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));
    selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC, Optional.<String>absent()));
    selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC, Optional.<String>absent()));
    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<=2147483647) t WHERE t.rownum_>0"));
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) 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