summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2014-12-21 17:12:37 +0100
committerAndi Albrecht <albrecht.andi@gmail.com>2014-12-21 17:12:37 +0100
commit3f5ec21ffcd4702fc54b4a769224760ffbc31de9 (patch)
tree98c91efa0e7f038e2073f09f9389c7c6ec0aec24
parent1e8a1b7cdb2d07eedfc525290dbfd3ac8e2882b6 (diff)
downloadsqlparse-3f5ec21ffcd4702fc54b4a769224760ffbc31de9.tar.gz
Improve formatting of HAVING statements.
-rw-r--r--CHANGES5
-rw-r--r--sqlparse/engine/grouping.py2
-rw-r--r--sqlparse/filters.py13
-rw-r--r--tests/test_format.py15
4 files changed, 32 insertions, 3 deletions
diff --git a/CHANGES b/CHANGES
index 5cac0f6..b7f5d7d 100644
--- a/CHANGES
+++ b/CHANGES
@@ -2,7 +2,10 @@ Development Version
-------------------
Bug Fixes
-* Fix a regression in identifiers with square bracktes notation (issue153).
+* Fix a regression for identifiers with square bracktes notation (issue153).
+
+Enhancements
+* Improved formatting of HAVING statements.
Release 0.1.14 (Nov 30, 2014)
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py
index ee534e3..a048128 100644
--- a/sqlparse/engine/grouping.py
+++ b/sqlparse/engine/grouping.py
@@ -301,7 +301,7 @@ def group_where(tlist):
if not isinstance(sgroup, sql.Where)]
idx = 0
token = tlist.token_next_match(idx, T.Keyword, 'WHERE')
- stopwords = ('ORDER', 'GROUP', 'LIMIT', 'UNION', 'EXCEPT')
+ stopwords = ('ORDER', 'GROUP', 'LIMIT', 'UNION', 'EXCEPT', 'HAVING')
while token:
tidx = tlist.token_index(token)
end = tlist.token_next_match(tidx + 1, T.Keyword, stopwords)
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index e576a26..47972e4 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -316,7 +316,7 @@ class ReindentFilter:
def _split_kwds(self, tlist):
split_words = ('FROM', 'STRAIGHT_JOIN$', 'JOIN$', 'AND', 'OR',
'GROUP', 'ORDER', 'UNION', 'VALUES',
- 'SET', 'BETWEEN', 'EXCEPT')
+ 'SET', 'BETWEEN', 'EXCEPT', 'HAVING')
def _next_token(i):
t = tlist.token_next_match(i, T.Keyword, split_words,
@@ -374,6 +374,17 @@ class ReindentFilter:
self._process_default(tlist)
self.indent -= 1
+ def _process_having(self, tlist):
+ token = tlist.token_next_match(0, T.Keyword, 'HAVING')
+ try:
+ tlist.insert_before(token, self.nl())
+ except ValueError: # issue121, errors in statement
+ pass
+ self.indent += 1
+ self._process_default(tlist)
+ self.indent -= 1
+
+
def _process_parenthesis(self, tlist):
first = tlist.token_next(0)
indented = False
diff --git a/tests/test_format.py b/tests/test_format.py
index b77b7a1..b789b17 100644
--- a/tests/test_format.py
+++ b/tests/test_format.py
@@ -326,3 +326,18 @@ def test_truncate_strings_invalid_option():
def test_truncate_strings_doesnt_truncate_identifiers(sql):
formatted = sqlparse.format(sql, truncate_strings=2)
assert formatted == sql
+
+
+def test_having_produces_newline():
+ sql = (
+ 'select * from foo, bar where bar.id = foo.bar_id'
+ ' having sum(bar.value) > 100')
+ formatted = sqlparse.format(sql, reindent=True)
+ expected = [
+ 'select *',
+ 'from foo,',
+ ' bar',
+ 'where bar.id = foo.bar_id',
+ 'having sum(bar.value) > 100'
+ ]
+ assert formatted == '\n'.join(expected)