]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Added math negation operator support, -x.
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 5 Feb 2010 15:49:02 +0000 (15:49 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 5 Feb 2010 15:49:02 +0000 (15:49 +0000)
CHANGES
lib/sqlalchemy/sql/compiler.py
lib/sqlalchemy/sql/expression.py
lib/sqlalchemy/sql/operators.py
test/orm/test_query.py
test/sql/test_select.py

diff --git a/CHANGES b/CHANGES
index dc69c7dac8361df97f2a40f4f8584781ccb1d211..fe0be1d4b987fee8b95d9d8e7ea476302972a33b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -3,6 +3,10 @@
 =======
 CHANGES
 =======
+0.6beta2
+- sql
+  - Added math negation operator support, -x.
+
 0.6beta1
 ========
 - Major Release
index e635e20e18afd5db80583834bb792129a948ed04..4486c24db4fce9ac978a4c3411eaf6261de0d68b 100644 (file)
@@ -73,6 +73,7 @@ OPERATORS =  {
     # end Py2K
     operators.mod : ' % ',
     operators.truediv : ' / ',
+    operators.neg : '-',
     operators.lt : ' < ',
     operators.le : ' <= ',
     operators.ne : ' != ',
index 5c61777fe1085512cccc7046db288a4916e07593..89137d2ce0f82de9d928f803a810ebd057ea7130 100644 (file)
@@ -1363,6 +1363,9 @@ class ColumnOperators(Operators):
     def __ge__(self, other):
         return self.operate(operators.ge, other)
 
+    def __neg__(self):
+        return self.operate(operators.neg)
+
     def concat(self, other):
         return self.operate(operators.concat_op, other)
 
@@ -1537,6 +1540,9 @@ class _CompareMixin(ColumnOperators):
 
         return self.__compare(op, ClauseList(*args).self_group(against=op), negate=negate_op)
 
+    def __neg__(self):
+        return _UnaryExpression(self, operator=operators.neg)
+        
     def startswith(self, other, escape=None):
         """Produce the clause ``LIKE '<other>%'``"""
 
index 879f0f3e517c0ecb2970e4684fef8d56a5df9979..6f70b1778d2cfeb7dfbdee363ac346a759c94f01 100644 (file)
@@ -4,7 +4,7 @@
 """Defines operators used in SQL expressions."""
 
 from operator import (
-    and_, or_, inv, add, mul, sub, mod, truediv, lt, le, ne, gt, ge, eq
+    and_, or_, inv, add, mul, sub, mod, truediv, lt, le, ne, gt, ge, eq, neg
     )
     
 # Py2K
@@ -98,6 +98,7 @@ _PRECEDENCE = {
     div: 7,
     # end Py2K
     mod: 7,
+    neg: 7,
     add: 6,
     sub: 6,
     concat_op: 6,
index c0835e74af46b3c642ad2d8cf106ebb7c9a67aa0..0d71b3025713da7931feb1239d56049f7b279033 100644 (file)
@@ -480,6 +480,10 @@ class OperatorTest(QueryTest, AssertsCompiledSQL):
 
     def test_in_on_relation_not_supported(self):
         assert_raises(NotImplementedError, Address.user.in_, [User(id=5)])
+    
+    def test_neg(self):
+        self._test(-User.id, "-users.id")
+        self._test(User.id + -User.id, "users.id + -users.id")
         
     def test_between(self):
         self._test(User.id.between('a', 'b'),
index 8390c73424c3a5fd2ea323b1884ab49ef24412dd..766ce8e9b789af479df037ca8f2dccd9c7effd63 100644 (file)
@@ -568,7 +568,20 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
                 self.assert_(compiled == fwd_sql or compiled == rev_sql,
                              "\n'" + compiled + "'\n does not match\n'" +
                              fwd_sql + "'\n or\n'" + rev_sql + "'")
-
+        
+        for (py_op, op) in (
+            (operator.neg, '-'),
+            (operator.inv, 'NOT '),
+        ):
+            for expr, sql in (
+                (table1.c.myid, "mytable.myid"),
+                (literal("foo"), ":param_1"),
+            ):
+            
+                compiled = str(py_op(expr))
+                sql = "%s%s" % (op, sql)
+                eq_(compiled, sql)
+        
         self.assert_compile(
          table1.select((table1.c.myid != 12) & ~(table1.c.name=='john')),
          "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid != :myid_1 AND mytable.name != :name_1"