Update SQLAlchemy
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# sql/operators.py
|
||||
# Copyright (C) 2005-2012 the SQLAlchemy authors and contributors <see AUTHORS file>
|
||||
# Copyright (C) 2005-2013 the SQLAlchemy authors and contributors <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
# the MIT License: http://www.opensource.org/licenses/mit-license.php
|
||||
@@ -21,25 +21,25 @@ from sqlalchemy.util import symbol
|
||||
|
||||
class Operators(object):
|
||||
"""Base of comparison and logical operators.
|
||||
|
||||
|
||||
Implements base methods :meth:`operate` and :meth:`reverse_operate`,
|
||||
as well as :meth:`__and__`, :meth:`__or__`, :meth:`__invert__`.
|
||||
|
||||
|
||||
Usually is used via its most common subclass
|
||||
:class:`.ColumnOperators`.
|
||||
|
||||
|
||||
"""
|
||||
def __and__(self, other):
|
||||
"""Implement the ``&`` operator.
|
||||
|
||||
|
||||
When used with SQL expressions, results in an
|
||||
AND operation, equivalent to
|
||||
:func:`~.expression.and_`, that is::
|
||||
|
||||
|
||||
a & b
|
||||
|
||||
|
||||
is equivalent to::
|
||||
|
||||
|
||||
from sqlalchemy import and_
|
||||
and_(a, b)
|
||||
|
||||
@@ -47,7 +47,7 @@ class Operators(object):
|
||||
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)
|
||||
|
||||
"""
|
||||
@@ -55,15 +55,15 @@ class Operators(object):
|
||||
|
||||
def __or__(self, other):
|
||||
"""Implement the ``|`` operator.
|
||||
|
||||
|
||||
When used with SQL expressions, results in an
|
||||
OR operation, equivalent to
|
||||
:func:`~.expression.or_`, that is::
|
||||
|
||||
|
||||
a | b
|
||||
|
||||
|
||||
is equivalent to::
|
||||
|
||||
|
||||
from sqlalchemy import or_
|
||||
or_(a, b)
|
||||
|
||||
@@ -71,7 +71,7 @@ class Operators(object):
|
||||
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)
|
||||
|
||||
"""
|
||||
@@ -79,15 +79,15 @@ class Operators(object):
|
||||
|
||||
def __invert__(self):
|
||||
"""Implement the ``~`` operator.
|
||||
|
||||
When used with SQL expressions, results in a
|
||||
NOT operation, equivalent to
|
||||
|
||||
When used with SQL expressions, results in a
|
||||
NOT operation, equivalent to
|
||||
:func:`~.expression.not_`, that is::
|
||||
|
||||
|
||||
~a
|
||||
|
||||
|
||||
is equivalent to::
|
||||
|
||||
|
||||
from sqlalchemy import not_
|
||||
not_(a)
|
||||
|
||||
@@ -123,16 +123,16 @@ class Operators(object):
|
||||
|
||||
def operate(self, op, *other, **kwargs):
|
||||
"""Operate on an argument.
|
||||
|
||||
|
||||
This is the lowest level of operation, raises
|
||||
:class:`NotImplementedError` by default.
|
||||
|
||||
Overriding this on a subclass can allow common
|
||||
behavior to be applied to all operations.
|
||||
|
||||
Overriding this on a subclass can allow common
|
||||
behavior to be applied to all operations.
|
||||
For example, overriding :class:`.ColumnOperators`
|
||||
to apply ``func.lower()`` to the left and right
|
||||
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))
|
||||
@@ -142,48 +142,48 @@ class Operators(object):
|
||||
be a single scalar for most operations.
|
||||
:param \**kwargs: modifiers. These may be passed by special
|
||||
operators such as :meth:`ColumnOperators.contains`.
|
||||
|
||||
|
||||
|
||||
|
||||
"""
|
||||
raise NotImplementedError(str(op))
|
||||
|
||||
def reverse_operate(self, op, other, **kwargs):
|
||||
"""Reverse operate on an argument.
|
||||
|
||||
|
||||
Usage is the same as :meth:`operate`.
|
||||
|
||||
|
||||
"""
|
||||
raise NotImplementedError(str(op))
|
||||
|
||||
class ColumnOperators(Operators):
|
||||
"""Defines comparison and math operations.
|
||||
|
||||
|
||||
By default all methods call down to
|
||||
:meth:`Operators.operate` or :meth:`Operators.reverse_operate`
|
||||
passing in the appropriate operator function from the
|
||||
passing in the appropriate operator function from the
|
||||
Python builtin ``operator`` module or
|
||||
a SQLAlchemy-specific operator function from
|
||||
a SQLAlchemy-specific operator function from
|
||||
:mod:`sqlalchemy.expression.operators`. For example
|
||||
the ``__eq__`` function::
|
||||
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.operate(operators.eq, other)
|
||||
|
||||
Where ``operators.eq`` is essentially::
|
||||
|
||||
|
||||
def eq(a, b):
|
||||
return a == b
|
||||
|
||||
|
||||
A SQLAlchemy construct like :class:`.ColumnElement` ultimately
|
||||
overrides :meth:`.Operators.operate` and others
|
||||
to return further :class:`.ClauseElement` constructs,
|
||||
to return further :class:`.ClauseElement` constructs,
|
||||
so that the ``==`` operation above is replaced by a clause
|
||||
construct.
|
||||
|
||||
|
||||
The docstrings here will describe column-oriented
|
||||
behavior of each operator. For ORM-based operators
|
||||
on related objects and collections, see :class:`.RelationshipProperty.Comparator`.
|
||||
|
||||
|
||||
"""
|
||||
|
||||
timetuple = None
|
||||
@@ -191,17 +191,17 @@ class ColumnOperators(Operators):
|
||||
|
||||
def __lt__(self, other):
|
||||
"""Implement the ``<`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a < b``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(lt, other)
|
||||
|
||||
def __le__(self, other):
|
||||
"""Implement the ``<=`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a <= b``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(le, other)
|
||||
|
||||
@@ -209,7 +209,7 @@ class ColumnOperators(Operators):
|
||||
|
||||
def __eq__(self, other):
|
||||
"""Implement the ``==`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a = b``.
|
||||
If the target is ``None``, produces ``a IS NULL``.
|
||||
|
||||
@@ -221,98 +221,128 @@ class ColumnOperators(Operators):
|
||||
|
||||
In a column context, produces the clause ``a != b``.
|
||||
If the target is ``None``, produces ``a IS NOT NULL``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(ne, other)
|
||||
|
||||
def __gt__(self, other):
|
||||
"""Implement the ``>`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a > b``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(gt, other)
|
||||
|
||||
def __ge__(self, other):
|
||||
"""Implement the ``>=`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a >= b``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(ge, other)
|
||||
|
||||
def __neg__(self):
|
||||
"""Implement the ``-`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``-a``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(neg)
|
||||
|
||||
def concat(self, other):
|
||||
"""Implement the 'concat' operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a || b``,
|
||||
or uses the ``concat()`` operator on MySQL.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(concat_op, other)
|
||||
|
||||
def like(self, other, escape=None):
|
||||
"""Implement the ``like`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a LIKE other``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(like_op, other, escape=escape)
|
||||
|
||||
def ilike(self, other, escape=None):
|
||||
"""Implement the ``ilike`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a ILIKE other``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(ilike_op, other, escape=escape)
|
||||
|
||||
def in_(self, other):
|
||||
"""Implement the ``in`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a IN other``.
|
||||
"other" may be a tuple/list of column expressions,
|
||||
or a :func:`~.expression.select` construct.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(in_op, other)
|
||||
|
||||
def is_(self, other):
|
||||
"""Implement the ``IS`` operator.
|
||||
|
||||
Normally, ``IS`` is generated automatically when comparing to a
|
||||
value of ``None``, which resolves to ``NULL``. However, explicit
|
||||
usage of ``IS`` may be desirable if comparing to boolean values
|
||||
on certain platforms.
|
||||
|
||||
.. versionadded:: 0.7.9
|
||||
|
||||
.. seealso:: :meth:`.ColumnOperators.isnot`
|
||||
|
||||
"""
|
||||
return self.operate(is_, other)
|
||||
|
||||
def isnot(self, other):
|
||||
"""Implement the ``IS NOT`` operator.
|
||||
|
||||
Normally, ``IS NOT`` is generated automatically when comparing to a
|
||||
value of ``None``, which resolves to ``NULL``. However, explicit
|
||||
usage of ``IS NOT`` may be desirable if comparing to boolean values
|
||||
on certain platforms.
|
||||
|
||||
.. versionadded:: 0.7.9
|
||||
|
||||
.. seealso:: :meth:`.ColumnOperators.is_`
|
||||
|
||||
"""
|
||||
return self.operate(isnot, other)
|
||||
|
||||
def startswith(self, other, **kwargs):
|
||||
"""Implement the ``startwith`` operator.
|
||||
|
||||
In a column context, produces the clause ``LIKE '<other>%'``
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(startswith_op, other, **kwargs)
|
||||
|
||||
def endswith(self, other, **kwargs):
|
||||
"""Implement the 'endswith' operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``LIKE '%<other>'``
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(endswith_op, other, **kwargs)
|
||||
|
||||
def contains(self, other, **kwargs):
|
||||
"""Implement the 'contains' operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``LIKE '%<other>%'``
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(contains_op, other, **kwargs)
|
||||
|
||||
def match(self, other, **kwargs):
|
||||
"""Implements the 'match' operator.
|
||||
|
||||
In a column context, this produces a MATCH clause, i.e.
|
||||
``MATCH '<other>'``. The allowed contents of ``other``
|
||||
|
||||
In a column context, this produces a MATCH clause, i.e.
|
||||
``MATCH '<other>'``. The allowed contents of ``other``
|
||||
are database backend specific.
|
||||
|
||||
"""
|
||||
@@ -347,7 +377,7 @@ class ColumnOperators(Operators):
|
||||
"""Implement the ``+`` operator in reverse.
|
||||
|
||||
See :meth:`__add__`.
|
||||
|
||||
|
||||
"""
|
||||
return self.reverse_operate(add, other)
|
||||
|
||||
@@ -355,7 +385,7 @@ class ColumnOperators(Operators):
|
||||
"""Implement the ``-`` operator in reverse.
|
||||
|
||||
See :meth:`__sub__`.
|
||||
|
||||
|
||||
"""
|
||||
return self.reverse_operate(sub, other)
|
||||
|
||||
@@ -363,7 +393,7 @@ class ColumnOperators(Operators):
|
||||
"""Implement the ``*`` operator in reverse.
|
||||
|
||||
See :meth:`__mul__`.
|
||||
|
||||
|
||||
"""
|
||||
return self.reverse_operate(mul, other)
|
||||
|
||||
@@ -371,7 +401,7 @@ class ColumnOperators(Operators):
|
||||
"""Implement the ``/`` operator in reverse.
|
||||
|
||||
See :meth:`__div__`.
|
||||
|
||||
|
||||
"""
|
||||
return self.reverse_operate(div, other)
|
||||
|
||||
@@ -386,61 +416,61 @@ class ColumnOperators(Operators):
|
||||
|
||||
def __add__(self, other):
|
||||
"""Implement the ``+`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a + b``
|
||||
if the parent object has non-string affinity.
|
||||
If the parent object has a string affinity,
|
||||
If the parent object has a string affinity,
|
||||
produces the concatenation operator, ``a || b`` -
|
||||
see :meth:`concat`.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(add, other)
|
||||
|
||||
def __sub__(self, other):
|
||||
"""Implement the ``-`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a - b``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(sub, other)
|
||||
|
||||
def __mul__(self, other):
|
||||
"""Implement the ``*`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a * b``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(mul, other)
|
||||
|
||||
def __div__(self, other):
|
||||
"""Implement the ``/`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a / b``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(div, other)
|
||||
|
||||
def __mod__(self, other):
|
||||
"""Implement the ``%`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a % b``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(mod, other)
|
||||
|
||||
def __truediv__(self, other):
|
||||
"""Implement the ``//`` operator.
|
||||
|
||||
|
||||
In a column context, produces the clause ``a / b``.
|
||||
|
||||
|
||||
"""
|
||||
return self.operate(truediv, other)
|
||||
|
||||
def __rtruediv__(self, other):
|
||||
"""Implement the ``//`` operator in reverse.
|
||||
|
||||
|
||||
See :meth:`__truediv__`.
|
||||
|
||||
|
||||
"""
|
||||
return self.reverse_operate(truediv, other)
|
||||
|
||||
@@ -469,13 +499,13 @@ def like_op(a, b, escape=None):
|
||||
return a.like(b, escape=escape)
|
||||
|
||||
def notlike_op(a, b, escape=None):
|
||||
raise NotImplementedError()
|
||||
return ~a.like(b, escape=escape)
|
||||
|
||||
def ilike_op(a, b, escape=None):
|
||||
return a.ilike(b, escape=escape)
|
||||
|
||||
def notilike_op(a, b, escape=None):
|
||||
raise NotImplementedError()
|
||||
return ~a.ilike(b, escape=escape)
|
||||
|
||||
def between_op(a, b, c):
|
||||
return a.between(b, c)
|
||||
@@ -484,7 +514,7 @@ def in_op(a, b):
|
||||
return a.in_(b)
|
||||
|
||||
def notin_op(a, b):
|
||||
raise NotImplementedError()
|
||||
return ~a.in_(b)
|
||||
|
||||
def distinct_op(a):
|
||||
return a.distinct()
|
||||
@@ -525,7 +555,7 @@ def is_commutative(op):
|
||||
return op in _commutative
|
||||
|
||||
def is_ordering_modifier(op):
|
||||
return op in (asc_op, desc_op,
|
||||
return op in (asc_op, desc_op,
|
||||
nullsfirst_op, nullslast_op)
|
||||
|
||||
_associative = _commutative.union([concat_op, and_, or_])
|
||||
|
||||
Reference in New Issue
Block a user