summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/postgresql/array.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-06-15 12:42:44 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-06-15 14:32:53 -0400
commit46c0fa56e904f6a00e56343302c4cb39955fa038 (patch)
tree7417ab317303299916ed7f2a6843bc5ce5e1634a /lib/sqlalchemy/dialects/postgresql/array.py
parent6d889b03dcd42b531001aeec2737949dca41d6d8 (diff)
downloadsqlalchemy-46c0fa56e904f6a00e56343302c4cb39955fa038.tar.gz
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
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/array.py')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/array.py51
1 files changed, 20 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,