summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
Diffstat (limited to 'sqlparse')
-rw-r--r--sqlparse/filters/aligned_indent.py17
-rw-r--r--sqlparse/filters/reindent.py2
-rw-r--r--sqlparse/keywords.py2
-rw-r--r--sqlparse/sql.py4
4 files changed, 17 insertions, 8 deletions
diff --git a/sqlparse/filters/aligned_indent.py b/sqlparse/filters/aligned_indent.py
index 906e330..4b195bc 100644
--- a/sqlparse/filters/aligned_indent.py
+++ b/sqlparse/filters/aligned_indent.py
@@ -15,11 +15,12 @@ class AlignedIndentFilter(object):
join_words = (r'((LEFT\s+|RIGHT\s+|FULL\s+)?'
r'(INNER\s+|OUTER\s+|STRAIGHT\s+)?|'
r'(CROSS\s+|NATURAL\s+)?)?JOIN\b')
+ by_words = ('GROUP BY', 'ORDER BY')
split_words = ('FROM',
join_words, 'ON',
'WHERE', 'AND', 'OR',
- 'GROUP', 'HAVING', 'LIMIT',
- 'ORDER', 'UNION', 'VALUES',
+ 'GROUP BY', 'HAVING', 'LIMIT',
+ 'ORDER BY', 'UNION', 'VALUES',
'SET', 'BETWEEN', 'EXCEPT')
def __init__(self, char=' ', n='\n'):
@@ -101,8 +102,12 @@ class AlignedIndentFilter(object):
def _split_kwds(self, tlist):
tidx, token = self._next_token(tlist)
while token:
- # joins are special case. only consider the first word as aligner
- if token.match(T.Keyword, self.join_words, regex=True):
+ # joins, group/order by are special case. only consider the first
+ # word as aligner
+ if (
+ token.match(T.Keyword, self.join_words, regex=True) or
+ token.match(T.Keyword, ('GROUP BY', 'ORDER BY'))
+ ):
token_indent = token.value.split()[0]
else:
token_indent = text_type(token)
@@ -117,7 +122,9 @@ class AlignedIndentFilter(object):
idx = tlist.token_index(sgroup)
pidx, prev_ = tlist.token_prev(idx)
# HACK: make "group/order by" work. Longer than max_len.
- offset_ = 3 if (prev_ and prev_.match(T.Keyword, 'BY')) else 0
+ offset_ = 3 if (
+ prev_ and prev_.match(T.Keyword, ('GROUP BY', 'ORDER BY'))
+ ) else 0
with offset(self, offset_):
self._process(sgroup)
diff --git a/sqlparse/filters/reindent.py b/sqlparse/filters/reindent.py
index 02a60df..acec8ca 100644
--- a/sqlparse/filters/reindent.py
+++ b/sqlparse/filters/reindent.py
@@ -54,7 +54,7 @@ class ReindentFilter(object):
def _next_token(self, tlist, idx=-1):
split_words = ('FROM', 'STRAIGHT_JOIN$', 'JOIN$', 'AND', 'OR',
- 'GROUP', 'ORDER', 'UNION', 'VALUES',
+ 'GROUP BY', 'ORDER BY', 'UNION', 'VALUES',
'SET', 'BETWEEN', 'EXCEPT', 'HAVING', 'LIMIT')
m_split = T.Keyword, split_words, True
tidx, token = tlist.token_next_by(m=m_split, idx=idx)
diff --git a/sqlparse/keywords.py b/sqlparse/keywords.py
index a3e2b9d..ad85368 100644
--- a/sqlparse/keywords.py
+++ b/sqlparse/keywords.py
@@ -78,6 +78,8 @@ SQL_REGEX = {
(r'UNION\s+ALL\b', tokens.Keyword),
(r'CREATE(\s+OR\s+REPLACE)?\b', tokens.Keyword.DDL),
(r'DOUBLE\s+PRECISION\b', tokens.Name.Builtin),
+ (r'GROUP\s+BY\b', tokens.Keyword),
+ (r'ORDER\s+BY\b', tokens.Keyword),
(r'[0-9_A-ZÀ-Ü][_$#\w]*', is_keyword),
diff --git a/sqlparse/sql.py b/sqlparse/sql.py
index 1f56117..d9702ca 100644
--- a/sqlparse/sql.py
+++ b/sqlparse/sql.py
@@ -536,14 +536,14 @@ class Where(TokenList):
"""A WHERE clause."""
M_OPEN = T.Keyword, 'WHERE'
M_CLOSE = T.Keyword, (
- 'ORDER', 'GROUP', 'LIMIT', 'UNION', 'UNION ALL', 'EXCEPT',
+ 'ORDER BY', 'GROUP BY', 'LIMIT', 'UNION', 'UNION ALL', 'EXCEPT',
'HAVING', 'RETURNING', 'INTO')
class Having(TokenList):
"""A HAVING clause."""
M_OPEN = T.Keyword, 'HAVING'
- M_CLOSE = T.Keyword, ('ORDER', 'LIMIT')
+ M_CLOSE = T.Keyword, ('ORDER BY', 'LIMIT')
class Case(TokenList):