~vcs-imports/python-mysqldb/old-svn-trunk

« back to all changes in this revision

Viewing changes to MySQLdb/converters.py

  • Committer: adustman
  • Date: 2009-03-30 20:21:24 UTC
  • Revision ID: vcs-imports@canonical.com-20090330202124-j3ehf98sy2zl06ih
Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
"""
36
36
 
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, \
44
44
import datetime
45
45
from decimal import Decimal
46
46
 
47
 
__revision__ = "$Revision: 623 $"[11:-2]
 
47
__revision__ = "$Revision: 624 $"[11:-2]
48
48
__author__ = "$Author: adustman $"[9:-2]
49
49
 
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))
53
53
 
55
55
    """Convert MySQL SET column to Python set."""
56
56
    return set([ i for i in value.split(',') if i ])
57
57
 
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)
61
 
 
62
 
def object_to_sql(obj, conv):
63
 
    """Convert something into a string via str()."""
64
 
    return str(obj)
65
 
 
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)
71
 
    return value.encode()
72
 
 
73
 
def float_to_sql(value, conv):
 
60
    return connection.string_literal(','.join(value))
 
61
 
 
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))
 
66
 
 
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()))
 
70
 
 
71
def float_to_sql(connection, value):
74
72
    return '%.15g' % value
75
73
 
76
 
def None_to_sql(value, conv):
 
74
def None_to_sql(connection, value):
77
75
    """Convert None to NULL."""
78
76
    return NULL # duh
79
77
 
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."""
85
 
 
86
 
    return string_literal(obj, conv)
87
 
 
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))
 
83
 
 
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)
103
99
 
104
 
def char_array(obj):
105
 
    return array.array('c', obj)
106
 
 
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())
109
102
 
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,
117
 
    dict: escape_dict,
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.
158
146
 
159
 
def filter_NULL(f):
160
 
    def _filter_NULL(o):
161
 
        if o is None: return o
162
 
        return f(o)
163
 
    _filter_NULL.__name__ = f.__name__
164
 
    return _filter_NULL
165
 
 
166
147
def default_decoder(field):
167
148
    return str
168
149
 
 
150
def default_encoder(value):
 
151
    return object_to_quoted_sql
 
152
    
169
153
def simple_decoder(field):
170
154
    return simple_field_decoders.get(field.type, None)
171
155
 
 
156
def simple_encoder(value):
 
157
    return simple_type_encoders.get(type(value), None)
 
158
 
172
159
character_types = [
173
160
    FIELD_TYPE.BLOB, 
174
161
    FIELD_TYPE.STRING,
195
182
    ]
196
183
 
197
184
default_encoders = [
 
185
    simple_encoder,
 
186
    default_encoder,
198
187
    ]
199
188
 
200
189