]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- result sets from CRUD operations close their underlying cursor immediately.
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 14 Jun 2007 18:17:05 +0000 (18:17 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 14 Jun 2007 18:17:05 +0000 (18:17 +0000)
    will also autoclose the connection if defined for the operation; this
    allows more efficient usage of connections for successive CRUD operations
    with less chance of "dangling connections".

CHANGES
lib/sqlalchemy/databases/postgres.py
lib/sqlalchemy/databases/sqlite.py
lib/sqlalchemy/engine/base.py
lib/sqlalchemy/engine/default.py

diff --git a/CHANGES b/CHANGES
index dab2fd67aa15ccb2d791a6ec344ce36aef3d3d13..9a4d9e2093e648aafb8ee339ed595352852bbf9b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
   - result.last_inserted_ids() should return a list that is identically
     sized to the primary key constraint of the table.  values that were 
     "passively" created and not available via cursor.lastrowid will be None.
+  - result sets from CRUD operations close their underlying cursor immediately.
+    will also autoclose the connection if defined for the operation; this 
+    allows more efficient usage of connections for successive CRUD operations
+    with less chance of "dangling connections".
   - long-identifier detection fixed to use > rather than >= for 
     max ident length [ticket:589]
   - fixed bug where selectable.corresponding_column(selectable.c.col)
index 0622958262d0a5df7967d84672e83dbcf224c542..7573b5a8d7077b16ea8de72265617f863cc170a2 100644 (file)
@@ -134,11 +134,11 @@ def descriptor():
 
 class PGExecutionContext(default.DefaultExecutionContext):
 
-    def is_select(self):
-        return re.match(r'SELECT', self.statement.lstrip(), re.I) and not re.search(r'FOR UPDATE\s*$', self.statement, re.I)
-    
+    def _is_server_side(self):
+        return self.dialect.server_side_cursors and self.is_select() and not re.search(r'FOR UPDATE(?: NOWAIT)?\s*$', self.statement, re.I)
+        
     def create_cursor(self):
-        if self.dialect.server_side_cursors and self.is_select():
+        if self._is_server_side():
             # use server-side cursors:
             # http://lists.initd.org/pipermail/psycopg/2007-January/005251.html
             ident = "c" + hex(random.randint(0, 65535))[2:]
@@ -147,7 +147,7 @@ class PGExecutionContext(default.DefaultExecutionContext):
             return self.connection.connection.cursor()
 
     def get_result_proxy(self):
-        if self.dialect.server_side_cursors and self.is_select():
+        if self._is_server_side():
             return base.BufferedRowResultProxy(self)
         else:
             return base.ResultProxy(self)
index 0d5fc45e9e6433d7171b82405da29a88c702af45..53525582f6c51cc879be5a5ca8404d8f8d01926f 100644 (file)
@@ -141,6 +141,9 @@ class SQLiteExecutionContext(default.DefaultExecutionContext):
         if self.compiled.isinsert:
             self._last_inserted_ids = [self.cursor.lastrowid] + self._last_inserted_ids[1:]
         super(SQLiteExecutionContext, self).post_exec()
+
+    def is_select(self):
+        return re.match(r'SELECT|PRAGMA', self.statement.lstrip(), re.I) is not None
         
 class SQLiteDialect(ansisql.ANSIDialect):
     
index e7eb108b537dea7e56ac7d8a647d7d1b3df8ad47..b1e1ee5cc9683bb5b158fe9d94ce912af3531024 100644 (file)
@@ -861,10 +861,20 @@ class ResultProxy(object):
         self.closed = False
         self.cursor = context.cursor
         self.__echo = logging.is_debug_enabled(context.engine.logger)
-        self._init_metadata()
-        
-    rowcount = property(lambda s:s.context.get_rowcount())
-    connection = property(lambda s:s.context.connection)
+        if context.is_select():
+            self._init_metadata()
+            self._rowcount = None
+        else:
+            self._rowcount = context.get_rowcount()
+            self.close()
+            
+    connection = property(lambda self:self.context.connection)
+    def _get_rowcount(self):
+        if self._rowcount is not None:
+            return self._rowcount
+        else:
+            return self.context.get_rowcount()
+    rowcount = property(_get_rowcount)
     
     def _init_metadata(self):
         if hasattr(self, '_ResultProxy__props'):
index 19dd623bfe51d627d8645c6fe9023243dc3d74e6..cde8ee0981d4c27cc161aaa1d5ca4feb6002e9ca 100644 (file)
@@ -191,7 +191,7 @@ class DefaultExecutionContext(base.ExecutionContext):
                 return proc(params)
                 
     def is_select(self):
-        return re.match(r'SELECT', self.statement.lstrip(), re.I)
+        return re.match(r'SELECT', self.statement.lstrip(), re.I) is not None
 
     def create_cursor(self):
         return self.connection.connection.cursor()