def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        if not self.is_class_level:
            def new_execute(state, dict_, row, **flags):
                # we are not the primary manager for this attribute on this class - set up a
                # per-instance lazyloader, which will override the class-level behavior.
                # this currently only happens when using a "lazyload" option on a "no load"
                # attribute - "eager" attributes always have a class-level lazyloader
                # installed.
                self._init_instance_attribute(state, callable_=LoadLazyAttribute(state, self.key))

            if self._should_log_debug:
                new_execute = self.debug_callable(new_execute, self.logger, None,
                    lambda state, dict_, row, **flags: "set instance-level lazy loader on %s" % \
                                                      mapperutil.state_attribute_str(state,
                                                                                     self.key)
                )

            return (new_execute, None)
        else:
            def new_execute(state, dict_, row, **flags):
                # we are the primary manager for this attribute on this class - reset its
                # per-instance attribute state, so that the class-level lazy loader is
                # executed when next referenced on this instance.  this is needed in
                # populate_existing() types of scenarios to reset any existing state.
                state.reset(self.key, dict_)

            if self._should_log_debug:
                new_execute = self.debug_callable(new_execute, self.logger, None,
                    lambda state, dict_, row, **flags: "set class-level lazy loader on %s" % \
                                                      mapperutil.state_attribute_str(state,
                                                                                     self.key)
                )

            return (new_execute, None)
Beispiel #2
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        key, col = self.key, self.columns[0]
        if adapter:
            col = adapter.columns[col]
        if col in row:

            def new_execute(state, row, **flags):
                state.dict[key] = row[col]

            if self._should_log_debug:
                new_execute = self.debug_callable(
                    new_execute, self.logger,
                    "%s returning active column fetcher" % self,
                    lambda state, row, **flags: "%s populating %s" %
                    (self, mapperutil.state_attribute_str(state, key)))
            return (new_execute, None)
        else:

            def new_execute(state, row, isnew, **flags):
                if isnew:
                    state.expire_attributes([key])

            if self._should_log_debug:
                self.logger.debug("%s deferring load" % self)
            return (new_execute, None)
Beispiel #3
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        key, columns, composite_class = self.key, self.columns, self.parent_property.composite_class
        if adapter:
            columns = [adapter.columns[c] for c in columns]
        for c in columns:
            if c not in row:

                def new_execute(state, row, isnew, **flags):
                    if isnew:
                        state.expire_attributes([key])

                if self._should_log_debug:
                    self.logger.debug("%s deferring load" % self)
                return (new_execute, None)
        else:

            def new_execute(state, row, **flags):
                state.dict[key] = composite_class(*[row[c] for c in columns])

            if self._should_log_debug:
                new_execute = self.debug_callable(
                    new_execute, self.logger,
                    "%s returning active composite column fetcher" % self,
                    lambda state, row, **flags: "populating %s" %
                    (mapperutil.state_attribute_str(state, key)))

            return (new_execute, None)
Beispiel #4
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        col = self.columns[0]
        if adapter:
            col = adapter.columns[col]
        if col in row:
            return self.parent_property._get_strategy(ColumnLoader).create_row_processor(
                selectcontext, path, mapper, row, adapter
            )

        elif not self.is_class_level or len(selectcontext.options):

            def new_execute(state, row, **flags):
                state.set_callable(self.key, self.setup_loader(state))

        else:

            def new_execute(state, row, **flags):
                state.reset(self.key)

        if self._should_log_debug:
            new_execute = self.debug_callable(
                new_execute,
                self.logger,
                None,
                lambda state, row, **flags: "set deferred callable on %s"
                % mapperutil.state_attribute_str(state, self.key),
            )
        return (new_execute, None)
