Skip to content

Commit

Permalink
Fix splitting of statements using CASE ... WHEN (fixes #580).
Browse files Browse the repository at this point in the history
  • Loading branch information
andialbrecht committed Sep 30, 2020
1 parent 990500a commit d7b1ee3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Bug Fixes
* Preserve line breaks when removing comments (issue484).
* Fix parsing error when using square bracket notation (issue583).
* Fix splitting when using DECLARE ... HANDLER (issue581).
* Fix splitting of statements using CASE ... WHEN (issue580).


Release 0.3.1 (Feb 29, 2020)
Expand Down
2 changes: 1 addition & 1 deletion sqlparse/engine/statement_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _change_splitlevel(self, ttype, value):
self._begin_depth = max(0, self._begin_depth - 1)
return -1

if (unified in ('IF', 'FOR', 'WHILE')
if (unified in ('IF', 'FOR', 'WHILE', 'CASE')
and self._is_create and self._begin_depth > 0):
return 1

Expand Down
8 changes: 8 additions & 0 deletions tests/files/casewhen_procedure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create procedure procName()
begin
select case when column = 'value' then column else 0 end;
end;
create procedure procName()
begin
select 1;
end;
6 changes: 6 additions & 0 deletions tests/test_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def test_split_casewhen():
assert len(stmts) == 2


def test_split_casewhen_procedure(load_file):
# see issue580
stmts = sqlparse.split(load_file('casewhen_procedure.sql'))
assert len(stmts) == 2


def test_split_cursor_declare():
sql = ('DECLARE CURSOR "foo" AS SELECT 1;\n'
'SELECT 2;')
Expand Down

0 comments on commit d7b1ee3

Please sign in to comment.