Skip to content

Commit 52ba8b1

Browse files
committed
Add suggestion when setting the search_path
To be suggested autocompletion when setting the search_path, the keyword's suggestion "search_path TO" will be suggested on a "SET" token, only if the latter is the first token of the SQL statement (to differentiate it from the "UPDATE foo SET"). Then, available schemas are suggested (after the "TO" token). Resolves #1511
1 parent 435cf7f commit 52ba8b1

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Contributors:
141141
* Josh Lynch (josh-lynch)
142142
* Fabio (3ximus)
143143
* Doug Harris (dougharris)
144+
* fbdb
144145

145146
Creator:
146147
--------

changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Features:
77
* Command line option `--init-command`
88
* Provide `init-command` in the config file
99
* Support dsn specific init-command in the config file
10+
* Add suggestion when setting the search_path
1011

1112
Internal:
1213
---------

pgcli/packages/pgliterals/pgliterals.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
"ROWS": [],
228228
"SELECT": [],
229229
"SESSION": [],
230-
"SET": [],
230+
"SET": ["SEARCH_PATH TO"],
231231
"SHARE": [],
232232
"SHOW": [],
233233
"SIZE": [],

pgcli/packages/sqlcompletion.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,15 @@ def suggest_based_on_last_token(token, stmt):
372372
# We're probably in a function argument list
373373
return _suggest_expression(token_v, stmt)
374374
elif token_v == "set":
375+
# "set" for changing a run-time parameter
376+
p = sqlparse.parse(stmt.text_before_cursor)[0]
377+
is_first_token = p.token_first().value.upper() == token_v.upper()
378+
if is_first_token:
379+
return (Keyword(token_v.upper()),)
380+
381+
# E.g. 'UPDATE foo SET'
375382
return (Column(table_refs=stmt.get_tables(), local_tables=stmt.local_tables),)
383+
376384
elif token_v in ("select", "where", "having", "order by", "distinct"):
377385
return _suggest_expression(token_v, stmt)
378386
elif token_v == "as":
@@ -494,6 +502,9 @@ def suggest_based_on_last_token(token, stmt):
494502
return tuple(suggestions)
495503
elif token_v in {"alter", "create", "drop"}:
496504
return (Keyword(token_v.upper()),)
505+
elif token_v == "to":
506+
# E.g. 'SET search_path TO'
507+
return (Schema(),)
497508
elif token.is_keyword:
498509
# token is a keyword we haven't implemented any special handling for
499510
# go backwards in the query until we find one we do recognize

tests/test_sqlcompletion.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,3 +918,13 @@ def test_handle_unrecognized_kw_generously():
918918
@pytest.mark.parametrize("sql", ["ALTER ", "ALTER TABLE foo ALTER "])
919919
def test_keyword_after_alter(sql):
920920
assert Keyword("ALTER") in set(suggest_type(sql, sql))
921+
922+
923+
def test_suggestion_when_setting_search_path():
924+
sql_set = "SET "
925+
suggestion_set = suggest_type(sql_set, sql_set)
926+
assert set(suggestion_set) == {Keyword("SET")}
927+
928+
sql_set_search_path_to = "SET search_path TO "
929+
suggestion_set_search_path_to = suggest_type(sql_set_search_path_to, sql_set_search_path_to)
930+
assert set(suggestion_set_search_path_to) == {Schema()}

0 commit comments

Comments
 (0)