(self, suggestion, word_before_cursor)
| 562 | return next(a for a in aliases if normalize_ref(a) not in tbls) |
| 563 | |
| 564 | def get_join_matches(self, suggestion, word_before_cursor): |
| 565 | tbls = suggestion.table_refs |
| 566 | cols = self.populate_scoped_cols(tbls) |
| 567 | # Set up some data structures for efficient access |
| 568 | qualified = {normalize_ref(t.ref): t.schema for t in tbls} |
| 569 | ref_prio = {normalize_ref(t.ref): n for n, t in enumerate(tbls)} |
| 570 | refs = {normalize_ref(t.ref) for t in tbls} |
| 571 | other_tbls = {(t.schema, t.name) for t in list(cols)[:-1]} |
| 572 | joins = [] |
| 573 | # Iterate over FKs in existing tables to find potential joins |
| 574 | fks = ((fk, rtbl, rcol) for rtbl, rcols in cols.items() for rcol in rcols for fk in rcol.foreignkeys) |
| 575 | col = namedtuple("col", "schema tbl col") |
| 576 | for fk, rtbl, rcol in fks: |
| 577 | right = col(rtbl.schema, rtbl.name, rcol.name) |
| 578 | child = col(fk.childschema, fk.childtable, fk.childcolumn) |
| 579 | parent = col(fk.parentschema, fk.parenttable, fk.parentcolumn) |
| 580 | left = child if parent == right else parent |
| 581 | if suggestion.schema and left.schema != suggestion.schema: |
| 582 | continue |
| 583 | c = self.case |
| 584 | if self.generate_aliases or normalize_ref(left.tbl) in refs: |
| 585 | lref = self.alias(left.tbl, suggestion.table_refs) |
| 586 | join = "{0} {4} ON {4}.{1} = {2}.{3}".format(c(left.tbl), c(left.col), rtbl.ref, c(right.col), lref) |
| 587 | else: |
| 588 | join = "{0} ON {0}.{1} = {2}.{3}".format(c(left.tbl), c(left.col), rtbl.ref, c(right.col)) |
| 589 | alias = generate_alias(self.case(left.tbl), alias_map=self.alias_map) |
| 590 | synonyms = [ |
| 591 | join, |
| 592 | "{0} ON {0}.{1} = {2}.{3}".format(alias, c(left.col), rtbl.ref, c(right.col)), |
| 593 | ] |
| 594 | # Schema-qualify if (1) new table in same schema as old, and old |
| 595 | # is schema-qualified, or (2) new in other schema, except public |
| 596 | if not suggestion.schema and ( |
| 597 | qualified[normalize_ref(rtbl.ref)] and left.schema == right.schema or left.schema not in (right.schema, "public") |
| 598 | ): |
| 599 | join = left.schema + "." + join |
| 600 | prio = ref_prio[normalize_ref(rtbl.ref)] * 2 + (0 if (left.schema, left.tbl) in other_tbls else 1) |
| 601 | joins.append(Candidate(join, prio, "join", synonyms=synonyms)) |
| 602 | |
| 603 | return self.find_matches(word_before_cursor, joins, meta="join") |
| 604 | |
| 605 | def get_join_condition_matches(self, suggestion, word_before_cursor): |
| 606 | col = namedtuple("col", "schema tbl col") |
nothing calls this directly
no test coverage detected