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

Method get_column_matches

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

Source from the content-addressed store, hash-verified

478 return [m.completion for m in matches]
479
480 def get_column_matches(self, suggestion, word_before_cursor):
481 tables = suggestion.table_refs
482 do_qualify = suggestion.qualifiable and {'always': True, 'never': False,
483 'if_more_than_one_table': \
484 len(tables) > 1}[self.qualify_columns]
485
486 def qualify(col, tbl):
487 return (tbl + '.' + self.case(col)) if do_qualify else self.case(col)
488
489 _logger.debug("Completion column scope: %r", tables)
490 scoped_cols = self.populate_scoped_cols(
491 tables, suggestion.local_tables)
492
493 def make_cand(name, ref):
494 synonyms = (name, generate_alias(self.case(name)))
495 return Candidate(qualify(name, ref), 0, 'column', synonyms)
496
497 def flat_cols():
498 return [make_cand(c.name, t.ref)
499 for t, cols in scoped_cols.items() for c in cols]
500 if suggestion.require_last_table:
501 # require_last_table is used for 'tb11 JOIN tbl2 USING (...' which should
502 # suggest only columns that appear in the last table and one more
503 ltbl = tables[-1].ref
504 other_tbl_cols = set(
505 c.name for t, cs in scoped_cols.items() if t.ref != ltbl for c in cs)
506 scoped_cols = {
507 t: [col for col in cols if col.name in other_tbl_cols]
508 for t, cols in scoped_cols.items()
509 if t.ref == ltbl
510 }
511 lastword = last_word(word_before_cursor, include='most_punctuations')
512 if lastword == '*':
513 if suggestion.context == 'insert':
514 def filter_col(col):
515 if not col.has_default:
516 return True
517 return not any(
518 p.match(col.default)
519 for p in self.insert_col_skip_patterns
520 )
521 scoped_cols = {
522 t: [col for col in cols if filter_col(col)] for t, cols in scoped_cols.items()
523 }
524 if self.asterisk_column_order == 'alphabetic':
525 for cols in scoped_cols.values():
526 cols.sort(key=operator.attrgetter('name'))
527 if lastword != word_before_cursor \
528 and len(tables) == 1 \
529 and word_before_cursor[-len(lastword) - 1] == '.':
530 # User typed x.*; replicate "x." for all columns except the
531 # first, which gets the original (as we only replace the "*"")
532 sep = ', ' + word_before_cursor[:-1]
533 collist = sep.join(self.case(c.completion)
534 for c in flat_cols())
535 else:
536 collist = ', '.join(qualify(c.name, t.ref)
537 for t, cs in scoped_cols.items() for c in cs)

Callers

nothing calls this directly

Calls 4

populate_scoped_colsMethod · 0.95
caseMethod · 0.95
find_matchesMethod · 0.95
last_wordFunction · 0.90

Tested by

no test coverage detected