Commits

Mike Bayer committed aa868af

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

Comments (0)

Files changed (2)

lib/sqlalchemy/engine/base.py

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

lib/sqlalchemy/engine/default.py

 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"""
 
         # 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):
         self._process_defaults()