]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Query.statement, Query.subquery(), etc. now transfer
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 15 Jun 2010 00:07:44 +0000 (20:07 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 15 Jun 2010 00:07:44 +0000 (20:07 -0400)
the values of bind parameters, i.e. those specified
by query.params(), into the resulting SQL expression.
Previously the values would not be transferred
and bind parameters would come out as None.

- Subquery-eager-loading now works with Query objects
which include params(), as well as get() Queries.

CHANGES
lib/sqlalchemy/orm/query.py
lib/sqlalchemy/orm/strategies.py
test/orm/test_query.py
test/orm/test_subquery_relations.py

diff --git a/CHANGES b/CHANGES
index 94e4961c5cbe331d1f6c87cd208366963e0444e4..53c90278baf00dd916e07abde009021626a43e76 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -12,6 +12,15 @@ CHANGES
     in one flush would fail to insert a row for both
     sides.  Regression from 0.5. [ticket:1824]
     
+  - Query.statement, Query.subquery(), etc. now transfer
+    the values of bind parameters, i.e. those specified
+    by query.params(), into the resulting SQL expression.
+    Previously the values would not be transferred
+    and bind parameters would come out as None.
+    
+  - Subquery-eager-loading now works with Query objects
+    which include params(), as well as get() Queries.
+    
 - sql
   - The warning emitted by the Unicode and String types
     with convert_unicode=True no longer embeds the actual
index ae6624d4739c6ee3978c8108cf7caf8efec0878d..50be13088e30de8fcc97846501cd014ab10ca0e0 100644 (file)
@@ -399,8 +399,11 @@ class Query(object):
         
         """
 
-        return self._compile_context(labels=self._with_labels).\
-                        statement._annotate({'_halt_adapt': True})
+        stmt = self._compile_context(labels=self._with_labels).\
+                        statement
+        if self._params:
+            stmt = stmt.params(self._params)
+        return stmt._annotate({'_halt_adapt': True})
 
     def subquery(self):
         """return the full SELECT statement represented by this Query, 
index 5b5dd312d82cfd1fce63e4b601e50f1d5940de21..62602ff37bd03a61884d2e57f8fedfb98f1c60df 100644 (file)
@@ -700,6 +700,7 @@ class SubqueryLoader(AbstractRelationshipLoader):
         # reformat the original query
         # to look only for significant columns
         q = orig_query._clone()
+
         # TODO: why does polymporphic etc. require hardcoding 
         # into _adapt_col_list ?  Does query.add_columns(...) work
         # with polymorphic loading ?
index ceb78ad1f181d8433e08314c07ad8e3dbaad7deb..d3c5c75f77e66549ae99afd694f22f562cf429cc 100644 (file)
@@ -584,6 +584,14 @@ class ExpressionTest(QueryTest, AssertsCompiledSQL):
         
         eq_(User(id=7), q.one())
         
+    def test_param_transfer(self):
+        session = create_session()
+        
+        q = session.query(User.id).filter(User.id==bindparam('foo')).params(foo=7).subquery()
+        
+        q = session.query(User).filter(User.id==q)
+        
+        eq_(User(id=7), q.one())
         
     def test_in(self):
         session = create_session()
index 5b9a46d075b7a5d0f40eb0b7575962d78f2f4751..827630c3d0e60109ef6b29cc0242292cbf0688e0 100644 (file)
@@ -1,7 +1,7 @@
 from sqlalchemy.test.testing import eq_, is_, is_not_
 from sqlalchemy.test import testing
 from sqlalchemy.test.schema import Table, Column
-from sqlalchemy import Integer, String, ForeignKey
+from sqlalchemy import Integer, String, ForeignKey, bindparam
 from sqlalchemy.orm import backref, subqueryload, subqueryload_all, \
                 mapper, relationship, clear_mappers,\
                 create_session, lazyload, aliased, joinedload,\
@@ -42,6 +42,45 @@ class EagerTest(_fixtures.FixtureTest, testing.AssertsCompiledSQL):
             )
         self.assert_sql_count(testing.db, go, 2)
 
+    @testing.resolve_artifact_names
+    def test_from_get(self):
+        mapper(User, users, properties={
+            'addresses':relationship(
+                            mapper(Address, addresses), 
+                            order_by=Address.id)
+        })
+        sess = create_session()
+        
+        q = sess.query(User).options(subqueryload(User.addresses))
+        def go():
+            eq_(
+                    User(id=7, addresses=[
+                            Address(id=1, email_address='jack@bean.com')]),
+                    q.get(7)
+            )
+        
+        self.assert_sql_count(testing.db, go, 2)
+
+    @testing.resolve_artifact_names
+    def test_from_params(self):
+        mapper(User, users, properties={
+            'addresses':relationship(
+                            mapper(Address, addresses), 
+                            order_by=Address.id)
+        })
+        sess = create_session()
+
+        q = sess.query(User).options(subqueryload(User.addresses))
+        def go():
+            eq_(
+                    User(id=7, addresses=[
+                            Address(id=1, email_address='jack@bean.com')]),
+                    q.filter(User.id==bindparam('foo')).params(foo=7).one()
+            )
+
+        self.assert_sql_count(testing.db, go, 2)
+        
+        
     @testing.resolve_artifact_names
     def test_many_to_many(self):
         mapper(Keyword, keywords)