summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Uriarte <victor.m.uriarte@intel.com>2016-06-07 20:52:38 -0700
committerVictor Uriarte <victor.m.uriarte@intel.com>2016-06-12 12:32:39 -0700
commit60f621abdf04b9388648a2fec87440f88c1d8ce1 (patch)
tree54466927bff2828a7b604569c3e6b6b6cb9c0851
parent82b3b6bb1937c1a6535ccdde332128abc55f1f4e (diff)
downloadsqlparse-60f621abdf04b9388648a2fec87440f88c1d8ce1.tar.gz
Restyle pprint_tree and align upto idx=99
-rw-r--r--sqlparse/sql.py14
-rw-r--r--tests/test_parse.py71
2 files changed, 69 insertions, 16 deletions
diff --git a/sqlparse/sql.py b/sqlparse/sql.py
index e24c10f..e6fbab2 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)
diff --git a/tests/test_parse.py b/tests/test_parse.py
index 8654ec4..0f5af04 100644
--- a/tests/test_parse.py
+++ b/tests/test_parse.py
@@ -83,6 +83,7 @@ class SQLParseTest(TestCaseBase):
def test_placeholder(self):
def _get_tokens(sql):
return sqlparse.parse(sql)[0].tokens[-1].tokens
+
t = _get_tokens('select * from foo where user = ?')
self.assert_(t[-1].ttype is sqlparse.tokens.Name.Placeholder)
self.assertEqual(t[-1].value, '?')
@@ -218,7 +219,7 @@ def test_single_quotes_with_linebreaks(): # issue118
def test_sqlite_identifiers():
# Make sure we still parse sqlite style escapes
p = sqlparse.parse('[col1],[col2]')[0].tokens
- id_names = [id.get_name() for id in p[0].get_identifiers()]
+ id_names = [id_.get_name() for id_ in p[0].get_identifiers()]
assert len(p) == 1
assert isinstance(p[0], sqlparse.sql.IdentifierList)
assert id_names == ['[col1]', '[col2]']
@@ -318,21 +319,65 @@ def test_get_token_at_offset():
def test_pprint():
- p = sqlparse.parse('select * from dual')[0]
+ p = sqlparse.parse('select a0, b0, c0, d0, e0 from '
+ '(select * from dual) q0 where 1=1 and 2=2')[0]
output = StringIO()
p._pprint_tree(f=output)
- pprint = u'\n'.join([
- " | 0 DML 'select'",
- " | 1 Whitespace ' '",
- " | 2 Wildcard '*'",
- " | 3 Whitespace ' '",
- " | 4 Keyword 'from'",
- " | 5 Whitespace ' '",
- " +-6 Identifier 'dual'",
- " | 0 Name 'dual'",
- "",
- ])
+ pprint = u'\n'.join([" 0 DML 'select'",
+ " 1 Whitespace ' '",
+ " 2 IdentifierList 'a0, b0...'",
+ " | 0 Identifier 'a0'",
+ " | | 0 Name 'a0'",
+ " | 1 Punctuation ','",
+ " | 2 Whitespace ' '",
+ " | 3 Identifier 'b0'",
+ " | | 0 Name 'b0'",
+ " | 4 Punctuation ','",
+ " | 5 Whitespace ' '",
+ " | 6 Identifier 'c0'",
+ " | | 0 Name 'c0'",
+ " | 7 Punctuation ','",
+ " | 8 Whitespace ' '",
+ " | 9 Identifier 'd0'",
+ " | | 0 Name 'd0'",
+ " | 10 Punctuation ','",
+ " | 11 Whitespace ' '",
+ " | 12 Float 'e0'",
+ " 3 Whitespace ' '",
+ " 4 Keyword 'from'",
+ " 5 Whitespace ' '",
+ " 6 Identifier '(selec...'",
+ " | 0 Parenthesis '(selec...'",
+ " | | 0 Punctuation '('",
+ " | | 1 DML 'select'",
+ " | | 2 Whitespace ' '",
+ " | | 3 Wildcard '*'",
+ " | | 4 Whitespace ' '",
+ " | | 5 Keyword 'from'",
+ " | | 6 Whitespace ' '",
+ " | | 7 Identifier 'dual'",
+ " | | | 0 Name 'dual'",
+ " | | 8 Punctuation ')'",
+ " | 1 Whitespace ' '",
+ " | 2 Identifier 'q0'",
+ " | | 0 Name 'q0'",
+ " 7 Whitespace ' '",
+ " 8 Where 'where ...'",
+ " | 0 Keyword 'where'",
+ " | 1 Whitespace ' '",
+ " | 2 Comparison '1=1'",
+ " | | 0 Integer '1'",
+ " | | 1 Comparison '='",
+ " | | 2 Integer '1'",
+ " | 3 Whitespace ' '",
+ " | 4 Keyword 'and'",
+ " | 5 Whitespace ' '",
+ " | 6 Comparison '2=2'",
+ " | | 0 Integer '2'",
+ " | | 1 Comparison '='",
+ " | | 2 Integer '2'",
+ "", ])
assert output.getvalue() == pprint