37
from _mysql import string_literal, escape_sequence, escape_dict, NULL
37
from _mysql import NULL
38
38
from MySQLdb.constants import FIELD_TYPE, FLAG
39
39
from MySQLdb.times import datetime_to_sql, timedelta_to_sql, \
40
40
timedelta_or_None, datetime_or_None, date_or_None, \
45
45
from decimal import Decimal
47
__revision__ = "$Revision: 623 $"[11:-2]
47
__revision__ = "$Revision: 624 $"[11:-2]
48
48
__author__ = "$Author: adustman $"[9:-2]
50
def bool_to_sql(boolean, conv):
50
def bool_to_sql(connection, boolean):
51
51
"""Convert a Python bool to an SQL literal."""
52
52
return str(int(boolean))
55
55
"""Convert MySQL SET column to Python set."""
56
56
return set([ i for i in value.split(',') if i ])
58
def Set_to_sql(value, conv):
58
def Set_to_sql(connection, value):
59
59
"""Convert a Python set to an SQL literal."""
60
return string_literal(','.join(value), conv)
62
def object_to_sql(obj, conv):
63
"""Convert something into a string via str()."""
66
def unicode_to_sql(value, conv):
67
"""Convert a unicode object to a string using the default encoding.
68
This is only used as a placeholder for the real function, which
69
is connection-dependent."""
70
assert isinstance(value, unicode)
73
def float_to_sql(value, conv):
60
return connection.string_literal(','.join(value))
62
def object_to_sql(connection, obj):
63
"""Convert something into a string via str().
64
The result will not be quoted."""
65
return connection.escape_string(str(obj))
67
def unicode_to_sql(connection, value):
68
"""Convert a unicode object to a string using the connection encoding."""
69
return connection.string_literal(value.encode(connection.character_set_name()))
71
def float_to_sql(connection, value):
74
72
return '%.15g' % value
76
def None_to_sql(value, conv):
74
def None_to_sql(connection, value):
77
75
"""Convert None to NULL."""
80
def object_to_quoted_sql(obj, conv):
81
"""Convert something into a SQL string literal. If using
82
MySQL-3.23 or newer, string_literal() is a method of the
83
_mysql.MYSQL object, and this function will be overridden with
84
that method when the connection is created."""
86
return string_literal(obj, conv)
88
def instance_to_sql(obj, conv):
78
def object_to_quoted_sql(connection, obj):
79
"""Convert something into a SQL string literal."""
80
if hasattr(obj, "__unicode__"):
81
return unicode_to_sql(connection, obj)
82
return connection.string_literal(str(obj))
84
def instance_to_sql(connection, obj):
89
85
"""Convert an Instance to a string representation. If the __str__()
90
86
method produces acceptable output, then you don't need to add the
91
87
class to conversions; it will be handled by the default
101
97
conv[obj.__class__] = conv[classes[0]]
102
98
return conv[classes[0]](obj, conv)
105
return array.array('c', obj)
107
def array_to_sql(obj, conv):
108
return object_to_quoted_sql(obj.tostring(), conv)
100
def array_to_sql(connection, obj):
101
return connection.string_literal(obj.tostring())
110
103
simple_type_encoders = {
111
104
int: object_to_sql,
112
105
long: object_to_sql,
113
106
float: float_to_sql,
114
107
type(None): None_to_sql,
115
tuple: escape_sequence,
116
list: escape_sequence,
118
InstanceType: instance_to_sql,
119
array.array: array_to_sql,
120
108
unicode: unicode_to_sql,
121
109
object: instance_to_sql,
122
110
bool: bool_to_sql,
156
144
# returns None, this decoder will be ignored and the next decoder
157
145
# on the stack will be checked.
161
if o is None: return o
163
_filter_NULL.__name__ = f.__name__
166
147
def default_decoder(field):
150
def default_encoder(value):
151
return object_to_quoted_sql
169
153
def simple_decoder(field):
170
154
return simple_field_decoders.get(field.type, None)
156
def simple_encoder(value):
157
return simple_type_encoders.get(type(value), None)
172
159
character_types = [
174
161
FIELD_TYPE.STRING,