]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a version of database.Connection.execute that returns the rowcount.
authorBen Darnell <ben@bendarnell.com>
Sun, 28 Aug 2011 06:24:56 +0000 (23:24 -0700)
committerBen Darnell <ben@bendarnell.com>
Sun, 28 Aug 2011 06:24:56 +0000 (23:24 -0700)
Closes #333.

tornado/database.py

index 4a437fbf708383d8dd1f65d9176620daab00f0b5..97717137672005f62bc487f397a06a3984550b9e 100644 (file)
@@ -123,7 +123,13 @@ class Connection(object):
         else:
             return rows[0]
 
+    # rowcount is a more reasonable default return value than lastrowid,
+    # but for historical compatibility execute() must return lastrowid.
     def execute(self, query, *parameters):
+        """Executes the given query, returning the lastrowid from the query."""
+        return self.execute_lastrowid(query, *parameters)
+
+    def execute_lastrowid(self, query, *parameters):
         """Executes the given query, returning the lastrowid from the query."""
         cursor = self._cursor()
         try:
@@ -132,9 +138,25 @@ class Connection(object):
         finally:
             cursor.close()
 
+    def execute_rowcount(self, query, *parameters):
+        """Executes the given query, returning the rowcount from the query."""
+        cursor = self._cursor()
+        try:
+            self._execute(cursor, query, parameters)
+            return cursor.rowcount
+        finally:
+            cursor.close()
+
     def executemany(self, query, parameters):
         """Executes the given query against all the given param sequences.
 
+        We return the lastrowid from the query.
+        """
+        return self.executemany_lastrowid(query, parameters)
+
+    def executemany_lastrowid(self, query, parameters):
+        """Executes the given query against all the given param sequences.
+
         We return the lastrowid from the query.
         """
         cursor = self._cursor()
@@ -144,6 +166,18 @@ class Connection(object):
         finally:
             cursor.close()
 
+    def executemany_rowcount(self, query, parameters):
+        """Executes the given query against all the given param sequences.
+
+        We return the rowcount from the query.
+        """
+        cursor = self._cursor()
+        try:
+            cursor.executemany(query, parameters)
+            return cursor.rowcount
+        finally:
+            cursor.close()
+
     def _ensure_connected(self):
         # Mysql by default closes client connections that are idle for
         # 8 hours, but the client library does not report this fact until