From: Mike Bayer Date: Sun, 12 Aug 2007 19:37:47 +0000 (+0000) Subject: added "should_commit()" hook to ExecutionContext. dialects can override with specifi... X-Git-Tag: rel_0_4beta2~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8b004dc7af444cd7f0e5394835b9e2f90a7c9434;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added "should_commit()" hook to ExecutionContext. dialects can override with specific tests --- diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index a4e41e996c..56a07018b5 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -373,6 +373,11 @@ class ExecutionContext(object): raise NotImplementedError() + def should_autocommit(self): + """return True if this context's statement should be 'committed' automatically in a non-transactional context""" + + raise NotImplementedError() + def last_inserted_ids(self): """Return the list of the primary key values for the last insert statement executed. @@ -695,11 +700,11 @@ class Connection(Connectable): self.__engine.dialect.do_commit_twophase(self, xid, is_prepared) self.__transaction = None - def _autocommit(self, statement): + def _autocommit(self, context): """When no Transaction is present, this is called after executions to provide "autocommit" behavior.""" # TODO: have the dialect determine if autocommit can be set on the connection directly without this # extra step - if not self.in_transaction() and re.match(r'UPDATE|INSERT|CREATE|DELETE|DROP|ALTER', statement.lstrip(), re.I): + if not self.in_transaction() and context.should_autocommit(): self._commit_impl() def _autorollback(self): @@ -782,7 +787,7 @@ class Connection(Connectable): self.__executemany(context) else: self.__execute(context) - self._autocommit(context.statement) + self._autocommit(context) def __execute(self, context): if context.parameters is None: diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index f7410bb0f8..badd86134a 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -11,6 +11,8 @@ import sys, re, random from sqlalchemy.engine import base +AUTOCOMMIT_REGEXP = re.compile(r'UPDATE|INSERT|CREATE|DELETE|DROP|ALTER', re.I) + class DefaultDialect(base.Dialect): """Default implementation of Dialect""" @@ -27,7 +29,7 @@ class DefaultDialect(base.Dialect): # are unhashable). So far Oracle can return it. return {} - + def create_execution_context(self, **kwargs): return DefaultExecutionContext(self, **kwargs) @@ -236,6 +238,9 @@ class DefaultExecutionContext(base.ExecutionContext): def result(self): return self.get_result_proxy() + + def should_autocommit(self): + return AUTOCOMMIT_REGEXP.match(self.statement.lstrip()) def pre_exec(self): self._process_defaults()