1
0
Fork 0

Separate case sensitive and insensitive commands.

This commit is contained in:
Amjith Ramanujam 2014-12-12 07:10:55 -08:00
parent c4a44b8e3e
commit f8f7d4847d
3 changed files with 26 additions and 12 deletions

View File

@ -15,7 +15,8 @@ from prompt_toolkit.key_bindings.emacs import emacs_bindings
from pygments.lexers.sql import SqlLexer
from .packages.tabulate import tabulate
from .packages.pgspecial import COMMANDS
from .packages.pgspecial import (CASE_SENSITIVE_COMMANDS,
NON_CASE_SENSITIVE_COMMANDS)
from .pgcompleter import PGCompleter
from .pgtoolbar import PGToolbar
from .pgstyle import PGStyle
@ -59,7 +60,8 @@ def cli(database, user, password, host, port):
bottom_toolbars=[
PGToolbar()])
completer = PGCompleter(smart_completion)
completer.extend_special_commands(COMMANDS.keys())
completer.extend_special_commands(CASE_SENSITIVE_COMMANDS.keys())
completer.extend_special_commands(NON_CASE_SENSITIVE_COMMANDS.keys())
completer.extend_table_names(pgexecute.tables())
completer.extend_column_names(pgexecute.all_columns())
completer.extend_database_names(pgexecute.databases())

View File

@ -687,8 +687,7 @@ def sql_name_pattern(pattern):
return schema, relname
COMMANDS = {
'describe': describe_table_details,
CASE_SENSITIVE_COMMANDS = {
'\d': describe_table_details,
'\dt': '''SELECT n.nspname as "Schema", c.relname as "Name", CASE
c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN
@ -699,8 +698,12 @@ COMMANDS = {
= 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;'''
}
1,2;''',
}
NON_CASE_SENSITIVE_COMMANDS = {
'describe': describe_table_details,
}
def execute(cur, sql):
"""Execute a special command and return the results. If the special command
@ -708,9 +711,17 @@ def execute(cur, sql):
"""
command, verbose, arg = parse_special_command(sql)
global COMMANDS
command_executor = COMMANDS[command]
# Look up the command in the case-sensitive dict, if it's not there look in
# non-case-sensitive dict. If not there either, throw a KeyError exception.
global CASE_SENSITIVE_COMMANDS
global NON_CASE_SENSITIVE_COMMANDS
try:
command_executor = CASE_SENSITIVE_COMMANDS[command]
except KeyError:
command_executor = NON_CASE_SENSITIVE_COMMANDS[command.lower()]
# If the command executor is a function, then call the function with the
# args. If it's a string, then assume it's an SQL command and run it.
if callable(command_executor):
return command_executor(cur, arg, verbose)
elif isinstance(command_executor, str):

View File

@ -60,7 +60,8 @@ class PGCompleter(Completer):
@staticmethod
def find_matches(text, collection):
for item in collection:
if item.startswith(text) or item.startswith(text.upper()):
if item.startswith(text) or item.startswith(text.upper() or
item.startswith(text.lower())):
yield Completion(item, -len(text))
def get_completions(self, document, complete_event):
@ -92,11 +93,11 @@ class PGCompleter(Completer):
if last_token.lower() in ('select', 'where', 'having', 'set',
'order by', 'group by'):
return self.find_matches(word_before_cursor, self.column_names)
elif last_token.lower() in ('from', 'update', 'into'):
elif last_token.lower() in ('from', 'update', 'into', 'describe'):
return self.find_matches(word_before_cursor, self.table_names)
elif last_token in ('d', 'describe'): # This for the \d special command.
elif last_token.lower() in ('d',): # This for the \d special command.
return self.find_matches(word_before_cursor, self.table_names)
elif last_token in ('c', 'use'): # This for the \c special command.
elif last_token.lower() in ('c', 'use'): # This for the \c special command.
return self.find_matches(word_before_cursor, self.database_names)
else:
return self.find_matches(word_before_cursor,