ranges.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 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 the Range Operators table of the `PostgreSQL documentation`__
  12. for Range 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. __ https://www.postgresql.org/docs/devel/static/functions-range.html
  16. No extra support is provided for the Range Functions listed in the Range
  17. Functions table 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. kwargs may be ignored by this operator but are required for API
  35. conformance.
  36. """
  37. return self.expr.op("@>", is_comparison=True)(other)
  38. def contained_by(self, other):
  39. """Boolean expression. Returns true if the column is contained
  40. within the right hand operand.
  41. """
  42. return self.expr.op("<@", is_comparison=True)(other)
  43. def overlaps(self, other):
  44. """Boolean expression. Returns true if the column overlaps
  45. (has points in common with) the right hand operand.
  46. """
  47. return self.expr.op("&&", is_comparison=True)(other)
  48. def strictly_left_of(self, other):
  49. """Boolean expression. Returns true if the column is strictly
  50. left of the right hand operand.
  51. """
  52. return self.expr.op("<<", is_comparison=True)(other)
  53. __lshift__ = strictly_left_of
  54. def strictly_right_of(self, other):
  55. """Boolean expression. Returns true if the column is strictly
  56. right of the right hand operand.
  57. """
  58. return self.expr.op(">>", is_comparison=True)(other)
  59. __rshift__ = strictly_right_of
  60. def not_extend_right_of(self, other):
  61. """Boolean expression. Returns true if the range in the column
  62. does not extend right of the range in the operand.
  63. """
  64. return self.expr.op("&<", is_comparison=True)(other)
  65. def not_extend_left_of(self, other):
  66. """Boolean expression. Returns true if the range in the column
  67. does not extend left of the range in the operand.
  68. """
  69. return self.expr.op("&>", is_comparison=True)(other)
  70. def adjacent_to(self, other):
  71. """Boolean expression. Returns true if the range in the column
  72. is adjacent to the range in the operand.
  73. """
  74. return self.expr.op("-|-", is_comparison=True)(other)
  75. def __add__(self, other):
  76. """Range expression. Returns the union of the two ranges.
  77. Will raise an exception if the resulting range is not
  78. contiguous.
  79. """
  80. return self.expr.op("+")(other)
  81. class INT4RANGE(RangeOperators, sqltypes.TypeEngine):
  82. """Represent the PostgreSQL INT4RANGE type."""
  83. __visit_name__ = "INT4RANGE"
  84. class INT8RANGE(RangeOperators, sqltypes.TypeEngine):
  85. """Represent the PostgreSQL INT8RANGE type."""
  86. __visit_name__ = "INT8RANGE"
  87. class NUMRANGE(RangeOperators, sqltypes.TypeEngine):
  88. """Represent the PostgreSQL NUMRANGE type."""
  89. __visit_name__ = "NUMRANGE"
  90. class DATERANGE(RangeOperators, sqltypes.TypeEngine):
  91. """Represent the PostgreSQL DATERANGE type."""
  92. __visit_name__ = "DATERANGE"
  93. class TSRANGE(RangeOperators, sqltypes.TypeEngine):
  94. """Represent the PostgreSQL TSRANGE type."""
  95. __visit_name__ = "TSRANGE"
  96. class TSTZRANGE(RangeOperators, sqltypes.TypeEngine):
  97. """Represent the PostgreSQL TSTZRANGE type."""
  98. __visit_name__ = "TSTZRANGE"