Skip to content

Commit

Permalink
Add TablesContext.tableAliasNameMap (#33562)
Browse files Browse the repository at this point in the history
* Add TablesContext.tableAliasNameMap

* Add TablesContext.tableAliasNameMap
  • Loading branch information
terrymanu authored Nov 6, 2024
1 parent 93d7781 commit 937ba15
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SubqueryTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -66,6 +67,8 @@ public final class TablesContext {
@Getter(AccessLevel.NONE)
private final Map<String, Collection<SubqueryTableContext>> subqueryTables = new HashMap<>();

private final Map<String, String> tableAliasNameMap = new HashMap<>();

public TablesContext(final SimpleTableSegment table, final DatabaseType databaseType, final String currentDatabaseName) {
this(null == table ? Collections.emptyList() : Collections.singletonList(table), databaseType, currentDatabaseName);
}
Expand All @@ -81,13 +84,17 @@ public TablesContext(final Collection<? extends TableSegment> tables, final Map<
}
this.tables.addAll(tables);
for (TableSegment each : tables) {
if (each instanceof SimpleTableSegment && !"DUAL".equalsIgnoreCase(((SimpleTableSegment) each).getTableName().getIdentifier().getValue())) {
SimpleTableSegment simpleTableSegment = (SimpleTableSegment) each;
simpleTables.add(simpleTableSegment);
tableNames.add(simpleTableSegment.getTableName().getIdentifier().getValue());
// TODO use sql binder result when statement which contains tables support bind logic
simpleTableSegment.getOwner().ifPresent(optional -> schemaNames.add(optional.getIdentifier().getValue()));
databaseNames.add(findDatabaseName(simpleTableSegment, databaseType).orElse(currentDatabaseName));
if (each instanceof SimpleTableSegment) {
String tableName = ((SimpleTableSegment) each).getTableName().getIdentifier().getValue();
if (!"DUAL".equalsIgnoreCase(tableName)) {
SimpleTableSegment simpleTableSegment = (SimpleTableSegment) each;
simpleTables.add(simpleTableSegment);
tableNames.add(tableName);
// TODO use SQL binder result when statement which contains tables support bind logic
simpleTableSegment.getOwner().ifPresent(optional -> schemaNames.add(optional.getIdentifier().getValue()));
databaseNames.add(findDatabaseName(simpleTableSegment, databaseType).orElse(currentDatabaseName));
tableAliasNameMap.put(each.getAlias().map(IdentifierValue::getValue).orElse(tableName).toLowerCase(), tableName.toLowerCase());
}
}
if (each instanceof SubqueryTableSegment) {
subqueryTables.putAll(createSubqueryTables(subqueryContexts, (SubqueryTableSegment) each));
Expand Down

0 comments on commit 937ba15

Please sign in to comment.