General cleanups.

* Turn autocommit off initially

 * Add support for mysql_autocommit, _commit, and _rollback API functions
   (new in MySQL-4.1)

 * Remove Connection.begin(); use SQL BEGIN or START TRANSACTION instead

 * pytimes (standard datetime module) is now the default implementation

 * Detect and handle MySQL-4.1 and newer TIMESTAMP (looks like DATETIME)

 * UnicodeType and ObjectType now always handled (required features)

 * Ditch support for L at the end of long integer

 * Remove z and crypt libs if building for Windows

 * Version 1.1.2
This commit is contained in:
adustman
2004-09-06 21:53:40 +00:00
parent 69efaa470e
commit ceabe0ed1b
8 changed files with 691 additions and 52 deletions

View File

@@ -18,7 +18,7 @@ __revision__ = """$Revision$"""[11:-2]
version_info = (
1,
1,
1,
2,
"final",
1)
if version_info[3] == "final": __version__ = "%d.%d.%d" % version_info[:3]

View File

@@ -97,6 +97,9 @@ class Connection(_mysql.connection):
self.converter[types.StringType] = self.string_literal
self.converter[types.UnicodeType] = self.unicode_literal
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
if self._transactional:
# PEP-249 requires autocommit to be initially off
self.autocommit(0)
self.messages = []
def __del__(self):
@@ -104,23 +107,32 @@ class Connection(_mysql.connection):
self.close()
except:
pass
def begin(self):
"""Explicitly begin a transaction. Non-standard."""
self.query("BEGIN")
def commit(self):
"""Commit the current transaction."""
if self._transactional:
self.query("COMMIT")
def rollback(self):
"""Rollback the current transaction."""
if self._transactional:
self.query("ROLLBACK")
def autocommit(self, flag):
"""Set the autocommit stage. A True value enables autocommit;
a False value disables it. PEP-249 requires autocommit to be
initially off."""
flag = flag == True
s = super(Connection, self)
if hasattr(s, 'autocommit'):
s.autocommit(self, flag)
else:
self.errorhandler(None,
NotSupportedError, "Not supported by server")
self.query("SET AUTOCOMMIT=%d" % flag)
if not hasattr(_mysql.connection, 'commit'):
def commit(self):
"""Commit the current transaction."""
if self._transactional:
self.query("COMMIT")
def rollback(self):
"""Rollback the current transaction."""
if self._transactional:
self.query("ROLLBACK")
else:
self.errorhandler(None,
NotSupportedError, "Not supported by server")
def cursor(self, cursorclass=None):
"""

View File

@@ -45,13 +45,7 @@ def Unicode2Str(s, d):
"""Convert a unicode object to a string using latin1 encoding."""
return s.encode('latin')
# Python 1.5.2 compatibility hack
if str(0L)[-1]=='L':
def Long2Int(l, d):
"""Convert a long integer to a string, chopping the L."""
return str(l)[:-1]
else:
Long2Int = Thing2Str
Long2Int = Thing2Str
def Float2Str(o, d):
return '%.15g' % o
@@ -115,6 +109,8 @@ conversions = {
types.InstanceType: Instance2Str,
array.ArrayType: array2Str,
types.StringType: Thing2Literal, # default
types.UnicodeType: Unicode2Str,
types.ObjectType: Instance2Str,
DateTimeType: DateTime2literal,
DateTimeDeltaType: DateTimeDelta2literal,
FIELD_TYPE.TINY: int,
@@ -136,10 +132,3 @@ conversions = {
(None, None),
],
}
if hasattr(types, 'UnicodeType'):
conversions[types.UnicodeType] = Unicode2Str
if hasattr(types, 'ObjectType'):
conversions[types.ObjectType] = Instance2Str

View File

@@ -147,8 +147,8 @@ class BaseCursor(object):
qargs = self.connection.literal(args)
try:
q = [ query % qargs[0] ]
for a in qargs[1:]: q.append( qv % a )
except TypeError, msg:
q.extend([ qv % a for a in qargs[1:] ])
except TypeError, msg:
if msg.args[0] in ("not enough arguments for format string",
"not all arguments converted"):
self.errorhandler(self, ProgrammingError, msg.args[0])

View File

@@ -6,11 +6,11 @@ This module provides some Date and Time classes for dealing with MySQL data.
from _mysql import string_literal
try:
from mxdatetimes import *
from pytimes import *
except ImportError:
try:
from pytimes import *
from mxdatetimes import *
except ImportError:
# no DateTime? We'll muddle through somehow.
@@ -26,6 +26,8 @@ def DateTimeDelta2literal(d, c):
def mysql_timestamp_converter(s):
"""Convert a MySQL TIMESTAMP to a Timestamp object."""
# MySQL>4.1 returns TIMESTAMP in the same format as DATETIME
if s[4] == '-': return DateTime_or_None(s)
s = s + "0"*(14-len(s)) # padding
parts = map(int, filter(None, (s[:4],s[4:6],s[6:8],
s[8:10],s[10:12],s[12:14])))