From 30b8c86a8bb29451689735ba9cab95c6e104797c Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Tue, 18 Oct 2022 16:24:37 +0530 Subject: Support server side SQL query retrieval. * gn3/monads.py: Import SSDictCursor from MySQLdb.cursors. (query_sql): Add server_side argument. --- gn3/monads.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'gn3') diff --git a/gn3/monads.py b/gn3/monads.py index 964dce2..1fe4b33 100644 --- a/gn3/monads.py +++ b/gn3/monads.py @@ -12,7 +12,7 @@ from collections import UserDict from functools import partial from typing import Any, Hashable, Iterator -from MySQLdb.cursors import DictCursor +from MySQLdb.cursors import DictCursor, SSDictCursor from pymonad.maybe import Maybe, Just, Nothing @@ -124,9 +124,15 @@ class MonadicDict(UserDict): pass -def query_sql(conn, query: str) -> Iterator[MonadicDict]: - """Execute SQL query and return a generator of MonadicDict objects.""" - with conn.cursor(DictCursor) as cursor: +def query_sql(conn, query: str, server_side: bool=False) -> Iterator[MonadicDict]: + """Execute SQL query and return a generator of MonadicDict objects. + + If server_side is False, the result set is stored in the client. If + server_side is True, the result set is stored in the server. Therefore, + when server_side is True, this query must be completed before a new one + can be executed. + """ + with conn.cursor(SSDictCursor if server_side else DictCursor) as cursor: cursor.execute(query) while row := cursor.fetchone(): yield MonadicDict(row) -- cgit v1.2.3