summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-08-29 20:10:54 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-08-29 20:10:54 -0400
commit04f0e11051f0f00cbb64dd37dfa287e6173463ba (patch)
treee1331bfa8047ca41b68fb86c2ec51a2b6ff876f9
parenta52f84d4d8419b1d0839d7d4432df4a868e21ff8 (diff)
downloadsqlalchemy-04f0e11051f0f00cbb64dd37dfa287e6173463ba.tar.gz
- do the polymorphic thing as a decorator so it's out of the way otherwise
-rw-r--r--lib/sqlalchemy/orm/loading.py53
1 files changed, 22 insertions, 31 deletions
diff --git a/lib/sqlalchemy/orm/loading.py b/lib/sqlalchemy/orm/loading.py
index 28843a528..11d0055d6 100644
--- a/lib/sqlalchemy/orm/loading.py
+++ b/lib/sqlalchemy/orm/loading.py
@@ -230,12 +230,6 @@ def instance_processor(mapper, context, result, path, adapter,
pk_cols = mapper.primary_key
- if polymorphic_from or refresh_state:
- polymorphic_switch = None
- else:
- polymorphic_switch = _polymorphic_switch(
- context, mapper, result, path, polymorphic_discriminator, adapter)
-
version_id_col = mapper.version_id_col
if adapter:
@@ -289,14 +283,6 @@ def instance_processor(mapper, context, result, path, adapter,
def _instance(row):
- # if we are doing polymorphic, dispatch
- # to a different _instance() method specific to
- # the subclass mapper
- if polymorphic_switch is not None:
- result = polymorphic_switch(row)
- if result is not False:
- return result
-
# determine the state that we'll be populating
if refresh_identity_key:
# fixed state that we're refreshing
@@ -414,6 +400,14 @@ def instance_processor(mapper, context, result, path, adapter,
state._commit(dict_, to_load)
return instance
+
+ if not polymorphic_from and not refresh_state:
+ # if we are doing polymorphic, dispatch to a different _instance()
+ # method specific to the subclass mapper
+ _instance = _decorate_polymorphic_switch(
+ _instance, context, mapper, result, path,
+ polymorphic_discriminator, adapter)
+
return _instance
@@ -490,14 +484,18 @@ def _populate_partial(
return to_load
-def _polymorphic_switch(
- context, mapper, result, path, polymorphic_discriminator, adapter):
+def _decorate_polymorphic_switch(
+ instance_fn, context, mapper, result, path,
+ polymorphic_discriminator, adapter):
if polymorphic_discriminator is not None:
polymorphic_on = polymorphic_discriminator
else:
polymorphic_on = mapper.polymorphic_on
if polymorphic_on is None:
- return None
+ return instance_fn
+
+ if adapter:
+ polymorphic_on = adapter.columns[polymorphic_on]
def configure_subclass_mapper(discriminator):
try:
@@ -506,32 +504,25 @@ def _polymorphic_switch(
raise AssertionError(
"No such polymorphic_identity %r is defined" %
discriminator)
- if sub_mapper is mapper:
- return None
+ else:
+ if sub_mapper is mapper:
+ return None
- return instance_processor(
- sub_mapper,
- context,
- result,
- path,
- adapter,
- polymorphic_from=mapper)
+ return instance_processor(
+ sub_mapper, context, result,
+ path, adapter, polymorphic_from=mapper)
polymorphic_instances = util.PopulateDict(
configure_subclass_mapper
)
- if adapter:
- polymorphic_on = adapter.columns[polymorphic_on]
-
def polymorphic_instance(row):
discriminator = row[polymorphic_on]
if discriminator is not None:
_instance = polymorphic_instances[discriminator]
if _instance:
return _instance(row)
- else:
- return False
+ return instance_fn(row)
return polymorphic_instance