mirror of
https://github.com/PyMySQL/mysqlclient.git
synced 2025-08-15 11:10:58 +08:00
refactoring.
This commit is contained in:
@ -1,8 +1,12 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
if sys.version_info[0] == 3:
|
if sys.version_info[0] == 3:
|
||||||
|
PY2 = False
|
||||||
unicode = str
|
unicode = str
|
||||||
unichr = chr
|
unichr = chr
|
||||||
|
long = int
|
||||||
else:
|
else:
|
||||||
|
PY2 = True
|
||||||
unicode = unicode
|
unicode = unicode
|
||||||
unichr = unichr
|
unichr = unichr
|
||||||
|
long = long
|
||||||
|
@ -7,7 +7,7 @@ override Connection.default_cursor with a non-standard Cursor class.
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
from MySQLdb import cursors
|
from MySQLdb import cursors
|
||||||
from MySQLdb.compat import unicode
|
from MySQLdb.compat import unicode, PY2
|
||||||
from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \
|
from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \
|
||||||
DatabaseError, OperationalError, IntegrityError, InternalError, \
|
DatabaseError, OperationalError, IntegrityError, InternalError, \
|
||||||
NotSupportedError, ProgrammingError
|
NotSupportedError, ProgrammingError
|
||||||
@ -15,8 +15,6 @@ import _mysql
|
|||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
PY2 = sys.version_info[0] == 2
|
|
||||||
|
|
||||||
|
|
||||||
def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
|
def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
|
||||||
"""
|
"""
|
||||||
@ -123,6 +121,7 @@ class Connection(_mysql.connection):
|
|||||||
columns are returned as strings. columns are returned as
|
columns are returned as strings. columns are returned as
|
||||||
normal strings. Unicode objects will always be encoded to
|
normal strings. Unicode objects will always be encoded to
|
||||||
the connection's character set regardless of this setting.
|
the connection's character set regardless of this setting.
|
||||||
|
Default to False on Python 2 and True on Python 3.
|
||||||
|
|
||||||
charset
|
charset
|
||||||
If supplied, the connection character set will be changed
|
If supplied, the connection character set will be changed
|
||||||
@ -207,15 +206,18 @@ class Connection(_mysql.connection):
|
|||||||
|
|
||||||
db = proxy(self)
|
db = proxy(self)
|
||||||
def _get_string_literal():
|
def _get_string_literal():
|
||||||
|
# Note: string_literal() is called for bytes object on Python 3.
|
||||||
def string_literal(obj, dummy=None):
|
def string_literal(obj, dummy=None):
|
||||||
return db.string_literal(obj)
|
return db.string_literal(obj)
|
||||||
return string_literal
|
return string_literal
|
||||||
|
|
||||||
def _get_unicode_literal():
|
def _get_unicode_literal():
|
||||||
if PY2:
|
if PY2:
|
||||||
|
# unicode_literal is called for only unicode object.
|
||||||
def unicode_literal(u, dummy=None):
|
def unicode_literal(u, dummy=None):
|
||||||
return db.literal(u.encode(unicode_literal.charset))
|
return db.literal(u.encode(unicode_literal.charset))
|
||||||
else:
|
else:
|
||||||
|
# unicode_literal() is called for arbitrary object.
|
||||||
def unicode_literal(u, dummy=None):
|
def unicode_literal(u, dummy=None):
|
||||||
return db.literal(str(u).encode(unicode_literal.charset))
|
return db.literal(str(u).encode(unicode_literal.charset))
|
||||||
return unicode_literal
|
return unicode_literal
|
||||||
@ -288,6 +290,11 @@ class Connection(_mysql.connection):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
s = self.escape(o, self.encoders)
|
s = self.escape(o, self.encoders)
|
||||||
|
# Python 3 doesn't support % operation for bytes object.
|
||||||
|
# We should decode it before using %.
|
||||||
|
# Decoding with ascii and surrogateescape allows convert arbitrary
|
||||||
|
# bytes to unicode and back again.
|
||||||
|
# See http://python.org/dev/peps/pep-0383/
|
||||||
if not PY2 and isinstance(s, bytes):
|
if not PY2 and isinstance(s, bytes):
|
||||||
return s.decode('ascii', 'surrogateescape')
|
return s.decode('ascii', 'surrogateescape')
|
||||||
return s
|
return s
|
||||||
|
@ -35,18 +35,9 @@ MySQL.connect().
|
|||||||
from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
|
from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
|
||||||
from MySQLdb.constants import FIELD_TYPE, FLAG
|
from MySQLdb.constants import FIELD_TYPE, FLAG
|
||||||
from MySQLdb.times import *
|
from MySQLdb.times import *
|
||||||
|
from MySQLdb.compat import PY2, long
|
||||||
|
|
||||||
try:
|
NoneType = type(None)
|
||||||
from types import IntType, LongType, FloatType, NoneType, TupleType, ListType, DictType, InstanceType, \
|
|
||||||
ObjectType, BooleanType
|
|
||||||
PY2 = True
|
|
||||||
except ImportError:
|
|
||||||
# Python 3
|
|
||||||
long = int
|
|
||||||
IntType, LongType, FloatType, NoneType = int, long, float, type(None)
|
|
||||||
TupleType, ListType, DictType, InstanceType = tuple, list, dict, None
|
|
||||||
ObjectType, BooleanType = object, bool
|
|
||||||
PY2 = False
|
|
||||||
|
|
||||||
import array
|
import array
|
||||||
|
|
||||||
@ -78,8 +69,6 @@ def Unicode2Str(s, d):
|
|||||||
is connection-dependent."""
|
is connection-dependent."""
|
||||||
return s.encode()
|
return s.encode()
|
||||||
|
|
||||||
Long2Int = Thing2Str
|
|
||||||
|
|
||||||
def Float2Str(o, d):
|
def Float2Str(o, d):
|
||||||
return '%.15g' % o
|
return '%.15g' % o
|
||||||
|
|
||||||
@ -88,12 +77,10 @@ def None2NULL(o, d):
|
|||||||
return NULL # duh
|
return NULL # duh
|
||||||
|
|
||||||
def Thing2Literal(o, d):
|
def Thing2Literal(o, d):
|
||||||
|
|
||||||
"""Convert something into a SQL string literal. If using
|
"""Convert something into a SQL string literal. If using
|
||||||
MySQL-3.23 or newer, string_literal() is a method of the
|
MySQL-3.23 or newer, string_literal() is a method of the
|
||||||
_mysql.MYSQL object, and this function will be overridden with
|
_mysql.MYSQL object, and this function will be overridden with
|
||||||
that method when the connection is created."""
|
that method when the connection is created."""
|
||||||
|
|
||||||
return string_literal(o, d)
|
return string_literal(o, d)
|
||||||
|
|
||||||
|
|
||||||
@ -107,19 +94,19 @@ def quote_tuple(t, d):
|
|||||||
return "(%s)" % (','.join(escape_sequence(t, d)))
|
return "(%s)" % (','.join(escape_sequence(t, d)))
|
||||||
|
|
||||||
conversions = {
|
conversions = {
|
||||||
IntType: Thing2Str,
|
int: Thing2Str,
|
||||||
LongType: Long2Int,
|
long: Thing2Str,
|
||||||
FloatType: Float2Str,
|
float: Float2Str,
|
||||||
NoneType: None2NULL,
|
NoneType: None2NULL,
|
||||||
TupleType: quote_tuple,
|
tuple: quote_tuple,
|
||||||
ListType: quote_tuple,
|
list: quote_tuple,
|
||||||
DictType: escape_dict,
|
dict: escape_dict,
|
||||||
ArrayType: array2Str,
|
ArrayType: array2Str,
|
||||||
BooleanType: Bool2Str,
|
bool: Bool2Str,
|
||||||
Date: Thing2Literal,
|
Date: Thing2Literal,
|
||||||
DateTimeType: DateTime2literal,
|
DateTimeType: DateTime2literal,
|
||||||
DateTimeDeltaType: DateTimeDelta2literal,
|
DateTimeDeltaType: DateTimeDelta2literal,
|
||||||
str: str, # default
|
str: Thing2Literal, # default
|
||||||
set: Set2Str,
|
set: Set2Str,
|
||||||
FIELD_TYPE.TINY: int,
|
FIELD_TYPE.TINY: int,
|
||||||
FIELD_TYPE.SHORT: int,
|
FIELD_TYPE.SHORT: int,
|
||||||
@ -152,7 +139,7 @@ conversions = {
|
|||||||
if PY2:
|
if PY2:
|
||||||
conversions[unicode] = Unicode2Str
|
conversions[unicode] = Unicode2Str
|
||||||
else:
|
else:
|
||||||
conversions[bytes] = bytes
|
conversions[bytes] = Thing2Literal
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
@ -160,6 +147,3 @@ try:
|
|||||||
conversions[FIELD_TYPE.NEWDECIMAL] = Decimal
|
conversions[FIELD_TYPE.NEWDECIMAL] = Decimal
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user