summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2009-04-24 15:05:23 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2009-04-24 15:05:23 +0200
commitbe3b2d73ef118fa5bbf1549e2f9a71baae709cea (patch)
tree1460b34e1f0a29dd5c17b3fb3a226091f70b79b6
parent3bbc94129d3a9597f8218ba19c097620cf352137 (diff)
downloadsqlparse-be3b2d73ef118fa5bbf1549e2f9a71baae709cea.tar.gz
Handle wildcards in identifiers.
-rw-r--r--sqlparse/engine/grouping.py4
-rw-r--r--sqlparse/sql.py13
-rw-r--r--tests/test_format.py5
-rw-r--r--tests/test_grouping.py12
4 files changed, 32 insertions, 2 deletions
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py
index 3a57496..41e123e 100644
--- a/sqlparse/engine/grouping.py
+++ b/sqlparse/engine/grouping.py
@@ -102,7 +102,9 @@ def group_case(tlist):
def group_identifier(tlist):
def _consume_cycle(tl, i):
x = itertools.cycle((lambda y: y.match(T.Punctuation, '.'),
- lambda y: y.ttype in (T.String.Symbol, T.Name)))
+ lambda y: y.ttype in (T.String.Symbol,
+ T.Name,
+ T.Wildcard)))
for t in tl.tokens[i:]:
if x.next()(t):
yield t
diff --git a/sqlparse/sql.py b/sqlparse/sql.py
index db9f1c1..3ab93ba 100644
--- a/sqlparse/sql.py
+++ b/sqlparse/sql.py
@@ -320,7 +320,18 @@ class Identifier(TokenList):
def get_real_name(self):
"""Returns the real name (object name) of this identifier."""
- return self.token_next_by_type(0, T.Name).value
+ # a.b
+ dot = self.token_next_match(0, T.Punctuation, '.')
+ if dot is None:
+ return self.token_next_by_type(0, T.Name).value
+ else:
+ return self.token_next_by_type(self.token_index(dot),
+ (T.Name, T.Wildcard)).value
+
+ def is_wildcard(self):
+ """Return ``True`` if this identifier contains a wildcard."""
+ token = self.token_next_by_type(0, T.Wildcard)
+ return token is not None
def get_typecast(self):
"""Returns the typecast or ``None`` of this object as a string."""
diff --git a/tests/test_format.py b/tests/test_format.py
index 3180d10..4c2742d 100644
--- a/tests/test_format.py
+++ b/tests/test_format.py
@@ -134,6 +134,11 @@ class TestFormatReindent(TestCaseBase):
'from table1,',
' table2',
'where 1 = 2']))
+ s = 'select a.*, b.id from a, b'
+ self.ndiffAssertEqual(f(s), '\n'.join(['select a.*,',
+ ' b.id',
+ 'from a,',
+ ' b']))
def test_case(self):
f = lambda sql: sqlparse.format(sql, reindent=True)
diff --git a/tests/test_grouping.py b/tests/test_grouping.py
index fa72275..26e5d10 100644
--- a/tests/test_grouping.py
+++ b/tests/test_grouping.py
@@ -43,6 +43,18 @@ class TestGrouping(TestCaseBase):
self.ndiffAssertEqual(s, parsed.to_unicode())
self.assert_(isinstance(parsed.tokens[-1].tokens[3], Identifier))
+ def test_identifier_wildcard(self):
+ p = sqlparse.parse('a.*, b.id')[0]
+ self.assert_(isinstance(p.tokens[0], IdentifierList))
+ self.assert_(isinstance(p.tokens[0].tokens[0], Identifier))
+ self.assert_(isinstance(p.tokens[0].tokens[-1], Identifier))
+
+ def test_identifier_name_wildcard(self):
+ p = sqlparse.parse('a.*')[0]
+ t = p.tokens[0]
+ self.assertEqual(t.get_name(), '*')
+ self.assertEqual(t.is_wildcard(), True)
+
def test_where(self):
s = 'select * from foo where bar = 1 order by id desc'
p = sqlparse.parse(s)[0]