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 changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Bug Fixes
--------
* Reduce duplicated `--checkup` output.
* Handle errors generating completions on stored procedures.
* Fix whitespace/inline comments breaking destructive `UPDATE … WHERE` statement detection.


Internal
Expand Down
7 changes: 4 additions & 3 deletions mycli/packages/parseutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,11 @@ def query_has_where_clause(query: str) -> bool:
# todo: handle "UPDATE LOW_PRIORITY" and "UPDATE IGNORE"
def query_is_single_table_update(query: str) -> bool:
"""Check if a query is a simple single-table UPDATE."""
cleaned_query = sqlparse.format(query, strip_comments=True)
if not cleaned_query:
cleaned_query_for_parsing_only = sqlparse.format(query, strip_comments=True)
cleaned_query_for_parsing_only = re.sub(r'\s+', ' ', cleaned_query_for_parsing_only)
if not cleaned_query_for_parsing_only:
return False
parsed = sqlparse.parse(cleaned_query)
parsed = sqlparse.parse(cleaned_query_for_parsing_only)
if not parsed:
return False
statement = parsed[0]
Expand Down
5 changes: 5 additions & 0 deletions test/test_parseutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ def test_is_destructive_update_with_where_clause():
assert is_destructive(["update"], sql) is False


def test_is_destructive_update_with_where_clause_and_comment():
sql = "use test;\nshow databases;\nUPDATE /* inline comment */ test SET x = 1 WHERE id = 1;"
assert is_destructive(["update"], sql) is False


def test_is_destructive_update_multiple_tables_with_where_clause():
sql = "use test;\nshow databases;\nUPDATE test, foo SET x = 1 WHERE id = 1;"
assert is_destructive(["update"], sql) is True
Expand Down