Author: ianb
Date: 2005-07-31 07:36:00 +0000 (Sun, 31 Jul 2005)
New Revision: 863
Added:
trunk/SQLObject/sqlobject/util/backports.py
Modified:
trunk/SQLObject/
trunk/SQLObject/sqlobject/col.py
trunk/SQLObject/sqlobject/main.py
trunk/SQLObject/sqlobject/tests/test_basic.py
Log:
Make columns keep their order of creation.
Property changes on: trunk/SQLObject
___________________________________________________________________
Name: svn:ignore
- MANIFEST
data
build
dist
*.pyc
*.pyo
+ MANIFEST
data
build
dist
*.pyc
*.pyo
*.tmp
Modified: trunk/SQLObject/sqlobject/col.py
===================================================================
--- trunk/SQLObject/sqlobject/col.py 2005-07-31 07:27:37 UTC (rev 862)
+++ trunk/SQLObject/sqlobject/col.py 2005-07-31 07:36:00 UTC (rev 863)
@@ -30,6 +30,7 @@
from include import validators
from classregistry import findClass
from converters import array_type
+from util.backports import count
NoDefault = sqlbuilder.NoDefault
True, False = 1==1, 0==1
@@ -75,6 +76,8 @@
__all__.append("MXDATETIME_IMPLEMENTATION")
+creationOrder = count()
+
class SQLValidator(validators.All):
def attemptConvert(self, value, state, validate):
if validate is validators.toPython:
@@ -100,6 +103,7 @@
def __init__(self,
name,
soClass,
+ creationOrder,
dbName=None,
default=NoDefault,
foreignKey=None,
@@ -131,6 +135,7 @@
assert name, "You must provide a name for all columns"
self.columnDef = columnDef
+ self.creationOrder = creationOrder
self.immutable = immutable
@@ -361,6 +366,7 @@
self._name = name
kw['columnDef'] = self
self.kw = kw
+ self.creationOrder = creationOrder.next()
def _set_name(self, value):
assert self._name is None or self._name == value, (
@@ -374,7 +380,9 @@
name = property(_get_name, _set_name)
def withClass(self, soClass):
- return self.baseClass(soClass=soClass, name=self._name, **self.kw)
+ return self.baseClass(soClass=soClass, name=self._name,
+ creationOrder=self.creationOrder,
+ **self.kw)
def __repr__(self):
return '<%s %s %s>' % (
Modified: trunk/SQLObject/sqlobject/main.py
===================================================================
--- trunk/SQLObject/sqlobject/main.py 2005-07-31 07:27:37 UTC (rev 862)
+++ trunk/SQLObject/sqlobject/main.py 2005-07-31 07:36:00 UTC (rev 863)
@@ -133,12 +133,13 @@
return depends
def _collectAttributes(cls, new_attrs, look_for_class, delete=True,
- set_name=False):
+ set_name=False, sort=False):
"""
Finds all attributes in `new_attrs` that are instances of
`look_for_class`. Returns them as a list. If `delete` is true
they are also removed from the `cls`. If `set_name` is true, then
- the ``.name`` attribute is set for any matching objects.
+ the ``.name`` attribute is set for any matching objects. If
+ `sort` is true, then they will be sorted by ``obj.creationOrder``.
"""
result = []
for attr, value in new_attrs.items():
@@ -148,6 +149,9 @@
value.name = attr
if delete:
delattr(cls, attr)
+ if sort:
+ result.sort(
+ lambda a, b: cmp(a.creationOrder, b.creationOrder))
return result
class CreateNewSQLObject:
@@ -620,7 +624,7 @@
cls._SO_setupSqlmeta(new_attrs, is_base)
implicitColumns = _collectAttributes(
- cls, new_attrs, col.Col, set_name=True)
+ cls, new_attrs, col.Col, set_name=True, sort=True)
implicitJoins = _collectAttributes(
cls, new_attrs, joins.Join, set_name=True)
implicitIndexes = _collectAttributes(
Modified: trunk/SQLObject/sqlobject/tests/test_basic.py
===================================================================
--- trunk/SQLObject/sqlobject/tests/test_basic.py 2005-07-31 07:27:37 UTC (rev 862)
+++ trunk/SQLObject/sqlobject/tests/test_basic.py 2005-07-31 07:36:00 UTC (rev 863)
@@ -83,6 +83,8 @@
def test_foreignKey():
setupClass([TestSO4, TestSO3])
+ test3_order = [col.name for col in TestSO3.sqlmeta.columnList]
+ assert test3_order == ['name', 'otherID', 'other2ID']
tc3 = TestSO3(name='a')
assert tc3.other is None
assert tc3.other2 is None
Added: trunk/SQLObject/sqlobject/util/backports.py
===================================================================
--- trunk/SQLObject/sqlobject/util/backports.py 2005-07-31 07:27:37 UTC (rev 862)
+++ trunk/SQLObject/sqlobject/util/backports.py 2005-07-31 07:36:00 UTC (rev 863)
@@ -0,0 +1,21 @@
+from __future__ import generators
+
+__all__ = ['count', 'enumerate']
+
+# An implementation of itertools' count()
+try:
+ from itertools import count
+except ImportError:
+ def count(start=0):
+ while 1:
+ yield start
+ start += 1
+
+try:
+ enumerate
+except NameError:
+ def enumerate(lst):
+ i = 0
+ for item in lst:
+ yield i, item
+ i += 1
|