]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Adjusted the logic which applies names to the .c collection when
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 26 Feb 2014 20:45:52 +0000 (15:45 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 26 Feb 2014 20:45:52 +0000 (15:45 -0500)
a no-name :class:`.BindParameter` is received, e.g. via :func:`.sql.literal`
or similar; the "key" of the bind param is used as the key within
.c. rather than the rendered name.  Since these binds have "anonymous"
names in any case, this allows individual bound parameters to
have their own name within a selectable if they are otherwise unlabeled.
fixes #2974

doc/build/changelog/changelog_09.rst
lib/sqlalchemy/sql/elements.py
test/sql/test_compiler.py
test/sql/test_update.py

index 9a1ff9f277ee10199fef72f88f4d99a27a32437b..a5c22aefc9acae488907723fb2a3702b5f15452c 100644 (file)
 .. changelog::
     :version: 0.9.4
 
+    .. change::
+        :tags: bug, sql
+        :tickets: 2974
+
+        Adjusted the logic which applies names to the .c collection when
+        a no-name :class:`.BindParameter` is received, e.g. via :func:`.sql.literal`
+        or similar; the "key" of the bind param is used as the key within
+        .c. rather than the rendered name.  Since these binds have "anonymous"
+        names in any case, this allows individual bound parameters to
+        have their own name within a selectable if they are otherwise unlabeled.
+
     .. change::
         :tags: bug, sql
         :tickets: 2974
index 1b49a7cd1c9cdfbfaf27ac13410fe5962c7d5bf6..f2ce0619c7194e9b77e2f9f3b5a2687e74f25646 100644 (file)
@@ -588,7 +588,7 @@ class ColumnElement(ClauseElement, operators.ColumnOperators):
     primary_key = False
     foreign_keys = []
     _label = None
-    _key_label = None
+    _key_label = key = None
     _alt_names = ()
 
     def self_group(self, against=None):
@@ -681,10 +681,14 @@ class ColumnElement(ClauseElement, operators.ColumnOperators):
         """
         if name is None:
             name = self.anon_label
-            try:
-                key = str(self)
-            except exc.UnsupportedCompilationError:
-                key = self.anon_label
+            if self.key:
+                key = self.key
+            else:
+                try:
+                    key = str(self)
+                except exc.UnsupportedCompilationError:
+                    key = self.anon_label
+
         else:
             key = name
         co = ColumnClause(
@@ -755,7 +759,6 @@ class ColumnElement(ClauseElement, operators.ColumnOperators):
                                 'name', 'anon')))
 
 
-
 class BindParameter(ColumnElement):
     """Represent a "bound expression".
 
index 25aa78b0353c7579b36a01bc49306efd25716a18..b1c807df69b2f6fe1840462d2502565a1ebbec83 100644 (file)
@@ -2082,6 +2082,10 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
         )
 
     def test_naming(self):
+        # TODO: the part where we check c.keys() are  not "compile" tests, they
+        # belong probably in test_selectable, or some broken up
+        # version of that suite
+
         f1 = func.hoho(table1.c.name)
         s1 = select([table1.c.myid, table1.c.myid.label('foobar'),
                     f1,
@@ -2098,7 +2102,8 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
         exprs = (
             table1.c.myid == 12,
             func.hoho(table1.c.myid),
-            cast(table1.c.name, Numeric)
+            cast(table1.c.name, Numeric),
+            literal('x'),
         )
         for col, key, expr, label in (
             (table1.c.name, 'name', 'mytable.name', None),
@@ -2108,7 +2113,8 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
                         'CAST(mytable.name AS NUMERIC)', 'anon_1'),
             (t1.c.col1, 'col1', 'mytable.col1', None),
             (column('some wacky thing'), 'some wacky thing',
-                '"some wacky thing"', '')
+                '"some wacky thing"', ''),
+            (exprs[3], exprs[3].key, ":param_1", "anon_1")
         ):
             if getattr(col, 'table', None) is not None:
                 t = col.table
index 10306372b2078d5d958d4348034669d9c90f4661..a1b64d86285f1171eac947f1505268beca2d512b 100644 (file)
@@ -203,7 +203,7 @@ class UpdateTest(_UpdateFromTestBase, fixtures.TablesTest, AssertsCompiledSQL):
         """
         table1 = self.tables.mytable
         expr = func.foo(table1.c.myid)
-        assert not hasattr(expr, 'key')
+        eq_(expr.key, None)
         self.assert_compile(table1.update().values({expr: 'bar'}),
             'UPDATE mytable SET foo(myid)=:param_1')