def _get_from_clause_for_tables(table_names, fk_relations):
Returns list of SqlSpan tuples for FROM clause.
if not table_names:
raise ParseError("No table names provided.")
if len(table_names) == 1:
return [make_sql_span(table_name=table_names[0])]
// Map of (table_a, table_b) to (column_a, column_b)
fk_relations_map = {}
for relation in fk_relations:
fk_relations_map[(relation.child_table,
relation.parent_table)] = (relation.child_column,
relation.parent_column)
// Also add the reverse.
fk_relations_map[(relation.parent_table,
relation.child_table)] = (relation.parent_column,
relation.child_column)
// Naively try every permutation of table_names until we get a hit.
// TODO(petershaw): Could probably speed up this up quite a bit and provide
// better error handling for ambiguous cases.
After Change
sql_spans = []
sql_spans.append(make_sql_span(table_name=visited_tables[0]))
for i in range(len(visited_tables) - 1):
table_a = visited_tables[i]
table_b = visited_tables[i + 1]
column_a, column_b = fk_relations[i]
// join table_b on table_a.column_a = table_b.column_b
sql_spans.append(make_sql_span(sql_token="join"))