Search in sources :

Example 1 with Table

use of io.shardingjdbc.core.parsing.parser.context.table.Table 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 Table

use of io.shardingjdbc.core.parsing.parser.context.table.Table 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)

Example 3 with Table

use of io.shardingjdbc.core.parsing.parser.context.table.Table in project sharding-jdbc by shardingjdbc.

the class TableAssert method assertTables.

/**
 * Assert tables.
 *
 * @param actual actual tables
 * @param expected expected tables
 */
public void assertTables(final Tables actual, final List<ExpectedTable> expected) {
    assertThat(assertMessage.getFullAssertMessage("Tables size assertion error: "), actual.getTableNames().size(), is(expected.size()));
    for (ExpectedTable each : expected) {
        Optional<Table> table;
        if (null != each.getAlias()) {
            table = actual.find(each.getAlias());
        } else {
            table = actual.find(each.getName());
        }
        assertTrue(assertMessage.getFullAssertMessage("Table should exist: "), table.isPresent());
        assertTable(table.get(), each);
    }
}
Also used : ExpectedTable(io.shardingjdbc.core.parsing.integrate.jaxb.table.ExpectedTable) ExpectedTable(io.shardingjdbc.core.parsing.integrate.jaxb.table.ExpectedTable) Table(io.shardingjdbc.core.parsing.parser.context.table.Table)

Example 4 with Table

use of io.shardingjdbc.core.parsing.parser.context.table.Table in project sharding-jdbc by shardingjdbc.

the class TableReferencesClauseParser method parseTableFactor.

protected final void parseTableFactor(final SQLStatement sqlStatement, final boolean isSingleTableOnly) {
    final int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
    String literals = lexerEngine.getCurrentToken().getLiterals();
    lexerEngine.nextToken();
    if (lexerEngine.skipIfEqual(Symbol.DOT)) {
        literals = lexerEngine.getCurrentToken().getLiterals();
    }
    String tableName = SQLUtil.getExactlyValue(literals);
    if (Strings.isNullOrEmpty(tableName)) {
        return;
    }
    Optional<String> alias = aliasExpressionParser.parseTableAlias();
    if (isSingleTableOnly || shardingRule.tryFindTableRuleByLogicTable(tableName).isPresent() || shardingRule.findBindingTableRule(tableName).isPresent() || shardingRule.getDataSourceNames().contains(shardingRule.getDefaultDataSourceName())) {
        sqlStatement.getSqlTokens().add(new TableToken(beginPosition, literals));
        sqlStatement.getTables().add(new Table(tableName, alias));
    }
    parseForceIndex(tableName, sqlStatement);
    parseJoinTable(sqlStatement);
    if (isSingleTableOnly && !sqlStatement.getTables().isSingleTable()) {
        throw new UnsupportedOperationException("Cannot support Multiple-Table.");
    }
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) Table(io.shardingjdbc.core.parsing.parser.context.table.Table)

Aggregations

Table (io.shardingjdbc.core.parsing.parser.context.table.Table)4 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)3 ExpectedTable (io.shardingjdbc.core.parsing.integrate.jaxb.table.ExpectedTable)1 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)1 Test (org.junit.Test)1