summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/postgresql/ranges.py
blob: 2054ef137429624ab44ad50fb564978b82ba23e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (C) 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

from .base import ischema_names
from ... import types as sqltypes

__all__ = ('INT4RANGE', 'INT8RANGE', 'NUMRANGE')

class RangeOperators(object):

    class comparator_factory(sqltypes.Concatenable.Comparator):
        """Define comparison operations for range types."""

        def __ne__(self, other):
            "Boolean expression. Returns true if two ranges are not equal" 
            return self.expr.op('<>')(other)

        def contains(self, other, **kw):
            """Boolean expression. Returns true if the right hand operand,
            which can be an element or a range, is contained within the
            column.
            """
            return self.expr.op('@>')(other)

        def contained_by(self, other):
            """Boolean expression. Returns true if the column is contained
            within the right hand operand.
            """
            return self.expr.op('<@')(other)

        def overlaps(self, other):
            """Boolean expression. Returns true if the column overlaps
            (has points in common with) the right hand operand.
            """
            return self.expr.op('&&')(other)

        def strictly_left_of(self, other):
            """Boolean expression. Returns true if the column is strictly
            left of the right hand operand.
            """
            return self.expr.op('<<')(other)
            
        __lshift__ = strictly_left_of

        def strictly_right_of(self, other):
            """Boolean expression. Returns true if the column is strictly
            right of the right hand operand.
            """
            return self.expr.op('>>')(other)
            
        __rshift__ = strictly_right_of
        
        def not_extend_right_of(self, other):
            """Boolean expression. Returns true if the range in the column
            does not extend right of the range in the operand.
            """
            return self.expr.op('&<')(other)
        
        def not_extend_left_of(self, other):
            """Boolean expression. Returns true if the range in the column
            does not extend left of the range in the operand.
            """
            return self.expr.op('&>')(other)

        def adjacent_to(self, other):
            """Boolean expression. Returns true if the range in the column
            is adjacent to the range in the operand.
            """
            return self.expr.op('-|-')(other)

        def __add__(self, other):
            """Range expression. Returns the union of the two ranges.
            Will raise an exception if the resulting range is not
            contigous.
            """
            return self.expr.op('+')(other)

class INT4RANGE(RangeOperators, sqltypes.TypeEngine):
    "Represent the Postgresql INT4RANGE type."
    
    __visit_name__ = 'INT4RANGE'

ischema_names['int4range'] = INT4RANGE

class INT8RANGE(RangeOperators, sqltypes.TypeEngine):
    "Represent the Postgresql INT8RANGE type."
    
    __visit_name__ = 'INT8RANGE'

ischema_names['int8range'] = INT8RANGE

class NUMRANGE(RangeOperators, sqltypes.TypeEngine):
    "Represent the Postgresql NUMRANGE type."
    
    __visit_name__ = 'NUMRANGE'

ischema_names['numrange'] = NUMRANGE

class DATERANGE(RangeOperators, sqltypes.TypeEngine):
    "Represent the Postgresql DATERANGE type."
    
    __visit_name__ = 'DATERANGE'

ischema_names['daterange'] = DATERANGE

class TSRANGE(RangeOperators, sqltypes.TypeEngine):
    "Represent the Postgresql TSRANGE type."
    
    __visit_name__ = 'TSRANGE'

ischema_names['tsrange'] = TSRANGE

class TSTZRANGE(RangeOperators, sqltypes.TypeEngine):
    "Represent the Postgresql TSTZRANGE type."
    
    __visit_name__ = 'TSTZRANGE'

ischema_names['tstzrange'] = TSTZRANGE