diff options
author | Andi Albrecht <albrecht.andi@gmail.com> | 2015-02-21 08:15:09 +0100 |
---|---|---|
committer | Andi Albrecht <albrecht.andi@gmail.com> | 2015-02-21 08:15:09 +0100 |
commit | 9cec0cde3818005d70b0473f3c99241f5df68394 (patch) | |
tree | 195c683a37221e307c600980c00dcddc9a2b76da /sqlparse/sql.py | |
parent | 71af186659923dfe8721c551d5dbf4db7c4854d9 (diff) | |
parent | 77e0789aea8918a2fbbc6f20196cd0bcdecccf52 (diff) | |
download | sqlparse-9cec0cde3818005d70b0473f3c99241f5df68394.tar.gz |
Merge master into v0.2.0 branch.
Diffstat (limited to 'sqlparse/sql.py')
-rw-r--r-- | sqlparse/sql.py | 90 |
1 files changed, 53 insertions, 37 deletions
diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 17509eb..367204d 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -383,21 +383,17 @@ class TokenList(Token): def get_alias(self): """Returns the alias for this identifier or ``None``.""" + + # "name AS alias" kw = self.token_next_match(0, T.Keyword, 'AS') if kw is not None: - alias = self.token_next(self.token_index(kw)) - if alias is None: - return None - else: - next_ = self.token_next_by_instance(0, Identifier) - if next_ is None: - next_ = self.token_next_by_type(0, T.String.Symbol) - if next_ is None: - return None - alias = next_ - if isinstance(alias, Identifier): - return alias.get_name() - return self._remove_quotes(compat.text_type(alias)) + return self._get_first_name(kw, keywords=True) + + # "name alias" or "complicated column expression alias" + if len(self.tokens) > 2: + return self._get_first_name(reverse=True) + + return None def get_name(self): """Returns the name of this identifier. @@ -415,18 +411,43 @@ class TokenList(Token): """Returns the real name (object name) of this identifier.""" # a.b dot = self.token_next_match(0, T.Punctuation, '.') + if dot is not None: + return self._get_first_name(self.token_index(dot)) + + return self._get_first_name() + + def get_parent_name(self): + """Return name of the parent object if any. + + A parent object is identified by the first occuring dot. + """ + dot = self.token_next_match(0, T.Punctuation, '.') if dot is None: - next_ = self.token_next_by_type(0, T.Name) - if next_ is not None: - return self._remove_quotes(next_.value) return None - - next_ = self.token_next_by_type(self.token_index(dot), - (T.Name, T.Wildcard, T.String.Symbol)) - if next_ is None: # invalid identifier, e.g. "a." + prev_ = self.token_prev(self.token_index(dot)) + if prev_ is None: # something must be verry wrong here.. return None - return self._remove_quotes(next_.value) + return self._remove_quotes(prev_.value) + + def _get_first_name(self, idx=None, reverse=False, keywords=False): + """Returns the name of the first token with a name""" + + if idx and not isinstance(idx, int): + idx = self.token_index(idx) + 1 + tokens = self.tokens[idx:] if idx else self.tokens + tokens = reversed(tokens) if reverse else tokens + types = [T.Name, T.Wildcard, T.String.Symbol] + + if keywords: + types.append(T.Keyword) + + for tok in tokens: + if tok.ttype in types: + return self._remove_quotes(tok.value) + elif isinstance(tok, Identifier) or isinstance(tok, Function): + return tok.get_name() + return None class Statement(TokenList): """Represents a SQL statement.""" @@ -460,19 +481,6 @@ class Identifier(TokenList): __slots__ = ('value', 'ttype', 'tokens') - def get_parent_name(self): - """Return name of the parent object if any. - - A parent object is identified by the first occuring dot. - """ - dot = self.token_next_match(0, T.Punctuation, '.') - if dot is None: - return None - prev_ = self.token_prev(self.token_index(dot)) - if prev_ is None: # something must be verry wrong here.. - return None - return self._remove_quotes(prev_.value) - def is_wildcard(self): """Return ``True`` if this identifier contains a wildcard.""" token = self.token_next_by_type(0, T.Wildcard) @@ -495,6 +503,13 @@ class Identifier(TokenList): return None return ordering.value.upper() + def get_array_indices(self): + """Returns an iterator of index expressions as strings""" + + # Use [1:-1] index to discard the square brackets + return (tok.value[1:-1] for tok in self.tokens + if tok.ttype in T.ArrayIndex) + class IdentifierList(TokenList): """A list of :class:`~sqlparse.sql.Identifier`\'s.""" @@ -622,9 +637,10 @@ class Function(TokenList): for t in parenthesis.tokens: if isinstance(t, IdentifierList): return t.get_identifiers() - elif (isinstance(t, (Identifier, Function)) - or t.ttype in T.Literal): - return [t] + elif isinstance(t, Identifier) or \ + isinstance(t, Function) or \ + t.ttype in T.Literal: + return [t,] return [] |