Logo Search packages:      
Sourcecode: sqlalchemy version File versions  Download package

def sqlalchemy::orm::mapper::Mapper::_instance (   self,
  context,
  row,
  result = None 
) [private]

pulls an object instance from the given row and appends it to the given result
list. if the instance already exists in the given identity map, its not added.  in
either case, executes all the property loaders on the instance to also process extra
information in the row.

Definition at line 1123 of file mapper.py.

01123                                                     :
        """pulls an object instance from the given row and appends it to the given result
        list. if the instance already exists in the given identity map, its not added.  in
        either case, executes all the property loaders on the instance to also process extra
        information in the row."""

        if self.polymorphic_on is not None:
            discriminator = row[self.polymorphic_on]
            mapper = self.polymorphic_map[discriminator]
            if mapper is not self:
                row = self.translate_row(mapper, row)
                return mapper._instance(context, row, result=result)
        
        # look in main identity map.  if its there, we dont do anything to it,
        # including modifying any of its related items lists, as its already
        # been exposed to being modified by the application.
        
        populate_existing = context.populate_existing or self.always_refresh
        identitykey = self.identity_key_from_row(row)
        if context.session.has_key(identitykey):
            instance = context.session._get(identitykey)
            self.__log_debug("_instance(): using existing instance %s identity %s" % (mapperutil.instance_str(instance), str(identitykey)))
            isnew = False
            if context.version_check and self.version_id_col is not None and self.get_attr_by_column(instance, self.version_id_col) != row[self.version_id_col]:
                raise exceptions.ConcurrentModificationError("Instance '%s' version of %s does not match %s" % (instance, self.get_attr_by_column(instance, self.version_id_col), row[self.version_id_col]))
                        
            if populate_existing or context.session.is_expired(instance, unexpire=True):
                if not context.identity_map.has_key(identitykey):
                    context.identity_map[identitykey] = instance
            if self.extension.populate_instance(self, context, row, instance, identitykey, True) is EXT_PASS:
                self.populate_instance(context, instance, row, identitykey, True)
            if self.extension.append_result(self, context, row, instance, identitykey, result, isnew) is EXT_PASS:
                if result is not None:
                    result.append(instance)
            return instance
        else:
            self.__log_debug("_instance(): identity key %s not in session" % str(identitykey) + repr([mapperutil.instance_str(x) for x in context.session]))
        # look in result-local identitymap for it.
        exists = context.identity_map.has_key(identitykey)      
        if not exists:
            if self.allow_null_pks:
                # check if *all* primary key cols in the result are None - this indicates 
                # an instance of the object is not present in the row.  
                for col in self.pks_by_table[self.mapped_table]:
                    if row[col] is not None:
                        break
                else:
                    return None
            else:
                # otherwise, check if *any* primary key cols in the result are None - this indicates 
                # an instance of the object is not present in the row.  
                for col in self.pks_by_table[self.mapped_table]:
                    if row[col] is None:
                        return None
            
            # plugin point
            instance = self.extension.create_instance(self, context, row, self.class_)
            if instance is EXT_PASS:
                instance = self._create_instance(context.session)
            self.__log_debug("_instance(): created new instance %s identity %s" % (mapperutil.instance_str(instance), str(identitykey)))
            context.identity_map[identitykey] = instance
            isnew = True
        else:
            instance = context.identity_map[identitykey]
            isnew = False

        # 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, context, row, instance, identitykey, isnew) is EXT_PASS:
            self.populate_instance(context, instance, row, identitykey, isnew)
        if self.extension.append_result(self, context, row, instance, identitykey, result, isnew) is EXT_PASS:
            if result is not None:
                result.append(instance)
        return instance

    def _create_instance(self, session):


Generated by  Doxygen 1.6.0   Back to index