.. seealso::
+ :ref:`change_7156`
+
:ref:`postgresql_ranges`
.. change::
--- /dev/null
+.. change::
+ :tags: usecase, postgresql
+ :tickets: 8690
+
+ Refined the new approach to range objects described at :ref:`change_7156`
+ to accommodate driver-specific range and multirange objects, to better
+ accommodate both legacy code as well as when passing results from raw SQL
+ result sets back into new range or multirange expressions.
Column("value", Float(5).with_variant(oracle.FLOAT(16), "oracle")),
)
+.. _change_7156:
+
+New RANGE / MULTIRANGE support and changes for PostgreSQL backends
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+RANGE / MULTIRANGE support has been fully implemented for psycopg2, psycopg3,
+and asyncpg dialects. The new support uses a new SQLAlchemy-specific
+:class:`_postgresql.Range` object that is agnostic of the different backends
+and does not require the use of backend-specific imports or extension
+steps. For multirange support, lists of :class:`_postgresql.Range`
+objects are used.
+
+Code that used the previous psycopg2-specific types should be modified
+to use :class:`_postgresql.Range`, which presents a compatible interface.
+
+See the documentation at :ref:`postgresql_ranges` for background on
+using the new feature.
+
+
+.. seealso::
+
+ :ref:`postgresql_ranges`
+
.. _change_7086:
``match()`` operator on PostgreSQL uses ``plainto_tsquery()`` rather than ``to_tsquery()``
class _AsyncpgRange(ranges.AbstractRangeImpl):
def bind_processor(self, dialect):
- Range = dialect.dbapi.asyncpg.Range
-
- NoneType = type(None)
+ asyncpg_Range = dialect.dbapi.asyncpg.Range
def to_range(value):
- if not isinstance(value, (str, NoneType)):
- value = Range(
+ if isinstance(value, ranges.Range):
+ value = asyncpg_Range(
value.lower,
value.upper,
lower_inc=value.bounds[0] == "[",
class _AsyncpgMultiRange(ranges.AbstractMultiRangeImpl):
def bind_processor(self, dialect):
- Range = dialect.dbapi.asyncpg.Range
+ asyncpg_Range = dialect.dbapi.asyncpg.Range
NoneType = type(None)
return value
def to_range(value):
- if not isinstance(value, (str, NoneType)):
- value = Range(
+ if isinstance(value, ranges.Range):
+ value = asyncpg_Range(
value.lower,
value.upper,
lower_inc=value.bounds[0] == "[",
class _PsycopgRange(ranges.AbstractRangeImpl):
def bind_processor(self, dialect):
- Range = cast(PGDialect_psycopg, dialect)._psycopg_Range
-
- NoneType = type(None)
+ psycopg_Range = cast(PGDialect_psycopg, dialect)._psycopg_Range
def to_range(value):
- if not isinstance(value, (str, NoneType)):
- value = Range(
+ if isinstance(value, ranges.Range):
+ value = psycopg_Range(
value.lower, value.upper, value.bounds, value.empty
)
return value
class _PsycopgMultiRange(ranges.AbstractMultiRangeImpl):
def bind_processor(self, dialect):
- Range = cast(PGDialect_psycopg, dialect)._psycopg_Range
- Multirange = cast(PGDialect_psycopg, dialect)._psycopg_Multirange
+ psycopg_Range = cast(PGDialect_psycopg, dialect)._psycopg_Range
+ psycopg_Multirange = cast(
+ PGDialect_psycopg, dialect
+ )._psycopg_Multirange
NoneType = type(None)
def to_range(value):
- if isinstance(value, (str, NoneType)):
+ if isinstance(value, (str, NoneType, psycopg_Multirange)):
return value
- return Multirange(
+ return psycopg_Multirange(
[
- Range(
+ psycopg_Range(
element.lower,
element.upper,
element.bounds,
_psycopg2_range_cls = "none"
def bind_processor(self, dialect):
- Range = getattr(
+ psycopg2_Range = getattr(
cast(PGDialect_psycopg2, dialect)._psycopg2_extras,
self._psycopg2_range_cls,
)
- NoneType = type(None)
-
def to_range(value):
- if not isinstance(value, (str, NoneType)):
- value = Range(
+ if isinstance(value, ranges.Range):
+ value = psycopg2_Range(
value.lower, value.upper, value.bounds, value.empty
)
return value
cols = insp.get_columns("data_table")
assert isinstance(cols[0]["type"], self._col_type)
+ def test_textual_round_trip_w_dialect_type(self, connection):
+ """test #8690"""
+ data_table = self.tables.data_table
+
+ data_obj = self._data_obj()
+ connection.execute(
+ self.tables.data_table.insert(), {"range": data_obj}
+ )
+
+ q1 = text("SELECT range from data_table")
+ v = connection.scalar(q1)
+
+ q2 = select(data_table).where(data_table.c.range == v)
+ v2 = connection.scalar(q2)
+
+ eq_(data_obj, v2)
+
def _assert_data(self, conn):
data = conn.execute(select(self.tables.data_table.c.range)).fetchall()
eq_(data, [(self._data_obj(),)])
data = conn.execute(select(self.tables.data_table.c.range)).fetchall()
eq_(data, [(self._data_obj(),)])
+ def test_textual_round_trip_w_dialect_type(self, connection):
+ """test #8690"""
+ data_table = self.tables.data_table
+
+ data_obj = self._data_obj()
+ connection.execute(
+ self.tables.data_table.insert(), {"range": data_obj}
+ )
+
+ q1 = text("SELECT range from data_table")
+ v = connection.scalar(q1)
+
+ q2 = select(data_table).where(data_table.c.range == v)
+ v2 = connection.scalar(q2)
+
+ eq_(data_obj, v2)
+
def test_insert_obj(self, connection):
connection.execute(
self.tables.data_table.insert(), {"range": self._data_obj()}