Skip to content

Add suggestion when setting the search_path #1513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Contributors:
* Josh Lynch (josh-lynch)
* Fabio (3ximus)
* Doug Harris (dougharris)
* fbdb

Creator:
--------
Expand Down
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Features:
* Command line option `--init-command`
* Provide `init-command` in the config file
* Support dsn specific init-command in the config file
* Add suggestion when setting the search_path

Internal:
---------
Expand Down
2 changes: 1 addition & 1 deletion pgcli/packages/pgliterals/pgliterals.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
"ROWS": [],
"SELECT": [],
"SESSION": [],
"SET": [],
"SET": ["SEARCH_PATH TO"],
"SHARE": [],
"SHOW": [],
"SIZE": [],
Expand Down
11 changes: 11 additions & 0 deletions pgcli/packages/sqlcompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,15 @@ def suggest_based_on_last_token(token, stmt):
# We're probably in a function argument list
return _suggest_expression(token_v, stmt)
elif token_v == "set":
# "set" for changing a run-time parameter
p = sqlparse.parse(stmt.text_before_cursor)[0]
is_first_token = p.token_first().value.upper() == token_v.upper()
if is_first_token:
return (Keyword(token_v.upper()),)

# E.g. 'UPDATE foo SET'
return (Column(table_refs=stmt.get_tables(), local_tables=stmt.local_tables),)

elif token_v in ("select", "where", "having", "order by", "distinct"):
return _suggest_expression(token_v, stmt)
elif token_v == "as":
Expand Down Expand Up @@ -494,6 +502,9 @@ def suggest_based_on_last_token(token, stmt):
return tuple(suggestions)
elif token_v in {"alter", "create", "drop"}:
return (Keyword(token_v.upper()),)
elif token_v == "to":
# E.g. 'SET search_path TO'
return (Schema(),)
elif token.is_keyword:
# token is a keyword we haven't implemented any special handling for
# go backwards in the query until we find one we do recognize
Expand Down
10 changes: 10 additions & 0 deletions tests/test_sqlcompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,3 +918,13 @@ def test_handle_unrecognized_kw_generously():
@pytest.mark.parametrize("sql", ["ALTER ", "ALTER TABLE foo ALTER "])
def test_keyword_after_alter(sql):
assert Keyword("ALTER") in set(suggest_type(sql, sql))


def test_suggestion_when_setting_search_path():
sql_set = "SET "
suggestion_set = suggest_type(sql_set, sql_set)
assert set(suggestion_set) == {Keyword("SET")}

sql_set_search_path_to = "SET search_path TO "
suggestion_set_search_path_to = suggest_type(sql_set_search_path_to, sql_set_search_path_to)
assert set(suggestion_set_search_path_to) == {Schema()}