]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added "should_commit()" hook to ExecutionContext. dialects can override with specifi...
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 12 Aug 2007 19:37:47 +0000 (19:37 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 12 Aug 2007 19:37:47 +0000 (19:37 +0000)
lib/sqlalchemy/engine/base.py
lib/sqlalchemy/engine/default.py

index a4e41e996c1d8a2faaba5ac045430b7e8d7e056b..56a07018b534dd0bc562b32e03190f7905f0e3e8 100644 (file)
@@ -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:
index f7410bb0f8994a9f55a272a6a89044ddfd18b14f..badd86134aebc4e7e9702e96efd0a8693e2860aa 100644 (file)
@@ -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()