def test_suggest_cte_names(self, completer):
     text = '''
         WITH cte1 AS (SELECT a, b, c FROM foo),
             cte2 AS (SELECT d, e, f FROM bar)
         SELECT * FROM
     '''
     result = result_set(completer, text)
     expected = set([
         PGCompletion('cte1', 0, display_meta='table'),
         PGCompletion('cte2', 0, display_meta='table'),
     ])
     self.assertTrue(expected <= result)
    def test_suggest_columns_from_cte(self, completer):
        result = result_set(
            completer,
            'WITH cte AS (SELECT foo, bar FROM baz) SELECT  FROM cte',
            len('WITH cte AS (SELECT foo, bar FROM baz) SELECT ')
        )
        expected = (
            [
                PGCompletion('foo', 0, display_meta='column'),
                PGCompletion('bar', 0, display_meta='column'),
            ] + testdata.functions_and_keywords()
        )

        self.assertSetEqual(result, set(expected))
Esempio n. 3
0
def function(text, pos=0, display=None):
    return PGCompletion(
        text,
        display=display or text,
        start_position=pos,
        display_meta='function'
    )
 def test_keyword_after_alter(self, completer):
     text = 'ALTER TABLE users ALTER '
     expected = PGCompletion('COLUMN', start_position=0, display_meta='keyword')
     completions = result_set(completer, text)
     self.assertTrue(expected in set(completions))
 def test_cte_qualified_columns(self, completer, text):
     result = result_set(completer, text)
     expected = [PGCompletion('foo', 0, display_meta='column')]
     self.assertSetEqual(result, set(expected))
Esempio n. 6
0
def wildcard_expansion(cols, pos=-1):
    return PGCompletion(
        cols, start_position=pos, display_meta='columns', display='*')
Esempio n. 7
0
def completion(display_meta, text, pos=0):
    return PGCompletion(text, start_position=pos, display_meta=display_meta)
Esempio n. 8
0
    def get_column_matches(self, suggestion, word_before_cursor):
        tables = suggestion.table_refs
        do_qualify = suggestion.qualifiable and {'always': True, 'never': False, 'if_more_than_one_table': len(tables) > 1}[self.qualify_columns]

        def qualify(col, tbl): return ((tbl + '.' + self.case(col)) if do_qualify else self.case(col))     # noqa
        self._log(False, "Completion column scope: %r", tables)
        scoped_cols = self.populate_scoped_cols(tables, suggestion.local_tables)

        def make_cand(name, ref):
            synonyms = (name, generate_alias(self.case(name)))
            return Candidate(qualify(name, ref), 0, 'column', synonyms, schema=None)

        def flat_cols():
            return [make_cand(c.name, t.ref) for t, cols in scoped_cols.items() for c in cols]
        if suggestion.require_last_table:
            # require_last_table is used for 'tb11 JOIN tbl2 USING (...' which should
            # suggest only columns that appear in the last table and one more
            ltbl = tables[-1].ref
            other_tbl_cols = set(
                c.name for t, cs in scoped_cols.items() if t.ref != ltbl for c in cs)
            scoped_cols = {
                t: [col for col in cols if col.name in other_tbl_cols]
                for t, cols in scoped_cols.items()
                if t.ref == ltbl
            }
        lastword = last_word(word_before_cursor, include='most_punctuations')
        if lastword == '*':
            if suggestion.context == 'insert':
                def filter(col):
                    if not col.has_default:
                        return True
                    return not any(
                        p.match(col.default)
                        for p in self.insert_col_skip_patterns
                    )
                scoped_cols = {
                    t: [col for col in cols if filter(col)] for t, cols in scoped_cols.items()
                }
            if self.asterisk_column_order == 'alphabetic':
                for cols in scoped_cols.values():
                    cols.sort(key=operator.attrgetter('name'))
            if (lastword != word_before_cursor and len(tables) == 1 and word_before_cursor[-len(lastword) - 1] == '.'):
                # User typed x.*; replicate "x." for all columns except the
                # first, which gets the original (as we only replace the "*"")
                sep = ', ' + word_before_cursor[:-1]
                collist = sep.join(self.case(c.completion)
                                   for c in flat_cols())
            else:
                collist = ', '.join(qualify(c.name, t.ref)
                                    for t, cs in scoped_cols.items() for c in cs)

            return [Match(
                completion=PGCompletion(
                    collist,
                    -1,
                    display_meta='columns',
                    display='*'
                ),
                priority=(1, 1, 1)
            )]

        return self.find_matches(word_before_cursor, flat_cols(), meta='column')