You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.5KB

  1. # Copyright (C) 2013-2021 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. from ... import types as sqltypes
  7. __all__ = ("INT4RANGE", "INT8RANGE", "NUMRANGE")
  8. class RangeOperators(object):
  9. """
  10. This mixin provides functionality for the Range Operators
  11. listed in Table 9-44 of the `PostgreSQL documentation`__ for Range
  12. Functions and Operators. It is used by all the range types
  13. provided in the ``postgres`` dialect and can likely be used for
  14. any range types you create yourself.
  15. __ http://www.postgresql.org/docs/devel/static/functions-range.html
  16. No extra support is provided for the Range Functions listed in
  17. Table 9-45 of the PostgreSQL documentation. For these, the normal
  18. :func:`~sqlalchemy.sql.expression.func` object should be used.
  19. """
  20. class comparator_factory(sqltypes.Concatenable.Comparator):
  21. """Define comparison operations for range types."""
  22. def __ne__(self, other):
  23. "Boolean expression. Returns true if two ranges are not equal"
  24. if other is None:
  25. return super(RangeOperators.comparator_factory, self).__ne__(
  26. other
  27. )
  28. else:
  29. return self.expr.op("<>", is_comparison=True)(other)
  30. def contains(self, other, **kw):
  31. """Boolean expression. Returns true if the right hand operand,
  32. which can be an element or a range, is contained within the
  33. column.
  34. """
  35. return self.expr.op("@>", is_comparison=True)(other)
  36. def contained_by(self, other):
  37. """Boolean expression. Returns true if the column is contained
  38. within the right hand operand.
  39. """
  40. return self.expr.op("<@", is_comparison=True)(other)
  41. def overlaps(self, other):
  42. """Boolean expression. Returns true if the column overlaps
  43. (has points in common with) the right hand operand.
  44. """
  45. return self.expr.op("&&", is_comparison=True)(other)
  46. def strictly_left_of(self, other):
  47. """Boolean expression. Returns true if the column is strictly
  48. left of the right hand operand.
  49. """
  50. return self.expr.op("<<", is_comparison=True)(other)
  51. __lshift__ = strictly_left_of
  52. def strictly_right_of(self, other):
  53. """Boolean expression. Returns true if the column is strictly
  54. right of the right hand operand.
  55. """
  56. return self.expr.op(">>", is_comparison=True)(other)
  57. __rshift__ = strictly_right_of
  58. def not_extend_right_of(self, other):
  59. """Boolean expression. Returns true if the range in the column
  60. does not extend right of the range in the operand.
  61. """
  62. return self.expr.op("&<", is_comparison=True)(other)
  63. def not_extend_left_of(self, other):
  64. """Boolean expression. Returns true if the range in the column
  65. does not extend left of the range in the operand.
  66. """
  67. return self.expr.op("&>", is_comparison=True)(other)
  68. def adjacent_to(self, other):
  69. """Boolean expression. Returns true if the range in the column
  70. is adjacent to the range in the operand.
  71. """
  72. return self.expr.op("-|-", is_comparison=True)(other)
  73. def __add__(self, other):
  74. """Range expression. Returns the union of the two ranges.
  75. Will raise an exception if the resulting range is not
  76. contiguous.
  77. """
  78. return self.expr.op("+")(other)
  79. class INT4RANGE(RangeOperators, sqltypes.TypeEngine):
  80. """Represent the PostgreSQL INT4RANGE type."""
  81. __visit_name__ = "INT4RANGE"
  82. class INT8RANGE(RangeOperators, sqltypes.TypeEngine):
  83. """Represent the PostgreSQL INT8RANGE type."""
  84. __visit_name__ = "INT8RANGE"
  85. class NUMRANGE(RangeOperators, sqltypes.TypeEngine):
  86. """Represent the PostgreSQL NUMRANGE type."""
  87. __visit_name__ = "NUMRANGE"
  88. class DATERANGE(RangeOperators, sqltypes.TypeEngine):
  89. """Represent the PostgreSQL DATERANGE type."""
  90. __visit_name__ = "DATERANGE"
  91. class TSRANGE(RangeOperators, sqltypes.TypeEngine):
  92. """Represent the PostgreSQL TSRANGE type."""
  93. __visit_name__ = "TSRANGE"
  94. class TSTZRANGE(RangeOperators, sqltypes.TypeEngine):
  95. """Represent the PostgreSQL TSTZRANGE type."""
  96. __visit_name__ = "TSTZRANGE"