1
0
Fork 0

Don't quote identifiers that coïncide with non-reserved keywords

Keep track of which keywords are reserved, and quote identifiers
that coïncide with those.
https://www.postgresql.org/docs/10/static/sql-keywords-appendix.html
This commit is contained in:
Joakim Koljonen 2017-08-06 08:59:03 +02:00
parent 9d59fa5a8a
commit b90beeb4d3
No known key found for this signature in database
GPG Key ID: AF0327B5131CD164
6 changed files with 111 additions and 11 deletions

View File

@ -5,6 +5,7 @@ Features:
---------
* Add fish-style auto-suggestion from history. (Thanks: `Amjith Ramanujam`_)
* Don't quote identifiers that are non-reserved keywords. (Thanks: `Joakim Koljonen`_)
* Remove the ``...`` in the continuation prompt and use empty space instead. (Thanks: `Amjith Ramanujam`_)
Bug Fixes:

View File

@ -288,5 +288,107 @@
"TEXT",
"VARCHAR",
"VOID"
],
"reserved": [
"ALL",
"ANALYSE",
"ANALYZE",
"AND",
"ANY",
"ARRAY",
"AS",
"ASC",
"ASYMMETRIC",
"BOTH",
"CASE",
"CAST",
"CHECK",
"COLLATE",
"COLUMN",
"CONSTRAINT",
"CREATE",
"CURRENT_CATALOG",
"CURRENT_DATE",
"CURRENT_ROLE",
"CURRENT_TIME",
"CURRENT_TIMESTAMP",
"CURRENT_USER",
"DEFAULT",
"DEFERRABLE",
"DESC",
"DISTINCT",
"DO",
"ELSE",
"END",
"EXCEPT",
"FALSE",
"FETCH",
"FOR",
"FOREIGN",
"FROM",
"GRANT",
"GROUP",
"HAVING",
"IN",
"INITIALLY",
"INTERSECT",
"INTO",
"LATERAL",
"LEADING",
"LIMIT",
"LOCALTIME",
"LOCALTIMESTAMP",
"NOT",
"NULL",
"OFFSET",
"ON",
"ONLY",
"OR",
"ORDER",
"PLACING",
"PRIMARY",
"REFERENCES",
"RETURNING",
"SELECT",
"SESSION_USER",
"SOME",
"SYMMETRIC",
"TABLE",
"THEN",
"TO",
"TRAILING",
"TRUE",
"UNION",
"UNIQUE",
"USER",
"USING",
"VARIADIC",
"WHEN",
"WHERE",
"WINDOW",
"WITH",
"AUTHORIZATION",
"BINARY",
"COLLATION",
"CONCURRENTLY",
"CROSS",
"CURRENT_SCHEMA",
"FREEZE",
"FULL",
"ILIKE",
"INNER",
"IS",
"ISNULL",
"JOIN",
"LEFT",
"LIKE",
"NATURAL",
"NOTNULL",
"OUTER",
"OVERLAPS",
"RIGHT",
"SIMILAR",
"TABLESAMPLE",
"VERBOSE"
]
}

View File

@ -74,6 +74,7 @@ class PGCompleter(Completer):
keywords = tuple(set(chain(keywords_tree.keys(), *keywords_tree.values())))
functions = get_literals('functions')
datatypes = get_literals('datatypes')
reserved_words = set(get_literals('reserved'))
def __init__(self, smart_completion=True, pgspecial=None, settings=None):
super(PGCompleter, self).__init__()
@ -110,10 +111,6 @@ class PGCompleter(Completer):
if keyword_casing not in ('upper', 'lower', 'auto'):
keyword_casing = 'upper'
self.keyword_casing = keyword_casing
self.reserved_words = set()
for x in self.keywords:
self.reserved_words.update(x.split())
self.name_pattern = re.compile(r"^[_a-z][_a-z0-9\$]*$")
self.databases = []

View File

@ -13,7 +13,7 @@ no_qual = ['if_more_than_one_table', 'never']
def escape(name):
if not name.islower() or name in ('select', 'insert'):
if not name.islower() or name in ('select', 'localtimestamp'):
return '"' + name + '"'
return name

View File

@ -9,7 +9,7 @@ metadata = {
'public': {
'users': ['id', 'email', 'first_name', 'last_name'],
'orders': ['id', 'ordered_date', 'status', 'datestamp'],
'select': ['id', 'insert', 'ABC']
'select': ['id', 'localtime', 'ABC']
},
'custom': {
'users': ['id', 'phone_number'],
@ -364,7 +364,7 @@ def test_wildcard_column_expansion_with_table_qualifier(completer):
completions = get_result(completer, text, position)
col_list = 'id, "select"."insert", "select"."ABC"'
col_list = 'id, "select"."localtime", "select"."ABC"'
expected = [wildcard_expansion(col_list)]
assert expected == completions
@ -377,7 +377,7 @@ def test_wildcard_column_expansion_with_two_tables(completer):
completions = get_result(completer, text, position)
cols = ('"select".id, "select"."insert", "select"."ABC", '
cols = ('"select".id, "select"."localtime", "select"."ABC", '
'users.id, users.phone_number')
expected = [wildcard_expansion(cols)]
assert completions == expected
@ -390,7 +390,7 @@ def test_wildcard_column_expansion_with_two_tables_and_parent(completer):
completions = get_result(completer, text, position)
col_list = 'id, "select"."insert", "select"."ABC"'
col_list = 'id, "select"."localtime", "select"."ABC"'
expected = [wildcard_expansion(col_list)]
assert expected == completions

View File

@ -724,7 +724,7 @@ def test_wildcard_column_expansion_with_two_tables(completer):
completions = get_result(completer, text, position)
cols = ('"select".id, "select"."insert", "select"."ABC", '
cols = ('"select".id, "select".insert, "select"."ABC", '
'u.id, u.parentid, u.email, u.first_name, u.last_name')
expected = [wildcard_expansion(cols)]
assert completions == expected
@ -737,7 +737,7 @@ def test_wildcard_column_expansion_with_two_tables_and_parent(completer):
completions = get_result(completer, text, position)
col_list = 'id, "select"."insert", "select"."ABC"'
col_list = 'id, "select".insert, "select"."ABC"'
expected = [wildcard_expansion(col_list)]
assert expected == completions