mirror of https://github.com/dbcli/pgcli
Merge branch 'master' into pr127
This commit is contained in:
commit
41927cd8d3
|
@ -182,10 +182,10 @@ class PGCli(object):
|
|||
logger.debug('sql: %r', document.text)
|
||||
successful = False
|
||||
start = time()
|
||||
# Initialized to None because res might never get
|
||||
# initialized if an exception occurs in pgexecute.run().
|
||||
# Which causes finally clause to fail.
|
||||
res = None
|
||||
# Initialized to [] because res might never get initialized
|
||||
# if an exception occurs in pgexecute.run(). Which causes
|
||||
# finally clause to fail.
|
||||
res = []
|
||||
res = pgexecute.run(document.text)
|
||||
duration = time() - start
|
||||
successful = True
|
||||
|
|
|
@ -46,11 +46,15 @@ class PGCompleter(Completer):
|
|||
def __init__(self, smart_completion=True):
|
||||
super(self.__class__, self).__init__()
|
||||
self.smart_completion = smart_completion
|
||||
|
||||
self.reserved_words = set()
|
||||
for x in self.keywords:
|
||||
self.reserved_words.update(x.split())
|
||||
self.name_pattern = compile("^[_a-z][_a-z0-9\$]*$")
|
||||
|
||||
def escape_name(self, name):
|
||||
if not self.name_pattern.match(name) or name.upper() in self.keywords or name.upper() in self.functions:
|
||||
if ((not self.name_pattern.match(name))
|
||||
or (name.upper() in self.reserved_words)
|
||||
or (name.upper() in self.functions)):
|
||||
name = '"%s"' % name
|
||||
|
||||
return name
|
||||
|
|
|
@ -6,7 +6,8 @@ from prompt_toolkit.document import Document
|
|||
schemata = {
|
||||
'public': {
|
||||
'users': ['id', 'email', 'first_name', 'last_name'],
|
||||
'orders': ['id', 'ordered_date', 'status']
|
||||
'orders': ['id', 'ordered_date', 'status'],
|
||||
'select': ['id', 'insert', 'ABC'],
|
||||
},
|
||||
'custom': {
|
||||
'products': ['id', 'product_name', 'price'],
|
||||
|
@ -14,7 +15,6 @@ schemata = {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def completer():
|
||||
|
||||
|
@ -268,3 +268,28 @@ def test_suggested_tables_after_on(completer, complete_event):
|
|||
assert set(result) == set([
|
||||
Completion(text='users', start_position=0),
|
||||
Completion(text='orders', start_position=0)])
|
||||
|
||||
def test_table_names_after_from(completer, complete_event):
|
||||
text = 'SELECT * FROM '
|
||||
position = len('SELECT * FROM ')
|
||||
result = set(completer.get_completions(
|
||||
Document(text=text, cursor_position=position),
|
||||
complete_event))
|
||||
assert set(result) == set([
|
||||
Completion(text='users', start_position=0),
|
||||
Completion(text='orders', start_position=0),
|
||||
Completion(text='"select"', start_position=0),
|
||||
])
|
||||
|
||||
def test_auto_escaped_col_names(completer, complete_event):
|
||||
text = 'SELECT from "select"'
|
||||
position = len('SELECT ')
|
||||
result = set(completer.get_completions(
|
||||
Document(text=text, cursor_position=position),
|
||||
complete_event))
|
||||
assert set(result) == set([
|
||||
Completion(text='*', start_position=0),
|
||||
Completion(text='id', start_position=0),
|
||||
Completion(text='"insert"', start_position=0),
|
||||
Completion(text='"ABC"', start_position=0), ] +
|
||||
list(map(Completion, completer.functions)))
|
||||
|
|
Loading…
Reference in New Issue