Author: phd
Date: 2005-06-03 16:13:48 +0000 (Fri, 03 Jun 2005)
New Revision: 810
Added:
trunk/SQLObject/sqlobject/inheritance/tests/test_inherited_foreignKey.py
Modified:
trunk/SQLObject/sqlobject/inheritance/__init__.py
trunk/SQLObject/sqlobject/main.py
Log:
Added a class method _notifyFinishClassCreation(). The method is used
in InheritableSQLObject to propagate columns and joins from parent classes
to children if it hasn't been done already. Added a test for that propagation.
Modified: trunk/SQLObject/sqlobject/inheritance/__init__.py
===================================================================
--- trunk/SQLObject/sqlobject/inheritance/__init__.py 2005-06-01 13:48:41 UTC (rev 809)
+++ trunk/SQLObject/sqlobject/inheritance/__init__.py 2005-06-03 16:13:48 UTC (rev 810)
@@ -108,7 +108,8 @@
makeProperties(cls)
return
- super(InheritableSQLObject, cls).addColumn(columnDef, changeSchema, connection)
+ if columnDef:
+ super(InheritableSQLObject, cls).addColumn(columnDef, changeSchema, connection)
#DSM: Update each child class if needed and existing (only for new
#DSM: dynamic column as no child classes exists at object creation)
@@ -148,7 +149,8 @@
makeProperties(cls)
return
- super(InheritableSQLObject, cls).addJoin(joinDef)
+ if joinDef:
+ super(InheritableSQLObject, cls).addJoin(joinDef)
#DSM: Update each child class if needed and existing (only for new
#DSM: dynamic join as no child classes exists at object creation)
@@ -167,6 +169,17 @@
delJoin = classmethod(delJoin)
+ def _notifyFinishClassCreation(cls):
+ if not cls._columns:
+ # There are no columns - call addColumn to propagate columns
+ # from parent classes to children
+ cls.addColumn(None)
+ if not cls._joins:
+ # There are no joins - call addJoin to propagate joins
+ # from parent classes to children
+ cls.addJoin(None)
+ _notifyFinishClassCreation = classmethod(_notifyFinishClassCreation)
+
def _create(self, id, **kw):
#DSM: If we were called by a children class,
Added: trunk/SQLObject/sqlobject/inheritance/tests/test_inherited_foreignKey.py
===================================================================
--- trunk/SQLObject/sqlobject/inheritance/tests/test_inherited_foreignKey.py 2005-06-01 13:48:41 UTC (rev 809)
+++ trunk/SQLObject/sqlobject/inheritance/tests/test_inherited_foreignKey.py 2005-06-03 16:13:48 UTC (rev 810)
@@ -0,0 +1,37 @@
+from sqlobject import *
+from sqlobject.tests.dbtest import *
+from sqlobject.inheritance import InheritableSQLObject
+
+
+class Note(SQLObject):
+ text = StringCol()
+
+class PersonWithNotes(InheritableSQLObject):
+ firstName = StringCol()
+ lastName = StringCol()
+ note = ForeignKey("Note", default=None)
+
+class EmployeeWithNotes(PersonWithNotes):
+ _inheritable = False
+
+def setup():
+ setupClass(Note)
+ setupClass(PersonWithNotes)
+ setupClass(EmployeeWithNotes)
+
+ note = Note(text="person")
+ PersonWithNotes(firstName='Oneof', lastName='Authors', note=note)
+ note = Note(text="employee")
+ EmployeeWithNotes(firstName='Project', lastName='Leader', note=note)
+
+
+def test_inheritance():
+ setup()
+
+ person = PersonWithNotes.get(1)
+ assert isinstance(person, PersonWithNotes) and not isinstance(person, EmployeeWithNotes)
+ assert person.note.text == "person"
+
+ employee = EmployeeWithNotes.get(2)
+ assert isinstance(employee, EmployeeWithNotes)
+ assert employee.note.text == "employee"
Modified: trunk/SQLObject/sqlobject/main.py
===================================================================
--- trunk/SQLObject/sqlobject/main.py 2005-06-01 13:48:41 UTC (rev 809)
+++ trunk/SQLObject/sqlobject/main.py 2005-06-03 16:13:48 UTC (rev 810)
@@ -475,6 +475,7 @@
# We don't setup the properties until we're finished with the
# batch adding of all the columns...
+ cls._notifyFinishClassCreation()
cls._SO_finishedClassCreation = True
makeProperties(cls)
@@ -796,6 +797,10 @@
delJoin = classmethod(delJoin)
+ def _notifyFinishClassCreation(cls):
+ pass
+ _notifyFinishClassCreation = classmethod(_notifyFinishClassCreation)
+
def _init(self, id, connection=None, selectResults=None):
assert id is not None
# This function gets called only when the object is
|