From a7eb91710a11afcad0469252057a67277ca2a1dd Mon Sep 17 00:00:00 2001 From: Dave Stinson Date: Thu, 5 Feb 2015 08:51:33 +0000 Subject: [PATCH 1/2] Provide support for multi-statement init_command. --- pymysql/connections.py | 3 +++ pymysql/tests/test_connection.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/pymysql/connections.py b/pymysql/connections.py index e91bba8..d833298 100644 --- a/pymysql/connections.py +++ b/pymysql/connections.py @@ -844,6 +844,9 @@ class Connection(object): if self.init_command is not None: c = self.cursor() c.execute(self.init_command) + # Wind pass any additional results + while self._result.has_next: + self.next_result() self.commit() if self.autocommit_mode is not None: diff --git a/pymysql/tests/test_connection.py b/pymysql/tests/test_connection.py index 7580ca6..db75b25 100644 --- a/pymysql/tests/test_connection.py +++ b/pymysql/tests/test_connection.py @@ -69,3 +69,13 @@ class TestConnection(base.PyMySQLTestCase): # error occures while reading, not writing because of socket buffer. #self.assertEquals(cm.exception.args[0], 2006) self.assertIn(cm.exception.args[0], (2006, 2013)) + + def test_init_command(self): + conn = pymysql.connect( + init_command='SELECT "bar"; SELECT "baz"', + **self.databases[0] + ) + c = conn.cursor() + c.execute('select "foobar";') + self.assertEqual(('foobar',), c.fetchone()) + conn.close() From ac0cc7e51a3109e301767cbc290b28f1474847aa Mon Sep 17 00:00:00 2001 From: Dave Stinson Date: Thu, 5 Feb 2015 14:11:17 +0000 Subject: [PATCH 2/2] Explicitly close connection to drain results. --- pymysql/connections.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pymysql/connections.py b/pymysql/connections.py index d833298..147e4ee 100644 --- a/pymysql/connections.py +++ b/pymysql/connections.py @@ -844,9 +844,7 @@ class Connection(object): if self.init_command is not None: c = self.cursor() c.execute(self.init_command) - # Wind pass any additional results - while self._result.has_next: - self.next_result() + c.close() self.commit() if self.autocommit_mode is not None: