]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
changed "_source_column" to simpler "_distance"
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 Jul 2007 07:11:56 +0000 (07:11 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 Jul 2007 07:11:56 +0000 (07:11 +0000)
lib/sqlalchemy/schema.py
lib/sqlalchemy/sql.py
test/sql/selectable.py

index d7d728f2b62d6f9e3c5e75c8bd473a743f784c34..9d68dc007909ff951c7c8a61d295795ce0f40ac6 100644 (file)
@@ -604,7 +604,7 @@ class Column(SchemaItem, sql._ColumnClause):
         c = Column(name or self.name, self.type, self.default, key = name or self.key, primary_key = self.primary_key, nullable = self.nullable, _is_oid = self._is_oid, quote=self.quote, *fk)
         c.table = selectable
         c.orig_set = self.orig_set
-        c._source_column = self
+        c._distance = self._distance + 1
         c.__originating_column = self.__originating_column
         if not c._is_oid:
             selectable.columns.add(c)
index 2a22a40c131751540650072dbebf6a84466f4c52..c4272c92694fcabc59efc37e186bba5c2469e6c5 100644 (file)
@@ -1543,16 +1543,6 @@ class ColumnElement(Selectable, _CompareMixin):
         else:
             return False
     
-    def _distance(self, othercolumn):
-        c = othercolumn
-        count = 0
-        while c is not self:
-            c = c._source_column
-            if c is None:
-                return -1
-            count += 1
-        return count
-        
     def _make_proxy(self, selectable, name=None):
         """Create a new ``ColumnElement`` representing this
         ``ColumnElement`` as it appears in the select list of a
@@ -1768,7 +1758,10 @@ class FromClause(Selectable):
             cp = self._proxy_column(co)
             for ci in cp.orig_set:
                 cx = self._orig_cols.get(ci)
-                if cx is None or ci._distance(cp) < ci._distance(cx):
+                # TODO: the '=' thing here relates to the order of columns as they are placed in the
+                # "columns" collection of a CompositeSelect, illustrated in test/sql/selectable.SelectableTest.testunion
+                # make this relationship less brittle
+                if cx is None or cp._distance <= cx._distance:
                     self._orig_cols[ci] = cp
         if self.oid_column is not None:
             for ci in self.oid_column.orig_set:
@@ -2088,7 +2081,7 @@ class _Cast(ColumnElement):
         self.type = sqltypes.to_instance(totype)
         self.clause = clause
         self.typeclause = _TypeClause(self.type)
-        self._source_column = None
+        self._distance = 0
         
     def get_children(self, **kwargs):
         return self.clause, self.typeclause
@@ -2101,7 +2094,7 @@ class _Cast(ColumnElement):
     def _make_proxy(self, selectable, name=None):
         if name is not None:
             co = _ColumnClause(name, selectable, type=self.type)
-            co._source_column = self
+            co._distance = self._distance + 1
             co.orig_set = self.orig_set
             selectable.columns[name]= co
             return co
@@ -2524,7 +2517,7 @@ class _ColumnClause(ColumnElement):
         self.table = selectable
         self.type = sqltypes.to_instance(type)
         self._is_oid = _is_oid
-        self._source_column = None
+        self._distance = 0
         self.__label = None
         self.case_sensitive = case_sensitive
         self.is_literal = is_literal
@@ -2584,7 +2577,7 @@ class _ColumnClause(ColumnElement):
         is_literal = self.is_literal and (name is None or name == self.name)
         c = _ColumnClause(name or self.name, selectable=selectable, _is_oid=self._is_oid, type=self.type, is_literal=is_literal)
         c.orig_set = self.orig_set
-        c._source_column = self
+        c._distance = self._distance + 1
         if not self._is_oid:
             selectable.columns[c.name] = c
         return c
index 340c55837d5b0d51d38b8b72dd9da878dac047ac..853821f9af0245d9ba2457d3219de630fb60745e 100755 (executable)
@@ -85,6 +85,8 @@ class SelectableTest(testbase.AssertMixin):
         print ["%d %s" % (id(c),c.key) for c in u.c]\r
         c = u.corresponding_column(s1.c.table1_col2)\r
         print "%d %s" % (id(c), c.key)\r
+        print id(u.corresponding_column(s1.c.table1_col2).table)\r
+        print id(u.c.col2.table)\r
         assert u.corresponding_column(s1.c.table1_col2) is u.c.col2\r
         assert u.corresponding_column(s2.c.table2_col2) is u.c.col2\r
 \r