summaryrefslogtreecommitdiff
path: root/sqlparse/engine
diff options
context:
space:
mode:
authorVictor Uriarte <victor.m.uriarte@intel.com>2016-06-15 03:04:41 -0700
committerVictor Uriarte <victor.m.uriarte@intel.com>2016-06-15 13:30:31 -0700
commita653650432b76447255e69cd93ba2d2e2c34d037 (patch)
treebdb080aa9d2245df55a55e3fffaed3b97c8bbb4c /sqlparse/engine
parent49979e9ca1159190320e2faad989c8bd267c8000 (diff)
downloadsqlparse-a653650432b76447255e69cd93ba2d2e2c34d037.tar.gz
remove extra recurse and rename vars
# Conflicts: # sqlparse/engine/grouping.py
Diffstat (limited to 'sqlparse/engine')
-rw-r--r--sqlparse/engine/grouping.py73
1 files changed, 43 insertions, 30 deletions
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py
index 1be4f53..b0b8836 100644
--- a/sqlparse/engine/grouping.py
+++ b/sqlparse/engine/grouping.py
@@ -118,11 +118,11 @@ def group_assignment(tlist):
def group_comparison(tlist):
- I_COMPERABLE = (sql.Parenthesis, sql.Function, sql.Identifier,
+ sqlcls = (sql.Parenthesis, sql.Function, sql.Identifier,
sql.Operation)
- T_COMPERABLE = T_NUMERICAL + T_STRING + T_NAME
+ ttypes = T_NUMERICAL + T_STRING + T_NAME
- func = lambda tk: (imt(tk, t=T_COMPERABLE, i=I_COMPERABLE) or
+ func = lambda tk: (imt(tk, t=ttypes, i=sqlcls) or
(tk and tk.is_keyword and tk.normalized == 'NULL'))
_group_left_right(tlist, (T.Operator.Comparison, None), sql.Comparison,
@@ -131,63 +131,74 @@ def group_comparison(tlist):
@recurse(sql.Identifier)
def group_identifier(tlist):
- T_IDENT = (T.String.Symbol, T.Name)
+ ttypes = (T.String.Symbol, T.Name)
- tidx, token = tlist.token_next_by(t=T_IDENT)
+ tidx, token = tlist.token_next_by(t=ttypes)
while token:
tlist.group_tokens(sql.Identifier, tidx, tidx)
- tidx, token = tlist.token_next_by(t=T_IDENT, idx=tidx)
+ tidx, token = tlist.token_next_by(t=ttypes, idx=tidx)
def group_arrays(tlist):
- tidx, token = tlist.token_next_by(i=sql.SquareBrackets)
- while token:
- pidx, prev_ = tlist.token_prev(tidx)
- if imt(prev_, i=(sql.SquareBrackets, sql.Identifier, sql.Function),
- t=(T.Name, T.String.Symbol,)):
- tlist.group_tokens(sql.Identifier, pidx, tidx, extend=True)
- tidx = pidx
- tidx, token = tlist.token_next_by(i=sql.SquareBrackets, idx=tidx)
+ sqlcls = sql.SquareBrackets, sql.Identifier, sql.Function
+ ttypes = T.Name, T.String.Symbol
+
+ def match(token):
+ return isinstance(token, sql.SquareBrackets)
+
+ def valid_prev(token):
+ return imt(token, i=sqlcls, t=ttypes)
+
+ def valid_next(token):
+ return True
+
+ def post(tlist, pidx, tidx, nidx):
+ return pidx, tidx
+
+ _group(tlist, sql.Identifier, match,
+ valid_prev, valid_next, post, extend=True, recurse=False)
-@recurse(sql.Identifier)
def group_operator(tlist):
ttypes = T_NUMERICAL + T_STRING + T_NAME
- clss = (sql.SquareBrackets, sql.Parenthesis, sql.Function,
- sql.Identifier, sql.Operation)
+ sqlcls = (sql.SquareBrackets, sql.Parenthesis, sql.Function,
+ sql.Identifier, sql.Operation)
def match(token):
return imt(token, t=(T.Operator, T.Wildcard))
def valid(token):
- return imt(token, i=clss, t=ttypes)
+ return imt(token, i=sqlcls, t=ttypes)
def post(tlist, pidx, tidx, nidx):
tlist[tidx].ttype = T.Operator
return pidx, nidx
- _group(tlist, sql.Operation, match, valid, valid, post, extend=False)
+ valid_prev = valid_next = valid
+ _group(tlist, sql.Operation, match,
+ valid_prev, valid_next, post, extend=False)
def group_identifier_list(tlist):
m_role = T.Keyword, ('null', 'role')
m_comma = T.Punctuation, ','
- clss = (sql.Function, sql.Case, sql.Identifier, sql.Comparison,
- sql.IdentifierList, sql.Operation)
+ sqlcls = (sql.Function, sql.Case, sql.Identifier, sql.Comparison,
+ sql.IdentifierList, sql.Operation)
ttypes = (T_NUMERICAL + T_STRING + T_NAME +
(T.Keyword, T.Comment, T.Wildcard))
def match(token):
return imt(token, m=m_comma)
- def func(token):
- return imt(token, i=clss, m=m_role, t=ttypes)
+ def valid(token):
+ return imt(token, i=sqlcls, m=m_role, t=ttypes)
def post(tlist, pidx, tidx, nidx):
return pidx, nidx
+ valid_prev = valid_next = valid
_group(tlist, sql.IdentifierList, match,
- valid_left=func, valid_right=func, post=post, extend=True)
+ valid_prev, valid_next, post, extend=True)
@recurse(sql.Comment)
@@ -308,10 +319,12 @@ def group(stmt):
def _group(tlist, cls, match,
- valid_left=lambda t: True,
- valid_right=lambda t: True,
+ valid_prev=lambda t: True,
+ valid_next=lambda t: True,
post=None,
- extend=True):
+ extend=True,
+ recurse=True
+ ):
"""Groups together tokens that are joined by a middle token. ie. x < y"""
tidx_offset = 0
@@ -322,12 +335,12 @@ def _group(tlist, cls, match,
if token.is_whitespace():
continue
- if token.is_group() and not isinstance(token, cls):
- _group(token, cls, match, valid_left, valid_right, post, extend)
+ if recurse and token.is_group() and not isinstance(token, cls):
+ _group(token, cls, match, valid_prev, valid_next, post, extend)
if match(token):
nidx, next_ = tlist.token_next(tidx)
- if valid_left(prev_) and valid_right(next_):
+ if valid_prev(prev_) and valid_next(next_):
from_idx, to_idx = post(tlist, pidx, tidx, nidx)
grp = tlist.group_tokens(cls, from_idx, to_idx, extend=extend)