Beispiel #5
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        key, columns, composite_class = self.key, self.columns, self.parent_property.composite_class
        if adapter:
            columns = [adapter.columns[c] for c in columns]
        for c in columns:
            if c not in row:

                def new_execute(state, row, isnew, **flags):
                    if isnew:
                        state.expire_attributes([key])

                if self._should_log_debug:
                    self.logger.debug("%s deferring load" % self)
                return (new_execute, None)
        else:

            def new_execute(state, row, **flags):
                state.dict[key] = composite_class(*[row[c] for c in columns])

            if self._should_log_debug:
                new_execute = self.debug_callable(
                    new_execute,
                    self.logger,
                    "%s returning active composite column fetcher" % self,
                    lambda state, row, **flags: "populating %s" % (mapperutil.state_attribute_str(state, key)),
                )

            return (new_execute, None)
Beispiel #6
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        key, col = self.key, self.columns[0]
        if adapter:
            col = adapter.columns[col]
        if col in row:

            def new_execute(state, row, **flags):
                state.dict[key] = row[col]

            if self._should_log_debug:
                new_execute = self.debug_callable(
                    new_execute,
                    self.logger,
                    "%s returning active column fetcher" % self,
                    lambda state, row, **flags: "%s populating %s" % (self, mapperutil.state_attribute_str(state, key)),
                )
            return (new_execute, None)
        else:

            def new_execute(state, row, isnew, **flags):
                if isnew:
                    state.expire_attributes([key])

            if self._should_log_debug:
                self.logger.debug("%s deferring load" % self)
            return (new_execute, None)
Beispiel #7
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        col = self.columns[0]
        if adapter:
            col = adapter.columns[col]
        if col in row:
            return self.parent_property._get_strategy(
                ColumnLoader).create_row_processor(selectcontext, path, mapper,
                                                   row, adapter)

        elif not self.is_class_level:

            def new_execute(state, dict_, row, **flags):
                state.set_callable(self.key,
                                   LoadDeferredColumns(state, self.key))
        else:

            def new_execute(state, dict_, row, **flags):
                # reset state on the key so that deferred callables
                # fire off on next access.
                state.reset(self.key, dict_)

        if self._should_log_debug:
            new_execute = self.debug_callable(new_execute, self.logger, None,
                lambda state, dict_, row, **flags: "set deferred callable on %s" % \
                                                  mapperutil.state_attribute_str(state, self.key)
            )
        return (new_execute, None)
Beispiel #8
0
    def __call__(self):
        state = self.state
        if not mapper._state_has_identity(state):
            return None

        instance_mapper = mapper._state_mapper(state)
        prop = instance_mapper.get_property(self.key)
        strategy = prop._get_strategy(LazyLoader)

        if strategy._should_log_debug:
            strategy.logger.debug(
                "loading %s" % mapperutil.state_attribute_str(state, self.key))

        session = sessionlib._state_session(state)
        if session is None:
            raise sa_exc.UnboundExecutionError(
                "Parent instance %s is not bound to a Session; "
                "lazy load operation of attribute '%s' cannot proceed" %
                (mapperutil.state_str(state), self.key))

        q = session.query(prop.mapper)._adapt_all_clauses()

        if self.path:
            q = q._with_current_path(self.path)

        # if we have a simple primary key load, use mapper.get()
        # to possibly save a DB round trip
        if strategy.use_get:
            ident = []
            allnulls = True
            for primary_key in prop.mapper.primary_key:
                val = instance_mapper._get_committed_state_attr_by_column(
                    state, strategy._equated_columns[primary_key])
                allnulls = allnulls and val is None
                ident.append(val)
            if allnulls:
                return None
            if self.options:
                q = q._conditional_options(*self.options)
            return q.get(ident)

        if prop.order_by:
            q = q.order_by(*util.to_list(prop.order_by))

        if self.options:
            q = q._conditional_options(*self.options)
        q = q.filter(strategy.lazy_clause(state))

        result = q.all()
        if strategy.uselist:
            return result
        else:
            if result:
                return result[0]
            else:
                return None
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        def new_execute(state, dict_, row, **flags):
            self._init_instance_attribute(state)

        if self._should_log_debug:
            new_execute = self.debug_callable(new_execute, self.logger, None,
                lambda state, dict_, row, **flags: "initializing blank scalar/collection on %s" % \
                                                  mapperutil.state_attribute_str(state, self.key)
            )
        return (new_execute, None)
