diff options
author | Andi Albrecht <albrecht.andi@gmail.com> | 2013-10-24 08:38:21 +0200 |
---|---|---|
committer | Andi Albrecht <albrecht.andi@gmail.com> | 2013-10-24 08:38:21 +0200 |
commit | c08c750e799af5c17ccbebf9b34b95a38f8cfb55 (patch) | |
tree | 96597aa0afe0b0714b6dc9411613e8fa2de8bda0 | |
parent | e3d32fadd637b53863d8c43593c39707a777c897 (diff) | |
download | sqlparse-c08c750e799af5c17ccbebf9b34b95a38f8cfb55.tar.gz |
Improve grouping of expressions (targets #23).
-rw-r--r-- | sqlparse/engine/grouping.py | 2 | ||||
-rw-r--r-- | sqlparse/sql.py | 8 | ||||
-rw-r--r-- | tests/test_grouping.py | 13 |
3 files changed, 20 insertions, 3 deletions
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 3536a8b..47e77ac 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -125,7 +125,7 @@ def group_comparison(tlist): return (token.ttype in (T.String.Symbol, T.Name, T.Number, T.Number.Integer, T.Literal, T.Literal.Number.Integer) - or isinstance(token, (sql.Identifier,)) + or isinstance(token, (sql.Identifier, sql.Parenthesis)) or (token.ttype is T.Keyword and token.value.upper() in ['NULL', ])) _group_left_right(tlist, T.Operator.Comparison, None, sql.Comparison, diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 3f1e98b..9567aa5 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -546,6 +546,14 @@ class Comparison(TokenList): """A comparison used for example in WHERE clauses.""" __slots__ = ('value', 'ttype', 'tokens') + @property + def left(self): + return self.tokens[0] + + @property + def right(self): + return self.tokens[-1] + class Comment(TokenList): """A comment.""" diff --git a/tests/test_grouping.py b/tests/test_grouping.py index fe6aa7d..f4bfb1a 100644 --- a/tests/test_grouping.py +++ b/tests/test_grouping.py @@ -262,9 +262,18 @@ def test_comparison_with_keywords(): # issue90 assert len(p.tokens) == 1 assert isinstance(p.tokens[0], sql.Comparison) assert len(p.tokens[0].tokens) == 5 - assert p.tokens[0].tokens[0].value == 'foo' - assert p.tokens[0].tokens[-1].value == 'NULL' + assert p.tokens[0].left.value == 'foo' + assert p.tokens[0].right.value == 'NULL' # make sure it's case-insensitive p = sqlparse.parse('foo = null')[0] assert len(p.tokens) == 1 assert isinstance(p.tokens[0], sql.Comparison) + + +def test_comparison_with_parenthesis(): # issue23 + p = sqlparse.parse('(3 + 4) = 7')[0] + assert len(p.tokens) == 1 + assert isinstance(p.tokens[0], sql.Comparison) + comp = p.tokens[0] + assert isinstance(comp.left, sql.Parenthesis) + assert comp.right.ttype is T.Number.Integer |