ranges.py 4.7 KB

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