]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- improved behavior of text() expressions when used as
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 26 Apr 2008 16:34:14 +0000 (16:34 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 26 Apr 2008 16:34:14 +0000 (16:34 +0000)
FROM clauses, such as select().select_from(text("sometext"))
[ticket:1014]
- removed _TextFromClause; _TextClause just adds necessary FromClause descriptors
at the class level

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

diff --git a/CHANGES b/CHANGES
index c23442baf140f096793491c78f3f94e9ddc3644f..102aa725db62b49786a9c47b2f0cf9ee699bbdcb 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -47,6 +47,10 @@ CHANGES
     - Fixed bug with union() when applied to non-Table connected
       select statements
 
+    - improved behavior of text() expressions when used as 
+      FROM clauses, such as select().select_from(text("sometext"))
+      [ticket:1014]
+      
 - engines
     - Pool listeners can now be provided as a dictionary of
       callables or a (possibly partial) duck-type of
index 8c439ce2cbc728de830ab6efe538e27cd305bce2..94c145613d9266d3ac6facb24be71958d8efb52c 100644 (file)
@@ -1788,12 +1788,6 @@ class FromClause(Selectable):
     def _populate_column_collection(self):
         pass
 
-class _TextFromClause(FromClause):
-    __visit_name__ = 'fromclause'
-
-    def __init__(self, text):
-        self.name = text
-
 class _BindParamClause(ClauseElement, _CompareMixin):
     """Represent a bind parameter.
 
@@ -1914,6 +1908,9 @@ class _TextClause(ClauseElement):
 
     _bind_params_regex = re.compile(r'(?<![:\w\x5c]):(\w+)(?!:)', re.UNICODE)
 
+    _hide_froms = []
+    oid_column = None
+
     def __init__(self, text = "", bind=None, bindparams=None, typemap=None, autocommit=False):
         self._bind = bind
         self.bindparams = {}
@@ -1949,7 +1946,7 @@ class _TextClause(ClauseElement):
 
     def _get_from_objects(self, **modifiers):
         return []
-
+    
     def supports_execution(self):
         return True
 
@@ -3007,7 +3004,7 @@ class Select(_SelectBaseMixin, FromClause):
         
         if from_obj:
             self._froms = util.Set([
-                _is_literal(f) and _TextFromClause(f) or f
+                _is_literal(f) and _TextClause(f) or f
                 for f in util.to_list(from_obj)
             ])
         else:
@@ -3184,7 +3181,7 @@ class Select(_SelectBaseMixin, FromClause):
 
         s = self._generate()
         if _is_literal(fromclause):
-            fromclause = _TextFromClause(fromclause)
+            fromclause = _TextClause(fromclause)
 
         s._froms = s._froms.union([fromclause])
         return s
@@ -3261,7 +3258,7 @@ class Select(_SelectBaseMixin, FromClause):
 
         """
         if _is_literal(fromclause):
-            fromclause = _TextFromClause(fromclause)
+            fromclause = _TextClause(fromclause)
 
         self._froms = self._froms.union([fromclause])
 
index 1e96835926e9671b9a6b775564d1e233639f40c1..08faae096865315131c9fedb65e69f93bcb767ca 100644 (file)
@@ -717,6 +717,15 @@ FROM mytable, myothertable WHERE foo.id = foofoo(lala) AND datetime(foo) = Today
         ),
         "SELECT t.myid, t.name, t.description, foo.f FROM mytable AS t, (select f from bar where lala=heyhey) foo WHERE foo.f = t.id")
 
+        # test Text embedded within select_from(), using binds
+        generate_series = text("generate_series(:x, :y, :z) as s(a)", bindparams=[bindparam('x'), bindparam('y'), bindparam('z')])
+
+        s =select([(func.current_date() + literal_column("s.a")).label("dates")]).select_from(generate_series)
+        self.assert_compile(s, "SELECT CURRENT_DATE + s.a AS dates FROM generate_series(:x, :y, :z) as s(a)", checkparams={'y': None, 'x': None, 'z': None})
+        
+        self.assert_compile(s.params(x=5, y=6, z=7), "SELECT CURRENT_DATE + s.a AS dates FROM generate_series(:x, :y, :z) as s(a)", checkparams={'y': 6, 'x': 5, 'z': 7})
+        
+
     def test_literal(self):
         self.assert_compile(select([literal("foo") + literal("bar")], from_obj=[table1]),
             "SELECT :param_1 || :param_2 AS anon_1 FROM mytable")
@@ -1042,6 +1051,8 @@ UNION SELECT mytable.myid FROM mytable"
         s = select([table1], or_(table1.c.myid==7, table1.c.myid==8, table1.c.myid==bindparam('myid_1')))
         self.assertRaisesMessage(exceptions.CompileError, "conflicts with unique bind parameter of the same name", str, s)
 
+
+
     def test_bind_as_col(self):
         t = table('foo', column('id'))