[Sqlalchemy-commits] [2084] sqlalchemy/trunk: - improvement to single table inheritance to load ful
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-11-03 18:56:29
|
<!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>[2084] sqlalchemy/trunk: - improvement to single table inheritance to load full hierarchies beneath </title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>2084</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-11-03 13:57:39 -0600 (Fri, 03 Nov 2006)</dd> </dl> <h3>Log Message</h3> <pre>- improvement to single table inheritance to load full hierarchies beneath the target class</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="#sqlalchemytrunktestormalltestspy">sqlalchemy/trunk/test/orm/alltests.py</a></li> </ul> <h3>Added Paths</h3> <ul> <li><a href="#sqlalchemytrunktestormsinglepy">sqlalchemy/trunk/test/orm/single.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunkCHANGES"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/CHANGES (2083 => 2084)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/CHANGES 2006-11-03 01:17:28 UTC (rev 2083) +++ sqlalchemy/trunk/CHANGES 2006-11-03 19:57:39 UTC (rev 2084) </span><span class="lines">@@ -24,6 +24,8 @@ </span><span class="cx"> (i.e. if A.items stores B objects, raise an error if a C is appended to A.items) </span><span class="cx"> - new extension sqlalchemy.ext.associationproxy, provides transparent "association object" </span><span class="cx"> mappings. new example examples/association/proxied_association.py illustrates. </span><ins>+- improvement to single table inheritance to load full hierarchies beneath +the target class </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 (2083 => 2084)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/orm/mapper.py 2006-11-03 01:17:28 UTC (rev 2083) +++ sqlalchemy/trunk/lib/sqlalchemy/orm/mapper.py 2006-11-03 19:57:39 UTC (rev 2084) </span><span class="lines">@@ -595,13 +595,17 @@ </span><span class="cx"> m = m.inherits </span><span class="cx"> </span><span class="cx"> def polymorphic_iterator(self): </span><del>- m = self.base_mapper() </del><ins>+ """iterates through the collection including this mapper and all descendant mappers. + + this includes not just the immediately inheriting mappers but all their inheriting mappers as well. + + To iterate through an entire hierarchy, use mapper.base_mapper().polymorphic_iterator().""" </ins><span class="cx"> def iterate(m): </span><span class="cx"> yield m </span><span class="cx"> for mapper in m._inheriting_mappers: </span><span class="cx"> for x in iterate(mapper): </span><span class="cx"> yield x </span><del>- return iterate(m) </del><ins>+ return iterate(self) </ins><span class="cx"> </span><span class="cx"> def add_properties(self, dict_of_properties): </span><span class="cx"> """adds the given dictionary of properties to this mapper, using add_property.""" </span><span class="lines">@@ -831,7 +835,7 @@ </span><span class="cx"> updated_objects = util.Set() </span><span class="cx"> </span><span class="cx"> table_to_mapper = {} </span><del>- for mapper in self.polymorphic_iterator(): </del><ins>+ for mapper in self.base_mapper().polymorphic_iterator(): </ins><span class="cx"> for t in mapper.tables: </span><span class="cx"> table_to_mapper.setdefault(t, mapper) </span><span class="cx"> </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemyormquerypy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/orm/query.py (2083 => 2084)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/orm/query.py 2006-11-03 01:17:28 UTC (rev 2083) +++ sqlalchemy/trunk/lib/sqlalchemy/orm/query.py 2006-11-03 19:57:39 UTC (rev 2084) </span><span class="lines">@@ -414,7 +414,7 @@ </span><span class="cx"> raise exceptions.ArgumentError("Unknown lockmode '%s'" % lockmode) </span><span class="cx"> </span><span class="cx"> if self.mapper.single and self.mapper.polymorphic_on is not None and self.mapper.polymorphic_identity is not None: </span><del>- whereclause = sql.and_(whereclause, self.mapper.polymorphic_on==self.mapper.polymorphic_identity) </del><ins>+ whereclause = sql.and_(whereclause, self.mapper.polymorphic_on.in_(*[m.polymorphic_identity for m in self.mapper.polymorphic_iterator()])) </ins><span class="cx"> </span><span class="cx"> alltables = [] </span><span class="cx"> for l in [sql_util.TableFinder(x) for x in from_obj]: </span></span></pre></div> <a id="sqlalchemytrunktestormalltestspy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/test/orm/alltests.py (2083 => 2084)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/test/orm/alltests.py 2006-11-03 01:17:28 UTC (rev 2083) +++ sqlalchemy/trunk/test/orm/alltests.py 2006-11-03 19:57:39 UTC (rev 2084) </span><span class="lines">@@ -27,6 +27,7 @@ </span><span class="cx"> 'orm.inheritance', </span><span class="cx"> 'orm.inheritance2', </span><span class="cx"> 'orm.inheritance3', </span><ins>+ 'orm.single', </ins><span class="cx"> 'orm.polymorph' </span><span class="cx"> ) </span><span class="cx"> alltests = unittest.TestSuite() </span></span></pre></div> <a id="sqlalchemytrunktestormsinglepy"></a> <div class="addfile"><h4>Added: sqlalchemy/trunk/test/orm/single.py (2083 => 2084)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/test/orm/single.py 2006-11-03 01:17:28 UTC (rev 2083) +++ sqlalchemy/trunk/test/orm/single.py 2006-11-03 19:57:39 UTC (rev 2084) </span><span class="lines">@@ -0,0 +1,63 @@ </span><ins>+from sqlalchemy import * +import testbase + +class SingleInheritanceTest(testbase.AssertMixin): + def setUpAll(self): + metadata = BoundMetaData(testbase.db) + global employees_table + employees_table = Table('employees', metadata, + Column('employee_id', Integer, primary_key=True), + Column('name', String(50)), + Column('manager_data', String(50)), + Column('engineer_info', String(50)), + Column('type', String(20)) + ) + employees_table.create() + def tearDownAll(self): + employees_table.drop() + def testbasic(self): + class Employee(object): + def __init__(self, name): + self.name = name + def __repr__(self): + return self.__class__.__name__ + " " + self.name + + class Manager(Employee): + def __init__(self, name, manager_data): + self.name = name + self.manager_data = manager_data + def __repr__(self): + return self.__class__.__name__ + " " + self.name + " " + self.manager_data + + class Engineer(Employee): + def __init__(self, name, engineer_info): + self.name = name + self.engineer_info = engineer_info + def __repr__(self): + return self.__class__.__name__ + " " + self.name + " " + self.engineer_info + + class JuniorEngineer(Engineer): + pass + + employee_mapper = mapper(Employee, employees_table, polymorphic_on=employees_table.c.type) + manager_mapper = mapper(Manager, inherits=employee_mapper, polymorphic_identity='manager') + engineer_mapper = mapper(Engineer, inherits=employee_mapper, polymorphic_identity='engineer') + junior_engineer = mapper(JuniorEngineer, inherits=engineer_mapper, polymorphic_identity='juniorengineer') + + session = create_session() + + m1 = Manager('Tom', 'knows how to manage things') + e1 = Engineer('Kurt', 'knows how to hack') + e2 = JuniorEngineer('Ed', 'oh that ed') + session.save(m1) + session.save(e1) + session.save(e2) + session.flush() + + assert session.query(Employee).select() == [m1, e1, e2] + assert session.query(Engineer).select() == [e1, e2] + assert session.query(Manager).select() == [m1] + assert session.query(JuniorEngineer).select() == [e2] + +if __name__ == '__main__': + testbase.main() </ins><span class="cx">\ No newline at end of file </span></span></pre> </div> </div> </body> </html> |