summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Dustman <farcepest@gmail.com>2013-08-18 18:24:17 -0400
committerAndy Dustman <farcepest@gmail.com>2013-08-18 18:24:17 -0400
commit32d0364176d350cdc2701d63c4f9b1f711ecbb44 (patch)
treea49154fbafc0f1bdfd12240ed66e0e8d15112772
parentefb3bc3531d9c16da436423e7c496f7f819da3b6 (diff)
parentc8b2744ea2b1e4419b7e3d93928e92c95f366815 (diff)
downloadmysqldb1-MySQLdb-1.3.tar.gz
Merge branch 'master' into MySQLdb-1.3MySQLdb-1.3
Conflicts: MySQLdb/connections.py
-rw-r--r--MySQLdb/connections.py69
-rw-r--r--MySQLdb/times.py20
-rw-r--r--setup.py7
3 files changed, 68 insertions, 28 deletions
diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py
index f5eb0ed..650561e 100644
--- a/MySQLdb/connections.py
+++ b/MySQLdb/connections.py
@@ -40,14 +40,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))
@@ -59,7 +59,7 @@ class Connection(_mysql.connection):
"""MySQL Database Connection Object"""
default_cursor = cursors.Cursor
-
+
def __init__(self, *args, **kwargs):
"""
@@ -69,7 +69,7 @@ class Connection(_mysql.connection):
host
string, host to connect
-
+
user
string, user to connect as
@@ -126,7 +126,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)
@@ -139,7 +139,12 @@ class Connection(_mysql.connection):
local_infile
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.
@@ -151,7 +156,7 @@ class Connection(_mysql.connection):
import types
kwargs2 = kwargs.copy()
-
+
if 'conv' in kwargs:
conv = kwargs['conv']
else:
@@ -172,7 +177,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', '')
@@ -182,14 +187,17 @@ class Connection(_mysql.connection):
client_flag |= CLIENT.MULTI_STATEMENTS
if client_version >= (5, 0):
client_flag |= CLIENT.MULTI_RESULTS
-
+
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()
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)
@@ -207,7 +215,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()
@@ -227,11 +235,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
- self.autocommit(False)
+ if autocommit is not None:
+ 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):
"""
@@ -243,14 +269,17 @@ class Connection(_mysql.connection):
"""
return (cursorclass or self.cursorclass)(self)
- def __enter__(self): return self.cursor()
-
+ def __enter__(self):
+ if self.get_autocommit():
+ self.query("BEGIN")
+ return self.cursor()
+
def __exit__(self, exc, value, tb):
if exc:
self.rollback()
else:
self.commit()
-
+
def literal(self, o):
"""
@@ -272,7 +301,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):
@@ -312,7 +341,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
@@ -323,7 +352,7 @@ class Connection(_mysql.connection):
r = self.store_result()
warnings = r.fetch_row(0)
return warnings
-
+
Warning = Warning
Error = Error
InterfaceError = InterfaceError
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
diff --git a/setup.py b/setup.py
index 798f96f..e2dee0f 100644
--- a/setup.py
+++ b/setup.py
@@ -3,8 +3,11 @@
import os
import sys
-from distribute_setup import use_setuptools
-use_setuptools()
+try:
+ import setuptools
+except ImportError:
+ from distribute_setup import use_setuptools
+ use_setuptools()
from setuptools import setup, Extension
if not hasattr(sys, "hexversion") or sys.hexversion < 0x02040000: