Skip to content
This repository
Browse code

Adding right-hand operand support to expressions

  • Loading branch information...
commit eeeefc9abfb6abfdf9b6ee54936d4ed216abada9 1 parent b41abf0
Charles Leifer authored October 08, 2012

Showing 2 changed files with 13 additions and 2 deletions. Show diff stats Hide diff stats

  1. 12  peewee.py
  2. 3  tests.py
12  peewee.py
@@ -195,9 +195,10 @@ def asc(self):
195 195
     def desc(self):
196 196
         return Ordering(self, False)
197 197
 
198  
-    def _expr(op, n=False):
  198
+    def _expr(op, n=False, inv=False):
199 199
         def inner(self, value):
200  
-            return BinaryExpr(self, op, value)
  200
+            lhs, rhs = (self, value) if not inv else (value, self)
  201
+            return BinaryExpr(lhs, op, rhs)
201 202
         return inner
202 203
 
203 204
     __add__ = _expr(OP_ADD)
@@ -207,6 +208,13 @@ def inner(self, value):
207 208
     __and__ = _expr(OP_AND)
208 209
     __or__ = _expr(OP_OR)
209 210
     __xor__ = _expr(OP_XOR)
  211
+    __radd__ = _expr(OP_ADD, inv=True)
  212
+    __rsub__ = _expr(OP_SUB, inv=True)
  213
+    __rmul__ = _expr(OP_MUL, inv=True)
  214
+    __rdiv__ = _expr(OP_DIV, inv=True)
  215
+    __rand__ = _expr(OP_AND, inv=True)
  216
+    __ror__ = _expr(OP_OR, inv=True)
  217
+    __rxor__ = _expr(OP_XOR, inv=True)
210 218
 
211 219
     def _q(op):
212 220
         def inner(self, value):
3  tests.py
@@ -495,6 +495,9 @@ def test_update(self):
495 495
         uq = UpdateQuery(User, {User.id: User.id + 5})
496 496
         self.assertUpdate(uq, [('"id"', '("id" + ?)')], [5])
497 497
 
  498
+        uq = UpdateQuery(User, {User.id: 5 * (3 + User.id)})
  499
+        self.assertUpdate(uq, [('"id"', '(? * (? + "id"))')], [5, 3])
  500
+
498 501
     def test_where(self):
499 502
         uq = UpdateQuery(User, {User.username: 'updated'}).where(User.id == 2)
500 503
         self.assertWhere(uq, 'users."id" = ?', [2])

0 notes on commit eeeefc9

Please sign in to comment.
Something went wrong with that request. Please try again.