Skip to content

Commit e92a032

Browse files
committed
Fix handling of IF EXISTS statements in BEGIN...END blocks (fixes #812).
1 parent 149bebf commit e92a032

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Bug Fixes
2525
* Remove shebang from cli.py and remove executable flag (pr818).
2626
* Fix strip_comments not removing all comments when input contains only
2727
comments (issue801, pr803 by stropysh).
28+
* Fix splitting statements with IF EXISTS/IF NOT EXISTS inside BEGIN...END
29+
blocks (issue812).
2830

2931

3032
Release 0.5.3 (Dez 10, 2024)

sqlparse/keywords.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
(r'((LEFT\s+|RIGHT\s+|FULL\s+)?(INNER\s+|OUTER\s+|STRAIGHT\s+)?'
7171
r'|(CROSS\s+|NATURAL\s+)?)?JOIN\b', tokens.Keyword),
7272
(r'END(\s+IF|\s+LOOP|\s+WHILE)?\b', tokens.Keyword),
73+
(r'IF\s+(NOT\s+)?EXISTS\b', tokens.Keyword),
7374
(r'NOT\s+NULL\b', tokens.Keyword),
7475
(r'(ASC|DESC)(\s+NULLS\s+(FIRST|LAST))?\b', tokens.Keyword.Order),
7576
(r'(ASC|DESC)\b', tokens.Keyword.Order),

tests/test_split.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,17 @@ def test_split_go(sql, num): # issue762
208208
def test_split_multiple_case_in_begin(load_file): # issue784
209209
stmts = sqlparse.split(load_file('multiple_case_in_begin.sql'))
210210
assert len(stmts) == 1
211+
212+
213+
def test_split_if_exists_in_begin_end(): # issue812
214+
# IF EXISTS should not be confused with control flow IF
215+
sql = """CREATE TASK t1 AS
216+
BEGIN
217+
CREATE OR REPLACE TABLE temp1;
218+
DROP TABLE IF EXISTS temp1;
219+
END;
220+
EXECUTE TASK t1;"""
221+
stmts = sqlparse.split(sql)
222+
assert len(stmts) == 2
223+
assert 'CREATE TASK' in stmts[0]
224+
assert 'EXECUTE TASK' in stmts[1]

0 commit comments

Comments
 (0)