From ba5337982e76e587b2c5148285b41a9a399806aa Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 27 Feb 2007 19:04:43 +0000 Subject: [PATCH] - fixed function execution with explicit connections, when you dont explicitly say "select()" off the function, i.e. conn.execute(func.dosomething()) --- CHANGES | 3 +++ lib/sqlalchemy/engine/base.py | 13 ++++++++++--- test/sql/query.py | 10 ++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 9c7d45f29e..96d70b2cb5 100644 --- 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 diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 10001e8a34..0a53da9284 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -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, diff --git a/test/sql/query.py b/test/sql/query.py index a7cb52dc45..f9ba9409d6 100644 --- a/test/sql/query.py +++ b/test/sql/query.py @@ -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""" -- 2.47.2