1
1
mirror of https://github.com/dbcli/pgcli.git synced 2024-10-04 01:08:51 +03:00

Add columns and table names for completion.

This commit is contained in:
Amjith Ramanujam 2014-11-23 15:02:24 -08:00
parent 5eceff01e3
commit 4438cf3376
3 changed files with 26 additions and 1 deletions

View File

@ -31,7 +31,10 @@ def pgcli(database, user, password, host, port):
layout = Layout(before_input=DefaultPrompt('%s> ' % database),
menus=[CompletionsMenu()],
lexer=SqlLexer)
line = Line(completer=PGCompleter())
completer = PGCompleter()
completer.extend_keywords(pgexecute.tables())
completer.extend_keywords(pgexecute.all_columns())
line = Line(completer=completer)
cli = CommandLineInterface(style=PGStyle, layout=layout, line=line)
try:

View File

@ -11,6 +11,10 @@ class PGCompleter(Completer):
'WHERE',
]
@classmethod
def extend_keywords(cls, additional_keywords):
cls.keywords.extend(additional_keywords)
def get_completions(self, document):
word_before_cursor = document.get_word_before_cursor()

View File

@ -7,6 +7,9 @@ class PGExecute(object):
'\dt': '''SELECT n.nspname as "Schema", c.relname as "Name", CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type", pg_catalog.pg_get_userbyid(c.relowner) as "Owner" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','') AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema' AND n.nspname !~ '^pg_toast' AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1,2;'''
}
tables_query = '''SELECT c.relname as "Name" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','') AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema' AND n.nspname !~ '^pg_toast' AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1;'''
columns_query = '''SELECT column_name FROM information_schema.columns WHERE table_name =%s;'''
def __init__(self, database, user, password, host, port):
self.conn = psycopg2.connect(database=database, user=user,
password=password, host=host, port=port)
@ -19,3 +22,18 @@ class PGExecute(object):
cur.execute(sql)
return cur.fetchall()
def tables(self):
with self.conn.cursor() as cur:
cur.execute(self.tables_query)
return (x[0] for x in cur.fetchall())
def columns(self, table):
with self.conn.cursor() as cur:
cur.execute(self.columns_query, (table,))
return (x[0] for x in cur.fetchall())
def all_columns(self):
columns = set()
for table in self.tables():
columns.update(self.columns(table))
return columns