Author: phd
Date: 2007-01-23 11:02:01 -0700 (Tue, 23 Jan 2007)
New Revision: 2240
Added:
home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_aggregates.py
home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_inheritance_tree.py
Modified:
home/phd/SQLObject/paramstyles/sqlobject/inheritance/__init__.py
home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_inheritance.py
home/phd/SQLObject/paramstyles/sqlobject/versioning/__init__.py
home/phd/SQLObject/paramstyles/sqlobject/versioning/test/test_version.py
Log:
Merged patches from the revisions 2227:2239 from the trunk
Modified: home/phd/SQLObject/paramstyles/sqlobject/inheritance/__init__.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/inheritance/__init__.py 2007-01-23 17:14:51 UTC (rev 2239)
+++ home/phd/SQLObject/paramstyles/sqlobject/inheritance/__init__.py 2007-01-23 18:02:01 UTC (rev 2240)
@@ -13,17 +13,33 @@
basestring = (types.StringType, types.UnicodeType)
+def tablesUsedDict(obj):
+ if hasattr(obj, "tablesUsedDict"):
+ return obj.tablesUsedDict()
+ elif isinstance(obj, (tuple, list)):
+ d = {}
+ for component in obj:
+ d.update(tablesUsedDict(component))
+ return d
+ else:
+ return {}
+
+
class InheritableSelectResults(SelectResults):
IterationClass = iteration.InheritableIteration
- def __init__(self, sourceClass, clause, clauseTables=None, **ops):
+ def __init__(self, sourceClass, clause, clauseTables=None,
+ inheritedTables=None, **ops):
if clause is None or isinstance(clause, str) and clause == 'all':
clause = sqlbuilder.SQLTrueClause
- tablesDict = sqlbuilder.tablesUsedDict(clause)
+ tablesDict = tablesUsedDict(clause)
tablesDict[sourceClass.sqlmeta.table] = 1
orderBy = ops.get('orderBy')
+ if inheritedTables:
+ for tableName in inheritedTables:
+ tablesDict[tableName] = 1
if orderBy and not isinstance(orderBy, basestring):
- tablesDict.update(sqlbuilder.tablesUsedDict(orderBy))
+ tablesDict.update(tablesUsedDict(orderBy))
#DSM: if this class has a parent, we need to link it
#DSM: and be sure the parent is in the table list.
#DSM: The following code is before clauseTables
@@ -68,6 +84,16 @@
super(InheritableSelectResults, self).__init__(sourceClass,
clause, clauseTables, **ops)
+ def accumulateMany(self, *attributes, **kw):
+ if kw.get("skipInherited"):
+ return super(InheritableSelectResults, self).accumulateMany(*attributes)
+ tables = []
+ for func_name, attribute in attributes:
+ if not isinstance(attribute, basestring):
+ tables.append(attribute.tableName)
+ clone = self.__class__(self.sourceClass, self.clause,
+ self.clauseTables, inheritedTables=tables, **self.ops)
+ return clone.accumulateMany(skipInherited=True, *attributes)
class InheritableSQLMeta(sqlmeta):
def addColumn(sqlmeta, columnDef, changeSchema=False, connection=None, childUpdate=False):
Copied: home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_aggregates.py (from rev 2239, SQLObject/trunk/sqlobject/inheritance/tests/test_aggregates.py)
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_aggregates.py (rev 0)
+++ home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_aggregates.py 2007-01-23 18:02:01 UTC (rev 2240)
@@ -0,0 +1,20 @@
+from sqlobject import *
+from sqlobject.inheritance import *
+from sqlobject.tests.dbtest import *
+
+class TestAggregate1(InheritableSQLObject):
+ value1 = IntCol()
+
+class TestAggregate2(TestAggregate1):
+ value2 = IntCol()
+
+def test_aggregates():
+ setupClass([TestAggregate1, TestAggregate2])
+
+ TestAggregate1(value1=1)
+ TestAggregate2(value1=2, value2=12)
+
+ assert TestAggregate1.select().max("value1") == 2
+ assert TestAggregate2.select().max("value1") == 2
+ raises(Exception, TestAggregate2.select().max, "value2")
+ assert TestAggregate2.select().max(TestAggregate2.q.value2) == 12
Modified: home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_inheritance.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_inheritance.py 2007-01-23 17:14:51 UTC (rev 2239)
+++ home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_inheritance.py 2007-01-23 18:02:01 UTC (rev 2240)
@@ -79,6 +79,24 @@
person = Employee.byLastName("Leader")
assert person.firstName == "Project"
+ persons = list(InheritablePerson.select(orderBy=InheritablePerson.q.lastName))
+ assert len(persons) == 2
+
+ persons = list(InheritablePerson.select(orderBy=(InheritablePerson.q.lastName, InheritablePerson.q.firstName)))
+ assert len(persons) == 2
+
+ persons = list(Employee.select(orderBy=Employee.q.lastName))
+ assert len(persons) == 1
+
+ persons = list(Employee.select(orderBy=(Employee.q.lastName, Employee.q.firstName)))
+ assert len(persons) == 1
+
+ persons = list(Employee.select(orderBy=Employee.q.position))
+ assert len(persons) == 1
+
+ persons = list(Employee.select(orderBy=(Employee.q.position, Employee.q.lastName)))
+ assert len(persons) == 1
+
def test_addDelColumn():
setup()
Copied: home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_inheritance_tree.py (from rev 2239, SQLObject/trunk/sqlobject/inheritance/tests/test_inheritance_tree.py)
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_inheritance_tree.py (rev 0)
+++ home/phd/SQLObject/paramstyles/sqlobject/inheritance/tests/test_inheritance_tree.py 2007-01-23 18:02:01 UTC (rev 2240)
@@ -0,0 +1,37 @@
+from sqlobject import *
+from sqlobject.inheritance import *
+from sqlobject.tests.dbtest import *
+
+########################################
+## Inheritance Tree
+########################################
+
+class Tree1(InheritableSQLObject):
+ aprop = StringCol(length=10)
+
+class Tree2(Tree1):
+ bprop = StringCol(length=10)
+
+class Tree3(Tree1):
+ cprop = StringCol(length=10)
+
+class Tree4(Tree2):
+ dprop = StringCol(length=10)
+
+class Tree5(Tree2):
+ eprop = StringCol(length=10)
+
+def test_tree():
+ setupClass([Tree1, Tree2, Tree3, Tree4, Tree5])
+
+ t1 = Tree1(aprop='t1')
+ t2 = Tree2(aprop='t2', bprop='t2')
+ t3 = Tree3(aprop='t3', cprop='t3')
+ t4 = Tree4(aprop='t4', bprop='t4', dprop='t4')
+ t5 = Tree5(aprop='t5', bprop='t5', eprop='t5')
+
+ # find just the t5 out of childs from Tree2
+ assert t5 == Tree1.select(Tree2.q.childName == 'Tree5')[0]
+
+ # t2,t4,t5 are all subclasses of Tree1 with t1 childName of 'Tree2'
+ assert list(Tree1.select(Tree1.q.childName == 'Tree2', orderBy="aprop")) == [t2, t4, t5]
Modified: home/phd/SQLObject/paramstyles/sqlobject/versioning/__init__.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/versioning/__init__.py 2007-01-23 17:14:51 UTC (rev 2239)
+++ home/phd/SQLObject/paramstyles/sqlobject/versioning/__init__.py 2007-01-23 18:02:01 UTC (rev 2240)
@@ -7,6 +7,8 @@
del values['id']
del values['masterID']
del values['dateArchived']
+ for col in self.extraCols:
+ del values[col]
self.masterClass.get(self.masterID).set(**values)
def nextVersion(self):
@@ -33,6 +35,12 @@
return super(Version, cls).select(clause, *args, **kw)
select = classmethod(select)
+ def __getattr__(self, attr):
+ if self.__dict__.has_key(attr):
+ return self.__dict__[attr]
+ else:
+ return getattr(self.master, attr)
+
def getColumns(columns, cls):
for column, defi in cls.sqlmeta.columnDefinitions.items():
if column.endswith("ID") and isinstance(defi, ForeignKey):
@@ -59,6 +67,7 @@
attrs = {'dateArchived': DateTimeCol(default=datetime.now),
'master': ForeignKey(self.soClass.__name__),
'masterClass' : self.soClass,
+ 'extraCols' : self.extraCols
}
getColumns (attrs, self.soClass)
@@ -69,6 +78,9 @@
(Version,),
attrs)
+ if '_connection' in self.soClass.__dict__:
+ self.versionClass._connection = self.soClass.__dict__['_connection']
+
events.listen(self.createTable,
soClass, events.CreateTableSignal)
events.listen(self.rowUpdate, soClass,
Modified: home/phd/SQLObject/paramstyles/sqlobject/versioning/test/test_version.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/versioning/test/test_version.py 2007-01-23 17:14:51 UTC (rev 2239)
+++ home/phd/SQLObject/paramstyles/sqlobject/versioning/test/test_version.py 2007-01-23 18:02:01 UTC (rev 2240)
@@ -121,6 +121,13 @@
assert monarchy.name == "USA"
assert monarchy.monarch == "Emperor Norton I"
+ extra = Extra(name='fleem')
+ extra.set(name='morx')
+ assert extra.name == "morx"
+ extra.versions[0].restore()
+ assert extra.name == "fleem"
+
+
def test_next():
setup()
base = Base(name='first', value=1)
|