From d56d420e809588d8dea8f36fd4ae3a8b4204be54 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 26 Mar 2010 14:47:53 -0600 Subject: mssql+mxodbc should use executedirect for all selects and execute for insert/update/delete. To support this, an is_crud property has been added to the DefaultExecutionContext. The behavior is forcable either way per execution using execution_options(native_odbc_parameters=True|False). Some tests have been added to demonstrate usage. (patch by zzzeek committed by bradallen) --- lib/sqlalchemy/connectors/mxodbc.py | 30 ++++++++++++++++++++---------- lib/sqlalchemy/engine/default.py | 5 ++++- 2 files changed, 24 insertions(+), 11 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/connectors/mxodbc.py b/lib/sqlalchemy/connectors/mxodbc.py index 4476ffd78..f50bff7da 100644 --- a/lib/sqlalchemy/connectors/mxodbc.py +++ b/lib/sqlalchemy/connectors/mxodbc.py @@ -97,10 +97,10 @@ class MxODBCConnector(Connector): """ opts = url.translate_connect_args(username='user') opts.update(url.query) - args = opts['host'], - kwargs = {'user':opts['user'], - 'password': opts['password']} - return args, kwargs + args = opts.pop('host') + opts.pop('port', None) + opts.pop('database', None) + return (args,), opts def is_disconnect(self, e): # eGenix recommends checking connection.closed here, @@ -126,10 +126,20 @@ class MxODBCConnector(Connector): return tuple(version) def do_execute(self, cursor, statement, parameters, context=None): - # temporary workaround until a more comprehensive solution can - # be found for controlling when to use executedirect - try: - cursor.execute(statement, parameters) - except (InterfaceError, ProgrammingError), e: - warnings.warn("cursor.execute failed; falling back to executedirect") + if context: + native_odbc_execute = context.execution_options.\ + get('native_odbc_execute', 'auto') + if native_odbc_execute is True: + # user specified native_odbc_execute=True + cursor.execute(statement, parameters) + elif native_odbc_execute is False: + # user specified native_odbc_execute=False + cursor.executedirect(statement, parameters) + elif context.is_crud: + # statement is UPDATE, DELETE, INSERT + cursor.execute(statement, parameters) + else: + # all other statements + cursor.executedirect(statement, parameters) + else: cursor.executedirect(statement, parameters) diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 720edf66c..6fb0a14a5 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -381,7 +381,10 @@ class DefaultExecutionContext(base.ExecutionContext): self.execution_options = self.execution_options.union(connection._execution_options) self.cursor = self.create_cursor() - + @util.memoized_property + def is_crud(self): + return self.isinsert or self.isupdate or self.isdelete + @util.memoized_property def should_autocommit(self): autocommit = self.execution_options.get('autocommit', -- cgit v1.2.1