summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2016-10-24 20:45:43 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2016-10-24 20:45:43 +0200
commite0970cc54af6e441aabfea602e412428d02b7374 (patch)
treed4f8a7daa4251abd6081d96032d1c0dfccb223a7
parentb68e4b781b8d74b0222411e83a16408a83cde7d6 (diff)
downloadsqlparse-e0970cc54af6e441aabfea602e412428d02b7374.tar.gz
Fix an edge-case with subselects in CASE clauses.
-rw-r--r--sqlparse/filters/reindent.py3
-rw-r--r--tests/test_grouping.py10
2 files changed, 12 insertions, 1 deletions
diff --git a/sqlparse/filters/reindent.py b/sqlparse/filters/reindent.py
index ff1b211..bf1f139 100644
--- a/sqlparse/filters/reindent.py
+++ b/sqlparse/filters/reindent.py
@@ -162,7 +162,8 @@ class ReindentFilter(object):
with offset(self, len("WHEN ")):
self._process_default(tlist)
end_idx, end = tlist.token_next_by(m=sql.Case.M_CLOSE)
- tlist.insert_before(end_idx, self.nl())
+ if end_idx is not None:
+ tlist.insert_before(end_idx, self.nl())
def _process_default(self, tlist, stmts=True):
self._split_statements(tlist) if stmts else None
diff --git a/tests/test_grouping.py b/tests/test_grouping.py
index 20151a1..02d211e 100644
--- a/tests/test_grouping.py
+++ b/tests/test_grouping.py
@@ -250,6 +250,16 @@ def test_grouping_alias_case():
assert p.tokens[0].get_alias() == 'foo'
+def test_grouping_subquery_no_parens():
+ # Not totally sure if this is the right approach...
+ # When a THEN clause contains a subquery w/o parens around it *and*
+ # a WHERE condition, the WHERE grouper consumes END too.
+ # This takes makes sure that it doesn't fail.
+ p = sqlparse.parse('CASE WHEN 1 THEN select 2 where foo = 1 end')[0]
+ assert len(p.tokens) == 1
+ assert isinstance(p.tokens[0], sql.Case)
+
+
def test_grouping_alias_returns_none():
# see issue185
p = sqlparse.parse('foo.bar')[0]