[SQL-CVS] r635 - in trunk/SQLObject/sqlobject: . inheritance inheritance/tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2005-02-22 12:32:45
|
Author: phd Date: 2005-02-22 12:32:34 +0000 (Tue, 22 Feb 2005) New Revision: 635 Modified: trunk/SQLObject/sqlobject/inheritance/__init__.py trunk/SQLObject/sqlobject/inheritance/tests/test_inheritance.py trunk/SQLObject/sqlobject/main.py Log: Fixed bugs in nherited byAlternateID() and selectBy(). Split _SO_fetchAlternateID() into _findAlternateID() overrided _findAlternateID() in InheritableSQLObject. Added more inheritance tests. Modified: trunk/SQLObject/sqlobject/inheritance/__init__.py =================================================================== --- trunk/SQLObject/sqlobject/inheritance/__init__.py 2005-02-21 16:50:34 UTC (rev 634) +++ trunk/SQLObject/sqlobject/inheritance/__init__.py 2005-02-22 12:32:34 UTC (rev 635) @@ -193,6 +193,37 @@ super(InheritableSQLObject, self)._create(id, **kw) + def _findAlternateID(cls, dbIDName, value, connection=None): + found = False + currentClass = cls + try: + while currentClass: + for col in currentClass.sqlmeta._columns: + if col.dbName == dbIDName: + found = True + raise StopIteration + currentClass = currentClass._parentClass + except StopIteration: + pass + if not found: + return [], None + result = list(cls.selectBy(connection, **{col.name: value})) + if not result: + return result, None + obj = result[0] + return [obj.id], obj + _findAlternateID = classmethod(_findAlternateID) + + def selectBy(cls, connection=None, **kw): + clause = [] + for name, value in kw.items(): + clause.append(getattr(cls.q, name) == value) + clause = reduce(sqlbuilder.AND, clause) + conn = connection or cls._connection + return cls.SelectResultsClass(cls, clause, connection=conn) + + selectBy = classmethod(selectBy) + def destroySelf(self): #DSM: If this object has parents, recursivly kill them if hasattr(self, '_parent') and self._parent: Modified: trunk/SQLObject/sqlobject/inheritance/tests/test_inheritance.py =================================================================== --- trunk/SQLObject/sqlobject/inheritance/tests/test_inheritance.py 2005-02-21 16:50:34 UTC (rev 634) +++ trunk/SQLObject/sqlobject/inheritance/tests/test_inheritance.py 2005-02-22 12:32:34 UTC (rev 635) @@ -10,7 +10,7 @@ class InherPerson(InheritableSQLObject): _inheritable = 1 # I want this class to be inherited firstName = StringCol() - lastName = StringCol() + lastName = StringCol(alternateID=True) class Employee(InherPerson): _inheritable = 0 # If I don't want this class to be inherited @@ -20,8 +20,8 @@ setupClass(InherPerson) setupClass(Employee) - Employee(firstName='Ian', lastName='Bicking', position='Project leader') - InherPerson(firstName='Daniel', lastName='Savard') + Employee(firstName='Project', lastName='Leader', position='Project leader') + InherPerson(firstName='Oneof', lastName='Authors') def test_inheritance(): @@ -43,8 +43,34 @@ persons = InherPerson.select(InherPerson.q.firstName <> None) assert persons.count() == 2 + persons = InherPerson.select(InherPerson.q.firstName == "phd") + assert persons.count() == 0 + employees = Employee.select(Employee.q.firstName <> None) assert employees.count() == 1 + employees = Employee.select(Employee.q.firstName == "phd") + assert employees.count() == 0 + employees = Employee.select(Employee.q.position <> None) assert employees.count() == 1 + + persons = InherPerson.selectBy(firstName="Project") + assert persons.count() == 1 + assert isinstance(persons[0], Employee) + + persons = Employee.selectBy(firstName="Project") + assert persons.count() == 1 + + try: + person = InherPerson.byLastName("Oneof") + except: + pass + else: + raise RuntimeError, "unknown person %s" % person + + person = InherPerson.byLastName("Leader") + assert person.firstName == "Project" + + person = Employee.byLastName("Leader") + assert person.firstName == "Project" Modified: trunk/SQLObject/sqlobject/main.py =================================================================== --- trunk/SQLObject/sqlobject/main.py 2005-02-21 16:50:34 UTC (rev 634) +++ trunk/SQLObject/sqlobject/main.py 2005-02-22 12:32:34 UTC (rev 635) @@ -1119,15 +1119,21 @@ def _SO_getID(self, obj): return getID(obj) - def _SO_fetchAlternateID(cls, dbIDName, value, connection=None): - result = (connection or cls._connection)._SO_selectOneAlt( + def _findAlternateID(cls, dbIDName, value, connection=None): + return (connection or cls._connection)._SO_selectOneAlt( cls, [cls.sqlmeta.idName] + [col.dbName for col in cls.sqlmeta._columns], dbIDName, - value) + value), None + _findAlternateID = classmethod(_findAlternateID) + + def _SO_fetchAlternateID(cls, dbIDName, value, connection=None): + result, obj = cls._findAlternateID(dbIDName, value, connection) if not result: raise SQLObjectNotFound, "The %s by alternateID %s=%s does not exist" % (cls.__name__, dbIDName, repr(value)) + if obj: + return obj if connection: obj = cls.get(result[0], connection=connection) else: |