]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- as_scalar(), label() can be called on a selectable
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 18 Sep 2010 17:34:04 +0000 (13:34 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 18 Sep 2010 17:34:04 +0000 (13:34 -0400)
which contains a Column that is not yet named.
[ticket:1862]

CHANGES
lib/sqlalchemy/sql/expression.py
test/sql/test_compiler.py

diff --git a/CHANGES b/CHANGES
index 87ca1c63e585b5da8642559ab1273eb933d8db90..7d72f8769fb7e1a05e899be1a9afc428a308cb9b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -98,7 +98,11 @@ CHANGES
      exported to the columns collection of an enclosing
      select() construct, or if any construct involving
      that column is compiled before its name is 
-     assigned.  [ticket:1862]
+     assigned.
+
+   - as_scalar(), label() can be called on a selectable
+     which contains a Column that is not yet named.
+     [ticket:1862]
      
 - engine
    
index 70d5f13fc05faff47c882020d15277bf00a5f937..5df3b879432f2d4fd186b31541b3ca3893e21174 100644 (file)
@@ -3685,8 +3685,7 @@ class _ScalarSelect(_Grouping):
 
     def __init__(self, element):
         self.element = element
-        cols = list(element.c)
-        self.type = cols[0].type
+        self.type = element._scalar_type()
 
     @property
     def columns(self):
@@ -3737,7 +3736,10 @@ class CompoundSelect(_SelectBaseMixin, FromClause):
             self.selects.append(s.self_group(self))
 
         _SelectBaseMixin.__init__(self, **kwargs)
-
+    
+    def _scalar_type(self):
+        return self.selects[0]._scalar_type()
+        
     def self_group(self, against=None):
         return _FromGrouping(self)
 
@@ -3910,6 +3912,11 @@ class Select(_SelectBaseMixin, FromClause):
 
         return froms
 
+    def _scalar_type(self):
+        elem = self._raw_columns[0]
+        cols = list(elem._select_iterable)
+        return cols[0].type
+
     @property
     def froms(self):
         """Return the displayed list of FromClause elements."""
index 355e5dc70cbbd4c3da961f993186ce4bb4b9149d..4c712ce3801377e718d5a028b49a6f5a13701533 100644 (file)
@@ -1909,19 +1909,23 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
             lambda: sel1.c
         )
         
+        # calling label or as_scalar doesn't compile
+        # anything.  
+        sel2 = select([func.substr(my_str, 2, 3)]).label('my_substr')
+        
         assert_raises_message(
             exc.CompileError,
             "Cannot compile Column object until it's 'name' is assigned.",
-            lambda: select([func.substr(my_str, 2, 3)]).label('my_substr')
+            str, sel2
         )
         
+        sel3 = select([my_str]).as_scalar()
         assert_raises_message(
-            exc.InvalidRequestError,
-            "Cannot initialize a sub-selectable with this Column",
-            lambda: select([my_str]).as_scalar()
+            exc.CompileError,
+            "Cannot compile Column object until it's 'name' is assigned.",
+            str, sel3
         )
         
-        
         my_str.name = 'foo'
         
         self.assert_compile(
@@ -1929,10 +1933,15 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
             "SELECT foo",
         )
         self.assert_compile(
-            select([func.substr(my_str, 2, 3)]).label('my_substr'),
+            sel2,
             '(SELECT substr(foo, :substr_2, :substr_3) AS substr_1)',
         )
         
+        self.assert_compile(
+            sel3,
+            "(SELECT foo)"
+        )
+        
     def test_naming(self):
         f1 = func.hoho(table1.c.name)
         s1 = select([table1.c.myid, table1.c.myid.label('foobar'),