summaryrefslogtreecommitdiff
path: root/sqlparse/sql.py
diff options
context:
space:
mode:
authorVik <vmuriart@gmail.com>2016-06-12 13:09:56 -0700
committerGitHub <noreply@github.com>2016-06-12 13:09:56 -0700
commit8ad44059d4d9ab5a8a7489a963dcb8de45ca3a0a (patch)
tree3fac58eff5e7f0150874e1205dfcc4dfe8a28455 /sqlparse/sql.py
parent50de51a5d6abb2a2f8649091912090983dab843d (diff)
parent42fb1d05b601444599f10d10c5d2dd0b431ccc15 (diff)
downloadsqlparse-8ad44059d4d9ab5a8a7489a963dcb8de45ca3a0a.tar.gz
Merge pull request #255 from vmuriart/console-script-examples-sqlOperation
Add Console-script, sql.Operation, fix examples
Diffstat (limited to 'sqlparse/sql.py')
-rw-r--r--sqlparse/sql.py46
1 files changed, 23 insertions, 23 deletions
diff --git a/sqlparse/sql.py b/sqlparse/sql.py
index 43a89e7..cee6af5 100644
--- a/sqlparse/sql.py
+++ b/sqlparse/sql.py
@@ -37,6 +37,10 @@ class Token(object):
def __str__(self):
return self.value
+ # Pending tokenlist __len__ bug fix
+ # def __len__(self):
+ # return len(self.value)
+
def __repr__(self):
cls = self._get_repr_name()
value = self._get_repr_value()
@@ -141,6 +145,10 @@ class TokenList(Token):
def __str__(self):
return ''.join(token.value for token in self.flatten())
+ # weird bug
+ # def __len__(self):
+ # return len(self.tokens)
+
def __iter__(self):
return iter(self.tokens)
@@ -152,12 +160,12 @@ class TokenList(Token):
def _pprint_tree(self, max_depth=None, depth=0, f=None):
"""Pretty-print the object tree."""
- ind = ' ' * (depth * 2)
+ indent = ' | ' * depth
for idx, token in enumerate(self.tokens):
- pre = ' +-' if token.is_group() else ' | '
cls = token._get_repr_name()
value = token._get_repr_value()
- print("{ind}{pre}{idx} {cls} '{value}'".format(**locals()), file=f)
+ print("{indent}{idx:2d} {cls} '{value}'"
+ .format(**locals()), file=f)
if token.is_group() and (max_depth is None or depth < max_depth):
token._pprint_tree(max_depth, depth + 1, f)
@@ -216,20 +224,6 @@ class TokenList(Token):
if func(token):
return token
- def token_first(self, skip_ws=True, skip_cm=False):
- """Returns the first child token.
-
- If *ignore_whitespace* is ``True`` (the default), whitespace
- tokens are ignored.
-
- if *ignore_comments* is ``True`` (default: ``False``), comments are
- ignored too.
- """
- # 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, i=Comment)))
- return self._token_matching(funcs)
-
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_matching(funcs, idx, end)
@@ -242,24 +236,26 @@ class TokenList(Token):
def token_matching(self, idx, funcs):
return self._token_matching(funcs, idx)
- def token_prev(self, idx, skip_ws=True, skip_cm=False):
+ def token_prev(self, idx=0, skip_ws=True, skip_cm=False):
"""Returns the previous token relative to *idx*.
If *skip_ws* is ``True`` (the default) whitespace tokens are ignored.
``None`` is returned if there's no previous token.
"""
funcs = lambda tk: not ((skip_ws and tk.is_whitespace()) or
- (skip_cm and imt(tk, t=T.Comment)))
+ (skip_cm and imt(tk, t=T.Comment, i=Comment)))
return self._token_matching(funcs, idx, reverse=True)
- def token_next(self, idx, skip_ws=True, skip_cm=False):
+ def token_next(self, idx=0, skip_ws=True, skip_cm=False):
"""Returns the next token relative to *idx*.
+ If called with idx = 0. Returns the first child token.
If *skip_ws* is ``True`` (the default) whitespace tokens are ignored.
+ If *skip_cm* is ``True`` (default: ``False``), comments are ignored.
``None`` is returned if there's no next token.
"""
funcs = lambda tk: not ((skip_ws and tk.is_whitespace()) or
- (skip_cm and imt(tk, t=T.Comment)))
+ (skip_cm and imt(tk, t=T.Comment, i=Comment)))
return self._token_matching(funcs, idx)
def token_index(self, token, start=0):
@@ -387,7 +383,7 @@ class Statement(TokenList):
Whitespaces and comments at the beginning of the statement
are ignored.
"""
- first_token = self.token_first(skip_cm=True)
+ first_token = self.token_next(skip_cm=True)
if first_token is None:
# An "empty" statement that either has not tokens at all
# or only whitespace tokens.
@@ -425,7 +421,7 @@ class Identifier(TokenList):
def get_typecast(self):
"""Returns the typecast or ``None`` of this object as a string."""
marker = self.token_next_by(m=(T.Punctuation, '::'))
- next_ = self.token_next(marker, False)
+ next_ = self.token_next(marker, skip_ws=False)
return next_.value if next_ else None
def get_ordering(self):
@@ -588,3 +584,7 @@ class Begin(TokenList):
"""A BEGIN/END block."""
M_OPEN = T.Keyword, 'BEGIN'
M_CLOSE = T.Keyword, 'END'
+
+
+class Operation(TokenList):
+ """Grouping of operations"""