Revision 3b877f08 lib/query.py
b/lib/query.py | ||
---|---|---|
357 | 357 |
|
358 | 358 |
- C{_OPTYPE_LOGIC}: Callable taking any number of arguments; used by |
359 | 359 |
L{_HandleLogicOp} |
360 |
- C{_OPTYPE_UNARY}: Callable taking exactly one parameter; used by |
|
361 |
L{_HandleUnaryOp} |
|
360 |
- C{_OPTYPE_UNARY}: Always C{None}; details handled by L{_HandleUnaryOp} |
|
362 | 361 |
- C{_OPTYPE_BINARY}: Callable taking exactly two parameters, the left- and |
363 | 362 |
right-hand side of the operator, used by L{_HandleBinaryOp} |
364 | 363 |
|
... | ... | |
369 | 368 |
qlang.OP_AND: (_OPTYPE_LOGIC, compat.all), |
370 | 369 |
|
371 | 370 |
# Unary operators |
372 |
qlang.OP_NOT: (_OPTYPE_UNARY, operator.not_), |
|
371 |
qlang.OP_NOT: (_OPTYPE_UNARY, None), |
|
372 |
qlang.OP_TRUE: (_OPTYPE_UNARY, None), |
|
373 | 373 |
|
374 | 374 |
# Binary operators |
375 | 375 |
qlang.OP_EQUAL: (_OPTYPE_BINARY, _EQUALITY_CHECKS), |
... | ... | |
449 | 449 |
|
450 | 450 |
return handler(hints_cb, level, op, op_data, operands) |
451 | 451 |
|
452 |
def _LookupField(self, name): |
|
453 |
"""Returns a field definition by name. |
|
454 |
|
|
455 |
""" |
|
456 |
try: |
|
457 |
return self._fields[name] |
|
458 |
except KeyError: |
|
459 |
raise errors.ParameterError("Unknown field '%s'" % name) |
|
460 |
|
|
452 | 461 |
def _HandleLogicOp(self, hints_fn, level, op, op_fn, operands): |
453 | 462 |
"""Handles logic operators. |
454 | 463 |
|
... | ... | |
485 | 494 |
@param operands: List of operands |
486 | 495 |
|
487 | 496 |
""" |
497 |
assert op_fn is None |
|
498 |
|
|
488 | 499 |
if hints_fn: |
489 | 500 |
hints_fn(op) |
490 | 501 |
|
... | ... | |
492 | 503 |
raise errors.ParameterError("Unary operator '%s' expects exactly one" |
493 | 504 |
" operand" % op) |
494 | 505 |
|
495 |
return compat.partial(_WrapUnaryOp, op_fn, |
|
496 |
self._Compile(operands[0], level + 1)) |
|
506 |
if op == qlang.OP_TRUE: |
|
507 |
(_, _, _, retrieval_fn) = self._LookupField(operands[0]) |
|
508 |
|
|
509 |
op_fn = operator.truth |
|
510 |
arg = retrieval_fn |
|
511 |
elif op == qlang.OP_NOT: |
|
512 |
op_fn = operator.not_ |
|
513 |
arg = self._Compile(operands[0], level + 1) |
|
514 |
else: |
|
515 |
raise errors.ProgrammerError("Can't handle operator '%s'" % op) |
|
516 |
|
|
517 |
return compat.partial(_WrapUnaryOp, op_fn, arg) |
|
497 | 518 |
|
498 | 519 |
def _HandleBinaryOp(self, hints_fn, level, op, op_data, operands): |
499 | 520 |
"""Handles binary operators. |
... | ... | |
516 | 537 |
raise errors.ParameterError("Invalid binary operator, expected exactly" |
517 | 538 |
" two operands") |
518 | 539 |
|
519 |
try: |
|
520 |
(fdef, datakind, field_flags, retrieval_fn) = self._fields[name] |
|
521 |
except KeyError: |
|
522 |
raise errors.ParameterError("Unknown field '%s'" % name) |
|
540 |
(fdef, datakind, field_flags, retrieval_fn) = self._LookupField(name) |
|
523 | 541 |
|
524 | 542 |
assert fdef.kind != QFT_UNKNOWN |
525 | 543 |
|
Also available in: Unified diff