From 52894fd137e040b67604b9a27f434567e508b485 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 5 Jan 2013 09:19:43 +0900 Subject: remove unused lines. --- MySQLdb/connections.py | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'MySQLdb') diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 27de110..6bc1613 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -39,14 +39,14 @@ re_numeric_part = re.compile(r"^(\d+)") def numeric_part(s): """Returns the leading numeric part of a string. - + >>> numeric_part("20-alpha") 20 >>> numeric_part("foo") >>> numeric_part("16b") 16 """ - + m = re_numeric_part.match(s) if m: return int(m.group(1)) @@ -58,7 +58,7 @@ class Connection(_mysql.connection): """MySQL Database Connection Object""" default_cursor = cursors.Cursor - + def __init__(self, *args, **kwargs): """ @@ -68,7 +68,7 @@ class Connection(_mysql.connection): host string, host to connect - + user string, user to connect as @@ -125,7 +125,7 @@ class Connection(_mysql.connection): If supplied, the session SQL mode will be changed to this setting (MySQL-4.1 and newer). For more details and legal values, see the MySQL documentation. - + client_flag integer, flags to use or 0 (see MySQL docs or constants/CLIENTS.py) @@ -138,19 +138,17 @@ class Connection(_mysql.connection): local_infile integer, non-zero enables LOAD LOCAL INFILE; zero disables - + 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 - from weakref import proxy, WeakValueDictionary - - import types + from weakref import proxy kwargs2 = kwargs.copy() - + if 'conv' in kwargs: conv = kwargs['conv'] else: @@ -171,7 +169,7 @@ class Connection(_mysql.connection): use_unicode = True else: use_unicode = False - + use_unicode = kwargs2.pop('use_unicode', use_unicode) sql_mode = kwargs2.pop('sql_mode', '') @@ -181,14 +179,14 @@ class Connection(_mysql.connection): client_flag |= CLIENT.MULTI_STATEMENTS if client_version >= (5, 0): client_flag |= CLIENT.MULTI_RESULTS - + kwargs2['client_flag'] = client_flag super(Connection, self).__init__(*args, **kwargs2) self.cursorclass = cursorclass self.encoders = dict([ (k, v) for k, v in conv.items() if type(k) is not int ]) - + self._server_version = tuple([ numeric_part(n) for n in self.get_server_info().split('.')[:2] ]) db = proxy(self) @@ -206,7 +204,7 @@ class Connection(_mysql.connection): def string_decoder(s): return s.decode(string_decoder.charset) return string_decoder - + string_literal = _get_string_literal() self.unicode_literal = unicode_literal = _get_unicode_literal() self.string_decoder = string_decoder = _get_string_decoder() @@ -230,7 +228,7 @@ class Connection(_mysql.connection): # PEP-249 requires autocommit to be initially off self.autocommit(False) self.messages = [] - + def cursor(self, cursorclass=None): """ @@ -242,14 +240,15 @@ class Connection(_mysql.connection): """ return (cursorclass or self.cursorclass)(self) - def __enter__(self): return self.cursor() - + def __enter__(self): + return self.cursor() + def __exit__(self, exc, value, tb): if exc: self.rollback() else: self.commit() - + def literal(self, o): """ @@ -271,7 +270,7 @@ class Connection(_mysql.connection): warn("begin() is non-standard and will be removed in 1.3", DeprecationWarning, 2) self.query("BEGIN") - + if not hasattr(_mysql.connection, 'warning_count'): def warning_count(self): @@ -311,7 +310,7 @@ class Connection(_mysql.connection): raise NotSupportedError("server is too old to set sql_mode") self.query("SET SESSION sql_mode='%s'" % sql_mode) self.store_result() - + def show_warnings(self): """Return detailed information about warnings as a sequence of tuples of (Level, Code, Message). This @@ -322,7 +321,7 @@ class Connection(_mysql.connection): r = self.store_result() warnings = r.fetch_row(0) return warnings - + Warning = Warning Error = Error InterfaceError = InterfaceError -- cgit v1.2.1 From 797d9c3029c0660a43b71130cac093343e15f0dc Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 5 Jan 2013 09:34:59 +0900 Subject: Support `autocommit=True` for constructor argument. --- MySQLdb/connections.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'MySQLdb') diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 6bc1613..aeac731 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -139,6 +139,9 @@ class Connection(_mysql.connection): 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 @@ class Connection(_mysql.connection): 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 = [] -- cgit v1.2.1 From c1b8e8a0474c44f60ed8e08b3be8dc7f293ce7b8 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 21 May 2013 12:09:55 +0900 Subject: autocommit=None means using server default. --- MySQLdb/connections.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'MySQLdb') diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index aeac731..2b423e7 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -140,7 +140,9 @@ class Connection(_mysql.connection): 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 @@ class Connection(_mysql.connection): 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): -- cgit v1.2.1 From 470eb56c4ddb742c70d9e11c4f6601ccf4071fe5 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 21 May 2013 13:32:23 +0900 Subject: Fix typo. --- MySQLdb/connections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'MySQLdb') diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 2b423e7..908d72a 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -233,7 +233,7 @@ class Connection(_mysql.connection): # 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): -- cgit v1.2.1 From f064692a36c49713463fccb5befa1e13a9beb8d4 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 22 May 2013 22:33:20 +0900 Subject: 'BEGIN' on __enter__ if autocommit is enabled. --- MySQLdb/connections.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'MySQLdb') 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): -- cgit v1.2.1 From 0226f1a727441746bbe2bc396de5eccea7f395f8 Mon Sep 17 00:00:00 2001 From: Dima Tyzhnenko Date: Mon, 15 Jul 2013 18:11:07 +0300 Subject: Fix problem with return None if Datetime field contained microsecond (Issue #24) --- MySQLdb/times.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'MySQLdb') diff --git a/MySQLdb/times.py b/MySQLdb/times.py index bc92eb4..f3a92d7 100644 --- a/MySQLdb/times.py +++ b/MySQLdb/times.py @@ -60,9 +60,13 @@ def DateTime_or_None(s): def TimeDelta_or_None(s): try: h, m, s = s.split(':') - h, m, s = int(h), int(m), float(s) - td = timedelta(hours=abs(h), minutes=m, seconds=int(s), - microseconds=int(math.modf(s)[0] * 1000000)) + if '.' in s: + s, ms = s.split('.') + else: + ms = 0 + h, m, s, ms = int(h), int(m), int(s), int(ms) + td = timedelta(hours=abs(h), minutes=m, seconds=s, + microseconds=ms) if h < 0: return -td else: @@ -74,9 +78,13 @@ def TimeDelta_or_None(s): def Time_or_None(s): try: h, m, s = s.split(':') - h, m, s = int(h), int(m), float(s) - return time(hour=h, minute=m, second=int(s), - microsecond=int(math.modf(s)[0] * 1000000)) + if '.' in s: + s, ms = s.split('.') + else: + ms = 0 + h, m, s, ms = int(h), int(m), int(s), int(ms) + return time(hour=h, minute=m, second=s, + microsecond=ms) except ValueError: return None -- cgit v1.2.1