Ejemplo n.º 1
0
    def instances(self, cursor, *mappers, **kwargs):
        limit = kwargs.get('limit', None)
        offset = kwargs.get('offset', None)

        result = util.HistoryArraySet()
        if len(mappers):
            otherresults = []
            for m in mappers:
                otherresults.append(util.HistoryArraySet())

        imap = {}
        while True:
            row = cursor.fetchone()
            if row is None:
                break
            self._instance(row, imap, result)
            i = 0
            for m in mappers:
                m._instance(row, imap, otherresults[i])
                i += 1

        # store new stuff in the identity map
        for value in imap.values():
            objectstore.get_session().register_clean(value)

        if len(mappers):
            return [result] + otherresults
        else:
            return result
Ejemplo n.º 2
0
 def init(self, *args, **kwargs):
     nohist = kwargs.pop('_mapper_nohistory', False)
     session = kwargs.pop('_sa_session', objectstore.get_session())
     if oldinit is not None:
         try:
             oldinit(self, *args, **kwargs)
         except TypeError, msg:
             # re-raise with the offending class name added to help in debugging
             raise TypeError, '%s.%s' % (self.__class__.__name__,
                                         msg)
Ejemplo n.º 3
0
 def _get(self, key, ident=None):
     try:
         return objectstore.get_session()._get(key)
     except KeyError:
         if ident is None:
             ident = key[2]
         i = 0
         params = {}
         for primary_key in self.pks_by_table[self.primarytable]:
             params["pk_" + primary_key.key] = ident[i]
             i += 1
         try:
             return self.select(self._get_clause, params=params)[0]
         except IndexError:
             return None
Ejemplo n.º 4
0
        def init(self, *args, **kwargs):
            self._entity_name = kwargs.pop('_sa_entity_name', None)

            # this gets the AttributeManager to do some pre-initialization,
            # in order to save on KeyErrors later on
            objectstore.global_attributes.init_attr(self)

            nohist = kwargs.pop('_mapper_nohistory', False)
            session = kwargs.pop('_sa_session', objectstore.get_session())
            if not nohist:
                # register new with the correct session, before the object's
                # constructor is called, since further assignments within the
                # constructor would otherwise bind it to whatever get_session() is.
                session.register_new(self)
            if oldinit is not None:
                oldinit(self, *args, **kwargs)
Ejemplo n.º 5
0
    def instances(self, cursor, *mappers, **kwargs):
        """given a cursor (ResultProxy) from an SQLEngine, returns a list of object instances
        corresponding to the rows in the cursor."""
        limit = kwargs.get('limit', None)
        offset = kwargs.get('offset', None)
        session = kwargs.get('session', None)
        if session is None:
            session = objectstore.get_session()
        populate_existing = kwargs.get('populate_existing', False)

        result = util.HistoryArraySet()
        if mappers:
            otherresults = []
            for m in mappers:
                otherresults.append(util.HistoryArraySet())

        imap = {}
        while True:
            row = cursor.fetchone()
            if row is None:
                break
            self._instance(session,
                           row,
                           imap,
                           result,
                           populate_existing=populate_existing)
            i = 0
            for m in mappers:
                m._instance(session, row, imap, otherresults[i])
                i += 1

        # store new stuff in the identity map
        for value in imap.values():
            session.register_clean(value)

        if mappers:
            result = [result] + otherresults
        return result
Ejemplo n.º 6
0
 def _get_session(self):
     if self._session is None:
         return objectstore.get_session()
     else:
         return self._session
Ejemplo n.º 7
0
    def _instance(self, row, imap, result=None, populate_existing=False):
        """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."""

        # 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.
        identitykey = self._identity_key(row)
        if objectstore.get_session().has_key(identitykey):
            instance = objectstore.get_session()._get(identitykey)

            isnew = False
            if populate_existing:
                isnew = not imap.has_key(identitykey)
                if isnew:
                    imap[identitykey] = instance
                for prop in self.props.values():
                    prop.execute(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

        # look in result-local identitymap for it.
        exists = imap.has_key(identitykey)
        if not exists:
            # check if 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.table]:
                if row[col] is None:
                    return None
            # plugin point
            instance = self.extension.create_instance(self, row, imap,
                                                      self.class_)
            if instance is None:
                instance = self.class_(_mapper_nohistory=True)
            instance._instance_key = identitykey

            imap[identitykey] = instance
            isnew = True
        else:
            instance = imap[identitykey]
            isnew = False

        # plugin point

        # call further mapper properties on the row, to pull further
        # instances from the row and possibly populate this item.
        for prop in self.props.values():
            prop.execute(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
Ejemplo n.º 8
0
 def using(self, session):
     """returns a new Query object with the given Session."""
     if objectstore.get_session() is session:
         return self.query
     else:
         return query.Query(self, session=session)