[SQL-CVS] r2188 - in SQLObject/trunk/sqlobject: . inheritance inheritance/tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2007-01-10 14:15:32
|
Author: phd Date: 2007-01-10 07:15:22 -0700 (Wed, 10 Jan 2007) New Revision: 2188 Added: SQLObject/trunk/sqlobject/inheritance/tests/test_encapsulate_hiding.py Modified: SQLObject/trunk/sqlobject/inheritance/__init__.py SQLObject/trunk/sqlobject/main.py Log: Patch 614267: Prevents half-constructed objects from showing up in queries. Modified: SQLObject/trunk/sqlobject/inheritance/__init__.py =================================================================== --- SQLObject/trunk/sqlobject/inheritance/__init__.py 2007-01-10 13:41:05 UTC (rev 2187) +++ SQLObject/trunk/sqlobject/inheritance/__init__.py 2007-01-10 14:15:22 UTC (rev 2188) @@ -1,6 +1,6 @@ from sqlobject import sqlbuilder from sqlobject import classregistry -from sqlobject.col import StringCol, ForeignKey +from sqlobject.col import StringCol, ForeignKey, BoolCol from sqlobject.main import sqlmeta, SQLObject, SelectResults, True, False, \ makeProperties, getterName, setterName import iteration @@ -63,6 +63,12 @@ parentClause.append(currentClass.q.id == parentClass.q.id) currentClass = parentClass tablesDict[currentClass.sqlmeta.table] = 1 + + for (currentClass, minParentClass) in tableRegistry.items(): + if InheritableSQLObject in minParentClass.__bases__: + parentClause.append(minParentClass.q.doneConstructing == True) + break + clause = reduce(sqlbuilder.AND, parentClause, clause) super(InheritableSelectResults, self).__init__(sourceClass, @@ -192,7 +198,7 @@ currentClass = cls.sqlmeta.parentClass while currentClass: for column in currentClass.sqlmeta.columnDefinitions.values(): - if column.name == 'childName': + if column.name == 'doneConstructing' or column.name == 'childName': continue if type(column) == ForeignKey: continue @@ -290,6 +296,12 @@ sqlmeta.addColumn(StringCol(name='childName', # limit string length to get VARCHAR and not CLOB length=255, default=None)) + + #only on top-level classes + if InheritableSQLObject in cls.__bases__: + #add the doneConstructing column to make half-constructed tasks invisible + sqlmeta.addColumn(BoolCol(name='doneConstructing', default=False)) + if not sqlmeta.columnList: # There are no columns - call addColumn to propagate columns # from parent classes to children @@ -300,7 +312,7 @@ sqlmeta.addJoin(None) _notifyFinishClassCreation = classmethod(_notifyFinishClassCreation) - def _create(self, id, **kw): + def _create(self, id, last_derived=False, **kw): #DSM: If we were called by a children class, #DSM: we must retreive the properties dictionary. @@ -331,7 +343,7 @@ raise TypeError, "%s() did not get expected keyword argument %s" % (self.__class__.__name__, col.name) parent_kw['childName'] = self.sqlmeta.childName - self._parent = parentClass(kw=parent_kw, + self._parent = parentClass(last_derived=False, kw=parent_kw, connection=self._connection) id = self._parent.id Added: SQLObject/trunk/sqlobject/inheritance/tests/test_encapsulate_hiding.py =================================================================== --- SQLObject/trunk/sqlobject/inheritance/tests/test_encapsulate_hiding.py (rev 0) +++ SQLObject/trunk/sqlobject/inheritance/tests/test_encapsulate_hiding.py 2007-01-10 14:15:22 UTC (rev 2188) @@ -0,0 +1,48 @@ +from py.test import raises +from sqlobject import * +from sqlobject.tests.dbtest import * +from sqlobject.inheritance import InheritableSQLObject + +def first_position(): + employees = Employee.select() + if employees.count(): + return employees[0].position + else: + return "Lackey" + +def first_floor(): + janitors = Janitor.select() + if janitors.count(): + return janitors[0].floor + else: + return 0 + + +class InheritablePerson(InheritableSQLObject): + firstName = StringCol() + lastName = StringCol(alternateID=True, length=255) + +class Employee(InheritablePerson): + position = StringCol(default=first_position) + +class Janitor(Employee): + _inheritable = False + floor = IntCol(default=first_floor) + +def setup(): + setupClass(InheritablePerson) + setupClass(Employee) + setupClass(Janitor) + +def test_liveness(): + + setup() + emp = Employee(firstName='Igor', lastName='Humpback') + assert emp.position == "Lackey" + emp.position = "Grunt" + + emp = Employee(firstName='Frank', lastName='Furter') + assert emp.position == "Grunt" + + jan = Janitor(firstName="George", lastName="Bush") + assert jan.floor == 0 Modified: SQLObject/trunk/sqlobject/main.py =================================================================== --- SQLObject/trunk/sqlobject/main.py 2007-01-10 13:41:05 UTC (rev 2187) +++ SQLObject/trunk/sqlobject/main.py 2007-01-10 14:15:22 UTC (rev 2188) @@ -1203,7 +1203,7 @@ else: return joinClass.get(id) - def __init__(self, **kw): + def __init__(self, last_derived = True, **kw): # We shadow the sqlmeta class with an instance of sqlmeta # that points to us (our sqlmeta buddy object; where the # sqlmeta class is our class's buddy class) @@ -1231,12 +1231,15 @@ else: id = None - self._create(id, **kw) + self._create(id, last_derived=False, **kw) + + if last_derived and hasattr(self, 'doneConstructing'): + self.doneConstructing = True + for func in post_funcs: func(self) - def _create(self, id, **kw): - + def _create(self, id, last_derived=False,**kw): self.sqlmeta._creating = True self._SO_createValues = {} self._SO_validatorState = SQLObjectState(self) |