diff options
Diffstat (limited to 'sqlparse')
| -rw-r--r-- | sqlparse/engine/grouping.py | 100 | ||||
| -rw-r--r-- | sqlparse/filters/aligned_indent.py | 10 | ||||
| -rw-r--r-- | sqlparse/filters/others.py | 14 | ||||
| -rw-r--r-- | sqlparse/filters/reindent.py | 26 | ||||
| -rw-r--r-- | sqlparse/sql.py | 46 |
5 files changed, 98 insertions, 98 deletions
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 88064cb..a229e3d 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -32,16 +32,16 @@ def _group_left_right(tlist, m, cls, continue tidx = tlist.token_index(token) - pidx, prev_ = tlist.token_idx_prev(tidx) - nidx, next_ = tlist.token_idx_next(tidx) + pidx, prev_ = tlist.token_prev(tidx) + nidx, next_ = tlist.token_next(tidx) if valid_left(prev_) and valid_right(next_): if semicolon: # only overwrite if a semicolon present. - snidx, _ = tlist.token_idx_next_by(m=M_SEMICOLON, idx=nidx) + snidx, _ = tlist.token_next_by(m=M_SEMICOLON, idx=nidx) nidx = snidx or nidx # Luckily, this leaves the position of `token` intact. - tlist.group_tokens_between(cls, pidx, nidx, extend=True) + tlist.group_tokens(cls, pidx, nidx, extend=True) def _group_matching(tlist, cls): @@ -64,7 +64,7 @@ def _group_matching(tlist, cls): # this indicates invalid sql and unbalanced tokens. # instead of break, continue in case other "valid" groups exist continue - tlist.group_tokens_between(cls, open_token, token) + tlist.group_tokens(cls, open_token, token) def group_if(tlist): @@ -115,10 +115,10 @@ def group_case(tlist): def group_identifier(tlist): T_IDENT = (T.String.Symbol, T.Name) - tidx, token = tlist.token_idx_next_by(t=T_IDENT) + tidx, token = tlist.token_next_by(t=T_IDENT) while token: - tlist.group_tokens_between(sql.Identifier, tidx, tidx) - tidx, token = tlist.token_idx_next_by(t=T_IDENT, idx=tidx + 1) + tlist.group_tokens(sql.Identifier, tidx, tidx) + tidx, token = tlist.token_next_by(t=T_IDENT, idx=tidx + 1) def group_period(tlist): @@ -133,14 +133,14 @@ def group_period(tlist): def group_arrays(tlist): - tidx, token = tlist.token_idx_next_by(i=sql.SquareBrackets) + tidx, token = tlist.token_next_by(i=sql.SquareBrackets) while token: - pidx, prev = tlist.token_idx_prev(tidx) - if imt(prev, i=(sql.SquareBrackets, sql.Identifier, sql.Function), + 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_between(sql.Identifier, pidx, tidx, extend=True) + tlist.group_tokens(sql.Identifier, pidx, tidx, extend=True) tidx = pidx - tidx, token = tlist.token_idx_next_by(i=sql.SquareBrackets, idx=tidx + 1) + tidx, token = tlist.token_next_by(i=sql.SquareBrackets, idx=tidx + 1) @recurse(sql.Identifier) @@ -151,18 +151,18 @@ def group_operator(tlist): T_CYCLE = T_NUMERICAL + T_STRING + T_NAME func = lambda tk: imt(tk, i=I_CYCLE, t=T_CYCLE) - tidx, token = tlist.token_idx_next_by(t=(T.Operator, T.Wildcard)) + tidx, token = tlist.token_next_by(t=(T.Operator, T.Wildcard)) while token: - pidx, prev_ = tlist.token_idx_prev(tidx) - nidx, next_ = tlist.token_idx_next(tidx) + pidx, prev_ = tlist.token_prev(tidx) + nidx, next_ = tlist.token_next(tidx) if func(prev_) and func(next_): token.ttype = T.Operator - tlist.group_tokens_between(sql.Operation, pidx, nidx) + tlist.group_tokens(sql.Operation, pidx, nidx) tidx = pidx - tidx, token = tlist.token_idx_next_by(t=(T.Operator, T.Wildcard), - idx=tidx + 1) + tidx, token = tlist.token_next_by(t=(T.Operator, T.Wildcard), + idx=tidx + 1) @recurse(sql.IdentifierList) @@ -174,15 +174,15 @@ def group_identifier_list(tlist): func = lambda t: imt(t, i=I_IDENT_LIST, m=M_ROLE, t=T_IDENT_LIST) - tidx, token = tlist.token_idx_next_by(m=M_COMMA) + tidx, token = tlist.token_next_by(m=M_COMMA) while token: - pidx, prev_ = tlist.token_idx_prev(tidx) - nidx, next_ = tlist.token_idx_next(tidx) + pidx, prev_ = tlist.token_prev(tidx) + nidx, next_ = tlist.token_next(tidx) if func(prev_) and func(next_): - tlist.group_tokens_between(sql.IdentifierList, pidx, nidx, extend=True) + tlist.group_tokens(sql.IdentifierList, pidx, nidx, extend=True) tidx = pidx - tidx, token = tlist.token_idx_next_by(m=M_COMMA, idx=tidx + 1) + tidx, token = tlist.token_next_by(m=M_COMMA, idx=tidx + 1) def group_brackets(tlist): @@ -195,23 +195,23 @@ def group_parenthesis(tlist): @recurse(sql.Comment) def group_comments(tlist): - tidx, token = tlist.token_idx_next_by(t=T.Comment) + tidx, token = tlist.token_next_by(t=T.Comment) while token: end = tlist.token_not_matching( lambda tk: imt(tk, t=T.Comment) or tk.is_whitespace(), idx=tidx + 1) if end is not None: eidx = tlist.token_index(end) - eidx, end = tlist.token_idx_prev(eidx, skip_ws=False) - tlist.group_tokens_between(sql.Comment, tidx, eidx) + eidx, end = tlist.token_prev(eidx, skip_ws=False) + tlist.group_tokens(sql.Comment, tidx, eidx) - tidx, token = tlist.token_idx_next_by(t=T.Comment, idx=tidx + 1) + tidx, token = tlist.token_next_by(t=T.Comment, idx=tidx + 1) @recurse(sql.Where) def group_where(tlist): - tidx, token = tlist.token_idx_next_by(m=sql.Where.M_OPEN) + tidx, token = tlist.token_next_by(m=sql.Where.M_OPEN) while token: - eidx, end = tlist.token_idx_next_by(m=sql.Where.M_CLOSE, idx=tidx + 1) + eidx, end = tlist.token_next_by(m=sql.Where.M_CLOSE, idx=tidx + 1) if end is None: end = tlist._groupable_tokens[-1] @@ -219,8 +219,8 @@ def group_where(tlist): end = tlist.tokens[eidx - 1] # TODO: convert this to eidx instead of end token. # i think above values are len(tlist) and eidx-1 - tlist.group_tokens_between(sql.Where, tidx, end) - tidx, token = tlist.token_idx_next_by(m=sql.Where.M_OPEN, idx=tidx + 1) + tlist.group_tokens(sql.Where, tidx, end) + tidx, token = tlist.token_next_by(m=sql.Where.M_OPEN, idx=tidx + 1) @recurse() @@ -228,12 +228,12 @@ def group_aliased(tlist): I_ALIAS = (sql.Parenthesis, sql.Function, sql.Case, sql.Identifier, sql.Operation) - tidx, token = tlist.token_idx_next_by(i=I_ALIAS, t=T.Number) + tidx, token = tlist.token_next_by(i=I_ALIAS, t=T.Number) while token: - nidx, next_ = tlist.token_idx_next(tidx) + nidx, next_ = tlist.token_next(tidx) if imt(next_, i=sql.Identifier): - tlist.group_tokens_between(sql.Identifier, tidx, nidx, extend=True) - tidx, token = tlist.token_idx_next_by(i=I_ALIAS, t=T.Number, idx=tidx + 1) + tlist.group_tokens(sql.Identifier, tidx, nidx, extend=True) + tidx, token = tlist.token_next_by(i=I_ALIAS, t=T.Number, idx=tidx + 1) def group_typecasts(tlist): @@ -252,34 +252,34 @@ def group_functions(tlist): if has_create and has_table: return - tidx, token = tlist.token_idx_next_by(t=T.Name) + tidx, token = tlist.token_next_by(t=T.Name) while token: - nidx, next_ = tlist.token_idx_next(tidx) + nidx, next_ = tlist.token_next(tidx) if isinstance(next_, sql.Parenthesis): - tlist.group_tokens_between(sql.Function, tidx, nidx) - tidx, token = tlist.token_idx_next_by(t=T.Name, idx=tidx + 1) + tlist.group_tokens(sql.Function, tidx, nidx) + tidx, token = tlist.token_next_by(t=T.Name, idx=tidx + 1) def group_order(tlist): """Group together Identifier and Asc/Desc token""" - tidx, token = tlist.token_idx_next_by(t=T.Keyword.Order) + tidx, token = tlist.token_next_by(t=T.Keyword.Order) while token: - pidx, prev = tlist.token_idx_prev(tidx) - if imt(prev, i=sql.Identifier, t=T.Number): - tlist.group_tokens_between(sql.Identifier, pidx, tidx) + pidx, prev_ = tlist.token_prev(tidx) + if imt(prev_, i=sql.Identifier, t=T.Number): + tlist.group_tokens(sql.Identifier, pidx, tidx) tidx = pidx - tidx, token = tlist.token_idx_next_by(t=T.Keyword.Order, idx=tidx + 1) + tidx, token = tlist.token_next_by(t=T.Keyword.Order, idx=tidx + 1) @recurse() def align_comments(tlist): - tidx, token = tlist.token_idx_next_by(i=sql.Comment) + tidx, token = tlist.token_next_by(i=sql.Comment) while token: - pidx, prev = tlist.token_idx_prev(tidx) - if isinstance(prev, sql.TokenList): - tlist.group_tokens_between(sql.TokenList, pidx, tidx, extend=True) + pidx, prev_ = tlist.token_prev(tidx) + if isinstance(prev_, sql.TokenList): + tlist.group_tokens(sql.TokenList, pidx, tidx, extend=True) tidx = pidx - tidx, token = tlist.token_idx_next_by(i=sql.Comment, idx=tidx + 1) + tidx, token = tlist.token_next_by(i=sql.Comment, idx=tidx + 1) def group(stmt): diff --git a/sqlparse/filters/aligned_indent.py b/sqlparse/filters/aligned_indent.py index 719b450..ed5e15e 100644 --- a/sqlparse/filters/aligned_indent.py +++ b/sqlparse/filters/aligned_indent.py @@ -46,7 +46,7 @@ class AlignedIndentFilter(object): def _process_parenthesis(self, tlist): # if this isn't a subquery, don't re-indent - _, token = tlist.token_idx_next_by(m=(T.DML, 'SELECT')) + _, token = tlist.token_next_by(m=(T.DML, 'SELECT')) if token is not None: with indent(self): tlist.insert_after(tlist[0], self.nl('SELECT')) @@ -67,7 +67,7 @@ class AlignedIndentFilter(object): offset_ = len('case ') + len('when ') cases = tlist.get_cases(skip_ws=True) # align the end as well - _, end_token = tlist.token_idx_next_by(m=(T.Keyword, 'END')) + _, end_token = tlist.token_next_by(m=(T.Keyword, 'END')) cases.append((None, [end_token])) condition_width = [len(' '.join(map(text_type, cond))) if cond else 0 @@ -88,7 +88,7 @@ class AlignedIndentFilter(object): def _next_token(self, tlist, idx=0): split_words = T.Keyword, self.split_words, True - tidx, token = tlist.token_idx_next_by(m=split_words, idx=idx) + tidx, token = tlist.token_next_by(m=split_words, idx=idx) # treat "BETWEEN x and y" as a single statement if token and token.normalized == 'BETWEEN': tidx, token = self._next_token(tlist, tidx + 1) @@ -113,9 +113,9 @@ class AlignedIndentFilter(object): # process any sub-sub statements for sgroup in tlist.get_sublists(): idx = tlist.token_index(sgroup) - pidx, prev = tlist.token_idx_prev(idx) + 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, 'BY')) else 0 with offset(self, offset_): self._process(sgroup) diff --git a/sqlparse/filters/others.py b/sqlparse/filters/others.py index ecde2fe..a23a6c6 100644 --- a/sqlparse/filters/others.py +++ b/sqlparse/filters/others.py @@ -14,12 +14,12 @@ class StripCommentsFilter(object): def _process(tlist): def get_next_comment(): # TODO(andi) Comment types should be unified, see related issue38 - return tlist.token_idx_next_by(i=sql.Comment, t=T.Comment) + return tlist.token_next_by(i=sql.Comment, t=T.Comment) tidx, token = get_next_comment() while token: - pidx, prev_ = tlist.token_idx_prev(tidx, skip_ws=False) - nidx, next_ = tlist.token_idx_next(tidx, skip_ws=False) + pidx, prev_ = tlist.token_prev(tidx, skip_ws=False) + nidx, next_ = tlist.token_next(tidx, skip_ws=False) # Replace by whitespace if prev and next exist and if they're not # whitespaces. This doesn't apply if prev or next is a paranthesis. if (prev_ is None or next_ is None or @@ -87,19 +87,19 @@ class SpacesAroundOperatorsFilter(object): def _process(tlist): ttypes = (T.Operator, T.Comparison) - tidx, token = tlist.token_idx_next_by(t=ttypes) + tidx, token = tlist.token_next_by(t=ttypes) while token: - nidx, next_ = tlist.token_idx_next(tidx, skip_ws=False) + nidx, next_ = tlist.token_next(tidx, skip_ws=False) if next_ and next_.ttype != T.Whitespace: tlist.insert_after(tidx, sql.Token(T.Whitespace, ' ')) - pidx, prev_ = tlist.token_idx_prev(tidx, skip_ws=False) + pidx, prev_ = tlist.token_prev(tidx, skip_ws=False) if prev_ and prev_.ttype != T.Whitespace: tlist.insert_before(tidx, sql.Token(T.Whitespace, ' ')) tidx += 1 # has to shift since token inserted before it # assert tlist.token_index(token) == tidx - tidx, token = tlist.token_idx_next_by(t=ttypes, idx=tidx + 1) + tidx, token = tlist.token_next_by(t=ttypes, idx=tidx + 1) def process(self, stmt): [self.process(sgroup) for sgroup in stmt.get_sublists()] diff --git a/sqlparse/filters/reindent.py b/sqlparse/filters/reindent.py index d13fdf3..d23a8d5 100644 --- a/sqlparse/filters/reindent.py +++ b/sqlparse/filters/reindent.py @@ -49,7 +49,7 @@ class ReindentFilter(object): 'GROUP', 'ORDER', 'UNION', 'VALUES', 'SET', 'BETWEEN', 'EXCEPT', 'HAVING') m_split = T.Keyword, split_words, True - tidx, token = tlist.token_idx_next_by(m=m_split, idx=idx) + tidx, token = tlist.token_next_by(m=m_split, idx=idx) if token and token.normalized == 'BETWEEN': tidx, token = self._next_token(tlist, tidx + 1) @@ -63,10 +63,10 @@ class ReindentFilter(object): tidx, token = self._next_token(tlist) while token: tidx = tlist.token_index(token) - pidx, prev = tlist.token_idx_prev(tidx, skip_ws=False) - uprev = text_type(prev) + pidx, prev_ = tlist.token_prev(tidx, skip_ws=False) + uprev = text_type(prev_) - if prev and prev.is_whitespace(): + if prev_ and prev_.is_whitespace(): del tlist.tokens[pidx] tidx -= 1 @@ -77,17 +77,17 @@ class ReindentFilter(object): tidx, token = self._next_token(tlist, tidx + 1) def _split_statements(self, tlist): - tidx, token = tlist.token_idx_next_by(t=(T.Keyword.DDL, T.Keyword.DML)) + tidx, token = tlist.token_next_by(t=(T.Keyword.DDL, T.Keyword.DML)) while token: - pidx, prev = tlist.token_idx_prev(tidx, skip_ws=False) - if prev and prev.is_whitespace(): + pidx, prev_ = tlist.token_prev(tidx, skip_ws=False) + if prev_ and prev_.is_whitespace(): del tlist.tokens[pidx] tidx -= 1 # only break if it's not the first token - if prev: + if prev_: tlist.insert_before(tidx, self.nl()) tidx += 1 - tidx, token = tlist.token_idx_next_by( + tidx, token = tlist.token_next_by( t=(T.Keyword.DDL, T.Keyword.DML), idx=tidx + 1) def _process(self, tlist): @@ -96,7 +96,7 @@ class ReindentFilter(object): func(tlist) def _process_where(self, tlist): - tidx, token = tlist.token_idx_next_by(m=(T.Keyword, 'WHERE')) + tidx, token = tlist.token_next_by(m=(T.Keyword, 'WHERE')) # issue121, errors in statement fixed?? tlist.insert_before(tidx, self.nl()) @@ -105,8 +105,8 @@ class ReindentFilter(object): def _process_parenthesis(self, tlist): ttypes = T.Keyword.DML, T.Keyword.DDL - _, is_dml_dll = tlist.token_idx_next_by(t=ttypes) - fidx, first = tlist.token_idx_next_by(m=sql.Parenthesis.M_OPEN) + _, is_dml_dll = tlist.token_next_by(t=ttypes) + fidx, first = tlist.token_next_by(m=sql.Parenthesis.M_OPEN) with indent(self, 1 if is_dml_dll else 0): tlist.tokens.insert(0, self.nl()) if is_dml_dll else None @@ -143,7 +143,7 @@ class ReindentFilter(object): # len "when ", "then ", "else " with offset(self, len("WHEN ")): self._process_default(tlist) - end_idx, end = tlist.token_idx_next_by(m=sql.Case.M_CLOSE) + end_idx, end = tlist.token_next_by(m=sql.Case.M_CLOSE) tlist.insert_before(end_idx, self.nl()) def _process_default(self, tlist, stmts=True): diff --git a/sqlparse/sql.py b/sqlparse/sql.py index af282a3..ed56793 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -204,7 +204,7 @@ class TokenList(Token): def _groupable_tokens(self): return self.tokens - def _token_idx_matching(self, funcs, start=0, end=None, reverse=False): + def _token_matching(self, funcs, start=0, end=None, reverse=False): """next token that match functions""" if start is None: return None @@ -238,21 +238,21 @@ class TokenList(Token): # this on is inconsistent, using Comment instead of T.Comment... funcs = lambda tk: not ((skip_ws and tk.is_whitespace()) or (skip_cm and imt(tk, t=T.Comment, i=Comment))) - return self._token_idx_matching(funcs)[1] + return self._token_matching(funcs)[1] - def token_idx_next_by(self, i=None, m=None, t=None, idx=0, end=None): + def token_next_by(self, i=None, m=None, t=None, idx=0, end=None): funcs = lambda tk: imt(tk, i, m, t) - return self._token_idx_matching(funcs, idx, end) + return self._token_matching(funcs, idx, end) def token_not_matching(self, funcs, idx): funcs = (funcs,) if not isinstance(funcs, (list, tuple)) else funcs funcs = [lambda tk: not func(tk) for func in funcs] - return self._token_idx_matching(funcs, idx)[1] + return self._token_matching(funcs, idx)[1] def token_matching(self, funcs, idx): - return self._token_idx_matching(funcs, idx)[1] + return self._token_matching(funcs, idx)[1] - def token_idx_prev(self, idx, skip_ws=True, skip_cm=False): + def token_prev(self, idx, skip_ws=True, skip_cm=False): """Returns the previous token relative to *idx*. If *skip_ws* is ``True`` (the default) whitespace tokens are ignored. @@ -263,11 +263,11 @@ class TokenList(Token): idx += 1 # alot of code usage current pre-compensates for this funcs = lambda tk: not ((skip_ws and tk.is_whitespace()) or (skip_cm and imt(tk, t=T.Comment, i=Comment))) - return self._token_idx_matching(funcs, idx, reverse=True) + return self._token_matching(funcs, idx, reverse=True) # TODO: May need to implement skip_cm for upstream changes. # TODO: May need to re-add default value to idx - def token_idx_next(self, idx, skip_ws=True, skip_cm=False): + def token_next(self, idx, skip_ws=True, skip_cm=False): """Returns the next token relative to *idx*. If *skip_ws* is ``True`` (the default) whitespace tokens are ignored. @@ -293,8 +293,8 @@ class TokenList(Token): start = start if isinstance(start, int) else self.token_index(start) return start + self.tokens[start:].index(token) - def group_tokens_between(self, grp_cls, start, end, include_end=True, - extend=False): + def group_tokens(self, grp_cls, start, end, include_end=True, + extend=False): """Replace tokens by an instance of *grp_cls*.""" if isinstance(start, int): start_idx = start @@ -338,7 +338,7 @@ class TokenList(Token): """Inserts *token* after *where*.""" if not isinstance(where, int): where = self.token_index(where) - nidx, next_ = self.token_idx_next(where, skip_ws=skip_ws) + nidx, next_ = self.token_next(where, skip_ws=skip_ws) token.parent = self if next_ is None: self.tokens.append(token) @@ -353,12 +353,12 @@ class TokenList(Token): """Returns the alias for this identifier or ``None``.""" # "name AS alias" - kw_idx, kw = self.token_idx_next_by(m=(T.Keyword, 'AS')) + kw_idx, kw = self.token_next_by(m=(T.Keyword, 'AS')) if kw is not None: return self._get_first_name(kw_idx + 1, keywords=True) # "name alias" or "complicated column expression alias" - _, ws = self.token_idx_next_by(t=T.Whitespace) + _, ws = self.token_next_by(t=T.Whitespace) if len(self.tokens) > 2 and ws is not None: return self._get_first_name(reverse=True) @@ -374,7 +374,7 @@ class TokenList(Token): def get_real_name(self): """Returns the real name (object name) of this identifier.""" # a.b - dot_idx, _ = self.token_idx_next_by(m=(T.Punctuation, '.')) + dot_idx, _ = self.token_next_by(m=(T.Punctuation, '.')) return self._get_first_name(dot_idx) def get_parent_name(self): @@ -382,8 +382,8 @@ class TokenList(Token): A parent object is identified by the first occuring dot. """ - dot_idx, _ = self.token_idx_next_by(m=(T.Punctuation, '.')) - _, prev_ = self.token_idx_prev(dot_idx) + dot_idx, _ = self.token_next_by(m=(T.Punctuation, '.')) + _, prev_ = self.token_prev(dot_idx) return remove_quotes(prev_.value) if prev_ is not None else None def _get_first_name(self, idx=None, reverse=False, keywords=False): @@ -433,9 +433,9 @@ class Statement(TokenList): # an IdentifierList containing the CTE definitions; the actual # DML keyword (e.g. SELECT, INSERT) will follow next. fidx = self.token_index(first_token) - tidx, token = self.token_idx_next(fidx, skip_ws=True) + tidx, token = self.token_next(fidx, skip_ws=True) if isinstance(token, (Identifier, IdentifierList)): - _, dml_keyword = self.token_idx_next(tidx, skip_ws=True) + _, dml_keyword = self.token_next(tidx, skip_ws=True) if dml_keyword.ttype == T.Keyword.DML: return dml_keyword.normalized @@ -452,18 +452,18 @@ class Identifier(TokenList): def is_wildcard(self): """Return ``True`` if this identifier contains a wildcard.""" - _, token = self.token_idx_next_by(t=T.Wildcard) + _, token = self.token_next_by(t=T.Wildcard) return token is not None def get_typecast(self): """Returns the typecast or ``None`` of this object as a string.""" - midx, marker = self.token_idx_next_by(m=(T.Punctuation, '::')) - nidx, next_ = self.token_idx_next(midx, skip_ws=False) + midx, marker = self.token_next_by(m=(T.Punctuation, '::')) + nidx, next_ = self.token_next(midx, skip_ws=False) return next_.value if next_ else None def get_ordering(self): """Returns the ordering or ``None`` as uppercase string.""" - _, ordering = self.token_idx_next_by(t=T.Keyword.Order) + _, ordering = self.token_next_by(t=T.Keyword.Order) return ordering.normalized if ordering else None def get_array_indices(self): |
