From 7f6c66e4e940698fb17f399c1be1ed632cb39478 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sat, 27 Aug 2011 23:24:56 -0700 Subject: [PATCH] Add a version of database.Connection.execute that returns the rowcount. Closes #333. --- tornado/database.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tornado/database.py b/tornado/database.py index 4a437fbf7..977171376 100644 --- a/tornado/database.py +++ b/tornado/database.py @@ -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 -- 2.47.2