summaryrefslogtreecommitdiff
path: root/MySQLdb
diff options
context:
space:
mode:
Diffstat (limited to 'MySQLdb')
-rw-r--r--MySQLdb/connections.py19
-rw-r--r--MySQLdb/converters.py7
-rw-r--r--MySQLdb/cursors.py16
-rw-r--r--MySQLdb/times.py29
4 files changed, 57 insertions, 14 deletions
diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py
index 6bc1613..908706a 100644
--- a/MySQLdb/connections.py
+++ b/MySQLdb/connections.py
@@ -139,6 +139,11 @@ 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.
@@ -182,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()
@@ -225,10 +233,15 @@ class Connection(_mysql.connection):
self.encoders[types.UnicodeType] = unicode_literal
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
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)
+ if self.get_autocommit() != on:
+ _mysql.connection.autocommit(self, on)
+
def cursor(self, cursorclass=None):
"""
@@ -241,6 +254,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):
diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py
index 491d49b..26c1f90 100644
--- a/MySQLdb/converters.py
+++ b/MySQLdb/converters.py
@@ -129,13 +129,16 @@ def char_array(s):
def array2Str(o, d):
return Thing2Literal(o.tostring(), d)
+def quote_tuple(t, d):
+ return "(%s)" % (','.join(escape_sequence(t, d)))
+
conversions = {
IntType: Thing2Str,
LongType: Long2Int,
FloatType: Float2Str,
NoneType: None2NULL,
- TupleType: escape_sequence,
- ListType: escape_sequence,
+ TupleType: quote_tuple,
+ ListType: quote_tuple,
DictType: escape_dict,
InstanceType: Instance2Str,
ArrayType: array2Str,
diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py
index 7e5a887..348a586 100644
--- a/MySQLdb/cursors.py
+++ b/MySQLdb/cursors.py
@@ -26,7 +26,7 @@ restr = r"""
(?:
(?:\(
# ( - editor hightlighting helper
- [^)]*
+ .*
\))
|
'
@@ -180,7 +180,11 @@ class BaseCursor(object):
if isinstance(query, unicode):
query = query.encode(db.unicode_literal.charset)
if args is not None:
- query = query % db.literal(args)
+ if isinstance(args, dict):
+ query = query % dict((key, db.literal(item))
+ for key, item in args.iteritems())
+ else:
+ query = query % tuple([db.literal(item) for item in args])
try:
r = None
r = self._query(query)
@@ -236,7 +240,13 @@ class BaseCursor(object):
e = m.end(1)
qv = m.group(1)
try:
- q = [ qv % db.literal(a) for a in args ]
+ q = []
+ for a in args:
+ if isinstance(a, dict):
+ q.append(qv % dict((key, db.literal(item))
+ for key, item in a.iteritems()))
+ else:
+ q.append(qv % tuple([db.literal(item) for item in a]))
except TypeError, msg:
if msg.args[0] in ("not enough arguments for format string",
"not all arguments converted"):
diff --git a/MySQLdb/times.py b/MySQLdb/times.py
index bc92eb4..0ff7476 100644
--- a/MySQLdb/times.py
+++ b/MySQLdb/times.py
@@ -51,7 +51,12 @@ def DateTime_or_None(s):
try:
d, t = s.split(sep, 1)
- return datetime(*[ int(x) for x in d.split('-')+t.split(':') ])
+ if '.' in t:
+ t, ms = t.split('.',1)
+ ms = ms.ljust(6, '0')
+ else:
+ ms = 0
+ return datetime(*[ int(x) for x in d.split('-')+t.split(':')+[ms] ])
except (SystemExit, KeyboardInterrupt):
raise
except:
@@ -60,9 +65,14 @@ 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('.')
+ ms = ms.ljust(6, '0')
+ 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 +84,14 @@ 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('.')
+ ms = ms.ljust(6, '0')
+ 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