Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding right-hand operand support to expressions

  • Loading branch information...
commit eeeefc9abfb6abfdf9b6ee54936d4ed216abada9 1 parent b41abf0
@coleifer authored
Showing with 13 additions and 2 deletions.
  1. +10 −2 peewee.py
  2. +3 −0  tests.py
View
12 peewee.py
@@ -195,9 +195,10 @@ def asc(self):
def desc(self):
return Ordering(self, False)
- def _expr(op, n=False):
+ def _expr(op, n=False, inv=False):
def inner(self, value):
- return BinaryExpr(self, op, value)
+ lhs, rhs = (self, value) if not inv else (value, self)
+ return BinaryExpr(lhs, op, rhs)
return inner
__add__ = _expr(OP_ADD)
@@ -207,6 +208,13 @@ def inner(self, value):
__and__ = _expr(OP_AND)
__or__ = _expr(OP_OR)
__xor__ = _expr(OP_XOR)
+ __radd__ = _expr(OP_ADD, inv=True)
+ __rsub__ = _expr(OP_SUB, inv=True)
+ __rmul__ = _expr(OP_MUL, inv=True)
+ __rdiv__ = _expr(OP_DIV, inv=True)
+ __rand__ = _expr(OP_AND, inv=True)
+ __ror__ = _expr(OP_OR, inv=True)
+ __rxor__ = _expr(OP_XOR, inv=True)
def _q(op):
def inner(self, value):
View
3  tests.py
@@ -495,6 +495,9 @@ def test_update(self):
uq = UpdateQuery(User, {User.id: User.id + 5})
self.assertUpdate(uq, [('"id"', '("id" + ?)')], [5])
+ uq = UpdateQuery(User, {User.id: 5 * (3 + User.id)})
+ self.assertUpdate(uq, [('"id"', '(? * (? + "id"))')], [5, 3])
+
def test_where(self):
uq = UpdateQuery(User, {User.username: 'updated'}).where(User.id == 2)
self.assertWhere(uq, 'users."id" = ?', [2])
Please sign in to comment.
Something went wrong with that request. Please try again.