diff options
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/ranges.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/ranges.py | 81 |
1 files changed, 75 insertions, 6 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/ranges.py b/lib/sqlalchemy/dialects/postgresql/ranges.py index b3a670d91..2054ef137 100644 --- a/lib/sqlalchemy/dialects/postgresql/ranges.py +++ b/lib/sqlalchemy/dialects/postgresql/ranges.py @@ -8,42 +8,111 @@ from ... import types as sqltypes __all__ = ('INT4RANGE', 'INT8RANGE', 'NUMRANGE') -class INT4RANGE(sqltypes.TypeEngine): +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(sqltypes.TypeEngine): +class INT8RANGE(RangeOperators, sqltypes.TypeEngine): "Represent the Postgresql INT8RANGE type." __visit_name__ = 'INT8RANGE' ischema_names['int8range'] = INT8RANGE -class NUMRANGE(sqltypes.TypeEngine): +class NUMRANGE(RangeOperators, sqltypes.TypeEngine): "Represent the Postgresql NUMRANGE type." __visit_name__ = 'NUMRANGE' ischema_names['numrange'] = NUMRANGE -class DATERANGE(sqltypes.TypeEngine): +class DATERANGE(RangeOperators, sqltypes.TypeEngine): "Represent the Postgresql DATERANGE type." __visit_name__ = 'DATERANGE' ischema_names['daterange'] = DATERANGE -class TSRANGE(sqltypes.TypeEngine): +class TSRANGE(RangeOperators, sqltypes.TypeEngine): "Represent the Postgresql TSRANGE type." __visit_name__ = 'TSRANGE' ischema_names['tsrange'] = TSRANGE -class TSTZRANGE(sqltypes.TypeEngine): +class TSTZRANGE(RangeOperators, sqltypes.TypeEngine): "Represent the Postgresql TSTZRANGE type." __visit_name__ = 'TSTZRANGE' |