summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Greenhall <agreenhall@lyft.com>2015-09-12 01:52:01 -0700
committerVictor Uriarte <victor.m.uriarte@intel.com>2016-06-06 06:31:35 -0700
commit74faef72d8e136a37bec93cc688817c97d162482 (patch)
treeb2cd48e9ff194d5b7d2b74e52a22e3e2021e8d97
parentb15c9c60e2479f6397b7bbcb0787ee66c499e7f8 (diff)
downloadsqlparse-74faef72d8e136a37bec93cc688817c97d162482.tar.gz
Fix Case statements Alignment
-rw-r--r--sqlparse/filters.py4
-rw-r--r--tests/test_format.py30
2 files changed, 32 insertions, 2 deletions
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index 2ce17e9..193029f 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -410,7 +410,7 @@ class AlignedIndentFilter:
end_token = tlist.token_next_match(0, T.Keyword, 'END')
cases.append((None, [end_token]))
- condition_width = max(len(str(cond)) for cond, value in cases)
+ condition_width = max(len(' '.join(map(str, cond))) for cond, value in cases if cond)
for i, (cond, value) in enumerate(cases):
if cond is None: # else or end
stmt = value[0]
@@ -421,7 +421,7 @@ class AlignedIndentFilter:
if i > 0:
tlist.insert_before(stmt, self.whitespace(base_offset + case_offset - len(str(stmt))))
if cond:
- tlist.insert_after(cond[-1], self.whitespace(condition_width - len(str(cond))))
+ tlist.insert_after(cond[-1], self.whitespace(condition_width - len(' '.join(map(str, cond)))))
if i < len(cases) - 1:
# if not the END add a newline
diff --git a/tests/test_format.py b/tests/test_format.py
index dccc3ec..8151bb4 100644
--- a/tests/test_format.py
+++ b/tests/test_format.py
@@ -193,6 +193,36 @@ class TestFormatReindentAligned(TestCaseBase):
' and b between 3 and 4'
]))
+ def test_case_statement_with_between(self):
+ sql = """
+ select a,
+ case when a = 0
+ then 1
+ when bb = 1 then 1
+ when c = 2 then 2
+ when d between 3 and 5 then 3
+ else 0 end as d,
+ extra_col
+ from table
+ where c is true
+ and b between 3 and 4
+ """
+ self.ndiffAssertEqual(
+ self.formatter(sql),
+ '\n'.join([
+ 'select a,',
+ ' case when a = 0 then 1',
+ ' when bb = 1 then 1',
+ ' when c = 2 then 2',
+ ' when d between 3 and 5 then 3',
+ ' else 0',
+ ' end as d,',
+ ' extra_col',
+ ' from table',
+ ' where c is true',
+ ' and b between 3 and 4'
+ ]))
+
def test_group_by(self):
sql = """
select a, b, c, sum(x) as sum_x, count(y) as cnt_y