Beispiel #10
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        def new_execute(state, dict_, row, **flags):
            self._init_instance_attribute(state)

        if self._should_log_debug:
            new_execute = self.debug_callable(new_execute, self.logger, None,
                lambda state, dict_, row, **flags: "initializing blank scalar/collection on %s" % \
                                                  mapperutil.state_attribute_str(state, self.key)
            )
        return (new_execute, None)
Beispiel #11
0
    def __call__(self):
        state = self.state
        if not mapper._state_has_identity(state):
            return None

        instance_mapper = mapper._state_mapper(state)
        prop = instance_mapper.get_property(self.key)
        strategy = prop._get_strategy(LazyLoader)
        
        if strategy._should_log_debug:
            strategy.logger.debug("loading %s" % mapperutil.state_attribute_str(state, self.key))

        session = sessionlib._state_session(state)
        if session is None:
            raise sa_exc.UnboundExecutionError(
                "Parent instance %s is not bound to a Session; "
                "lazy load operation of attribute '%s' cannot proceed" % 
                (mapperutil.state_str(state), self.key)
            )
        
        q = session.query(prop.mapper)._adapt_all_clauses()
        
        if self.path:
            q = q._with_current_path(self.path)
            
        # if we have a simple primary key load, use mapper.get()
        # to possibly save a DB round trip
        if strategy.use_get:
            ident = []
            allnulls = True
            for primary_key in prop.mapper.primary_key: 
                val = instance_mapper._get_committed_state_attr_by_column(state, strategy._equated_columns[primary_key])
                allnulls = allnulls and val is None
                ident.append(val)
            if allnulls:
                return None
            if self.options:
                q = q._conditional_options(*self.options)
            return q.get(ident)

        if prop.order_by:
            q = q.order_by(*util.to_list(prop.order_by))

        if self.options:
            q = q._conditional_options(*self.options)
        q = q.filter(strategy.lazy_clause(state))

        result = q.all()
        if strategy.uselist:
            return result
        else:
            if result:
                return result[0]
            else:
                return None
Beispiel #12
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        if not self.is_class_level or len(selectcontext.options):
            path = path + (self.key,)

            def new_execute(state, row, **flags):
                # we are not the primary manager for this attribute on this class - set up a per-instance lazyloader,
                # which will override the class-level behavior
                self._init_instance_attribute(
                    state,
                    callable_=self.setup_loader(state, selectcontext.options, selectcontext.query._current_path + path),
                )

            if self._should_log_debug:
                new_execute = self.debug_callable(
                    new_execute,
                    self.logger,
                    None,
                    lambda state, row, **flags: "set instance-level lazy loader on %s"
                    % mapperutil.state_attribute_str(state, self.key),
                )

            return (new_execute, None)
        else:

            def new_execute(state, row, **flags):
                # we are the primary manager for this attribute on this class - reset its per-instance attribute state,
                # so that the class-level lazy loader is executed when next referenced on this instance.
                # this usually is not needed unless the constructor of the object referenced the attribute before we got
                # to load data into it.
                state.reset(self.key)

            if self._should_log_debug:
                new_execute = self.debug_callable(
                    new_execute,
                    self.logger,
                    None,
                    lambda state, row, **flags: "set class-level lazy loader on %s"
                    % mapperutil.state_attribute_str(state, self.key),
                )

            return (new_execute, None)
