Author: phd
Date: 2007-01-10 07:19:12 -0700 (Wed, 10 Jan 2007)
New Revision: 2189
Added:
home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_encapsulate_hiding.py
Modified:
home/phd/SQLObject/paramstyles/sqlobject/inheritance/__init__.py
home/phd/SQLObject/paramstyles/sqlobject/main.py
Log:
Merged patches from the revisions 2187:2188 from the trunk
Modified: home/phd/SQLObject/paramstyles/sqlobject/inheritance/__init__.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/inheritance/__init__.py 2007-01-10 14:15:22 UTC (rev 2188)
+++ home/phd/SQLObject/paramstyles/sqlobject/inheritance/__init__.py 2007-01-10 14:19:12 UTC (rev 2189)
@@ -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
Copied: home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_encapsulate_hiding.py (from rev 2188, SQLObject/trunk/sqlobject/inheritance/tests/test_encapsulate_hiding.py)
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_encapsulate_hiding.py (rev 0)
+++ home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_encapsulate_hiding.py 2007-01-10 14:19:12 UTC (rev 2189)
@@ -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: home/phd/SQLObject/paramstyles/sqlobject/main.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/main.py 2007-01-10 14:15:22 UTC (rev 2188)
+++ home/phd/SQLObject/paramstyles/sqlobject/main.py 2007-01-10 14:19:12 UTC (rev 2189)
@@ -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)
|