(self, line, token, beg, end)
return res
Provide some completions for SQL

        :param line: The current complete input line
        :param token: The word readline is looking for matches
        :param beg: Integer offset of token in line
        :param end: Integer end of token in line
        :return: A list of completions, or an empty list if none
Provide some completions for SQL
Provide some completions for SQL
def complete_sql(self, line, token, beg, end):
        """Provide some completions for SQL

        :param line: The current complete input line
        :param token: The word readline is looking for matches
        :param beg: Integer offset of token in line
        :param end: Integer end of token in line
        :return: A list of completions, or an empty list if none
        """
        if self._completion_cache is None:
            cur = self.db.cursor()
            collations = [row[1] for row in cur.execute("pragma collation_list")]
            databases = [row[1] for row in cur.execute("pragma database_list")]
            other = []
            for db in databases:
                if db == "temp":
                    master = "sqlite_temp_master"
                else:
                    master = "[%s].sqlite_master" % (db, )
                for row in cur.execute("select * from " + master).fetchall():
                    for col in (1, 2):
                        if row[col] not in other and not row[col].startswith("sqlite_"):
                            other.append(row[col])
                    if row[0] == "table":
                        try:
                            for table in cur.execute("pragma [%s].table_info([%s])" % (
                                    db,
                                    row[1],
                            )).fetchall():
                                if table[1] not in other:
                                    other.append(table[1])
                                for item in table[2].split():
                                    if item not in other:
                                        other.append(item)
                        except apsw.SQLError:
                            # See https://github.com/rogerbinns/apsw/issues/86
                            pass
                functions = {}
                for row in cur.execute("pragma function_list"):
                    name = row[0]
                    narg = row[4]
                    functions[name] = max(narg, functions.get(name, -1))

                def fmtfunc(name, nargs):
                    if nargs == 0:
                        return name + "()"
                    return name + "("

                func_list = [fmtfunc(name, narg) for name, narg in functions.items()]

            self._completion_cache = [
                self._sqlite_keywords, func_list, self._sqlite_special_names, collations, databases, other
            ]
            for i in range(len(self._completion_cache)):
                self._completion_cache[i].sort()

        # be somewhat sensible about pragmas
        if "pragma " in line.lower():
            t = self._get_prev_tokens(line.lower(), end)

            # pragma foo = bar
            if len(t) > 2 and t[-3] == "pragma":
                # t[-2] should be a valid one
                for p in self._pragmas:
                    if p.replace("=", "") == t[-2]:
                        vals = self._pragmas[p]
                        if not vals:
                            return []
                        return [x + ";" for x in vals if x.startswith(token)]
            # at equals?
            if len(t) > 1 and t[-2] == "pragma" and line[:end].replace(" ", "").endswith("="):
                for p in self._pragmas:
                    if p.replace("=", "") == t[-1]:
                        vals = self._pragmas[p]
                        if not vals:
                            return []
                        return vals
            # pragma foo
            if len(t) > 1 and t[-2] == "pragma":
                res = [x for x in self._pragmas.keys() if x.startswith(token)]
                res.sort()
                return res

            # pragma
            if len(t) and t[-1] == "pragma":
                res = list(self._pragmas.keys())
                res.sort()
                return res

        # This is currently not context sensitive (eg it doesn't look
        # to see if last token was 'FROM' and hence next should only
        # be table names.  That is a SMOP like pragmas above
        res = []
        ut = token.upper()
        for corpus in self._completion_cache:
            for word in corpus:
                if word.upper().startswith(ut):
                    # potential match - now match case
                    if word.startswith(token):  # exact
                        if word not in res:
                            res.append(word)
                    elif word.lower().startswith(token):  # lower
                        if word.lower() not in res:
                            res.append(word.lower())
                    elif word.upper().startswith(token):  # upper
                        if word.upper() not in res:
                            res.append(word.upper())
                    else:
                        # match letter by letter otherwise readline mangles what was typed in
                        w = token + word[len(token):]
                        if w not in res:
                            res.append(w)
        return res
def complete_sql ( self , line , token , beg , end ) : if self . _completion_cache is None : cur = self . db . cursor ( ) collations = [ row [ 1 ] for row in cur . execute ( "pragma collation_list" ) ] databases = [ row [ 1 ] for row in cur . execute ( "pragma database_list" ) ] other = [ ] for db in databases : if db == "temp" : master = "sqlite_temp_master" else : master = "[%s].sqlite_master" % ( db , ) for row in cur . execute ( "select * from " + master ) . fetchall ( ) : for col in ( 1 , 2 ) : if row [ col ] not in other and not row [ col ] . startswith ( "sqlite_" ) : other . append ( row [ col ] ) if row [ 0 ] == "table" : try : for table in cur . execute ( "pragma [%s].table_info([%s])" % ( db , row [ 1 ] , ) ) . fetchall ( ) : if table [ 1 ] not in other : other . append ( table [ 1 ] ) for item in table [ 2 ] . split ( ) : if item not in other : other . append ( item ) except apsw . SQLError : # See https://github.com/rogerbinns/apsw/issues/86 pass functions = { } for row in cur . execute ( "pragma function_list" ) : name = row [ 0 ] narg = row [ 4 ] functions [ name ] = max ( narg , functions . get ( name , - 1 ) ) def fmtfunc ( name , nargs ) : if nargs == 0 : return name + "()" return name + "(" func_list = [ fmtfunc ( name , narg ) for name , narg in functions . items ( ) ] self . _completion_cache = [ self . _sqlite_keywords , func_list , self . _sqlite_special_names , collations , databases , other ] for i in range ( len ( self . _completion_cache ) ) : self . _completion_cache [ i ] . sort ( ) # be somewhat sensible about pragmas if "pragma " in line . lower ( ) : t = self . _get_prev_tokens ( line . lower ( ) , end ) # pragma foo = bar if len ( t ) > 2 and t [ - 3 ] == "pragma" : # t[-2] should be a valid one for p in self . _pragmas : if p . replace ( "=" , "" ) == t [ - 2 ] : vals = self . _pragmas [ p ] if not vals : return [ ] return [ x + ";" for x in vals if x . startswith ( token ) ] # at equals? if len ( t ) > 1 and t [ - 2 ] == "pragma" and line [ : end ] . replace ( " " , "" ) . endswith ( "=" ) : for p in self . _pragmas : if p . replace ( "=" , "" ) == t [ - 1 ] : vals = self . _pragmas [ p ] if not vals : return [ ] return vals # pragma foo if len ( t ) > 1 and t [ - 2 ] == "pragma" : res = [ x for x in self . _pragmas . keys ( ) if x . startswith ( token ) ] res . sort ( ) return res # pragma if len ( t ) and t [ - 1 ] == "pragma" : res = list ( self . _pragmas . keys ( ) ) res . sort ( ) return res # This is currently not context sensitive (eg it doesn't look # to see if last token was 'FROM' and hence next should only # be table names.  That is a SMOP like pragmas above res = [ ] ut = token . upper ( ) for corpus in self . _completion_cache : for word in corpus : if word . upper ( ) . startswith ( ut ) : # potential match - now match case if word . startswith ( token ) : # exact if word not in res : res . append ( word ) elif word . lower ( ) . startswith ( token ) : # lower if word . lower ( ) not in res : res . append ( word . lower ( ) ) elif word . upper ( ) . startswith ( token ) : # upper if word . upper ( ) not in res : res . append ( word . upper ( ) ) else : # match letter by letter otherwise readline mangles what was typed in w = token + word [ len ( token ) : ] if w not in res : res . append ( w ) return res