mirror of
https://github.com/PyMySQL/mysqlclient.git
synced 2026-03-13 08:00:02 +08:00
Initial conversion to modern Python.
This commit is contained in:
@@ -16,11 +16,11 @@ MySQLdb.converters module.
|
||||
__author__ = "Andy Dustman <andy@dustman.net>"
|
||||
__revision__ = """$Revision$"""[11:-2]
|
||||
version_info = (
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
9,
|
||||
3,
|
||||
"beta",
|
||||
3)
|
||||
"final",
|
||||
1)
|
||||
if version_info[3] == "final": __version__ = "%d.%d.%d" % version_info[:3]
|
||||
else: __version__ = "%d.%d.%d%1.1s%d" % version_info[:5]
|
||||
|
||||
@@ -61,7 +61,7 @@ def Binary(x): return str(x)
|
||||
def Connect(*args, **kwargs):
|
||||
"""Factory function for connections.Connection."""
|
||||
from connections import Connection
|
||||
return apply(Connection, args, kwargs)
|
||||
return Connection(*args, **kwargs)
|
||||
|
||||
connect = Connection = Connect
|
||||
|
||||
|
||||
@@ -38,14 +38,14 @@ if hasattr(types, "ObjectType"):
|
||||
class ConnectionBase(_mysql.connection):
|
||||
|
||||
def _make_connection(self, args, kwargs):
|
||||
apply(super(ConnectionBase, self).__init__, args, kwargs)
|
||||
super(ConnectionBase, self).__init__(*args, **kwargs)
|
||||
|
||||
else:
|
||||
|
||||
class ConnectionBase:
|
||||
|
||||
def _make_connection(self, args, kwargs):
|
||||
self._db = apply(_mysql.connect, args, kwargs)
|
||||
self._db = _mysql.connect(*args, **kwargs)
|
||||
|
||||
def __getattr__(self, attr):
|
||||
if hasattr(self, "_db"):
|
||||
|
||||
@@ -29,14 +29,13 @@ from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
|
||||
from constants import FIELD_TYPE, FLAG
|
||||
from sets import *
|
||||
from times import *
|
||||
from string import split
|
||||
import types
|
||||
import array
|
||||
|
||||
|
||||
def Str2Set(s):
|
||||
values = split(s, ',')
|
||||
return apply(Set, tuple(values))
|
||||
values = s.split(',')
|
||||
return Set(*values)
|
||||
|
||||
def Thing2Str(s, d):
|
||||
"""Convert something into a string via str()."""
|
||||
|
||||
@@ -133,7 +133,6 @@ class BaseCursor:
|
||||
execute().
|
||||
|
||||
"""
|
||||
from string import join
|
||||
from sys import exc_info
|
||||
del self.messages[:]
|
||||
if not args: return
|
||||
@@ -159,13 +158,14 @@ class BaseCursor:
|
||||
exc, value, tb = exc_info()
|
||||
del tb
|
||||
self.errorhandler(self, exc, value)
|
||||
r = self._query(join(q,',\n'))
|
||||
r = self._query(',\n'.join(q))
|
||||
self._executed = query
|
||||
return r
|
||||
|
||||
def __do_query(self, q):
|
||||
def _do_query(self, q):
|
||||
from warnings import warn
|
||||
from string import atoi
|
||||
|
||||
from string import split, atoi
|
||||
db = self._get_db()
|
||||
db.query(q)
|
||||
self._result = self._get_result()
|
||||
@@ -173,27 +173,14 @@ class BaseCursor:
|
||||
self.rownumber = 0
|
||||
self.description = self._result and self._result.describe() or None
|
||||
self.lastrowid = db.insert_id()
|
||||
self._check_for_warnings()
|
||||
info = db.info()
|
||||
if info:
|
||||
warnings = atoi(info.split()[-1])
|
||||
if warnings:
|
||||
warn(info, self.Warning, stacklevel=4)
|
||||
return self.rowcount
|
||||
|
||||
def _check_for_warnings(self): pass
|
||||
|
||||
_query = __do_query
|
||||
|
||||
def info(self):
|
||||
"""Return some information about the last query (db.info())
|
||||
DEPRECATED: Use messages attribute"""
|
||||
self._check_executed()
|
||||
if self.messages:
|
||||
return self.messages[-1]
|
||||
else:
|
||||
return ''
|
||||
|
||||
def insert_id(self):
|
||||
"""Return the last inserted ID on an AUTO_INCREMENT columns.
|
||||
DEPRECATED: use lastrowid attribute"""
|
||||
self._check_executed()
|
||||
return self.lastrowid
|
||||
def _query(self, q): return self._do_query(q)
|
||||
|
||||
def _fetch_row(self, size=1):
|
||||
if not self._result:
|
||||
@@ -214,22 +201,6 @@ class BaseCursor:
|
||||
ProgrammingError = ProgrammingError
|
||||
NotSupportedError = NotSupportedError
|
||||
|
||||
|
||||
class CursorWarningMixIn:
|
||||
|
||||
"""This is a MixIn class that provides the capability of raising
|
||||
the Warning exception when something went slightly wrong with your
|
||||
query."""
|
||||
|
||||
def _check_for_warnings(self):
|
||||
from string import atoi, split
|
||||
info = self._get_db().info()
|
||||
if not info:
|
||||
return
|
||||
warnings = atoi(split(info)[-1])
|
||||
if warnings:
|
||||
raise Warning, info
|
||||
|
||||
|
||||
class CursorStoreResultMixIn:
|
||||
|
||||
@@ -246,7 +217,7 @@ class CursorStoreResultMixIn:
|
||||
BaseCursor.close(self)
|
||||
|
||||
def _query(self, q):
|
||||
rowcount = self._BaseCursor__do_query(q)
|
||||
rowcount = self._do_query(q)
|
||||
self._rows = self._fetch_row(0)
|
||||
self._result = None
|
||||
return rowcount
|
||||
@@ -276,24 +247,6 @@ class CursorStoreResultMixIn:
|
||||
self.rownumber = len(self._rows)
|
||||
return result
|
||||
|
||||
def seek(self, row, whence=0):
|
||||
"""seek to a given row of the result set analogously to file.seek().
|
||||
This is non-standard extension. DEPRECATED: Use scroll method"""
|
||||
self._check_executed()
|
||||
if whence == 0:
|
||||
self.rownumber = row
|
||||
elif whence == 1:
|
||||
self.rownumber = self.rownumber + row
|
||||
elif whence == 2:
|
||||
self.rownumber = len(self._rows) + row
|
||||
|
||||
def tell(self):
|
||||
"""Return the current position in the result set analogously to
|
||||
file.tell(). This is a non-standard extension. DEPRECATED:
|
||||
use rownumber attribute"""
|
||||
self._check_executed()
|
||||
return self.rownumber
|
||||
|
||||
def scroll(self, value, mode='relative'):
|
||||
"""Scroll the cursor in the result set to a new position according
|
||||
to mode.
|
||||
@@ -400,58 +353,31 @@ class CursorOldDictRowsMixIn(CursorDictRowsMixIn):
|
||||
_fetch_type = 2
|
||||
|
||||
|
||||
class CursorNW(CursorStoreResultMixIn, CursorTupleRowsMixIn,
|
||||
BaseCursor):
|
||||
|
||||
"""This is a basic Cursor class that returns rows as tuples and
|
||||
stores the result set in the client. Warnings are not raised."""
|
||||
|
||||
|
||||
class Cursor(CursorWarningMixIn, CursorNW):
|
||||
class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn,
|
||||
BaseCursor):
|
||||
|
||||
"""This is the standard Cursor class that returns rows as tuples
|
||||
and stores the result set in the client. Warnings are raised as
|
||||
necessary."""
|
||||
and stores the result set in the client."""
|
||||
|
||||
|
||||
class DictCursorNW(CursorStoreResultMixIn, CursorDictRowsMixIn,
|
||||
class DictCursor(CursorStoreResultMixIn, CursorDictRowsMixIn,
|
||||
BaseCursor):
|
||||
|
||||
"""This is a Cursor class that returns rows as dictionaries and
|
||||
stores the result set in the client."""
|
||||
|
||||
|
||||
class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn,
|
||||
BaseCursor):
|
||||
|
||||
"""This is a Cursor class that returns rows as tuples and stores
|
||||
the result set in the server."""
|
||||
|
||||
|
||||
class SSDictCursor(CursorUseResultMixIn, CursorDictRowsMixIn,
|
||||
BaseCursor):
|
||||
|
||||
"""This is a Cursor class that returns rows as dictionaries and
|
||||
stores the result set in the client. Warnings are not raised."""
|
||||
|
||||
|
||||
class DictCursor(CursorWarningMixIn, DictCursorNW):
|
||||
|
||||
"""This is a Cursor class that returns rows as dictionaries and
|
||||
stores the result set in the client. Warnings are raised as
|
||||
necessary."""
|
||||
|
||||
|
||||
class SSCursorNW(CursorUseResultMixIn, CursorTupleRowsMixIn,
|
||||
BaseCursor):
|
||||
|
||||
"""This is a basic Cursor class that returns rows as tuples and
|
||||
stores the result set in the server. Warnings are not raised."""
|
||||
|
||||
|
||||
class SSCursor(CursorWarningMixIn, SSCursorNW):
|
||||
|
||||
"""This is a Cursor class that returns rows as tuples and stores
|
||||
the result set in the server. Warnings are raised as necessary."""
|
||||
|
||||
|
||||
class SSDictCursorNW(CursorUseResultMixIn, CursorDictRowsMixIn,
|
||||
BaseCursor):
|
||||
|
||||
"""This is a Cursor class that returns rows as dictionaries and
|
||||
stores the result set in the server. Warnings are not raised."""
|
||||
|
||||
|
||||
class SSDictCursor(CursorWarningMixIn, SSDictCursorNW):
|
||||
|
||||
"""This is a Cursor class that returns rows as dictionaries and
|
||||
stores the result set in the server. Warnings are raised as
|
||||
necessary."""
|
||||
stores the result set in the server."""
|
||||
|
||||
|
||||
|
||||
@@ -13,15 +13,15 @@ except ImportError:
|
||||
|
||||
def DateFromTicks(ticks):
|
||||
"""Convert UNIX ticks into a mx.DateTime.Date."""
|
||||
return apply(Date, localtime(ticks)[:3])
|
||||
return Date(*localtime(ticks)[:3])
|
||||
|
||||
def TimeFromTicks(ticks):
|
||||
"""Convert UNIX ticks into a mx.DateTime.Time."""
|
||||
return apply(Time, localtime(ticks)[3:6])
|
||||
return Time(*localtime(ticks)[3:6])
|
||||
|
||||
def TimestampFromTicks(ticks):
|
||||
"""Convert UNIX ticks into a mx.DateTime.Timestamp."""
|
||||
return apply(Timestamp, localtime(ticks)[:6])
|
||||
return Timestamp(*localtime(ticks)[:6])
|
||||
|
||||
def format_DATE(d):
|
||||
"""Format a DateTime object as an ISO date."""
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
"""Use Python datetime module to handle date and time columns."""
|
||||
|
||||
# datetime is only in Python 2.3 or newer, so it is safe to use
|
||||
# string methods. However, have to use apply(func, args) instead
|
||||
# of func(*args) because 1.5.2 will reject the syntax.
|
||||
# string methods.
|
||||
|
||||
from time import localtime
|
||||
from datetime import date, datetime, time, timedelta
|
||||
@@ -16,17 +15,21 @@ DateTimeType = type(datetime)
|
||||
|
||||
def DateFromTicks(ticks):
|
||||
"""Convert UNIX ticks into a date instance."""
|
||||
return apply(date, localtime(ticks)[:3])
|
||||
return date(*localtime(ticks)[:3])
|
||||
|
||||
def TimeFromTicks(ticks):
|
||||
"""Convert UNIX ticks into a time instance."""
|
||||
return apply(time, localtime(ticks)[3:6])
|
||||
return time(*localtime(ticks)[3:6])
|
||||
|
||||
def TimestampFromTicks(ticks):
|
||||
"""Convert UNIX ticks into a datetime instance."""
|
||||
return apply(datetime, localtime(ticks)[:6])
|
||||
return datetime(*localtime(ticks)[:6])
|
||||
|
||||
format_TIME = format_DATE = str
|
||||
|
||||
def format_TIMESTAMP(d):
|
||||
return d.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
format_TIME = format_TIMESTAMP = format_DATE = str
|
||||
|
||||
def DateTime_or_None(s):
|
||||
if ' ' in s:
|
||||
@@ -38,7 +41,7 @@ def DateTime_or_None(s):
|
||||
|
||||
try:
|
||||
d, t = s.split(sep, 1)
|
||||
return apply(datetime, tuple(map(int, d.split('-')+t.split(':'))))
|
||||
return datetime(*[ int(x) for x in d.split('-')+t.split(':') ])
|
||||
except:
|
||||
return None
|
||||
|
||||
@@ -65,5 +68,5 @@ def Time_or_None(s):
|
||||
return None
|
||||
|
||||
def Date_or_None(s):
|
||||
try: return apply(date, tuple(map(int, s.split('-',2))))
|
||||
try: return date(*[ int(x) for x in s.split('-',2)])
|
||||
except: return None
|
||||
|
||||
@@ -17,8 +17,7 @@ class Set:
|
||||
|
||||
def __str__(self):
|
||||
"""Returns the values as a comma-separated string."""
|
||||
from string import join
|
||||
return join(map(str, self._values),',')
|
||||
return ','.join([ str(x) for x in self._values])
|
||||
|
||||
def __repr__(self):
|
||||
return "%s%s" % (self.__class__.__name__, `self._values`)
|
||||
@@ -32,7 +31,7 @@ class Set:
|
||||
values.append(v)
|
||||
elif other not in self._values:
|
||||
values.append(other)
|
||||
return apply(self.__class__, values)
|
||||
return self.__class__(*values)
|
||||
|
||||
__add__ = __or__
|
||||
|
||||
@@ -44,7 +43,7 @@ class Set:
|
||||
values.remove(v)
|
||||
elif other in self:
|
||||
values.remove(other)
|
||||
return apply(self.__class__, tuple(values))
|
||||
return self.__class__(*values)
|
||||
|
||||
def __and__(self, other):
|
||||
"Intersection."
|
||||
@@ -55,7 +54,7 @@ class Set:
|
||||
values.append(v)
|
||||
elif other in self:
|
||||
values.append(other)
|
||||
return apply(self.__class__, tuple(values))
|
||||
return self.__class__(*values)
|
||||
|
||||
__mul__ = __and__
|
||||
|
||||
|
||||
@@ -29,5 +29,5 @@ def mysql_timestamp_converter(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])))
|
||||
try: return apply(Timestamp, tuple(parts))
|
||||
try: return Timestamp(*parts)
|
||||
except: return None
|
||||
|
||||
Reference in New Issue
Block a user