1. Richard Mitchell
  2. sqlalchemy


Mike Bayer  committed aa868af

added "should_commit()" hook to ExecutionContext. dialects can override with specific tests

  • Participants
  • Parent commits 2044583
  • Branches default

Comments (0)

Files changed (2)

File lib/sqlalchemy/engine/base.py

View file
         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.
             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():
     def _autorollback(self):
-        self._autocommit(context.statement)
+        self._autocommit(context)
     def __execute(self, context):
         if context.parameters is None:

File lib/sqlalchemy/engine/default.py

View file
 from sqlalchemy.engine import base
 class DefaultDialect(base.Dialect):
     """Default implementation of Dialect"""
         # are unhashable).  So far Oracle can return it.
         return {}
     def create_execution_context(self, **kwargs):
         return DefaultExecutionContext(self, **kwargs)
     def result(self):
         return self.get_result_proxy()
+    def should_autocommit(self):
+        return AUTOCOMMIT_REGEXP.match(self.statement.lstrip())
     def pre_exec(self):