MCPcopy Create free account
hub / github.com/dbcli/mssql-cli / get_join_matches

Method get_join_matches

mssqlcli/mssqlcompleter.py:569–611  ·  view source on GitHub ↗
(self, suggestion, word_before_cursor)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 8

populate_scoped_colsMethod · 0.95
aliasMethod · 0.95
caseMethod · 0.95
find_matchesMethod · 0.95
normalize_refFunction · 0.85
generate_aliasFunction · 0.85
CandidateFunction · 0.85
formatMethod · 0.45

Tested by

no test coverage detected