]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added temporary option "construct_new" to mapper which will cause the mapper to use...
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 23 Apr 2006 20:37:53 +0000 (20:37 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 23 Apr 2006 20:37:53 +0000 (20:37 +0000)
lib/sqlalchemy/mapping/mapper.py

index 302cb174112f393a137d2d3bbb2d9a44aa3a8791..4a26a4f38aec2cc683392d0acd44390b731dacb0 100644 (file)
@@ -49,6 +49,7 @@ class Mapper(object):
                 entity_name = None,
                 always_refresh = False,
                 version_id_col = None,
+                construct_new = False,
                 **kwargs):
 
         if primarytable is not None:
@@ -73,6 +74,7 @@ class Mapper(object):
         self._options = {}
         self.always_refresh = always_refresh
         self.version_id_col = version_id_col
+        self.construct_new = construct_new
         
         if not issubclass(class_, object):
             raise ArgumentError("Class '%s' is not a new-style class" % class_.__name__)
@@ -724,7 +726,7 @@ class Mapper(object):
             # plugin point
             instance = self.extension.create_instance(self, row, imap, self.class_)
             if instance is EXT_PASS:
-                instance = self.class_(_mapper_nohistory=True, _sa_entity_name=self.entity_name, _sa_session=session)
+                instance = self._create_instance(session)
             imap[identitykey] = instance
             isnew = True
         else:
@@ -742,6 +744,21 @@ class Mapper(object):
                 result.append_nohistory(instance)
         return instance
 
+    def _create_instance(self, session):
+        if not self.construct_new:
+            return self.class_(_mapper_nohistory=True, _sa_entity_name=self.entity_name, _sa_session=session)
+
+        obj = self.class_.__new__(self.class_)
+        obj._entity_name = self.entity_name
+
+        # this gets the AttributeManager to do some pre-initialization,
+        # in order to save on KeyErrors later on
+        objectstore.global_attributes.init_attr(obj)
+
+        session._bind_to(obj)
+
+        return obj
+
     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