ranges.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Copyright (C) 2013-2017 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 .base import ischema_names
  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 Table 9-44 of the `postgres documentation`__ for Range
  13. 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. __ http://www.postgresql.org/docs/devel/static/functions-range.html
  17. No extra support is provided for the Range Functions listed in
  18. Table 9-45 of the postgres documentation. For these, the normal
  19. :func:`~sqlalchemy.sql.expression.func` object should be used.
  20. .. versionadded:: 0.8.2 Support for PostgreSQL RANGE operations.
  21. """
  22. class comparator_factory(sqltypes.Concatenable.Comparator):
  23. """Define comparison operations for range types."""
  24. def __ne__(self, other):
  25. "Boolean expression. Returns true if two ranges are not equal"
  26. return self.expr.op('<>')(other)
  27. def contains(self, other, **kw):
  28. """Boolean expression. Returns true if the right hand operand,
  29. which can be an element or a range, is contained within the
  30. column.
  31. """
  32. return self.expr.op('@>')(other)
  33. def contained_by(self, other):
  34. """Boolean expression. Returns true if the column is contained
  35. within the right hand operand.
  36. """
  37. return self.expr.op('<@')(other)
  38. def overlaps(self, other):
  39. """Boolean expression. Returns true if the column overlaps
  40. (has points in common with) the right hand operand.
  41. """
  42. return self.expr.op('&&')(other)
  43. def strictly_left_of(self, other):
  44. """Boolean expression. Returns true if the column is strictly
  45. left of the right hand operand.
  46. """
  47. return self.expr.op('<<')(other)
  48. __lshift__ = strictly_left_of
  49. def strictly_right_of(self, other):
  50. """Boolean expression. Returns true if the column is strictly
  51. right of the right hand operand.
  52. """
  53. return self.expr.op('>>')(other)
  54. __rshift__ = strictly_right_of
  55. def not_extend_right_of(self, other):
  56. """Boolean expression. Returns true if the range in the column
  57. does not extend right of the range in the operand.
  58. """
  59. return self.expr.op('&<')(other)
  60. def not_extend_left_of(self, other):
  61. """Boolean expression. Returns true if the range in the column
  62. does not extend left of the range in the operand.
  63. """
  64. return self.expr.op('&>')(other)
  65. def adjacent_to(self, other):
  66. """Boolean expression. Returns true if the range in the column
  67. is adjacent to the range in the operand.
  68. """
  69. return self.expr.op('-|-')(other)
  70. def __add__(self, other):
  71. """Range expression. Returns the union of the two ranges.
  72. Will raise an exception if the resulting range is not
  73. contigous.
  74. """
  75. return self.expr.op('+')(other)
  76. class INT4RANGE(RangeOperators, sqltypes.TypeEngine):
  77. """Represent the PostgreSQL INT4RANGE type.
  78. .. versionadded:: 0.8.2
  79. """
  80. __visit_name__ = 'INT4RANGE'
  81. ischema_names['int4range'] = INT4RANGE
  82. class INT8RANGE(RangeOperators, sqltypes.TypeEngine):
  83. """Represent the PostgreSQL INT8RANGE type.
  84. .. versionadded:: 0.8.2
  85. """
  86. __visit_name__ = 'INT8RANGE'
  87. ischema_names['int8range'] = INT8RANGE
  88. class NUMRANGE(RangeOperators, sqltypes.TypeEngine):
  89. """Represent the PostgreSQL NUMRANGE type.
  90. .. versionadded:: 0.8.2
  91. """
  92. __visit_name__ = 'NUMRANGE'
  93. ischema_names['numrange'] = NUMRANGE
  94. class DATERANGE(RangeOperators, sqltypes.TypeEngine):
  95. """Represent the PostgreSQL DATERANGE type.
  96. .. versionadded:: 0.8.2
  97. """
  98. __visit_name__ = 'DATERANGE'
  99. ischema_names['daterange'] = DATERANGE
  100. class TSRANGE(RangeOperators, sqltypes.TypeEngine):
  101. """Represent the PostgreSQL TSRANGE type.
  102. .. versionadded:: 0.8.2
  103. """
  104. __visit_name__ = 'TSRANGE'
  105. ischema_names['tsrange'] = TSRANGE
  106. class TSTZRANGE(RangeOperators, sqltypes.TypeEngine):
  107. """Represent the PostgreSQL TSTZRANGE type.
  108. .. versionadded:: 0.8.2
  109. """
  110. __visit_name__ = 'TSTZRANGE'
  111. ischema_names['tstzrange'] = TSTZRANGE