[Sqlalchemy-commits] [2076] sqlalchemy/trunk: - fixed bug where eagerload() (nor lazyload()) option
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-10-31 19:39:12
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[2076] sqlalchemy/trunk: - fixed bug where eagerload() (nor lazyload()) option didn't properly</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>2076</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-10-31 14:40:22 -0600 (Tue, 31 Oct 2006)</dd> </dl> <h3>Log Message</h3> <pre>- fixed bug where eagerload() (nor lazyload()) option didn't properly instruct the Query whether or not to use "nesting" when producing a LIMIT query.</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunkCHANGES">sqlalchemy/trunk/CHANGES</a></li> <li><a href="#sqlalchemytrunklibsqlalchemyormmapperpy">sqlalchemy/trunk/lib/sqlalchemy/orm/mapper.py</a></li> <li><a href="#sqlalchemytrunklibsqlalchemyormquerypy">sqlalchemy/trunk/lib/sqlalchemy/orm/query.py</a></li> <li><a href="#sqlalchemytrunklibsqlalchemyormstrategiespy">sqlalchemy/trunk/lib/sqlalchemy/orm/strategies.py</a></li> <li><a href="#sqlalchemytrunktestormmapperpy">sqlalchemy/trunk/test/orm/mapper.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunkCHANGES"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/CHANGES (2075 => 2076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/CHANGES 2006-10-31 15:58:51 UTC (rev 2075) +++ sqlalchemy/trunk/CHANGES 2006-10-31 20:40:22 UTC (rev 2076) </span><span class="lines">@@ -9,6 +9,9 @@ </span><span class="cx"> - further fixes to sqlite booleans, weren't working as defaults </span><span class="cx"> - fix to postgres sequence quoting when using schemas </span><span class="cx"> - fixed direct execution of Compiled objects </span><ins>+- fixed bug where eagerload() (nor lazyload()) option didn't properly +instruct the Query whether or not to use "nesting" when producing a +LIMIT query. </ins><span class="cx"> </span><span class="cx"> 0.3.0 </span><span class="cx"> - General: </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemyormmapperpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/orm/mapper.py (2075 => 2076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/orm/mapper.py 2006-10-31 15:58:51 UTC (rev 2075) +++ sqlalchemy/trunk/lib/sqlalchemy/orm/mapper.py 2006-10-31 20:40:22 UTC (rev 2076) </span><span class="lines">@@ -160,6 +160,7 @@ </span><span class="cx"> # "polymorphic identity" of the row, which indicates which Mapper should be used </span><span class="cx"> # to construct a new object instance from that row. </span><span class="cx"> self.polymorphic_on = polymorphic_on </span><ins>+ self._eager_loaders = util.Set() </ins><span class="cx"> </span><span class="cx"> # our 'polymorphic identity', a string name that when located in a result set row </span><span class="cx"> # indicates this Mapper should be used to construct the object instance for that row. </span><span class="lines">@@ -711,7 +712,7 @@ </span><span class="cx"> </span><span class="cx"> def has_eager(self): </span><span class="cx"> """return True if one of the properties attached to this Mapper is eager loading""" </span><del>- return getattr(self, '_has_eager', False) </del><ins>+ return len(self._eager_loaders) > 0 </ins><span class="cx"> </span><span class="cx"> def instances(self, cursor, session, *mappers, **kwargs): </span><span class="cx"> """return a list of mapped instances corresponding to the rows in a given ResultProxy.""" </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemyormquerypy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/orm/query.py (2075 => 2076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/orm/query.py 2006-10-31 15:58:51 UTC (rev 2075) +++ sqlalchemy/trunk/lib/sqlalchemy/orm/query.py 2006-10-31 20:40:22 UTC (rev 2076) </span><span class="lines">@@ -374,7 +374,7 @@ </span><span class="cx"> generated query as a subquery inside of a larger eager-loading query. this is used </span><span class="cx"> with keywords like distinct, limit and offset and the mapper defines eager loads.""" </span><span class="cx"> return ( </span><del>- self.mapper.has_eager() </del><ins>+ len(querycontext.eager_loaders) > 0 </ins><span class="cx"> and self._nestable(**querycontext.select_args()) </span><span class="cx"> ) </span><span class="cx"> </span><span class="lines">@@ -473,6 +473,7 @@ </span><span class="cx"> self.distinct = kwargs.pop('distinct', False) </span><span class="cx"> self.limit = kwargs.pop('limit', None) </span><span class="cx"> self.offset = kwargs.pop('offset', None) </span><ins>+ self.eager_loaders = util.Set([x for x in query.mapper._eager_loaders]) </ins><span class="cx"> self.statement = None </span><span class="cx"> super(QueryContext, self).__init__(query.mapper, query.with_options, **kwargs) </span><span class="cx"> def select_args(self): </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemyormstrategiespy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/orm/strategies.py (2075 => 2076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/orm/strategies.py 2006-10-31 15:58:51 UTC (rev 2075) +++ sqlalchemy/trunk/lib/sqlalchemy/orm/strategies.py 2006-10-31 20:40:22 UTC (rev 2076) </span><span class="lines">@@ -289,7 +289,7 @@ </span><span class="cx"> super(EagerLoader, self).init() </span><span class="cx"> if self.parent.isa(self.mapper): </span><span class="cx"> raise exceptions.ArgumentError("Error creating eager relationship '%s' on parent class '%s' to child class '%s': Cant use eager loading on a self referential relationship." % (self.key, repr(self.parent.class_), repr(self.mapper.class_))) </span><del>- self.parent._has_eager = True </del><ins>+ self.parent._eager_loaders.add(self.parent_property) </ins><span class="cx"> </span><span class="cx"> self.clauses = {} </span><span class="cx"> self.clauses_by_lead_mapper = {} </span><span class="lines">@@ -518,6 +518,13 @@ </span><span class="cx"> def __init__(self, key, lazy=True): </span><span class="cx"> super(EagerLazyOption, self).__init__(key) </span><span class="cx"> self.lazy = lazy </span><ins>+ def process_query_property(self, context, prop): + if self.lazy: + if prop in context.eager_loaders: + context.eager_loaders.remove(prop) + else: + context.eager_loaders.add(prop) + super(EagerLazyOption, self).process_query_property(context, prop) </ins><span class="cx"> def get_strategy_class(self): </span><span class="cx"> if self.lazy: </span><span class="cx"> return LazyLoader </span></span></pre></div> <a id="sqlalchemytrunktestormmapperpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/test/orm/mapper.py (2075 => 2076)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/test/orm/mapper.py 2006-10-31 15:58:51 UTC (rev 2075) +++ sqlalchemy/trunk/test/orm/mapper.py 2006-10-31 20:40:22 UTC (rev 2076) </span><span class="lines">@@ -382,6 +382,30 @@ </span><span class="cx"> self.assert_result(l, User, *user_address_result) </span><span class="cx"> self.assert_sql_count(db, go, 0) </span><span class="cx"> </span><ins>+ def testeageroptionswithlimit(self): + sess = create_session() + mapper(User, users, properties = dict( + addresses = relation(mapper(Address, addresses), lazy = True) + )) + u = sess.query(User).options(eagerload('addresses')).get_by(user_id=8) + + def go(): + assert u.user_id == 8 + assert len(u.addresses) == 3 + self.assert_sql_count(db, go, 0) + + def testlazyoptionswithlimit(self): + sess = create_session() + mapper(User, users, properties = dict( + addresses = relation(mapper(Address, addresses), lazy = False) + )) + u = sess.query(User).options(lazyload('addresses')).get_by(user_id=8) + + def go(): + assert u.user_id == 8 + assert len(u.addresses) == 3 + self.assert_sql_count(db, go, 1) + </ins><span class="cx"> def testeagerdegrade(self): </span><span class="cx"> """tests that an eager relation automatically degrades to a lazy relation if eager columns are not available""" </span><span class="cx"> sess = create_session() </span></span></pre> </div> </div> </body> </html> |