From: Mike Bayer Date: Wed, 9 Jul 2008 18:30:07 +0000 (+0000) Subject: - Merged 0.5's declarative behavior such that all X-Git-Tag: rel_0_4_7~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=901d64bcab59fd5396344602b819e602672dd857;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Merged 0.5's declarative behavior such that all Column and MapperProperty objects keep a state variable indicating their creation order, which declarative_base() maintains when generating Table constructs. --- diff --git a/CHANGES b/CHANGES index b538d32776..173c712609 100644 --- a/CHANGES +++ b/CHANGES @@ -105,6 +105,13 @@ CHANGES which fulfills NUMERIC affinity, but that's not the same as REAL. +- extensions + - Merged 0.5's declarative behavior such that all + Column and MapperProperty objects keep a state + variable indicating their creation order, which + declarative_base() maintains when generating + Table constructs. + 0.4.6 ===== - orm diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index d736736e95..3140c064c6 100644 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -214,6 +214,9 @@ class DeclarativeMeta(type): prop = _deferred_relation(cls, value) our_stuff[k] = prop + # set up attributes in the order they were created + our_stuff.sort(lambda x, y: cmp(our_stuff[x]._creation_order, our_stuff[y]._creation_order)) + table = None if '__table__' not in cls.__dict__: if '__tablename__' in cls.__dict__: diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index a06de1b79c..8d56cb2bf0 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -38,6 +38,8 @@ class ColumnProperty(StrategizedProperty): self.group = kwargs.pop('group', None) self.deferred = kwargs.pop('deferred', False) self.comparator = ColumnProperty.ColumnComparator(self) + util.set_creation_order(self) + if self.deferred: self.strategy_class = strategies.DeferredColumnLoader else: @@ -151,6 +153,7 @@ class SynonymProperty(MapperProperty): self.name = name self.map_column=map_column self.descriptor = descriptor + util.set_creation_order(self) def setup(self, querycontext, **kwargs): pass @@ -187,7 +190,8 @@ class ComparableProperty(MapperProperty): def __init__(self, comparator_factory, descriptor=None): self.descriptor = descriptor self.comparator = comparator_factory(self) - + util.set_creation_order(self) + def do_init(self): """Set up a proxy to the unmanaged descriptor.""" @@ -236,6 +240,7 @@ class PropertyLoader(StrategizedProperty): self.comparator = PropertyLoader.Comparator(self) self.join_depth = join_depth self._arg_local_remote_pairs = _local_remote_pairs + util.set_creation_order(self) if strategy_class: self.strategy_class = strategy_class diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 11be70bdda..e0704f1121 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -525,6 +525,7 @@ class Column(SchemaItem, expression._ColumnClause): self.autoincrement = kwargs.pop('autoincrement', True) self.constraints = util.Set() self.foreign_keys = util.OrderedSet() + util.set_creation_order(self) if kwargs.get('info'): self._info = kwargs.pop('info') if kwargs: diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index e88c4b3b9b..af768ff36d 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -530,6 +530,9 @@ class OrderedDict(dict): self._list = [] dict.clear(self) + def sort(self, fn=None): + self._list.sort(fn) + def update(self, ____sequence=None, **kwargs): if ____sequence is not None: if hasattr(____sequence, 'keys'): @@ -1155,6 +1158,20 @@ def function_named(fn, name): fn.func_defaults, fn.func_closure) return fn + +_creation_order = 1 +def set_creation_order(instance): + """assign a '_creation_order' sequence to the given instance. + + This allows multiple instances to be sorted in order of + creation (typically within a single thread; the counter is + not particularly threadsafe). + + """ + global _creation_order + instance._creation_order = _creation_order + _creation_order +=1 + def conditional_cache_decorator(func): """apply conditional caching to the return value of a function."""