From 42f9f77e07efc1c4f248aa9e48401e11c0ae4601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sat, 19 May 2012 17:36:56 +0200 Subject: Cleaned group_identifier_list() --- sqlparse/engine/grouping.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'sqlparse/engine') diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 1487c24..8f5ccd9 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -185,9 +185,10 @@ 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 + 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 +203,40 @@ 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, ',') + + tcomma = tlist.token_next_match(0, T.Punctuation, ',') start = None - while tcomma is not None: + while tcomma: before = tlist.token_prev(tcomma) after = tlist.token_next(tcomma) + # Check if the tokens around tcomma belong to a 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 "," - start = None - tcomma = tlist.token_next_match(tlist.token_index(tcomma) + 1, - T.Punctuation, ',') - else: + + if bpassed and apassed: if start is None: start = before + next_ = tlist.token_next(after) - if next_ is None or not next_.match(T.Punctuation, ','): + if next_ and next_.match(T.Punctuation, ','): + tcomma = next_ + else: # 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_ + else: + # Something's wrong here, skip ahead to next "," + start = None + tcomma = tlist.token_next_match(tlist.token_index(tcomma) + 1, + T.Punctuation, ',') def group_parenthesis(tlist): -- cgit v1.2.1 From 1b1810e4eaa5af17fe8d4f40717ae8f0a30554d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sat, 19 May 2012 18:00:43 +0200 Subject: Commented group_identifier_list() --- sqlparse/engine/grouping.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'sqlparse/engine') diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 8f5ccd9..37e029b 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -185,6 +185,7 @@ def group_identifier(tlist): def group_identifier_list(tlist): + # First group the `tlist` sublists for sgroup in tlist.get_sublists(): if not isinstance(sgroup, sql.IdentifierList): group_identifier_list(sgroup) @@ -204,13 +205,14 @@ def group_identifier_list(tlist): lambda t: isinstance(t, sql.Comment), ] - tcomma = tlist.token_next_match(0, T.Punctuation, ',') start = 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 and func(before): @@ -218,22 +220,31 @@ def group_identifier_list(tlist): if after and func(after): apassed = True + # Both tokens around tcomma belong to a list if bpassed and apassed: - if start is None: + # 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_ and next_.match(T.Punctuation, ','): tcomma = next_ + + # Reached the end of the list else: - # 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(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: - # Something's wrong here, skip ahead to next "," start = None tcomma = tlist.token_next_match(tlist.token_index(tcomma) + 1, T.Punctuation, ',') -- cgit v1.2.1 From 9255cbc838b1fada24b2360ec9a12e311b9ced63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sat, 19 May 2012 18:36:36 +0200 Subject: Improvements on identifierlist --- sqlparse/engine/grouping.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'sqlparse/engine') diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 37e029b..095827c 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -228,19 +228,25 @@ def group_identifier_list(tlist): # Look if the next token is another comma next_ = tlist.token_next(after) - if next_ and next_.match(T.Punctuation, ','): - tcomma = next_ + if next_: + if next_.match(T.Punctuation, ','): + tcomma = next_ + continue + + elif(next_.ttype == T.Keyword + and next_.value not in ('FROM', 'WHERE', 'GROUP')): + tcomma = next_ + continue # Reached the end of the list - else: - # Create and group the identifiers list - tokens = tlist.tokens_between(start, after) - group = tlist.group_tokens(sql.IdentifierList, tokens) + # 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(group) + 1, - T.Punctuation, ',') + # Skip ahead to next "," + start = None + 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 "," @@ -249,6 +255,12 @@ def group_identifier_list(tlist): 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): _group_matching(tlist, T.Punctuation, '(', T.Punctuation, ')', -- cgit v1.2.1 From 7768a1a1bd67578ef2a591ac779782098716825d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Fri, 1 Jun 2012 23:05:56 +0200 Subject: Fixed bug on identifiers --- sqlparse/engine/grouping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sqlparse/engine') diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 095827c..4120fc3 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -234,7 +234,7 @@ def group_identifier_list(tlist): continue elif(next_.ttype == T.Keyword - and next_.value not in ('FROM', 'WHERE', 'GROUP')): + and next_.value.upper() not in ('FROM', 'WHERE', 'GROUP')): tcomma = next_ continue -- cgit v1.2.1