diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2022-06-16 02:30:04 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2022-06-16 02:30:04 +0000 |
| commit | efd4e6dd8a73b956c860d796606f8e6ad652c292 (patch) | |
| tree | fc4e387f37e421d54068310eab5a01facb5f91c8 /lib/sqlalchemy | |
| parent | 964c26feecc7607d6d3a66240c3f33f4ae9215d4 (diff) | |
| parent | 46c0fa56e904f6a00e56343302c4cb39955fa038 (diff) | |
| download | sqlalchemy-efd4e6dd8a73b956c860d796606f8e6ad652c292.tar.gz | |
Merge "implement literal stringification for arrays" into main
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/array.py | 51 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 58 | ||||
| -rw-r--r-- | lib/sqlalchemy/testing/suite/test_types.py | 57 |
3 files changed, 135 insertions, 31 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/array.py b/lib/sqlalchemy/dialects/postgresql/array.py index 3b5eaed30..515eb2d15 100644 --- a/lib/sqlalchemy/dialects/postgresql/array.py +++ b/lib/sqlalchemy/dialects/postgresql/array.py @@ -310,35 +310,6 @@ class ARRAY(sqltypes.ARRAY): def compare_values(self, x, y): return x == y - def _proc_array(self, arr, itemproc, dim, collection): - if dim is None: - arr = list(arr) - if ( - dim == 1 - or dim is None - and ( - # this has to be (list, tuple), or at least - # not hasattr('__iter__'), since Py3K strings - # etc. have __iter__ - not arr - or not isinstance(arr[0], (list, tuple)) - ) - ): - if itemproc: - return collection(itemproc(x) for x in arr) - else: - return collection(arr) - else: - return collection( - self._proc_array( - x, - itemproc, - dim - 1 if dim is not None else None, - collection, - ) - for x in arr - ) - @util.memoized_property def _against_native_enum(self): return ( @@ -346,6 +317,24 @@ class ARRAY(sqltypes.ARRAY): and self.item_type.native_enum ) + def literal_processor(self, dialect): + item_proc = self.item_type.dialect_impl(dialect).literal_processor( + dialect + ) + if item_proc is None: + return None + + def to_str(elements): + return f"ARRAY[{', '.join(elements)}]" + + def process(value): + inner = self._apply_item_processor( + value, item_proc, self.dimensions, to_str + ) + return inner + + return process + def bind_processor(self, dialect): item_proc = self.item_type.dialect_impl(dialect).bind_processor( dialect @@ -355,7 +344,7 @@ class ARRAY(sqltypes.ARRAY): if value is None: return value else: - return self._proc_array( + return self._apply_item_processor( value, item_proc, self.dimensions, list ) @@ -370,7 +359,7 @@ class ARRAY(sqltypes.ARRAY): if value is None: return value else: - return self._proc_array( + return self._apply_item_processor( value, item_proc, self.dimensions, diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 32f0813f5..b4b444f23 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -2964,6 +2964,64 @@ class ARRAY( if isinstance(self.item_type, SchemaEventTarget): self.item_type._set_parent_with_dispatch(parent) + def literal_processor(self, dialect): + item_proc = self.item_type.dialect_impl(dialect).literal_processor( + dialect + ) + if item_proc is None: + return None + + def to_str(elements): + return f"[{', '.join(elements)}]" + + def process(value): + inner = self._apply_item_processor( + value, item_proc, self.dimensions, to_str + ) + return inner + + return process + + def _apply_item_processor(self, arr, itemproc, dim, collection_callable): + """Helper method that can be used by bind_processor(), + literal_processor(), etc. to apply an item processor to elements of + an array value, taking into account the 'dimensions' for this + array type. + + See the Postgresql ARRAY datatype for usage examples. + + .. versionadded:: 2.0 + + """ + + if dim is None: + arr = list(arr) + if ( + dim == 1 + or dim is None + and ( + # this has to be (list, tuple), or at least + # not hasattr('__iter__'), since Py3K strings + # etc. have __iter__ + not arr + or not isinstance(arr[0], (list, tuple)) + ) + ): + if itemproc: + return collection_callable(itemproc(x) for x in arr) + else: + return collection_callable(arr) + else: + return collection_callable( + self._apply_item_processor( + x, + itemproc, + dim - 1 if dim is not None else None, + collection_callable, + ) + for x in arr + ) + class TupleType(TypeEngine[Tuple[Any, ...]]): """represent the composite type of a Tuple.""" diff --git a/lib/sqlalchemy/testing/suite/test_types.py b/lib/sqlalchemy/testing/suite/test_types.py index 391379956..9461298b9 100644 --- a/lib/sqlalchemy/testing/suite/test_types.py +++ b/lib/sqlalchemy/testing/suite/test_types.py @@ -17,6 +17,7 @@ from ..config import requirements from ..schema import Column from ..schema import Table from ... import and_ +from ... import ARRAY from ... import BigInteger from ... import bindparam from ... import Boolean @@ -222,6 +223,61 @@ class UnicodeTextTest(_UnicodeFixture, fixtures.TablesTest): self._test_null_strings(connection) +class ArrayTest(_LiteralRoundTripFixture, fixtures.TablesTest): + """Add ARRAY test suite, #8138. + + This only works on PostgreSQL right now. + + """ + + __requires__ = ("array_type",) + __backend__ = True + + @classmethod + def define_tables(cls, metadata): + Table( + "array_table", + metadata, + Column( + "id", Integer, primary_key=True, test_needs_autoincrement=True + ), + Column("single_dim", ARRAY(Integer)), + Column("multi_dim", ARRAY(String, dimensions=2)), + ) + + def test_array_roundtrip(self, connection): + array_table = self.tables.array_table + + connection.execute( + array_table.insert(), + { + "id": 1, + "single_dim": [1, 2, 3], + "multi_dim": [["one", "two"], ["thr'ee", "réve🐍 illé"]], + }, + ) + row = connection.execute( + select(array_table.c.single_dim, array_table.c.multi_dim) + ).first() + eq_(row, ([1, 2, 3], [["one", "two"], ["thr'ee", "réve🐍 illé"]])) + + def test_literal_simple(self, literal_round_trip): + literal_round_trip( + ARRAY(Integer), + ([1, 2, 3],), + ([1, 2, 3],), + support_whereclause=False, + ) + + def test_literal_complex(self, literal_round_trip): + literal_round_trip( + ARRAY(String, dimensions=2), + ([["one", "two"], ["thr'ee", "réve🐍 illé"]],), + ([["one", "two"], ["thr'ee", "réve🐍 illé"]],), + support_whereclause=False, + ) + + class BinaryTest(_LiteralRoundTripFixture, fixtures.TablesTest): __requires__ = ("binary_literals",) __backend__ = True @@ -1779,6 +1835,7 @@ class NativeUUIDTest(UuidTest): __all__ = ( + "ArrayTest", "BinaryTest", "UnicodeVarcharTest", "UnicodeTextTest", |
