]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
improved translation of rows when proxying rows from one mapper to another.
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 26 Mar 2006 07:21:28 +0000 (07:21 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 26 Mar 2006 07:21:28 +0000 (07:21 +0000)
examples/polymorph/polymorph2.py
lib/sqlalchemy/mapping/mapper.py

index eebac80c1812d22739ad2ce6b6a71fa134233b76..fb9504fbeddd7e10e4338b38942bdc989fcc3058 100644 (file)
@@ -73,7 +73,8 @@ person_join = select(
                     column("'engineer'").label('type')
                 ],
             people.c.person_id==engineers.c.person_id)).alias('pjoin')
-            
+
+print [c for c in person_join.c]            
     
 # MapperExtension object.
 class PersonLoader(MapperExtension):
@@ -87,10 +88,10 @@ class PersonLoader(MapperExtension):
             
     def populate_instance(self, mapper, instance, row, identitykey, imap, isnew):
         if row[person_join.c.type] =='engineer':
-            Engineer.mapper.populate_instance(instance, row, identitykey, imap, isnew)
+            Engineer.mapper.populate_instance(instance, row, identitykey, imap, isnew, frommapper=mapper)
             return False
         elif row[person_join.c.type] =='manager':
-            Manager.mapper.populate_instance(instance, row, identitykey, imap, isnew)
+            Manager.mapper.populate_instance(instance, row, identitykey, imap, isnew, frommapper=mapper)
             return False
         else:
             return True
index 32da013ac71833864b386285ca6f5bc0032c5be5..82257ae6239ccf256e14ac6307df7529e69e5f96 100644 (file)
@@ -854,19 +854,27 @@ class Mapper(object):
         # call further mapper properties on the row, to pull further 
         # instances from the row and possibly populate this item.
         if self.extension.populate_instance(self, instance, row, identitykey, imap, isnew):
-            self.populate_instance(instance, row, identitykey, imap, isnew, translate=False)
+            self.populate_instance(instance, row, identitykey, imap, isnew)
         if self.extension.append_result(self, row, imap, result, instance, isnew, populate_existing=populate_existing):
             if result is not None:
                 result.append_nohistory(instance)
         return instance
 
-    def populate_instance(self, instance, row, identitykey, imap, isnew, translate=True):
-        if translate:
-            newrow = {}
-            for table in self.tables:
-                for c in table.c:
-                    newrow[c] = row[c.key]
-            row = newrow
+    def translate_row(self, tomapper, row):
+        """attempts to take a row and translate its values to a row that can
+        be understood by another mapper.  breaks the column references down to their
+        bare keynames to accomplish this.  So far this works for the various polymorphic
+        examples."""
+        newrow = util.DictDecorator(row)
+        for c in self.table.c:
+            newrow[c.key] = row[c]
+        for c in tomapper.table.c:
+            newrow[c] = newrow[c.key]
+        return newrow
+        
+    def populate_instance(self, instance, row, identitykey, imap, isnew, frommapper=None):
+        if frommapper is not None:
+            row = frommapper.translate_row(self, row)
             
         for prop in self.props.values():
             prop.execute(instance, row, identitykey, imap, isnew)