diff options
author | Jesús Leganés Combarro "Piranna" <piranna@gmail.com> | 2012-02-03 15:51:42 +0100 |
---|---|---|
committer | Jesús Leganés Combarro "Piranna" <piranna@gmail.com> | 2012-02-03 15:51:42 +0100 |
commit | 10afc2054a39ea5a5a07a20ab181b18b89feee9c (patch) | |
tree | da4afffc1faa938ba8bcbdf6fbe153ecc1f10f19 /sqlparse/sql.py | |
parent | 4e8ae03682d346b2a57dc9d4760d838ec9674804 (diff) | |
download | sqlparse-10afc2054a39ea5a5a07a20ab181b18b89feee9c.tar.gz |
Changed get_case() to use a mode stateful variable
Diffstat (limited to 'sqlparse/sql.py')
-rw-r--r-- | sqlparse/sql.py | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 1bc6b08..72609e2 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -493,33 +493,43 @@ class Case(TokenList): If an ELSE exists condition is None. """ + CONDITION = 1 + VALUE = 2 + ret = [] - in_value = False - in_condition = True + mode = CONDITION + for token in self.tokens: + # Set mode from the current statement if token.match(T.Keyword, 'CASE'): continue + elif token.match(T.Keyword, 'WHEN'): ret.append(([], [])) - in_condition = True - in_value = False + mode = CONDITION + + elif token.match(T.Keyword, 'THEN'): + mode = VALUE + elif token.match(T.Keyword, 'ELSE'): ret.append((None, [])) - in_condition = False - in_value = True - elif token.match(T.Keyword, 'THEN'): - in_condition = False - in_value = True + mode = VALUE + elif token.match(T.Keyword, 'END'): - in_condition = False - in_value = False - if (in_condition or in_value) and not ret: - # First condition withou preceding WHEN + mode = None + + # First condition without preceding WHEN + if mode and not ret: ret.append(([], [])) - if in_condition: + + # Append token depending of the current mode + if mode == CONDITION: ret[-1][0].append(token) - elif in_value: + + elif mode == VALUE: ret[-1][1].append(token) + + # Return cases list return ret |