diff options
author | INADA Naoki <inada-n@klab.com> | 2013-05-22 22:33:20 +0900 |
---|---|---|
committer | INADA Naoki <inada-n@klab.com> | 2013-05-22 22:33:20 +0900 |
commit | f064692a36c49713463fccb5befa1e13a9beb8d4 (patch) | |
tree | 770a6fbbc1463efb79c743023576736e365d3974 | |
parent | 470eb56c4ddb742c70d9e11c4f6601ccf4071fe5 (diff) | |
download | mysqldb1-f064692a36c49713463fccb5befa1e13a9beb8d4.tar.gz |
'BEGIN' on __enter__ if autocommit is enabled.
-rw-r--r-- | MySQLdb/connections.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 908d72a..40a6150 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -187,6 +187,9 @@ class Connection(_mysql.connection): 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 @@ class Connection(_mysql.connection): 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 @@ class Connection(_mysql.connection): 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): |