From: Mike Bayer Date: Sat, 17 Feb 2007 03:14:36 +0000 (+0000) Subject: - extension() query option propigates to Mapper._instance() method so that X-Git-Tag: rel_0_3_5~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f4b1484ff570125ba511ed931e8e5e53d53f4866;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - extension() query option propigates to Mapper._instance() method so that all loading-related methods get called [ticket:454] --- diff --git a/CHANGES b/CHANGES index 4cdf2ec4ee..24f0f95e60 100644 --- a/CHANGES +++ b/CHANGES @@ -21,6 +21,8 @@ - implemented foreign_keys argument to mapper [ticket:385]. use in conjunction with primaryjoin/secondaryjoin arguments to specify/override foreign keys defined on the Table instance. + - extension() query option propigates to Mapper._instance() method so that + all loading-related methods get called [ticket:454] - eager relation to an inheriting mapper wont fail if no rows returned for the relationship. - fix for very large topological sorts, courtesy ants.aasma at gmail [ticket:423] diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 1d4ce6bb9c..18c4c3acb8 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -1200,9 +1200,9 @@ class Mapper(object): if not context.identity_map.has_key(identitykey): context.identity_map[identitykey] = instance isnew = True - if self.extension.populate_instance(self, context, row, instance, identitykey, isnew) is EXT_PASS: + if context.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 context.extension.append_result(self, context, row, instance, identitykey, result, isnew) is EXT_PASS: if result is not None: result.append(instance) return instance @@ -1228,7 +1228,7 @@ class Mapper(object): return None # plugin point - instance = self.extension.create_instance(self, context, row, self.class_) + instance = context.extension.create_instance(self, context, row, self.class_) if instance is EXT_PASS: instance = self._create_instance(context.session) else: @@ -1243,9 +1243,9 @@ 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, context, row, instance, identitykey, isnew) is EXT_PASS: + if context.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 context.extension.append_result(self, context, row, instance, identitykey, result, isnew) is EXT_PASS: if result is not None: result.append(instance) return instance diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 4c0a4b1ecd..e7b1243727 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -323,7 +323,7 @@ class Query(object): session = self.session - context = SelectionContext(self.select_mapper, session, with_options=self.with_options, **kwargs) + context = SelectionContext(self.select_mapper, session, self.extension, with_options=self.with_options, **kwargs) result = util.UniqueAppender([]) if mappers: @@ -525,10 +525,11 @@ class SelectionContext(OperationContext): to the freshly loaded value """ - def __init__(self, mapper, session, **kwargs): + def __init__(self, mapper, session, extension, **kwargs): self.populate_existing = kwargs.pop('populate_existing', False) self.version_check = kwargs.pop('version_check', False) self.session = session + self.extension = extension self.identity_map = {} super(SelectionContext, self).__init__(mapper, kwargs.pop('with_options', []), **kwargs) def accept_option(self, opt): diff --git a/test/orm/mapper.py b/test/orm/mapper.py index 46854b2cea..ed4cb89773 100644 --- a/test/orm/mapper.py +++ b/test/orm/mapper.py @@ -405,12 +405,26 @@ class MapperTest(MapperSuperTest): def testextensionoptions(self): sess = create_session() - mapper(User, users) + class ext1(MapperExtension): + def populate_instance(self, mapper, selectcontext, row, instance, identitykey, isnew): + """test options at the Mapper._instance level""" + instance.TEST = "hello world" + return EXT_PASS + mapper(User, users, extension=ext1()) class testext(MapperExtension): def select_by(self, *args, **kwargs): + """test options at the Query level""" return "HI" + def populate_instance(self, mapper, selectcontext, row, instance, identitykey, isnew): + """test options at the Mapper._instance level""" + instance.TEST_2 = "also hello world" + return EXT_PASS l = sess.query(User).options(extension(testext())).select_by(x=5) assert l == "HI" + l = sess.query(User).options(extension(testext())).get(7) + assert l.user_id == 7 + assert l.TEST == "hello world" + assert l.TEST_2 == "also hello world" def testeageroptions(self): """tests that a lazy relation can be upgraded to an eager relation via the options method"""