summaryrefslogtreecommitdiff
path: root/sqlparse/engine
diff options
context:
space:
mode:
authorJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-06-01 23:09:33 +0200
committerJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-06-01 23:09:33 +0200
commit5a3b9b44c1c4782eacf882c541be59db3bbec020 (patch)
tree01954dbfcb8f3f97d570b91d7caaefba4c4d7d47 /sqlparse/engine
parent2e84066066059891270ea27e3397ed3eb39d419f (diff)
parent7768a1a1bd67578ef2a591ac779782098716825d (diff)
downloadsqlparse-5a3b9b44c1c4782eacf882c541be59db3bbec020.tar.gz
Merge branch 'identifierlist' into issue_50
Conflicts: sqlparse/filters.py
Diffstat (limited to 'sqlparse/engine')
-rw-r--r--sqlparse/engine/grouping.py74
1 files changed, 51 insertions, 23 deletions
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py
index 1487c24..4120fc3 100644
--- a/sqlparse/engine/grouping.py
+++ b/sqlparse/engine/grouping.py
@@ -185,9 +185,11 @@ def group_identifier(tlist):
def group_identifier_list(tlist):
- [group_identifier_list(sgroup) for sgroup in tlist.get_sublists()
- if not isinstance(sgroup, sql.IdentifierList)]
- idx = 0
+ # First group the `tlist` sublists
+ for sgroup in tlist.get_sublists():
+ if not isinstance(sgroup, sql.IdentifierList):
+ group_identifier_list(sgroup)
+
# Allowed list items
fend1_funcs = [lambda t: isinstance(t, (sql.Identifier, sql.Function,
sql.Case)),
@@ -202,36 +204,62 @@ def group_identifier_list(tlist):
lambda t: isinstance(t, sql.Comparison),
lambda t: isinstance(t, sql.Comment),
]
- tcomma = tlist.token_next_match(idx, T.Punctuation, ',')
+
start = None
- while tcomma is not None:
+
+ tcomma = tlist.token_next_match(0, T.Punctuation, ',')
+ while tcomma:
before = tlist.token_prev(tcomma)
after = tlist.token_next(tcomma)
- # Check if the tokens around tcomma belong to a list
+
+ # Check if the tokens around tcomma belong to an identifier list
bpassed = apassed = False
for func in fend1_funcs:
- if before is not None and func(before):
+ if before and func(before):
bpassed = True
- if after is not None and func(after):
+ if after and func(after):
apassed = True
- if not bpassed or not apassed:
- # Something's wrong here, skip ahead to next ","
+
+ # Both tokens around tcomma belong to a list
+ if bpassed and apassed:
+ # Set the start of the identifier list if not defined before
+ if start == None:
+ start = before
+
+ # Look if the next token is another comma
+ next_ = tlist.token_next(after)
+ if next_:
+ if next_.match(T.Punctuation, ','):
+ tcomma = next_
+ continue
+
+ elif(next_.ttype == T.Keyword
+ and next_.value.upper() not in ('FROM', 'WHERE', 'GROUP')):
+ tcomma = next_
+ continue
+
+ # Reached the end of the list
+ # Create and group the identifiers list
+ tokens = tlist.tokens_between(start, after)
+ group = tlist.group_tokens(sql.IdentifierList, tokens)
+
+ # Skip ahead to next ","
start = None
- tcomma = tlist.token_next_match(tlist.token_index(tcomma) + 1,
+ tcomma = tlist.token_next_match(tlist.token_index(group) + 1,
T.Punctuation, ',')
+
+ # At least one of the tokens around tcomma don't belong to an
+ # identifier list. Something's wrong here, skip ahead to next ","
else:
- if start is None:
- start = before
- next_ = tlist.token_next(after)
- if next_ is None or not next_.match(T.Punctuation, ','):
- # Reached the end of the list
- tokens = tlist.tokens_between(start, after)
- group = tlist.group_tokens(sql.IdentifierList, tokens)
- start = None
- tcomma = tlist.token_next_match(tlist.token_index(group) + 1,
- T.Punctuation, ',')
- else:
- tcomma = next_
+ start = None
+ tcomma = tlist.token_next_match(tlist.token_index(tcomma) + 1,
+ T.Punctuation, ',')
+
+ # There's an open identifier list
+ if start:
+ # Create and group the identifiers list
+ tokens = tlist.tokens_between(start, after)
+ group = tlist.group_tokens(sql.IdentifierList, tokens)
def group_parenthesis(tlist):