Skip to content

Commit 10afc20

Browse files
committed
Changed get_case() to use a mode stateful variable
1 parent 4e8ae03 commit 10afc20

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

sqlparse/sql.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -493,33 +493,43 @@ def get_cases(self):
493493
494494
If an ELSE exists condition is None.
495495
"""
496+
CONDITION = 1
497+
VALUE = 2
498+
496499
ret = []
497-
in_value = False
498-
in_condition = True
500+
mode = CONDITION
501+
499502
for token in self.tokens:
503+
# Set mode from the current statement
500504
if token.match(T.Keyword, 'CASE'):
501505
continue
506+
502507
elif token.match(T.Keyword, 'WHEN'):
503508
ret.append(([], []))
504-
in_condition = True
505-
in_value = False
509+
mode = CONDITION
510+
511+
elif token.match(T.Keyword, 'THEN'):
512+
mode = VALUE
513+
506514
elif token.match(T.Keyword, 'ELSE'):
507515
ret.append((None, []))
508-
in_condition = False
509-
in_value = True
510-
elif token.match(T.Keyword, 'THEN'):
511-
in_condition = False
512-
in_value = True
516+
mode = VALUE
517+
513518
elif token.match(T.Keyword, 'END'):
514-
in_condition = False
515-
in_value = False
516-
if (in_condition or in_value) and not ret:
517-
# First condition withou preceding WHEN
519+
mode = None
520+
521+
# First condition without preceding WHEN
522+
if mode and not ret:
518523
ret.append(([], []))
519-
if in_condition:
524+
525+
# Append token depending of the current mode
526+
if mode == CONDITION:
520527
ret[-1][0].append(token)
521-
elif in_value:
528+
529+
elif mode == VALUE:
522530
ret[-1][1].append(token)
531+
532+
# Return cases list
523533
return ret
524534

525535

0 commit comments

Comments
 (0)