Skip to content

Commit 791e25d

Browse files
committed
Fix error when splitting statements that contain multiple CASE clauses within a BEGIN block (fixes andialbrecht#784).
1 parent 073099d commit 791e25d

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Bug Fixes
1414
previously used just `strip_comments=True`. `strip_comments` did some of the
1515
work that `strip_whitespace` should do.
1616

17+
* Fix error when splitting statements that contain multiple CASE clauses
18+
within a BEGIN block (issue784).
19+
1720

1821
Release 0.5.0 (Apr 13, 2024)
1922
----------------------------

sqlparse/engine/statement_splitter.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def __init__(self):
1717
def _reset(self):
1818
"""Set the filter attributes to its default values"""
1919
self._in_declare = False
20+
self._in_case = False
2021
self._is_create = False
2122
self._begin_depth = 0
2223

@@ -58,16 +59,18 @@ def _change_splitlevel(self, ttype, value):
5859
return 1
5960
return 0
6061

61-
# Should this respect a preceding BEGIN?
62-
# In CASE ... WHEN ... END this results in a split level -1.
63-
# Would having multiple CASE WHEN END and a Assignment Operator
64-
# cause the statement to cut off prematurely?
62+
# BEGIN and CASE/WHEN both end with END
6563
if unified == 'END':
66-
self._begin_depth = max(0, self._begin_depth - 1)
64+
if not self._in_case:
65+
self._begin_depth = max(0, self._begin_depth - 1)
66+
else:
67+
self._in_case = False
6768
return -1
6869

6970
if (unified in ('IF', 'FOR', 'WHILE', 'CASE')
7071
and self._is_create and self._begin_depth > 0):
72+
if unified == 'CASE':
73+
self._in_case = True
7174
return 1
7275

7376
if unified in ('END IF', 'END FOR', 'END WHILE'):
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TRIGGER mytrig
2+
AFTER UPDATE OF vvv ON mytable
3+
BEGIN
4+
UPDATE aa
5+
SET mycola = (CASE WHEN (A=1) THEN 2 END);
6+
UPDATE bb
7+
SET mycolb = (CASE WHEN (B=1) THEN 5 END);
8+
END;

tests/test_split.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,8 @@ def test_split_strip_semicolon_procedure(load_file):
203203
def test_split_go(sql, num): # issue762
204204
stmts = sqlparse.split(sql)
205205
assert len(stmts) == num
206+
207+
208+
def test_split_multiple_case_in_begin(load_file): # issue784
209+
stmts = sqlparse.split(load_file('multiple_case_in_begin.sql'))
210+
assert len(stmts) == 1

0 commit comments

Comments
 (0)