Beispiel #13
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        if not self.is_class_level:

            def new_execute(state, dict_, row, **flags):
                # we are not the primary manager for this attribute on this class - set up a
                # per-instance lazyloader, which will override the class-level behavior.
                # this currently only happens when using a "lazyload" option on a "no load"
                # attribute - "eager" attributes always have a class-level lazyloader
                # installed.
                self._init_instance_attribute(state,
                                              callable_=LoadLazyAttribute(
                                                  state, self.key))

            if self._should_log_debug:
                new_execute = self.debug_callable(new_execute, self.logger, None,
                    lambda state, dict_, row, **flags: "set instance-level lazy loader on %s" % \
                                                      mapperutil.state_attribute_str(state,
                                                                                     self.key)
                )

            return (new_execute, None)
        else:

            def new_execute(state, dict_, row, **flags):
                # we are the primary manager for this attribute on this class - reset its
                # per-instance attribute state, so that the class-level lazy loader is
                # executed when next referenced on this instance.  this is needed in
                # populate_existing() types of scenarios to reset any existing state.
                state.reset(self.key, dict_)

            if self._should_log_debug:
                new_execute = self.debug_callable(new_execute, self.logger, None,
                    lambda state, dict_, row, **flags: "set class-level lazy loader on %s" % \
                                                      mapperutil.state_attribute_str(state,
                                                                                     self.key)
                )

            return (new_execute, None)
Beispiel #14
0
    def __call__(self, **kw):
        if kw.get('passive') is attributes.PASSIVE_NO_FETCH:
            return attributes.PASSIVE_NO_RESULT

        state = self.state
        
        localparent = mapper._state_mapper(state)
        
        prop = localparent.get_property(self.key)
        strategy = prop._get_strategy(DeferredColumnLoader)

        if strategy.group:
            toload = [
                    p.key for p in 
                    localparent.iterate_properties 
                    if isinstance(p, StrategizedProperty) and 
                      isinstance(p.strategy, DeferredColumnLoader) and 
                      p.group==strategy.group
                    ]
        else:
            toload = [self.key]

        # narrow the keys down to just those which have no history
        group = [k for k in toload if k in state.unmodified]

        if strategy._should_log_debug():
            strategy.logger.debug(
                    "deferred load %s group %s",
                    (mapperutil.state_attribute_str(state, self.key), 
                    group and ','.join(group) or 'None')
            )

        session = sessionlib._state_session(state)
        if session is None:
            raise orm_exc.DetachedInstanceError(
                "Parent instance %s is not bound to a Session; "
                "deferred load operation of attribute '%s' cannot proceed" % 
                (mapperutil.state_str(state), self.key)
                )

        query = session.query(localparent)
        ident = state.key[1]
        query._get(None, ident=ident, 
                    only_load_props=group, refresh_state=state)
        return attributes.ATTR_WAS_SET
Beispiel #15
0
    def __call__(self):
        state = self.state

        if not mapper._state_has_identity(state):
            return None

        localparent = mapper._state_mapper(state)

        prop = localparent.get_property(self.key)
        strategy = prop._get_strategy(DeferredColumnLoader)

        if self.keys:
            toload = self.keys
        elif strategy.group:
            toload = [
                p.key for p in localparent.iterate_properties
                if isinstance(p, StrategizedProperty)
                and isinstance(p.strategy, DeferredColumnLoader)
                and p.group == strategy.group
            ]
        else:
            toload = [self.key]

        # narrow the keys down to just those which have no history
        group = [k for k in toload if k in state.unmodified]

        if strategy._should_log_debug:
            strategy.logger.debug(
                "deferred load %s group %s" % (mapperutil.state_attribute_str(
                    state, self.key), group and ','.join(group) or 'None'))

        session = sessionlib._state_session(state)
        if session is None:
            raise sa_exc.UnboundExecutionError(
                "Parent instance %s is not bound to a Session; deferred load operation of attribute '%s' cannot proceed"
                % (mapperutil.state_str(state), self.key))

        query = session.query(localparent)
        ident = state.key[1]
        query._get(None,
                   ident=ident,
                   only_load_props=group,
                   refresh_state=state)
        return attributes.ATTR_WAS_SET
