[SQL-CVS] r2533 - in SQLObject/branches/cache-deps-on-views/sqlobject: . tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2007-04-15 22:14:49
|
Author: luke Date: 2007-04-15 16:14:44 -0600 (Sun, 15 Apr 2007) New Revision: 2533 Added: SQLObject/branches/cache-deps-on-views/sqlobject/tests/test_sqlbuilder_joins_instances.py Modified: SQLObject/branches/cache-deps-on-views/sqlobject/main.py SQLObject/branches/cache-deps-on-views/sqlobject/sqlbuilder.py SQLObject/branches/cache-deps-on-views/sqlobject/tests/test_select_through.py Log: Bug in select.throughTo, use joins and instances in sqlbuilder expressions Modified: SQLObject/branches/cache-deps-on-views/sqlobject/main.py =================================================================== --- SQLObject/branches/cache-deps-on-views/sqlobject/main.py 2007-04-15 01:24:40 UTC (rev 2532) +++ SQLObject/branches/cache-deps-on-views/sqlobject/main.py 2007-04-15 22:14:44 UTC (rev 2533) @@ -49,6 +49,7 @@ import events from sresults import SelectResults from formencode import schema, compound +from converters import registerConverter import sys if sys.version_info[:3] < (2, 2, 0): @@ -1624,7 +1625,13 @@ cls._connection = value setConnection = classmethod(setConnection) + def __sqlrepr__(self, db): + return sqlbuilder.sqlrepr(self.__class__.q.id==self.id) + + def tablesUsedImmediate(self): + return [self.__class__.q] + def capitalize(name): return name[0].capitalize() + name[1:] Modified: SQLObject/branches/cache-deps-on-views/sqlobject/sqlbuilder.py =================================================================== --- SQLObject/branches/cache-deps-on-views/sqlobject/sqlbuilder.py 2007-04-15 01:24:40 UTC (rev 2532) +++ SQLObject/branches/cache-deps-on-views/sqlobject/sqlbuilder.py 2007-04-15 22:14:44 UTC (rev 2533) @@ -168,6 +168,9 @@ def __str__(self): return repr(self) +# def __hash__(self): +# return id(self) + def __cmp__(self, other): raise VersionError, "Python 2.1+ required" def __rcmp__(self, other): @@ -349,7 +352,6 @@ registerConverter(UnicodeField, SQLExprConverter) - class Table(SQLExpression): FieldClass = Field @@ -382,6 +384,16 @@ raise AttributeError if attr == 'id': return self.FieldClass(self.tableName, self.soClass.sqlmeta.idName, attr) + elif attr+'ID' in self.soClass.sqlmeta.columns: + column = self.soClass.sqlmeta.columns[attr+'ID'] + return getattr(self, column.name)==getattr(self.soClass, '_SO_class_'+column.foreignKey).q.id + elif attr in [x.joinMethodName for x in self.soClass.sqlmeta.joins]: + join = [x for x in self.soClass.sqlmeta.joins if x.joinMethodName == attr][0] + if hasattr(join, 'otherColumn'): + return AND(join.otherClass.q.id == Field(join.intermediateTable, join.otherColumn), + Field(join.intermediateTable, join.joinColumn) == self.soClass.q.id) + else: + return getattr(join.otherClass.q, join.joinColumn)==self.soClass.q.id elif attr not in self.soClass.sqlmeta.columns: raise AttributeError("%s instance has no attribute '%s'" % (self.soClass.__name__, attr)) else: Modified: SQLObject/branches/cache-deps-on-views/sqlobject/tests/test_select_through.py =================================================================== --- SQLObject/branches/cache-deps-on-views/sqlobject/tests/test_select_through.py 2007-04-15 01:24:40 UTC (rev 2532) +++ SQLObject/branches/cache-deps-on-views/sqlobject/tests/test_select_through.py 2007-04-15 22:14:44 UTC (rev 2533) @@ -48,6 +48,7 @@ def testThroughRelatedJoin(): assert list(threes[0].twos.throughTo.threes) == [threes[0], threes[1]] + assert list(SRThrough3.select(SRThrough3.q.id==threes[0].id).throughTo.twos) == list(threes[0].twos) def testThroughFKAndJoin(): assert list(threes[0].ones.throughTo.three.throughTo.twos) == [twos[0]] \ No newline at end of file Added: SQLObject/branches/cache-deps-on-views/sqlobject/tests/test_sqlbuilder_joins_instances.py =================================================================== --- SQLObject/branches/cache-deps-on-views/sqlobject/tests/test_sqlbuilder_joins_instances.py (rev 0) +++ SQLObject/branches/cache-deps-on-views/sqlobject/tests/test_sqlbuilder_joins_instances.py 2007-04-15 22:14:44 UTC (rev 2533) @@ -0,0 +1,62 @@ +from sqlobject import * +from sqlobject.sqlbuilder import * +from sqlobject.tests.dbtest import * + +''' Testing for expressing join, foreign keys, and instance identity in SQLBuilder expressions. +''' + +class SBPerson(SQLObject): + name = StringCol() + addresses = SQLMultipleJoin('SBAddress', joinColumn='personID') + sharedAddresses = SQLRelatedJoin('SBAddress', addRemoveName='SharedAddress') + +class SBAddress(SQLObject): + city = StringCol() + person = ForeignKey('SBPerson') + sharedPeople = SQLRelatedJoin('SBPerson') + + +def setup_module(mod): + setupClass([SBPerson, SBAddress]) + mod.ppl = inserts(SBPerson, [('James',), + ('Julia',)], + 'name') + mod.adds = inserts(SBAddress, [('London',mod.ppl[0].id), + ('Chicago',mod.ppl[1].id), + ('Abu Dhabi', mod.ppl[1].id)], + 'city personID') + mod.ppl[0].addSharedAddress(mod.adds[0]) + mod.ppl[0].addSharedAddress(mod.adds[1]) + mod.ppl[1].addSharedAddress(mod.adds[0]) + +def testJoin(): + assert list(SBPerson.select(AND(SBPerson.q.addresses,SBAddress.q.city=='London'))) == \ + list(SBPerson.select(AND(SBPerson.q.id==SBAddress.q.personID, SBAddress.q.city=='London'))) == \ + list(SBAddress.selectBy(city='London').throughTo.person) + +def testFK(): + assert list(SBPerson.select(AND(SBAddress.q.person, SBAddress.q.city=='London'))) == \ + list(SBPerson.select(AND(SBPerson.q.id==SBAddress.q.personID, SBAddress.q.city=='London'))) + +def testJoin2(): + assert list(SBAddress.select(AND(SBPerson.q.addresses, SBPerson.q.name=='Julia'))) == \ + list(SBAddress.select(AND(SBPerson.q.id==SBAddress.q.personID, SBPerson.q.name=='Julia'))) == \ + list(SBPerson.selectBy(name='Julia').throughTo.addresses) + +def testFK2(): + assert list(SBAddress.select(AND(SBAddress.q.person, SBPerson.q.name=='Julia'))) == \ + list(SBAddress.select(AND(SBPerson.q.id==SBAddress.q.personID, SBPerson.q.name=='Julia'))) + +def testRelatedJoin(): + assert list(SBAddress.select(AND(SBAddress.q.sharedPeople, SBPerson.q.name=='Julia'))) == \ + list(SBPerson.selectBy(name='Julia').throughTo.sharedAddresses) == \ + list(ppl[1].sharedAddresses) + +def testInstance(): + assert list(SBAddress.select(AND(SBAddress.q.person, ppl[0]))) == \ + list(SBAddress.select(AND(SBPerson.q.id==SBAddress.q.personID, SBPerson.q.id==ppl[0].id))) == \ + list(ppl[0].addresses) + +def testInstance2(): + assert list(SBAddress.select(AND(SBPerson.q.addresses, ppl[0]))) == \ + list(SBAddress.select(AND(SBPerson.q.id==SBAddress.q.personID, SBPerson.q.id==ppl[0].id))) \ No newline at end of file |