diff options
| author | Andi Albrecht <albrecht.andi@gmail.com> | 2009-07-12 12:33:56 +0200 |
|---|---|---|
| committer | Andi Albrecht <albrecht.andi@gmail.com> | 2009-07-12 12:33:56 +0200 |
| commit | bbca0a8e7525d871eb04d452dc064af833b4314f (patch) | |
| tree | 609aad21448b3e130e821aa44bf9f4e432cd0155 /sqlparse/sql.py | |
| parent | d8a0300bab3a77fba7fa6014ab7fa7c5ae388aaa (diff) | |
| download | sqlparse-bbca0a8e7525d871eb04d452dc064af833b4314f.tar.gz | |
Query token ancestry.
Diffstat (limited to 'sqlparse/sql.py')
| -rw-r--r-- | sqlparse/sql.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/sqlparse/sql.py b/sqlparse/sql.py index b5ba902..484b0f0 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -93,6 +93,32 @@ class Token(object): """Return ``True`` if this token is a whitespace token.""" return self.ttype and self.ttype in T.Whitespace + def within(self, group_cls): + """Returns ``True`` if this token is within *group_cls*. + + Use this method for example to check if an identifier is within + a function: ``t.within(sql.Function)``. + """ + parent = self.parent + while parent: + if isinstance(parent, group_cls): + return True + parent = parent.parent + return False + + def is_child_of(self, other): + """Returns ``True`` if this token is a direct child of *other*.""" + return self.parent == other + + def has_ancestor(self, other): + """Returns ``True`` if *other* is in this tokens ancestry.""" + parent = self.parent + while parent: + if parent == other: + return True + parent = parent.parent + return False + class TokenList(Token): """A group of tokens. |
