From 8a47e823380f03c4112476c7754592ab9ee2059b Mon Sep 17 00:00:00 2001 From: Luke Weber Date: Thu, 12 May 2016 17:53:42 -0700 Subject: [PATCH] Add prepareddict cursor --- lib/mysql/connector/connection.py | 5 +++-- lib/mysql/connector/cursor.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/mysql/connector/connection.py b/lib/mysql/connector/connection.py index 0aaa78c..37c69d9 100644 --- a/lib/mysql/connector/connection.py +++ b/lib/mysql/connector/connection.py @@ -41,7 +41,7 @@ CursorBase, MySQLCursor, MySQLCursorRaw, MySQLCursorBuffered, MySQLCursorBufferedRaw, MySQLCursorPrepared, MySQLCursorDict, MySQLCursorBufferedDict, MySQLCursorNamedTuple, - MySQLCursorBufferedNamedTuple) + MySQLCursorBufferedNamedTuple, MySQLCursorPreparedDict) from .network import MySQLUnixSocket, MySQLTCPSocket from .protocol import MySQLProtocol from .utils import int4store @@ -835,7 +835,8 @@ def cursor(self, buffered=None, raw=None, prepared=None, cursor_class=None, 5: MySQLCursorBufferedDict, 8: MySQLCursorNamedTuple, 9: MySQLCursorBufferedNamedTuple, - 16: MySQLCursorPrepared + 16: MySQLCursorPrepared, + 20: MySQLCursorPreparedDict } try: return (types[cursor_type])(self) diff --git a/lib/mysql/connector/cursor.py b/lib/mysql/connector/cursor.py index 7cc7362..9d8f1d6 100644 --- a/lib/mysql/connector/cursor.py +++ b/lib/mysql/connector/cursor.py @@ -1175,6 +1175,20 @@ def fetchall(self): return rows +class MySQLCursorPreparedDict(MySQLCursorPrepared): + def fetchone(self): + row = super().fetchone() + return dict(zip(self.column_names, row)) if row else None + + def fetchmany(self, size=None): + rows = super().fetchmany(size=size) + return [dict(zip(self.column_names, row)) for row in rows] + + def fetchall(self): + rows = super().fetchall() + return [dict(zip(self.column_names, row)) for row in rows] + + class MySQLCursorDict(MySQLCursor): """ Cursor fetching rows as dictionaries.