diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-06-15 12:42:44 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-06-15 14:32:53 -0400 |
| commit | 46c0fa56e904f6a00e56343302c4cb39955fa038 (patch) | |
| tree | 7417ab317303299916ed7f2a6843bc5ce5e1634a /lib/sqlalchemy/sql | |
| parent | 6d889b03dcd42b531001aeec2737949dca41d6d8 (diff) | |
| download | sqlalchemy-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/sql')
| -rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 58 |
1 files changed, 58 insertions, 0 deletions
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.""" |
