class Operators(object):
Known subclasses: sqlalchemy.sql.operators.ColumnOperators
Base of comparison and logical operators.
Implements base methods
~sqlalchemy.sql.operators.Operators.operate
and
~sqlalchemy.sql.operators.Operators.reverse_operate
, as well as
~sqlalchemy.sql.operators.Operators.__and__
,
~sqlalchemy.sql.operators.Operators.__or__
,
~sqlalchemy.sql.operators.Operators.__invert__
.
Usually is used via its most common subclass
.ColumnOperators
.
Method | __and__ |
Implement the & operator. |
Method | __invert__ |
Implement the ~ operator. |
Method | __or__ |
Implement the | operator. |
Method | bool_op |
Return a custom boolean operator. |
Method | op |
Produce a generic operator function. |
Method | operate |
Operate on an argument. |
Method | reverse_operate |
Reverse operate on an argument. |
Class Variable | __slots__ |
Undocumented |
Implement the & operator.
When used with SQL expressions, results in an
AND operation, equivalent to
_expression.and_
, that is:
a & b
is equivalent to:
from sqlalchemy import and_ and_(a, b)
Care should be taken when using & regarding operator precedence; the & operator has the highest precedence. The operands should be enclosed in parenthesis if they contain further sub expressions:
(a == 2) & (b == 4)
Implement the ~ operator.
When used with SQL expressions, results in a
NOT operation, equivalent to
_expression.not_
, that is:
~a
is equivalent to:
from sqlalchemy import not_ not_(a)
Implement the | operator.
When used with SQL expressions, results in an
OR operation, equivalent to
_expression.or_
, that is:
a | b
is equivalent to:
from sqlalchemy import or_ or_(a, b)
Care should be taken when using | regarding operator precedence; the | operator has the highest precedence. The operands should be enclosed in parenthesis if they contain further sub expressions:
(a == 2) | (b == 4)
Return a custom boolean operator.
This method is shorthand for calling
.Operators.op
and passing the
:paramref:`.Operators.op.is_comparison`
flag with True.
See Also
.Operators.op
Produce a generic operator function.
e.g.:
somecolumn.op("*")(5)
produces:
somecolumn * 5
This function can also be used to make bitwise operators explicit. For example:
somecolumn.op('&')(0xff)
is a bitwise AND of the value in somecolumn.
Parameters | |
opstring | Undocumented |
precedence | precedence to apply to the operator, when parenthesizing expressions. A lower number will cause the expression to be parenthesized when applied against another operator with higher precedence. The default value of 0 is lower than all operators except for the comma (,) and AS operators. A value of 100 will be higher or equal to all operators, and -100 will be lower than or equal to all operators. |
is_comparison | if True, the operator will be considered as a "comparison" operator, that is which evaluates to a boolean true/false value, like ==, >, etc. This flag should be set so that ORM relationships can establish that the operator is a comparison operator when used in a custom join condition.
New in version 0.9.2: - added the
:paramref:`.Operators.op.is_comparison` flag.
|
return_type | a .TypeEngine class or object that will
force the return type of an expression produced by this operator
to be of that type. By default, operators that specify
:paramref:`.Operators.op.is_comparison` will resolve to
.Boolean , and those that do not will be of the same
type as the left-hand operand. |
operator | a string which will be output as the infix operator between this element and the expression passed to the generated function. |
sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance
, sqlalchemy.orm.attributes.QueryableAttribute
, sqlalchemy.orm.properties.ColumnProperty.Comparator
, sqlalchemy.sql.expression.ColumnElement
, sqlalchemy.types.TypeEngine.Comparator
, sqlalchemy.ext.hybrid.ExprComparator
, sqlalchemy.orm.evaluator._NoObject
, sqlalchemy.sql.lambdas.PyWrapper
Operate on an argument.
This is the lowest level of operation, raises
NotImplementedError
by default.
Overriding this on a subclass can allow common
behavior to be applied to all operations.
For example, overriding .ColumnOperators
to apply func.lower() to the left and right
side:
class MyComparator(ColumnOperators): def operate(self, op, other): return op(func.lower(self), func.lower(other))
Parameters | |
op | Operator callable. |
*other | the 'other' side of the operation. Will be a single scalar for most operations. |
**kwargs | modifiers. These may be passed by special
operators such as ColumnOperators.contains . |
sqlalchemy.orm.attributes.QueryableAttribute
, sqlalchemy.orm.properties.ColumnProperty.Comparator
, sqlalchemy.sql.expression.ColumnElement
, sqlalchemy.types.TypeEngine.Comparator
, sqlalchemy.ext.hybrid.ExprComparator
, sqlalchemy.orm.evaluator._NoObject
, sqlalchemy.sql.lambdas.PyWrapper
Reverse operate on an argument.
Usage is the same as operate
.