Author: phd
Date: 2010-08-16 08:45:05 -0600 (Mon, 16 Aug 2010)
New Revision: 4230
Modified:
SQLObject/trunk/docs/News.txt
SQLObject/trunk/sqlobject/index.py
SQLObject/trunk/sqlobject/joins.py
SQLObject/trunk/sqlobject/main.py
Log:
The lists of columns/indices/joins are now sorted according to the order of creation.
Modified: SQLObject/trunk/docs/News.txt
===================================================================
--- SQLObject/trunk/docs/News.txt 2010-08-11 14:40:49 UTC (rev 4229)
+++ SQLObject/trunk/docs/News.txt 2010-08-16 14:45:05 UTC (rev 4230)
@@ -10,6 +10,12 @@
SQLObject (trunk)
=================
+Features & Interface
+--------------------
+
+* The lists of columns/indices/joins are now sorted according to the order
+ of creation.
+
SQLObject 0.13.0
================
Modified: SQLObject/trunk/sqlobject/index.py
===================================================================
--- SQLObject/trunk/sqlobject/index.py 2010-08-11 14:40:49 UTC (rev 4229)
+++ SQLObject/trunk/sqlobject/index.py 2010-08-16 14:45:05 UTC (rev 4230)
@@ -1,16 +1,21 @@
+from itertools import count
from types import *
from converters import sqlrepr
+creationOrder = count()
+
class SODatabaseIndex(object):
def __init__(self,
soClass,
name,
columns,
+ creationOrder,
unique=False):
self.soClass = soClass
self.name = name
self.descriptions = self.convertColumns(columns)
+ self.creationOrder = creationOrder
self.unique = unique
def get(self, *args, **kw):
@@ -33,7 +38,7 @@
kw[columns[i].foreignName] = args[i]
else:
kw[columns[i].name] = args[i]
- return self.soClass.selectBy(connection=connection, **kw).getOne()
+ return self.soClass.selectBy(connection=connection, **kw).getOne()
def convertColumns(self, columns):
"""
@@ -148,6 +153,7 @@
def __init__(self, *columns, **kw):
kw['columns'] = columns
self.kw = kw
+ self.creationOrder = creationOrder.next()
def setName(self, value):
assert self.kw.get('name') is None, "You cannot change a name after it has already been set (from %s to %s)" % (self.kw['name'], value)
@@ -162,7 +168,8 @@
name = property(_get_name, _set_name)
def withClass(self, soClass):
- return self.baseClass(soClass=soClass, **self.kw)
+ return self.baseClass(soClass=soClass,
+ creationOrder=self.creationOrder, **self.kw)
def __repr__(self):
return '<%s %s %s>' % (
Modified: SQLObject/trunk/sqlobject/joins.py
===================================================================
--- SQLObject/trunk/sqlobject/joins.py 2010-08-11 14:40:49 UTC (rev 4229)
+++ SQLObject/trunk/sqlobject/joins.py 2010-08-16 14:45:05 UTC (rev 4230)
@@ -1,12 +1,15 @@
-import sqlbuilder
-NoDefault = sqlbuilder.NoDefault
-import styles
+from itertools import count
import classregistry
import events
+import styles
+import sqlbuilder
__all__ = ['MultipleJoin', 'SQLMultipleJoin', 'RelatedJoin', 'SQLRelatedJoin',
'SingleJoin', 'ManyToMany', 'OneToMany']
+creationOrder = count()
+NoDefault = sqlbuilder.NoDefault
+
def getID(obj):
try:
return obj.id
@@ -19,6 +22,7 @@
kw['otherClass'] = otherClass
self.kw = kw
self._joinMethodName = self.kw.pop('joinMethodName', None)
+ self.creationOrder = self.kw.pop('creationOrder', None)
def _set_joinMethodName(self, value):
assert self._joinMethodName == value or self._joinMethodName is None, "You have already given an explicit joinMethodName (%s), and you are now setting it to %s" % (self._joinMethodName, value)
@@ -34,7 +38,8 @@
if self.kw.has_key('joinMethodName'):
self._joinMethodName = self.kw['joinMethodName']
del self.kw['joinMethodName']
- return self.baseClass(soClass=soClass,
+ return self.baseClass(creationOrder=self.creationOrder,
+ soClass=soClass,
joinDef=self,
joinMethodName=self._joinMethodName,
**self.kw)
@@ -45,12 +50,14 @@
class SOJoin(object):
def __init__(self,
+ creationOrder,
soClass=None,
otherClass=None,
joinColumn=None,
joinMethodName=None,
orderBy=NoDefault,
joinDef=None):
+ self.creationOrder = creationOrder
self.soClass = soClass
self.joinDef = joinDef
self.otherClassName = otherClass
Modified: SQLObject/trunk/sqlobject/main.py
===================================================================
--- SQLObject/trunk/sqlobject/main.py 2010-08-11 14:40:49 UTC (rev 4229)
+++ SQLObject/trunk/sqlobject/main.py 2010-08-16 14:45:05 UTC (rev 4230)
@@ -144,13 +144,12 @@
return depends
def _collectAttributes(cls, new_attrs, look_for_class, delete=True,
- set_name=False, sort=False):
+ set_name=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. If
- `sort` is true, then they will be sorted by ``obj.creationOrder``.
+ the ``.name`` attribute is set for any matching objects.
"""
result = []
for attr, value in new_attrs.items():
@@ -160,9 +159,6 @@
value.name = attr
if delete:
delattr(cls, attr)
- if sort:
- result.sort(
- lambda a, b: cmp(a.creationOrder, b.creationOrder))
return result
class CreateNewSQLObject:
@@ -209,9 +205,9 @@
columnDefinitions = {}
# These are lists of the join and index objects:
- joins = []
indexes = []
indexDefinitions = []
+ joins = []
joinDefinitions = []
__metaclass__ = declarative.DeclarativeMeta
@@ -735,7 +731,7 @@
cls._SO_setupSqlmeta(new_attrs, is_base)
implicitColumns = _collectAttributes(
- cls, new_attrs, col.Col, set_name=True, sort=True)
+ cls, new_attrs, col.Col, set_name=True)
implicitJoins = _collectAttributes(
cls, new_attrs, joins.Join, set_name=True)
implicitIndexes = _collectAttributes(
@@ -772,33 +768,42 @@
if connection and ('_connection' not in cls.__dict__):
cls.setConnection(connection)
+ sqlmeta = cls.sqlmeta
+
# We have to check if there are columns in the inherited
# _columns where the attribute has been set to None in this
# class. If so, then we need to remove that column from
# _columns.
- for key in cls.sqlmeta.columnDefinitions.keys():
+ for key in sqlmeta.columnDefinitions.keys():
if (key in new_attrs
and new_attrs[key] is None):
- del cls.sqlmeta.columnDefinitions[key]
+ del sqlmeta.columnDefinitions[key]
- for column in cls.sqlmeta.columnDefinitions.values():
- cls.sqlmeta.addColumn(column)
+ for column in sqlmeta.columnDefinitions.values():
+ sqlmeta.addColumn(column)
for column in implicitColumns:
- cls.sqlmeta.addColumn(column)
+ sqlmeta.addColumn(column)
# Now the class is in an essentially OK-state, so we can
# set up any magic attributes:
declarative.setup_attributes(cls, new_attrs)
- if cls.sqlmeta.fromDatabase:
- cls.sqlmeta.addColumnsFromDatabase()
+ if sqlmeta.fromDatabase:
+ sqlmeta.addColumnsFromDatabase()
for j in implicitJoins:
- cls.sqlmeta.addJoin(j)
+ sqlmeta.addJoin(j)
for i in implicitIndexes:
- cls.sqlmeta.addIndex(i)
+ sqlmeta.addIndex(i)
+ order_getter = lambda o: o.creationOrder
+ sqlmeta.columnList.sort(key=order_getter)
+ sqlmeta.indexes.sort(key=order_getter)
+ sqlmeta.indexDefinitions.sort(key=order_getter)
+ sqlmeta.joins.sort(key=order_getter)
+ sqlmeta.joinDefinitions.sort(key=order_getter)
+
# We don't setup the properties until we're finished with the
# batch adding of all the columns...
cls._notifyFinishClassCreation()
@@ -812,7 +817,7 @@
cls.q = sqlbuilder.SQLObjectTable(cls)
cls.j = sqlbuilder.SQLObjectTableWithJoins(cls)
- classregistry.registry(cls.sqlmeta.registry).addClass(cls)
+ classregistry.registry(sqlmeta.registry).addClass(cls)
# @classmethod
def _SO_setupSqlmeta(cls, new_attrs, is_base):
|