From: Ben Darnell Date: Sun, 28 Aug 2011 06:24:56 +0000 (-0700) Subject: Add a version of database.Connection.execute that returns the rowcount. X-Git-Tag: v2.1.0~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7f6c66e4e940698fb17f399c1be1ed632cb39478;p=thirdparty%2Ftornado.git Add a version of database.Connection.execute that returns the rowcount. Closes #333. --- 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