Skip to content
Merged
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 @@ -142,6 +142,7 @@ Contributors:
* Fabio (3ximus)
* Doug Harris (dougharris)
* Jay Knight (jay-knight)
* 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 @@ -276,7 +276,7 @@
return (Keyword(), Special())


def suggest_based_on_last_token(token, stmt):

Check notice

Code scanning / CodeQL

Returning tuples with varying lengths Note

suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 1
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 2
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and
tuple of size 1
.
suggest_based_on_last_token returns
tuple of size 0
and [tuple of
if isinstance(token, str):
token_v = token.lower()
elif isinstance(token, Comparison):
Expand Down Expand Up @@ -372,7 +372,15 @@
# 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 @@
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()}
Loading