]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- fixed function execution with explicit connections, when you dont
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 27 Feb 2007 19:04:43 +0000 (19:04 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 27 Feb 2007 19:04:43 +0000 (19:04 +0000)
explicitly say "select()" off the function, i.e.
conn.execute(func.dosomething())

CHANGES
lib/sqlalchemy/engine/base.py
test/sql/query.py

diff --git a/CHANGES b/CHANGES
index 9c7d45f29edc5f22735887a4d6aa1cf89f4e4d84..96d70b2cb5777ab37c82f68b5cddcb3208ce5696 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -2,6 +2,9 @@
     - exists() becomes useable as a standalone selectable, not just in a 
     WHERE clause
     - correlated subqueries work inside of ORDER BY, GROUP BY
+    - fixed function execution with explicit connections, when you dont 
+    explicitly say "select()" off the function, i.e. 
+    conn.execute(func.dosomething())
 - orm:
     - a full select() construct can be passed to query.select() (which
       worked anyway), but also query.selectfirst(), query.selectone() which
index 10001e8a340f82861287a5e63dd5cf6f77786883..0a53da92841c3772ebb990393a2743100dd1d426 100644 (file)
@@ -439,7 +439,11 @@ class Connection(Connectable):
         return self.execute(object, *multiparams, **params).scalar()
 
     def execute(self, object, *multiparams, **params):
-        return Connection.executors[type(object).__mro__[-2]](self, object, *multiparams, **params)
+        for c in type(object).__mro__:
+            if c in Connection.executors:
+                return Connection.executors[c](self, object, *multiparams, **params)
+        else:
+            raise exceptions.InvalidRequestError("Unexecuteable object type: " + str(type(object)))
 
     def execute_default(self, default, **kwargs):
         return default.accept_schema_visitor(self.__engine.dialect.defaultrunner(self.__engine, self.proxy, **kwargs))
@@ -467,7 +471,10 @@ class Connection(Connectable):
                 return [multiparams[0]]
         else:
             return multiparams
-
+    
+    def execute_function(self, func, *multiparams, **params):
+        return self.execute_clauseelement(func.select(), *multiparams, **params)
+        
     def execute_clauseelement(self, elem, *multiparams, **params):
         executemany = len(multiparams) > 0
         if executemany:
@@ -478,7 +485,6 @@ class Connection(Connectable):
 
     def execute_compiled(self, compiled, *multiparams, **params):
         """Execute a sql.Compiled object."""
-
         if not compiled.can_execute:
             raise exceptions.ArgumentError("Not an executeable clause: %s" % (str(compiled)))
         cursor = self.__engine.dialect.create_cursor(self.connection)
@@ -501,6 +507,7 @@ class Connection(Connectable):
 
     # poor man's multimethod/generic function thingy
     executors = {
+        sql._Function : execute_function,
         sql.ClauseElement : execute_clauseelement,
         sql.ClauseVisitor : execute_compiled,
         schema.SchemaItem:execute_default,
index a7cb52dc450e2faf5fc918ce986f4f5b6f0ac320..f9ba9409d689e5624be6cf88fab7267610623a53 100644 (file)
@@ -232,6 +232,16 @@ class QueryTest(PersistTest):
         z = testbase.db.func.current_date().scalar()
         assert x == y == z
 
+    def test_conn_functions(self):
+        conn = testbase.db.connect()
+        try:
+            x = conn.execute(func.current_date()).scalar()
+            y = conn.execute(func.current_date().select()).scalar()
+            z = conn.scalar(func.current_date())
+        finally:
+            conn.close()
+        assert x == y == z
+        
     def test_update_functions(self):
         """test sending functions and SQL expressions to the VALUES and SET clauses of INSERT/UPDATE instances,
         and that column-level defaults get overridden"""