diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-10-03 17:36:27 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-10-07 23:06:06 -0400 |
commit | 65aee6cce57fd1cca3a95814feff3ed99a5a51ee (patch) | |
tree | 0352d74938902a9242dfb97ca5215d9191a2ad16 /lib/sqlalchemy/ext/compiler.py | |
parent | ebd9788c986c56b8b845fa83609a6eb2c0cef083 (diff) | |
download | sqlalchemy-65aee6cce57fd1cca3a95814feff3ed99a5a51ee.tar.gz |
Add result map targeting for custom compiled, text objects
In order for text(), custom compiled objects, etc. to be usable
by Query(), they are all targeted by object key in the result map.
As we no longer want Query to implicitly label these, as well as that
text() has no label feature, support adding entries to the result
map that have no name, key, or type, only the object itself, and
then ensure that the compiler sets up for positional targeting
when this condition is detected.
Allows for more flexible ORM query usage with custom expressions
and text() while having less special logic in query itself.
Fixes: #4887
Change-Id: Ie073da127d292d43cb132a2b31bc90af88bfe2fd
Diffstat (limited to 'lib/sqlalchemy/ext/compiler.py')
-rw-r--r-- | lib/sqlalchemy/ext/compiler.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/sqlalchemy/ext/compiler.py b/lib/sqlalchemy/ext/compiler.py index 572c62b8e..4a5a8ba9c 100644 --- a/lib/sqlalchemy/ext/compiler.py +++ b/lib/sqlalchemy/ext/compiler.py @@ -398,6 +398,7 @@ Example usage:: """ from .. import exc +from ..sql import sqltypes from ..sql import visitors @@ -475,4 +476,22 @@ class _dispatcher(object): "compilation handler." % type(element) ) - return fn(element, compiler, **kw) + # if compilation includes add_to_result_map, collect add_to_result_map + # arguments from the user-defined callable, which are probably none + # because this is not public API. if it wasn't called, then call it + # ourselves. + arm = kw.get("add_to_result_map", None) + if arm: + arm_collection = [] + kw["add_to_result_map"] = lambda *args: arm_collection.append(args) + + expr = fn(element, compiler, **kw) + + if arm: + if not arm_collection: + arm_collection.append( + (None, None, (element,), sqltypes.NULLTYPE) + ) + for tup in arm_collection: + arm(*tup) + return expr |