[2d6a35]: MySQLdb / converters.py  Maximize  Restore  History

Download this file

205 lines (160 with data), 5.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
MySQLdb type conversion module
------------------------------
This module handles all the type conversions for MySQL. If the default type
conversions aren't what you need, you can make your own. The dictionary
conversions maps some kind of type to a conversion function which returns the
corresponding value:
Key: FIELD_TYPE.* (from MySQLdb.constants)
Conversion function:
Arguments: string
Returns: Python object
Key: Python type object (from types) or class
Conversion function:
Arguments: Python object of indicated type or class AND
conversion dictionary
Returns: SQL literal value
Notes: Most conversion functions can ignore the dictionary, but it is a
required parameter. It is necessary for converting things like sequences
and instances.
Don't modify conversions if you can avoid it. Instead, make copies
(with the copy() method), modify the copies, and then pass them to
MySQL.connect().
"""
from _mysql import string_literal, escape_sequence, escape_dict, NULL
from MySQLdb.constants import FIELD_TYPE, FLAG
from MySQLdb.times import datetime_to_sql, timedelta_to_sql, \
timedelta_or_None, datetime_or_None, date_or_None, \
mysql_timestamp_converter
from types import InstanceType
import array
import datetime
from decimal import Decimal
__revision__ = "$Revision$"[11:-2]
__author__ = "$Author$"[9:-2]
def bool_to_sql(boolean, conv):
"""Convert a Python bool to an SQL literal."""
return str(int(boolean))
def SET_to_Set(value):
"""Convert MySQL SET column to Python set."""
return set([ i for i in value.split(',') if i ])
def Set_to_sql(value, conv):
"""Convert a Python set to an SQL literal."""
return string_literal(','.join(value), conv)
def object_to_sql(obj, conv):
"""Convert something into a string via str()."""
return str(obj)
def unicode_to_sql(value, conv):
"""Convert a unicode object to a string using the default encoding.
This is only used as a placeholder for the real function, which
is connection-dependent."""
assert isinstance(value, unicode)
return value.encode()
def float_to_sql(value, conv):
return '%.15g' % value
def None_to_sql(value, conv):
"""Convert None to NULL."""
return NULL # duh
def object_to_quoted_sql(obj, conv):
"""Convert something into a SQL string literal. If using
MySQL-3.23 or newer, string_literal() is a method of the
_mysql.MYSQL object, and this function will be overridden with
that method when the connection is created."""
return string_literal(obj, conv)
def instance_to_sql(obj, conv):
"""Convert an Instance to a string representation. If the __str__()
method produces acceptable output, then you don't need to add the
class to conversions; it will be handled by the default
converter. If the exact class is not found in conv, it will use the
first class it can find for which obj is an instance.
"""
if obj.__class__ in conv:
return conv[obj.__class__](obj, conv)
classes = [ key for key in conv.keys()
if isinstance(obj, key) ]
if not classes:
return conv[types.StringType](obj, conv)
conv[obj.__class__] = conv[classes[0]]
return conv[classes[0]](obj, conv)
def char_array(obj):
return array.array('c', obj)
def array_to_sql(obj, conv):
return object_to_quoted_sql(obj.tostring(), conv)
simple_type_encoders = {
int: object_to_sql,
long: object_to_sql,
float: float_to_sql,
type(None): None_to_sql,
tuple: escape_sequence,
list: escape_sequence,
dict: escape_dict,
InstanceType: instance_to_sql,
array.array: array_to_sql,
unicode: unicode_to_sql,
object: instance_to_sql,
bool: bool_to_sql,
datetime.datetime: datetime_to_sql,
datetime.timedelta: timedelta_to_sql,
set: Set_to_sql,
str: object_to_quoted_sql, # default
}
# This is for MySQL column types that can be converted directly
# into Python types without having to look at metadata (flags,
# character sets, etc.). This should always be used as the last
# resort.
simple_field_decoders = {
FIELD_TYPE.TINY: int,
FIELD_TYPE.SHORT: int,
FIELD_TYPE.LONG: int,
FIELD_TYPE.FLOAT: float,
FIELD_TYPE.DOUBLE: float,
FIELD_TYPE.DECIMAL: Decimal,
FIELD_TYPE.NEWDECIMAL: Decimal,
FIELD_TYPE.LONGLONG: int,
FIELD_TYPE.INT24: int,
FIELD_TYPE.YEAR: int,
FIELD_TYPE.SET: SET_to_Set,
FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter,
FIELD_TYPE.DATETIME: datetime_or_None,
FIELD_TYPE.TIME: timedelta_or_None,
FIELD_TYPE.DATE: date_or_None,
}
# Decoder protocol
# Each decoder is passed a cursor object and a field object.
# The decoder returns a single value:
# * A callable that given an SQL value, returns a Python object.
# This can be as simple as int or str, etc. If the decoder
# returns None, this decoder will be ignored and the next decoder
# on the stack will be checked.
def filter_NULL(f):
def _filter_NULL(o):
if o is None: return o
return f(o)
_filter_NULL.__name__ = f.__name__
return _filter_NULL
def default_decoder(cursor, field):
return str
def simple_decoder(cursor, field):
return simple_field_decoders.get(field.type, None)
character_types = [
FIELD_TYPE.BLOB,
FIELD_TYPE.STRING,
FIELD_TYPE.VAR_STRING,
FIELD_TYPE.VARCHAR,
]
def character_decoder(cursor, field):
if field.type not in character_types:
return None
if field.charsetnr == 63: # BINARY
return str
charset = cursor.connection.character_set_name()
def char_to_unicode(s):
return s.decode(charset)
return char_to_unicode
default_decoders = [
character_decoder,
simple_decoder,
default_decoder,
]
default_encoders = [
]

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks