From 13c1d716a7798ae1a79c524dc967e9a33c534ff4 Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Thu, 29 Sep 2011 11:19:26 +0200 Subject: Detect alias for CASE statements (targets issue46). --- sqlparse/sql.py | 69 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 34 deletions(-) (limited to 'sqlparse/sql.py') diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 4d56bf3..244733b 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -315,38 +315,6 @@ class TokenList(Token): """Inserts *token* before *where*.""" self.tokens.insert(self.token_index(where), token) - -class Statement(TokenList): - """Represents a SQL statement.""" - - __slots__ = ('value', 'ttype', 'tokens') - - def get_type(self): - """Returns the type of a statement. - - The returned value is a string holding an upper-cased reprint of - the first DML or DDL keyword. If the first token in this group - isn't a DML or DDL keyword "UNKNOWN" is returned. - """ - first_token = self.token_first() - if first_token is None: - # An "empty" statement that either has not tokens at all - # or only whitespace tokens. - return 'UNKNOWN' - elif first_token.ttype in (T.Keyword.DML, T.Keyword.DDL): - return first_token.value.upper() - else: - return 'UNKNOWN' - - -class Identifier(TokenList): - """Represents an identifier. - - Identifiers may have aliases or typecasts. - """ - - __slots__ = ('value', 'ttype', 'tokens') - def has_alias(self): """Returns ``True`` if an alias is present.""" return self.get_alias() is not None @@ -359,8 +327,8 @@ class Identifier(TokenList): if alias is None: return None else: - next_ = self.token_next(0) - if next_ is None or not isinstance(next_, Identifier): + next_ = self.token_next_by_instance(0, Identifier) + if next_ is None: return None alias = next_ if isinstance(alias, Identifier): @@ -393,6 +361,39 @@ class Identifier(TokenList): return None return next_.value + + +class Statement(TokenList): + """Represents a SQL statement.""" + + __slots__ = ('value', 'ttype', 'tokens') + + def get_type(self): + """Returns the type of a statement. + + The returned value is a string holding an upper-cased reprint of + the first DML or DDL keyword. If the first token in this group + isn't a DML or DDL keyword "UNKNOWN" is returned. + """ + first_token = self.token_first() + if first_token is None: + # An "empty" statement that either has not tokens at all + # or only whitespace tokens. + return 'UNKNOWN' + elif first_token.ttype in (T.Keyword.DML, T.Keyword.DDL): + return first_token.value.upper() + else: + return 'UNKNOWN' + + +class Identifier(TokenList): + """Represents an identifier. + + Identifiers may have aliases or typecasts. + """ + + __slots__ = ('value', 'ttype', 'tokens') + def get_parent_name(self): """Return name of the parent object if any. -- cgit v1.2.1