[Sqlalchemy-commits] [1417] sqlalchemy/branches/schema/test: eager loader extends lazyloader, will d
Brought to you by:
zzzeek
<!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>[1417] sqlalchemy/branches/schema/test: eager loader extends lazyloader, will degrade to a lazy load upon execute if primary key columns arent present in the row (i.e.</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1417</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-05-06 13:28:48 -0500 (Sat, 06 May 2006)</dd> </dl> <h3>Log Message</h3> <pre>eager loader extends lazyloader, will degrade to a lazy load upon execute if primary key columns arent present in the row (i.e. not just None). allows alternate select statements to be plugged into a mapper without eager loads breaking</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemybranchesschemalibsqlalchemyormmapperpy">sqlalchemy/branches/schema/lib/sqlalchemy/orm/mapper.py</a></li> <li><a href="#sqlalchemybranchesschemalibsqlalchemyormpropertiespy">sqlalchemy/branches/schema/lib/sqlalchemy/orm/properties.py</a></li> <li><a href="#sqlalchemybranchesschemalibsqlalchemyschemapy">sqlalchemy/branches/schema/lib/sqlalchemy/schema.py</a></li> <li><a href="#sqlalchemybranchesschematestmapperpy">sqlalchemy/branches/schema/test/mapper.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemybranchesschemalibsqlalchemyormmapperpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/orm/mapper.py (1416 => 1417)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/orm/mapper.py 2006-05-06 18:12:19 UTC (rev 1416) +++ sqlalchemy/branches/schema/lib/sqlalchemy/orm/mapper.py 2006-05-06 18:28:48 UTC (rev 1417) </span><span class="lines">@@ -762,7 +762,7 @@ </span><span class="cx"> for c in prop.cascade_iterator(type, object, recursive): </span><span class="cx"> yield c </span><span class="cx"> </span><del>- def _identity_key(self, row): </del><ins>+ def _row_identity_key(self, row): </ins><span class="cx"> return sessionlib.get_row_key(row, self.class_, self.pks_by_table[self.select_table], self.entity_name) </span><span class="cx"> </span><span class="cx"> def _instance(self, session, row, imap, result = None, populate_existing = False): </span><span class="lines">@@ -784,7 +784,7 @@ </span><span class="cx"> # been exposed to being modified by the application. </span><span class="cx"> </span><span class="cx"> populate_existing = populate_existing or self.always_refresh </span><del>- identitykey = self._identity_key(row) </del><ins>+ identitykey = self._row_identity_key(row) </ins><span class="cx"> if session.has_key(identitykey): </span><span class="cx"> instance = session._get(identitykey) </span><span class="cx"> isnew = False </span></span></pre></div> <a id="sqlalchemybranchesschemalibsqlalchemyormpropertiespy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/orm/properties.py (1416 => 1417)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/orm/properties.py 2006-05-06 18:12:19 UTC (rev 1416) +++ sqlalchemy/branches/schema/lib/sqlalchemy/orm/properties.py 2006-05-06 18:28:48 UTC (rev 1417) </span><span class="lines">@@ -448,9 +448,11 @@ </span><span class="cx"> return (lazywhere, binds, reverse) </span><span class="cx"> </span><span class="cx"> </span><del>-class EagerLoader(PropertyLoader): </del><ins>+class EagerLoader(LazyLoader): </ins><span class="cx"> """loads related objects inline with a parent query.""" </span><span class="cx"> def do_init_subclass(self, key, parent, recursion_stack=None): </span><ins>+ if recursion_stack is None: + LazyLoader.do_init_subclass(self, key, parent) </ins><span class="cx"> parent._has_eager = True </span><span class="cx"> </span><span class="cx"> self.eagertarget = self.target.alias() </span><span class="lines">@@ -566,6 +568,15 @@ </span><span class="cx"> """receive a row. tell our mapper to look for a new object instance in the row, and attach </span><span class="cx"> it to a list on the parent instance.""" </span><span class="cx"> </span><ins>+ decorated_row = self._decorate_row(row) + try: + # check for identity key + identity_key = self.mapper._row_identity_key(decorated_row) + except KeyError: + # else degrade to a lazy loader + LazyLoader.execute(self, session, instance, row, identitykey, imap, isnew) + return + </ins><span class="cx"> if isnew: </span><span class="cx"> # new row loaded from the database. initialize a blank container on the instance. </span><span class="cx"> # this will override any per-class lazyloading type of stuff. </span><span class="lines">@@ -603,9 +614,8 @@ </span><span class="cx"> map[parent._label] = c </span><span class="cx"> map[parent.name] = c </span><span class="cx"> return DecoratorDict </span><del>- - def _instance(self, session, row, imap, result_list=None): - """gets an instance from a row, via this EagerLoader's mapper.""" </del><ins>+ + def _decorate_row(self, row): </ins><span class="cx"> # since the EagerLoader makes an Alias of its mapper's table, </span><span class="cx"> # we translate the actual result columns back to what they </span><span class="cx"> # would normally be into a "virtual row" which is passed to the child mapper. </span><span class="lines">@@ -613,9 +623,16 @@ </span><span class="cx"> # (neither do any MapperExtensions). The row is keyed off the Column object </span><span class="cx"> # (which is what mappers use) as well as its "label" (which might be what </span><span class="cx"> # user-defined code is using) </span><del>- row = self._row_decorator(row) - return self.mapper._instance(session, row, imap, result_list) </del><ins>+ try: + return self._row_decorator(row) + except AttributeError: + self._create_eager_chain() + return self._row_decorator(row) </ins><span class="cx"> </span><ins>+ def _instance(self, session, row, imap, result_list=None): + """gets an instance from a row, via this EagerLoader's mapper.""" + return self.mapper._instance(session, self._decorate_row(row), imap, result_list) + </ins><span class="cx"> class GenericOption(mapper.MapperOption): </span><span class="cx"> """a mapper option that can handle dotted property names, </span><span class="cx"> descending down through the relations of a mapper until it </span></span></pre></div> <a id="sqlalchemybranchesschemalibsqlalchemyschemapy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/schema.py (1416 => 1417)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/schema.py 2006-05-06 18:12:19 UTC (rev 1416) +++ sqlalchemy/branches/schema/lib/sqlalchemy/schema.py 2006-05-06 18:28:48 UTC (rev 1417) </span><span class="lines">@@ -395,10 +395,10 @@ </span><span class="cx"> object representing the relationship, or just its string name given as </span><span class="cx"> "tablename.columnname". schema can be specified as </span><span class="cx"> "schema.tablename.columnname" """ </span><ins>+ if isinstance(column, unicode): + column = str(column) </ins><span class="cx"> self._colspec = column </span><span class="cx"> self._column = None </span><del>- if isinstance(column, unicode): - column = str(column) </del><span class="cx"> </span><span class="cx"> def __repr__(self): </span><span class="cx"> return "ForeignKey(%s)" % repr(self._get_colspec()) </span></span></pre></div> <a id="sqlalchemybranchesschematestmapperpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/test/mapper.py (1416 => 1417)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/test/mapper.py 2006-05-06 18:12:19 UTC (rev 1416) +++ sqlalchemy/branches/schema/test/mapper.py 2006-05-06 18:28:48 UTC (rev 1417) </span><span class="lines">@@ -283,6 +283,27 @@ </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 testeagerdegrade(self): + """tests that an eager relation automatically degrades to a lazy relation if eager columns are not available""" + sess = create_session() + usermapper = mapper(User, users, properties = dict( + addresses = relation(mapper(Address, addresses), lazy = False) + )) + + # first test straight eager load, 1 statement + def go(): + l = usermapper.query(sess).select() + self.assert_result(l, User, *user_address_result) + self.assert_sql_count(db, go, 1) + + # then select just from users. run it into instances. + # then assert the data, which will launch 3 more lazy loads + def go(): + r = users.select().execute() + l = usermapper.instances(r, sess) + self.assert_result(l, User, *user_address_result) + self.assert_sql_count(db, go, 4) + </ins><span class="cx"> def testlazyoptions(self): </span><span class="cx"> """tests that an eager relation can be upgraded to a lazy relation via the options method""" </span><span class="cx"> sess = create_session() </span></span></pre> </div> </div> </body> </html> |