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();
}
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=?"));
}
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);
}
}
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.");
}
}
Aggregations