diff options
author | Andi Albrecht <albrecht.andi@gmail.com> | 2011-09-29 11:19:26 +0200 |
---|---|---|
committer | Andi Albrecht <albrecht.andi@gmail.com> | 2011-09-29 11:19:26 +0200 |
commit | 13c1d716a7798ae1a79c524dc967e9a33c534ff4 (patch) | |
tree | 5486734ad09578e3eb538a3faf545ae0681d0190 /sqlparse/sql.py | |
parent | ff50b33074f5c276b0cff8094e85582dcd467095 (diff) | |
download | sqlparse-13c1d716a7798ae1a79c524dc967e9a33c534ff4.tar.gz |
Detect alias for CASE statements (targets issue46).
Diffstat (limited to 'sqlparse/sql.py')
-rw-r--r-- | sqlparse/sql.py | 69 |
1 files changed, 35 insertions, 34 deletions
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. |