Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/ranges.py: 70%

40 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +0000

1# Copyright (C) 2013-2022 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: https://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 the Range Operators table of the `PostgreSQL documentation`__ 

17 for Range 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 __ https://www.postgresql.org/docs/current/static/functions-range.html 

22 

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

24 Functions table of the PostgreSQL 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("<>", is_comparison=True)(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 kwargs may be ignored by this operator but are required for API 

47 conformance. 

48 """ 

49 return self.expr.op("@>", is_comparison=True)(other) 

50 

51 def contained_by(self, other): 

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

53 within the right hand operand. 

54 """ 

55 return self.expr.op("<@", is_comparison=True)(other) 

56 

57 def overlaps(self, other): 

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

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

60 """ 

61 return self.expr.op("&&", is_comparison=True)(other) 

62 

63 def strictly_left_of(self, other): 

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

65 left of the right hand operand. 

66 """ 

67 return self.expr.op("<<", is_comparison=True)(other) 

68 

69 __lshift__ = strictly_left_of 

70 

71 def strictly_right_of(self, other): 

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

73 right of the right hand operand. 

74 """ 

75 return self.expr.op(">>", is_comparison=True)(other) 

76 

77 __rshift__ = strictly_right_of 

78 

79 def not_extend_right_of(self, other): 

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

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

82 """ 

83 return self.expr.op("&<", is_comparison=True)(other) 

84 

85 def not_extend_left_of(self, other): 

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

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

88 """ 

89 return self.expr.op("&>", is_comparison=True)(other) 

90 

91 def adjacent_to(self, other): 

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

93 is adjacent to the range in the operand. 

94 """ 

95 return self.expr.op("-|-", is_comparison=True)(other) 

96 

97 def __add__(self, other): 

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

99 Will raise an exception if the resulting range is not 

100 contiguous. 

101 """ 

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

103 

104 

105class INT4RANGE(RangeOperators, sqltypes.TypeEngine): 

106 """Represent the PostgreSQL INT4RANGE type.""" 

107 

108 __visit_name__ = "INT4RANGE" 

109 

110 

111class INT8RANGE(RangeOperators, sqltypes.TypeEngine): 

112 """Represent the PostgreSQL INT8RANGE type.""" 

113 

114 __visit_name__ = "INT8RANGE" 

115 

116 

117class NUMRANGE(RangeOperators, sqltypes.TypeEngine): 

118 """Represent the PostgreSQL NUMRANGE type.""" 

119 

120 __visit_name__ = "NUMRANGE" 

121 

122 

123class DATERANGE(RangeOperators, sqltypes.TypeEngine): 

124 """Represent the PostgreSQL DATERANGE type.""" 

125 

126 __visit_name__ = "DATERANGE" 

127 

128 

129class TSRANGE(RangeOperators, sqltypes.TypeEngine): 

130 """Represent the PostgreSQL TSRANGE type.""" 

131 

132 __visit_name__ = "TSRANGE" 

133 

134 

135class TSTZRANGE(RangeOperators, sqltypes.TypeEngine): 

136 """Represent the PostgreSQL TSTZRANGE type.""" 

137 

138 __visit_name__ = "TSTZRANGE"