From 46c0fa56e904f6a00e56343302c4cb39955fa038 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 15 Jun 2022 12:42:44 -0400 Subject: implement literal stringification for arrays as we already implement stringification for the contents, provide a bracketed syntax for default and ARRAY literal for PG specifically. ARRAY literal seems much simpler to render than their quoted syntax which requires double quotes for strings. also open up testing for pg8000 which has likely been fine with arrays for awhile now, bump the version pin also. Fixes: #8138 Change-Id: Id85b052b0a9564d6aa1489160e58b7359f130fdd --- lib/sqlalchemy/dialects/postgresql/array.py | 51 +++++++++++------------------ 1 file changed, 20 insertions(+), 31 deletions(-) (limited to 'lib/sqlalchemy/dialects/postgresql/array.py') 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, -- cgit v1.2.1