From 7e15cf86171ca1dd95467e37abda107138607ca3 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 3 Jul 2014 20:52:53 +0900 Subject: [PATCH] Add `waiter` to MySQLdb.Connection. --- MySQLdb/connections.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index cd80376..8d5c2d8 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -63,10 +63,10 @@ class Connection(_mysql.connection): """MySQL Database Connection Object""" default_cursor = cursors.Cursor + waiter = None def __init__(self, *args, **kwargs): """ - Create a connection to the database. It is strongly recommended that you only use keyword parameters. Consult the MySQL C API documentation for more information. @@ -150,9 +150,13 @@ class Connection(_mysql.connection): If True, autocommit is enabled. If None, autocommit isn't set and server default is used. + waiter + Callable accepts fd as an argument. It is called after sending + query and before reading response. + This is useful when using with greenlet and async io. + There are a number of undocumented, non-standard methods. See the documentation for the MySQL C API for some hints on what they do. - """ from MySQLdb.constants import CLIENT, FIELD_TYPE from MySQLdb.converters import conversions @@ -195,6 +199,7 @@ class Connection(_mysql.connection): # PEP-249 requires autocommit to be initially off autocommit = kwargs2.pop('autocommit', False) + self.waiter = kwargs2.pop('waiter', None) super(Connection, self).__init__(*args, **kwargs2) self.cursorclass = cursorclass @@ -266,6 +271,14 @@ class Connection(_mysql.connection): """ return (cursorclass or self.cursorclass)(self) + def query(self, query): + if self.waiter is not None: + self.send_query(query) + self.waiter(self.fileno()) + self.read_query_result() + else: + _mysql.connection.query(self, query) + def __enter__(self): if self.get_autocommit(): self.query("BEGIN")