~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/dialects/postgresql/ranges.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2013 the SQLAlchemy authors and contributors <see AUTHORS file>
 
2
#
 
3
# This module is part of SQLAlchemy and is released under
 
4
# the MIT License: http://www.opensource.org/licenses/mit-license.php
 
5
 
 
6
from .base import ischema_names
 
7
from ... import types as sqltypes
 
8
 
 
9
__all__ = ('INT4RANGE', 'INT8RANGE', 'NUMRANGE')
 
10
 
 
11
class RangeOperators(object):
 
12
    """
 
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.
 
18
 
 
19
    __ http://www.postgresql.org/docs/devel/static/functions-range.html
 
20
 
 
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.
 
24
 
 
25
    .. versionadded:: 0.8.2  Support for Postgresql RANGE operations.
 
26
 
 
27
    """
 
28
 
 
29
    class comparator_factory(sqltypes.Concatenable.Comparator):
 
30
        """Define comparison operations for range types."""
 
31
 
 
32
        def __ne__(self, other):
 
33
            "Boolean expression. Returns true if two ranges are not equal"
 
34
            return self.expr.op('<>')(other)
 
35
 
 
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
 
39
            column.
 
40
            """
 
41
            return self.expr.op('@>')(other)
 
42
 
 
43
        def contained_by(self, other):
 
44
            """Boolean expression. Returns true if the column is contained
 
45
            within the right hand operand.
 
46
            """
 
47
            return self.expr.op('<@')(other)
 
48
 
 
49
        def overlaps(self, other):
 
50
            """Boolean expression. Returns true if the column overlaps
 
51
            (has points in common with) the right hand operand.
 
52
            """
 
53
            return self.expr.op('&&')(other)
 
54
 
 
55
        def strictly_left_of(self, other):
 
56
            """Boolean expression. Returns true if the column is strictly
 
57
            left of the right hand operand.
 
58
            """
 
59
            return self.expr.op('<<')(other)
 
60
 
 
61
        __lshift__ = strictly_left_of
 
62
 
 
63
        def strictly_right_of(self, other):
 
64
            """Boolean expression. Returns true if the column is strictly
 
65
            right of the right hand operand.
 
66
            """
 
67
            return self.expr.op('>>')(other)
 
68
 
 
69
        __rshift__ = strictly_right_of
 
70
 
 
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.
 
74
            """
 
75
            return self.expr.op('&<')(other)
 
76
 
 
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.
 
80
            """
 
81
            return self.expr.op('&>')(other)
 
82
 
 
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.
 
86
            """
 
87
            return self.expr.op('-|-')(other)
 
88
 
 
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
 
92
            contigous.
 
93
            """
 
94
            return self.expr.op('+')(other)
 
95
 
 
96
class INT4RANGE(RangeOperators, sqltypes.TypeEngine):
 
97
    """Represent the Postgresql INT4RANGE type.
 
98
 
 
99
    .. versionadded:: 0.8.2
 
100
 
 
101
    """
 
102
 
 
103
    __visit_name__ = 'INT4RANGE'
 
104
 
 
105
ischema_names['int4range'] = INT4RANGE
 
106
 
 
107
class INT8RANGE(RangeOperators, sqltypes.TypeEngine):
 
108
    """Represent the Postgresql INT8RANGE type.
 
109
 
 
110
    .. versionadded:: 0.8.2
 
111
 
 
112
    """
 
113
 
 
114
    __visit_name__ = 'INT8RANGE'
 
115
 
 
116
ischema_names['int8range'] = INT8RANGE
 
117
 
 
118
class NUMRANGE(RangeOperators, sqltypes.TypeEngine):
 
119
    """Represent the Postgresql NUMRANGE type.
 
120
 
 
121
    .. versionadded:: 0.8.2
 
122
 
 
123
    """
 
124
 
 
125
    __visit_name__ = 'NUMRANGE'
 
126
 
 
127
ischema_names['numrange'] = NUMRANGE
 
128
 
 
129
class DATERANGE(RangeOperators, sqltypes.TypeEngine):
 
130
    """Represent the Postgresql DATERANGE type.
 
131
 
 
132
    .. versionadded:: 0.8.2
 
133
 
 
134
    """
 
135
 
 
136
    __visit_name__ = 'DATERANGE'
 
137
 
 
138
ischema_names['daterange'] = DATERANGE
 
139
 
 
140
class TSRANGE(RangeOperators, sqltypes.TypeEngine):
 
141
    """Represent the Postgresql TSRANGE type.
 
142
 
 
143
    .. versionadded:: 0.8.2
 
144
 
 
145
    """
 
146
 
 
147
    __visit_name__ = 'TSRANGE'
 
148
 
 
149
ischema_names['tsrange'] = TSRANGE
 
150
 
 
151
class TSTZRANGE(RangeOperators, sqltypes.TypeEngine):
 
152
    """Represent the Postgresql TSTZRANGE type.
 
153
 
 
154
    .. versionadded:: 0.8.2
 
155
 
 
156
    """
 
157
 
 
158
    __visit_name__ = 'TSTZRANGE'
 
159
 
 
160
ischema_names['tstzrange'] = TSTZRANGE