Menu

[c16ae2]: / MySQLdb / converters.py  Maximize  Restore  History

Download this file

198 lines (162 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
"""
MySQLdb type conversion module
------------------------------
"""
from _mysql import NULL
from MySQLdb.constants import FIELD_TYPE, FLAG
from MySQLdb.times import datetime_to_sql, timedelta_to_sql, \
timedelta_or_orig, datetime_or_orig, date_or_orig, \
timestamp_or_orig
from types import InstanceType
import array
import datetime
from decimal import Decimal
from itertools import izip
def bool_to_sql(connection, boolean):
"""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(connection, value):
"""Convert a Python set to an SQL literal."""
return connection.string_literal(','.join(value))
def object_to_sql(connection, obj):
"""Convert something into a string via str().
The result will not be quoted."""
return connection.escape_string(str(obj))
def unicode_to_sql(connection, value):
"""Convert a unicode object to a string using the connection encoding."""
return connection.string_literal(value.encode(connection.character_set_name()))
def float_to_sql(connection, value):
return '%.15g' % value
def None_to_sql(connection, value):
"""Convert None to NULL."""
return NULL # duh
def None_if_NULL(func):
if func is None: return func
def _None_if_NULL(value):
if value is None: return value
return func(value)
_None_if_NULL.__name__ = func.__name__+"_or_None_if_NULL"
return _None_if_NULL
int_or_None_if_NULL = None_if_NULL(int)
float_or_None_if_NULL = None_if_NULL(float)
Decimal_or_None_if_NULL = None_if_NULL(Decimal)
SET_to_Set_or_None_if_NULL = None_if_NULL(SET_to_Set)
timestamp_or_None_if_NULL = None_if_NULL(timestamp_or_orig)
datetime_or_None_if_NULL = None_if_NULL(datetime_or_orig)
date_or_None_if_NULL = None_if_NULL(date_or_orig)
timedelta_or_None_if_NULL = None_if_NULL(timedelta_or_orig)
def object_to_quoted_sql(connection, obj):
"""Convert something into a SQL string literal."""
if hasattr(obj, "__unicode__"):
return unicode_to_sql(connection, obj)
return connection.string_literal(str(obj))
def instance_to_sql(connection, obj):
"""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 array_to_sql(connection, obj):
return connection.string_literal(obj.tostring())
simple_type_encoders = {
int: object_to_sql,
long: object_to_sql,
float: float_to_sql,
type(None): None_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_or_None_if_NULL,
FIELD_TYPE.SHORT: int_or_None_if_NULL,
FIELD_TYPE.LONG: int_or_None_if_NULL,
FIELD_TYPE.FLOAT: float_or_None_if_NULL,
FIELD_TYPE.DOUBLE: float_or_None_if_NULL,
FIELD_TYPE.DECIMAL: Decimal_or_None_if_NULL,
FIELD_TYPE.NEWDECIMAL: Decimal_or_None_if_NULL,
FIELD_TYPE.LONGLONG: int_or_None_if_NULL,
FIELD_TYPE.INT24: int_or_None_if_NULL,
FIELD_TYPE.YEAR: int_or_None_if_NULL,
FIELD_TYPE.SET: SET_to_Set_or_None_if_NULL,
FIELD_TYPE.TIMESTAMP: timestamp_or_None_if_NULL,
FIELD_TYPE.DATETIME: datetime_or_None_if_NULL,
FIELD_TYPE.TIME: timedelta_or_None_if_NULL,
FIELD_TYPE.DATE: date_or_None_if_NULL,
}
# Decoder protocol
# Each decoder is passed 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 default_decoder(field):
return str
def default_encoder(value):
return object_to_quoted_sql
def simple_decoder(field):
return simple_field_decoders.get(field.type, None)
def simple_encoder(value):
return simple_type_encoders.get(type(value), None)
character_types = [
FIELD_TYPE.BLOB,
FIELD_TYPE.STRING,
FIELD_TYPE.VAR_STRING,
FIELD_TYPE.VARCHAR,
]
def character_decoder(field):
if field.type not in character_types:
return None
if field.charsetnr == 63: # BINARY
return str
charset = field.result.connection.character_set_name()
def char_to_unicode(s):
if s is None:
return s
return s.decode(charset)
return char_to_unicode
default_decoders = [
character_decoder,
simple_decoder,
default_decoder,
]
default_encoders = [
simple_encoder,
default_encoder,
]
def get_codec(field, codecs):
for c in codecs:
func = c(field)
if func:
return func
# the default codec is guaranteed to work
def iter_row_decoder(decoders, row):
if row is None:
return None
return ( d(col) for d, col in izip(decoders, row) )
def tuple_row_decoder(decoders, row):
if row is None:
return None
return tuple(iter_row_decoder(decoders, row))
default_row_formatter = tuple_row_decoder
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.