]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed missing _label attribute on Function object, others
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 1 Feb 2009 18:20:20 +0000 (18:20 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 1 Feb 2009 18:20:20 +0000 (18:20 +0000)
when used in a select() with use_labels (such as when used
in an ORM column_property()).  [ticket:1302]

CHANGES
lib/sqlalchemy/orm/query.py
lib/sqlalchemy/sql/expression.py
test/sql/functions.py
test/sql/select.py

diff --git a/CHANGES b/CHANGES
index 5753d30f604e651e405006b14fb4728409067116..bc3ce8df601ad51f713316f8638568831ab56694 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -18,6 +18,10 @@ CHANGES
       determined.
       
 - sql
+    - Fixed missing _label attribute on Function object, others
+      when used in a select() with use_labels (such as when used
+      in an ORM column_property()).  [ticket:1302]
+      
     - the __selectable__() interface has been replaced entirely
       by __clause_element__().
 
index 87019521be9f4fe0607de3caf83b663ce4797fc0..50f51355f448091662e3b749fa9759d04453957c 100644 (file)
@@ -2006,7 +2006,12 @@ class _ColumnEntity(_QueryEntity):
         if not isinstance(column, sql.ColumnElement):
             raise sa_exc.InvalidRequestError("Invalid column expression '%r'" % column)
 
-        if not hasattr(column, '_label'):
+        # if the Column is unnamed, give it a
+        # label() so that mutable column expressions
+        # can be located in the result even
+        # if the expression's identity has been changed
+        # due to adaption
+        if not column._label:
             column = column.label(None)
 
         query._entities.append(self)
index 1eada57dc7b2a2edcb8537c3e89acd27f6159043..f790555bc9980e638e4c4a4c79f780b08ce1e818 100644 (file)
@@ -1566,7 +1566,8 @@ class ColumnElement(ClauseElement, _CompareMixin):
     primary_key = False
     foreign_keys = []
     quote = None
-
+    _label = None
+    
     @property
     def _select_iterable(self):
         return (self, )
index ac9b7e3292a831943acbbe3010752b02024e3678..1519575036901bcb97588f3d96316f739ec6d9d1 100644 (file)
@@ -37,7 +37,11 @@ class CompileTest(TestBase, AssertsCompiledSQL):
                     GenericFunction.__init__(self, args=[arg], **kwargs)
                 
             self.assert_compile(fake_func('foo'), "fake_func(%s)" % bindtemplate % {'name':'param_1', 'position':1}, dialect=dialect)
-    
+            
+    def test_use_labels(self):
+        self.assert_compile(select([func.foo()], use_labels=True), 
+            "SELECT foo() AS foo_1"
+        )
     def test_underscores(self):
         self.assert_compile(func.if_(), "if()")
         
index 2b721ba102aebb1827648dbe68f7d353a62e7913..aeb53bf195a17e9936cc93dcdef418d58b38a177 100644 (file)
@@ -131,6 +131,30 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
             select([ClauseList(column('a'), column('b'))]).select_from('sometable'), 
             'SELECT a, b FROM sometable'
         )
+        
+    def test_use_labels(self):
+        self.assert_compile(
+            select([table1.c.myid==5], use_labels=True),
+            "SELECT mytable.myid = :myid_1 AS anon_1 FROM mytable"
+        )
+
+        self.assert_compile(
+            select([func.foo()], use_labels=True),
+            "SELECT foo() AS foo_1"
+        )
+
+        self.assert_compile(
+            select([not_(True)], use_labels=True),
+            "SELECT NOT :param_1"       # TODO: should this make an anon label ??
+        )
+
+        self.assert_compile(
+            select([cast("data", sqlite.SLInteger)], use_labels=True),      # this will work with plain Integer in 0.6
+            "SELECT CAST(:param_1 AS INTEGER) AS anon_1"
+        )
+        
+        
+        
     def test_nested_uselabels(self):
         """test nested anonymous label generation.  this
         essentially tests the ANONYMOUS_LABEL regex.