1
# Copyright (C) 2013 the SQLAlchemy authors and contributors <see AUTHORS file>
3
# This module is part of SQLAlchemy and is released under
4
# the MIT License: http://www.opensource.org/licenses/mit-license.php
6
from .base import ischema_names
7
from ... import types as sqltypes
9
__all__ = ('INT4RANGE', 'INT8RANGE', 'NUMRANGE')
11
class RangeOperators(object):
13
This mixin provides functionality for the Range Operators
14
listed in Table 9-44 of the `postgres documentation`__ for Range
15
Functions and Operators. It is used by all the range types
16
provided in the ``postgres`` dialect and can likely be used for
17
any range types you create yourself.
19
__ http://www.postgresql.org/docs/devel/static/functions-range.html
21
No extra support is provided for the Range Functions listed in
22
Table 9-45 of the postgres documentation. For these, the normal
23
:func:`~sqlalchemy.sql.expression.func` object should be used.
25
.. versionadded:: 0.8.2 Support for Postgresql RANGE operations.
29
class comparator_factory(sqltypes.Concatenable.Comparator):
30
"""Define comparison operations for range types."""
32
def __ne__(self, other):
33
"Boolean expression. Returns true if two ranges are not equal"
34
return self.expr.op('<>')(other)
36
def contains(self, other, **kw):
37
"""Boolean expression. Returns true if the right hand operand,
38
which can be an element or a range, is contained within the
41
return self.expr.op('@>')(other)
43
def contained_by(self, other):
44
"""Boolean expression. Returns true if the column is contained
45
within the right hand operand.
47
return self.expr.op('<@')(other)
49
def overlaps(self, other):
50
"""Boolean expression. Returns true if the column overlaps
51
(has points in common with) the right hand operand.
53
return self.expr.op('&&')(other)
55
def strictly_left_of(self, other):
56
"""Boolean expression. Returns true if the column is strictly
57
left of the right hand operand.
59
return self.expr.op('<<')(other)
61
__lshift__ = strictly_left_of
63
def strictly_right_of(self, other):
64
"""Boolean expression. Returns true if the column is strictly
65
right of the right hand operand.
67
return self.expr.op('>>')(other)
69
__rshift__ = strictly_right_of
71
def not_extend_right_of(self, other):
72
"""Boolean expression. Returns true if the range in the column
73
does not extend right of the range in the operand.
75
return self.expr.op('&<')(other)
77
def not_extend_left_of(self, other):
78
"""Boolean expression. Returns true if the range in the column
79
does not extend left of the range in the operand.
81
return self.expr.op('&>')(other)
83
def adjacent_to(self, other):
84
"""Boolean expression. Returns true if the range in the column
85
is adjacent to the range in the operand.
87
return self.expr.op('-|-')(other)
89
def __add__(self, other):
90
"""Range expression. Returns the union of the two ranges.
91
Will raise an exception if the resulting range is not
94
return self.expr.op('+')(other)
96
class INT4RANGE(RangeOperators, sqltypes.TypeEngine):
97
"""Represent the Postgresql INT4RANGE type.
99
.. versionadded:: 0.8.2
103
__visit_name__ = 'INT4RANGE'
105
ischema_names['int4range'] = INT4RANGE
107
class INT8RANGE(RangeOperators, sqltypes.TypeEngine):
108
"""Represent the Postgresql INT8RANGE type.
110
.. versionadded:: 0.8.2
114
__visit_name__ = 'INT8RANGE'
116
ischema_names['int8range'] = INT8RANGE
118
class NUMRANGE(RangeOperators, sqltypes.TypeEngine):
119
"""Represent the Postgresql NUMRANGE type.
121
.. versionadded:: 0.8.2
125
__visit_name__ = 'NUMRANGE'
127
ischema_names['numrange'] = NUMRANGE
129
class DATERANGE(RangeOperators, sqltypes.TypeEngine):
130
"""Represent the Postgresql DATERANGE type.
132
.. versionadded:: 0.8.2
136
__visit_name__ = 'DATERANGE'
138
ischema_names['daterange'] = DATERANGE
140
class TSRANGE(RangeOperators, sqltypes.TypeEngine):
141
"""Represent the Postgresql TSRANGE type.
143
.. versionadded:: 0.8.2
147
__visit_name__ = 'TSRANGE'
149
ischema_names['tsrange'] = TSRANGE
151
class TSTZRANGE(RangeOperators, sqltypes.TypeEngine):
152
"""Represent the Postgresql TSTZRANGE type.
154
.. versionadded:: 0.8.2
158
__visit_name__ = 'TSTZRANGE'
160
ischema_names['tstzrange'] = TSTZRANGE