From 797d9c3029c0660a43b71130cac093343e15f0dc Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 5 Jan 2013 09:34:59 +0900 Subject: [PATCH 1/4] Support `autocommit=True` for constructor argument. --- MySQLdb/connections.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 6bc1613f..aeac731b 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -139,6 +139,9 @@ class object, used to create cursors (keyword only) local_infile integer, non-zero enables LOAD LOCAL INFILE; zero disables + autocommit + If True, autocommit is enabled. + There are a number of undocumented, non-standard methods. See the documentation for the MySQL C API for some hints on what they do. @@ -224,7 +227,7 @@ def string_decoder(s): self.encoders[types.StringType] = string_literal self.encoders[types.UnicodeType] = unicode_literal self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS - if self._transactional: + if self._transactional and not kwargs2.pop('autocommit', False): # PEP-249 requires autocommit to be initially off self.autocommit(False) self.messages = [] From c1b8e8a0474c44f60ed8e08b3be8dc7f293ce7b8 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 21 May 2013 12:09:55 +0900 Subject: [PATCH 2/4] autocommit=None means using server default. --- MySQLdb/connections.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index aeac731b..2b423e7b 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -140,7 +140,9 @@ class object, used to create cursors (keyword only) integer, non-zero enables LOAD LOCAL INFILE; zero disables autocommit + If False (default), autocommit is disabled. If True, autocommit is enabled. + If None, autocommit isn't set and server default is used. There are a number of undocumented, non-standard methods. See the documentation for the MySQL C API for some hints on what they do. @@ -227,9 +229,11 @@ def string_decoder(s): self.encoders[types.StringType] = string_literal self.encoders[types.UnicodeType] = unicode_literal self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS - if self._transactional and not kwargs2.pop('autocommit', False): + if self._transactional: # PEP-249 requires autocommit to be initially off - self.autocommit(False) + autocommit = kwargs2.pop('autocommit', False) + if autocommit is not None: + self.autocommit(bool(True)) self.messages = [] def cursor(self, cursorclass=None): From 470eb56c4ddb742c70d9e11c4f6601ccf4071fe5 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 21 May 2013 13:32:23 +0900 Subject: [PATCH 3/4] Fix typo. --- MySQLdb/connections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 2b423e7b..908d72aa 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -233,7 +233,7 @@ def string_decoder(s): # PEP-249 requires autocommit to be initially off autocommit = kwargs2.pop('autocommit', False) if autocommit is not None: - self.autocommit(bool(True)) + self.autocommit(bool(autocommit)) self.messages = [] def cursor(self, cursorclass=None): From f064692a36c49713463fccb5befa1e13a9beb8d4 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 22 May 2013 22:33:20 +0900 Subject: [PATCH 4/4] 'BEGIN' on __enter__ if autocommit is enabled. --- MySQLdb/connections.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 908d72aa..40a61508 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -187,6 +187,9 @@ class object, used to create cursors (keyword only) kwargs2['client_flag'] = client_flag + # PEP-249 requires autocommit to be initially off + autocommit = kwargs2.pop('autocommit', False) + super(Connection, self).__init__(*args, **kwargs2) self.cursorclass = cursorclass self.encoders = dict([ (k, v) for k, v in conv.items() @@ -229,13 +232,29 @@ def string_decoder(s): self.encoders[types.StringType] = string_literal self.encoders[types.UnicodeType] = unicode_literal self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS + self._autocommit = None if self._transactional: - # PEP-249 requires autocommit to be initially off - autocommit = kwargs2.pop('autocommit', False) if autocommit is not None: - self.autocommit(bool(autocommit)) + self.autocommit(autocommit) self.messages = [] + def autocommit(self, on): + on = bool(on) + _mysql.connection.autocommit(self, on) + self._autocommit = on + + def get_autocommit(self): + if self._autocommit is None: + self._update_autocommit() + return self._autocommit + + def _update_autocommit(self): + cursor = cursors.Cursor(self) + cursor.execute("SELECT @@AUTOCOMMIT") + row = cursor.fetchone() + self._autocommit = bool(row[0]) + cursor.close() + def cursor(self, cursorclass=None): """ @@ -248,6 +267,8 @@ def cursor(self, cursorclass=None): return (cursorclass or self.cursorclass)(self) def __enter__(self): + if self.get_autocommit(): + self.query("BEGIN") return self.cursor() def __exit__(self, exc, value, tb):