Beispiel #16
0
    def __call__(self):
        state = self.state

        if not mapper._state_has_identity(state):
            return None

        localparent = mapper._state_mapper(state)

        prop = localparent.get_property(self.key)
        strategy = prop._get_strategy(DeferredColumnLoader)

        if self.keys:
            toload = self.keys
        elif strategy.group:
            toload = [
                p.key
                for p in localparent.iterate_properties
                if isinstance(p, StrategizedProperty)
                and isinstance(p.strategy, DeferredColumnLoader)
                and p.group == strategy.group
            ]
        else:
            toload = [self.key]

        # narrow the keys down to just those which have no history
        group = [k for k in toload if k in state.unmodified]

        if strategy._should_log_debug:
            strategy.logger.debug(
                "deferred load %s group %s"
                % (mapperutil.state_attribute_str(state, self.key), group and ",".join(group) or "None")
            )

        session = sessionlib._state_session(state)
        if session is None:
            raise sa_exc.UnboundExecutionError(
                "Parent instance %s is not bound to a Session; deferred load operation of attribute '%s' cannot proceed"
                % (mapperutil.state_str(state), self.key)
            )

        query = session.query(localparent)
        ident = state.key[1]
        query._get(None, ident=ident, only_load_props=group, refresh_state=state)
        return attributes.ATTR_WAS_SET
Beispiel #17
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        col = self.columns[0]
        if adapter:
            col = adapter.columns[col]
        if col in row:
            return self.parent_property._get_strategy(ColumnLoader).create_row_processor(selectcontext, path, mapper, row, adapter)

        elif not self.is_class_level:
            def new_execute(state, dict_, row, **flags):
                state.set_callable(self.key, LoadDeferredColumns(state, self.key))
        else:
            def new_execute(state, dict_, row, **flags):
                # reset state on the key so that deferred callables
                # fire off on next access.
                state.reset(self.key, dict_)

        if self._should_log_debug:
            new_execute = self.debug_callable(new_execute, self.logger, None,
                lambda state, row, **flags: "set deferred callable on %s" % mapperutil.state_attribute_str(state, self.key)
            )
        return (new_execute, None)
Beispiel #18
0
    def create_row_processor(self, selectcontext, path, mapper, row, adapter):
        col = self.columns[0]
        if adapter:
            col = adapter.columns[col]
        if col in row:
            return self.parent_property._get_strategy(
                ColumnLoader).create_row_processor(selectcontext, path, mapper,
                                                   row, adapter)

        elif not self.is_class_level or len(selectcontext.options):

            def new_execute(state, row, **flags):
                state.set_callable(self.key, self.setup_loader(state))
        else:

            def new_execute(state, row, **flags):
                state.reset(self.key)

        if self._should_log_debug:
            new_execute = self.debug_callable(
                new_execute, self.logger, None,
                lambda state, row, **flags: "set deferred callable on %s" %
                mapperutil.state_attribute_str(state, self.key))
        return (new_execute, None)
Beispiel #19
0
    def __call__(self, **kw):
        state = self.state
        instance_mapper = mapper._state_mapper(state)
        prop = instance_mapper.get_property(self.key)
        strategy = prop._get_strategy(LazyLoader)

        if kw.get("passive") is attributes.PASSIVE_NO_FETCH and not strategy.use_get:
            return attributes.PASSIVE_NO_RESULT

        if strategy._should_log_debug():
            strategy.logger.debug("loading %s", mapperutil.state_attribute_str(state, self.key))

        session = sessionlib._state_session(state)
        if session is None:
            raise orm_exc.DetachedInstanceError(
                "Parent instance %s is not bound to a Session; "
                "lazy load operation of attribute '%s' cannot proceed" % (mapperutil.state_str(state), self.key)
            )

        q = session.query(prop.mapper)._adapt_all_clauses()

        if state.load_path:
            q = q._with_current_path(state.load_path + (self.key,))

        # if we have a simple primary key load, use mapper.get()
        # to possibly save a DB round trip
        if strategy.use_get:
            ident = []
            allnulls = True
            for primary_key in prop.mapper.primary_key:
                val = instance_mapper._get_committed_state_attr_by_column(
                    state, state.dict, strategy._equated_columns[primary_key], **kw
                )
                if val is attributes.PASSIVE_NO_RESULT:
                    return val
                allnulls = allnulls and val is None
                ident.append(val)

            if allnulls:
                return None

            if state.load_options:
                q = q._conditional_options(*state.load_options)

            key = prop.mapper.identity_key_from_primary_key(ident)
            return q._get(key, ident, **kw)

        if prop.order_by:
            q = q.order_by(*util.to_list(prop.order_by))

        for rev in prop._reverse_property:
            # reverse props that are MANYTOONE are loading *this*
            # object from get(), so don't need to eager out to those.
            if rev.direction is interfaces.MANYTOONE and rev._use_get and not isinstance(rev.strategy, LazyLoader):
                q = q.options(EagerLazyOption(rev.key, lazy="select"))

        if state.load_options:
            q = q._conditional_options(*state.load_options)

        q = q.filter(strategy.lazy_clause(state))

        result = q.all()
        if strategy.uselist:
            return result
        else:
            l = len(result)
            if l:
                if l > 1:
                    util.warn("Multiple rows returned with " "uselist=False for lazily-loaded attribute '%s' " % prop)

                return result[0]
            else:
                return None
Beispiel #20
0
    def __call__(self, **kw):
        state = self.state
        instance_mapper = mapper._state_mapper(state)
        prop = instance_mapper.get_property(self.key)
        strategy = prop._get_strategy(LazyLoader)

        if kw.get('passive') is attributes.PASSIVE_NO_FETCH and \
                                    not strategy.use_get:
            return attributes.PASSIVE_NO_RESULT

        if strategy._should_log_debug():
            strategy.logger.debug(
                "loading %s", mapperutil.state_attribute_str(state, self.key))

        session = sessionlib._state_session(state)
        if session is None:
            raise orm_exc.DetachedInstanceError(
                "Parent instance %s is not bound to a Session; "
                "lazy load operation of attribute '%s' cannot proceed" %
                (mapperutil.state_str(state), self.key))

        q = session.query(prop.mapper)._adapt_all_clauses()

        if state.load_path:
            q = q._with_current_path(state.load_path + (self.key, ))

        # if we have a simple primary key load, use mapper.get()
        # to possibly save a DB round trip
        if strategy.use_get:
            ident = []
            allnulls = True
            for primary_key in prop.mapper.primary_key:
                val = instance_mapper.\
                                _get_committed_state_attr_by_column(
                                    state,
                                    state.dict,
                                    strategy._equated_columns[primary_key],
                                    **kw)
                if val is attributes.PASSIVE_NO_RESULT:
                    return val
                allnulls = allnulls and val is None
                ident.append(val)

            if allnulls:
                return None

            if state.load_options:
                q = q._conditional_options(*state.load_options)

            key = prop.mapper.identity_key_from_primary_key(ident)
            return q._get(key, ident, **kw)

        if prop.order_by:
            q = q.order_by(*util.to_list(prop.order_by))

        for rev in prop._reverse_property:
            # reverse props that are MANYTOONE are loading *this*
            # object from get(), so don't need to eager out to those.
            if rev.direction is interfaces.MANYTOONE and \
                        rev._use_get and \
                        not isinstance(rev.strategy, LazyLoader):
                q = q.options(EagerLazyOption(rev.key, lazy='select'))

        if state.load_options:
            q = q._conditional_options(*state.load_options)

        q = q.filter(strategy.lazy_clause(state))

        result = q.all()
        if strategy.uselist:
            return result
        else:
            l = len(result)
            if l:
                if l > 1:
                    util.warn(
                        "Multiple rows returned with "
                        "uselist=False for lazily-loaded attribute '%s' " %
                        prop)

                return result[0]
            else:
                return None