diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-11-25 12:20:13 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-11-25 12:20:13 -0500 |
commit | 77f641429f019d06cc467ec4e57ae94f808d70bd (patch) | |
tree | 94ddacadb8915d19d3ee3cdf7a3771fa63ba0184 /lib/sqlalchemy/sql/expression.py | |
parent | 5247c244ca6595f2685f1c0c271618eda7cb5e62 (diff) | |
download | sqlalchemy-77f641429f019d06cc467ec4e57ae94f808d70bd.tar.gz |
- Fixed operator precedence rules for multiple
chains of a single non-associative operator.
I.e. "x - (y - z)" will compile as "x - (y - z)"
and not "x - y - z". Also works with labels,
i.e. "x - (y - z).label('foo')"
[ticket:1984]
- Single element tuple expressions inside an IN clause
parenthesize correctly, also from [ticket:1984],
added tests for PG
- re-fix again importlater, [ticket:1983]
Diffstat (limited to 'lib/sqlalchemy/sql/expression.py')
-rw-r--r-- | lib/sqlalchemy/sql/expression.py | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index c3dc339a5..6e22e7ca5 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -1289,6 +1289,29 @@ class ClauseElement(Visitable): return [] def self_group(self, against=None): + """Apply a 'grouping' to this :class:`.ClauseElement`. + + This method is overridden by subclasses to return a + "grouping" construct, i.e. parenthesis. In particular + it's used by "binary" expressions to provide a grouping + around themselves when placed into a larger expression, + as well as by :func:`.select` constructs when placed into + the FROM clause of another :func:`.select`. (Note that + subqueries should be normally created using the + :func:`.Select.alias` method, as many platforms require + nested SELECT statements to be named). + + As expressions are composed together, the application of + :meth:`self_group` is automatic - end-user code should never + need to use this method directly. Note that SQLAlchemy's + clause constructs take operator precedence into account - + so parenthesis might not be needed, for example, in + an expression like ``x OR (y AND z)`` - AND takes precedence + over OR. + + The base :meth:`self_group` method of :class:`.ClauseElement` + just returns self. + """ return self # TODO: remove .bind as a method from the root ClauseElement. @@ -2657,8 +2680,7 @@ class ClauseList(ClauseElement): return list(itertools.chain(*[c._from_objects for c in self.clauses])) def self_group(self, against=None): - if self.group and self.operator is not against and \ - operators.is_precedent(self.operator, against): + if self.group and operators.is_precedent(self.operator, against): return _Grouping(self) else: return self @@ -2984,10 +3006,7 @@ class _BinaryExpression(ColumnElement): ) def self_group(self, against=None): - # use small/large defaults for comparison so that unknown - # operators are always parenthesized - if self.operator is not against and \ - operators.is_precedent(self.operator, against): + if operators.is_precedent(self.operator, against): return _Grouping(self) else: return self @@ -3343,7 +3362,16 @@ class _Label(ColumnElement): @util.memoized_property def element(self): return self._element.self_group(against=operators.as_) - + + def self_group(self, against=None): + sub_element = self._element.self_group(against=against) + if sub_element is not self._element: + return _Label(self.name, + sub_element, + type_=self._type) + else: + return self._element + @property def primary_key(self): return self.element.primary_key |