Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright (C) 2013-2020 the SQLAlchemy authors and contributors 

2# <see AUTHORS file> 

3# 

4# This module is part of SQLAlchemy and is released under 

5# the MIT License: http://www.opensource.org/licenses/mit-license.php 

6 

7from ... import types as sqltypes 

8 

9 

10__all__ = ("INT4RANGE", "INT8RANGE", "NUMRANGE") 

11 

12 

13class RangeOperators(object): 

14 """ 

15 This mixin provides functionality for the Range Operators 

16 listed in Table 9-44 of the `postgres documentation`__ for Range 

17 Functions and Operators. It is used by all the range types 

18 provided in the ``postgres`` dialect and can likely be used for 

19 any range types you create yourself. 

20 

21 __ http://www.postgresql.org/docs/devel/static/functions-range.html 

22 

23 No extra support is provided for the Range Functions listed in 

24 Table 9-45 of the postgres documentation. For these, the normal 

25 :func:`~sqlalchemy.sql.expression.func` object should be used. 

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 if other is None: 

35 return super(RangeOperators.comparator_factory, self).__ne__( 

36 other 

37 ) 

38 else: 

39 return self.expr.op("<>")(other) 

40 

41 def contains(self, other, **kw): 

42 """Boolean expression. Returns true if the right hand operand, 

43 which can be an element or a range, is contained within the 

44 column. 

45 """ 

46 return self.expr.op("@>")(other) 

47 

48 def contained_by(self, other): 

49 """Boolean expression. Returns true if the column is contained 

50 within the right hand operand. 

51 """ 

52 return self.expr.op("<@")(other) 

53 

54 def overlaps(self, other): 

55 """Boolean expression. Returns true if the column overlaps 

56 (has points in common with) the right hand operand. 

57 """ 

58 return self.expr.op("&&")(other) 

59 

60 def strictly_left_of(self, other): 

61 """Boolean expression. Returns true if the column is strictly 

62 left of the right hand operand. 

63 """ 

64 return self.expr.op("<<")(other) 

65 

66 __lshift__ = strictly_left_of 

67 

68 def strictly_right_of(self, other): 

69 """Boolean expression. Returns true if the column is strictly 

70 right of the right hand operand. 

71 """ 

72 return self.expr.op(">>")(other) 

73 

74 __rshift__ = strictly_right_of 

75 

76 def not_extend_right_of(self, other): 

77 """Boolean expression. Returns true if the range in the column 

78 does not extend right of the range in the operand. 

79 """ 

80 return self.expr.op("&<")(other) 

81 

82 def not_extend_left_of(self, other): 

83 """Boolean expression. Returns true if the range in the column 

84 does not extend left of the range in the operand. 

85 """ 

86 return self.expr.op("&>")(other) 

87 

88 def adjacent_to(self, other): 

89 """Boolean expression. Returns true if the range in the column 

90 is adjacent to the range in the operand. 

91 """ 

92 return self.expr.op("-|-")(other) 

93 

94 def __add__(self, other): 

95 """Range expression. Returns the union of the two ranges. 

96 Will raise an exception if the resulting range is not 

97 contigous. 

98 """ 

99 return self.expr.op("+")(other) 

100 

101 

102class INT4RANGE(RangeOperators, sqltypes.TypeEngine): 

103 """Represent the PostgreSQL INT4RANGE type. 

104 

105 """ 

106 

107 __visit_name__ = "INT4RANGE" 

108 

109 

110class INT8RANGE(RangeOperators, sqltypes.TypeEngine): 

111 """Represent the PostgreSQL INT8RANGE type. 

112 

113 """ 

114 

115 __visit_name__ = "INT8RANGE" 

116 

117 

118class NUMRANGE(RangeOperators, sqltypes.TypeEngine): 

119 """Represent the PostgreSQL NUMRANGE type. 

120 

121 """ 

122 

123 __visit_name__ = "NUMRANGE" 

124 

125 

126class DATERANGE(RangeOperators, sqltypes.TypeEngine): 

127 """Represent the PostgreSQL DATERANGE type. 

128 

129 """ 

130 

131 __visit_name__ = "DATERANGE" 

132 

133 

134class TSRANGE(RangeOperators, sqltypes.TypeEngine): 

135 """Represent the PostgreSQL TSRANGE type. 

136 

137 """ 

138 

139 __visit_name__ = "TSRANGE" 

140 

141 

142class TSTZRANGE(RangeOperators, sqltypes.TypeEngine): 

143 """Represent the PostgreSQL TSTZRANGE type. 

144 

145 """ 

146 

147 __visit_name__ = "TSTZRANGE"