Mercurial > p > mysql-python > mysqldb-2
comparison MySQLdb/converters.py @ 67:98d968f5af11 MySQLdb
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.
author | adustman |
---|---|
date | Mon, 30 Mar 2009 20:21:24 +0000 |
parents | 5a7c30cd9de2 |
children | c0c00294239b |
comparison
equal
deleted
inserted
replaced
66:5a7c30cd9de2 | 67:98d968f5af11 |
---|---|
32 (with the copy() method), modify the copies, and then pass them to | 32 (with the copy() method), modify the copies, and then pass them to |
33 MySQL.connect(). | 33 MySQL.connect(). |
34 | 34 |
35 """ | 35 """ |
36 | 36 |
37 from _mysql import string_literal, escape_sequence, escape_dict, NULL | 37 from _mysql import NULL |
38 from MySQLdb.constants import FIELD_TYPE, FLAG | 38 from MySQLdb.constants import FIELD_TYPE, FLAG |
39 from MySQLdb.times import datetime_to_sql, timedelta_to_sql, \ | 39 from MySQLdb.times import datetime_to_sql, timedelta_to_sql, \ |
40 timedelta_or_None, datetime_or_None, date_or_None, \ | 40 timedelta_or_None, datetime_or_None, date_or_None, \ |
41 mysql_timestamp_converter | 41 mysql_timestamp_converter |
42 from types import InstanceType | 42 from types import InstanceType |
45 from decimal import Decimal | 45 from decimal import Decimal |
46 | 46 |
47 __revision__ = "$Revision$"[11:-2] | 47 __revision__ = "$Revision$"[11:-2] |
48 __author__ = "$Author$"[9:-2] | 48 __author__ = "$Author$"[9:-2] |
49 | 49 |
50 def bool_to_sql(boolean, conv): | 50 def bool_to_sql(connection, boolean): |
51 """Convert a Python bool to an SQL literal.""" | 51 """Convert a Python bool to an SQL literal.""" |
52 return str(int(boolean)) | 52 return str(int(boolean)) |
53 | 53 |
54 def SET_to_Set(value): | 54 def SET_to_Set(value): |
55 """Convert MySQL SET column to Python set.""" | 55 """Convert MySQL SET column to Python set.""" |
56 return set([ i for i in value.split(',') if i ]) | 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 """Convert a Python set to an SQL literal.""" | 59 """Convert a Python set to an SQL literal.""" |
60 return string_literal(','.join(value), conv) | 60 return connection.string_literal(','.join(value)) |
61 | 61 |
62 def object_to_sql(obj, conv): | 62 def object_to_sql(connection, obj): |
63 """Convert something into a string via str().""" | 63 """Convert something into a string via str(). |
64 return str(obj) | 64 The result will not be quoted.""" |
65 return connection.escape_string(str(obj)) | |
65 | 66 |
66 def unicode_to_sql(value, conv): | 67 def unicode_to_sql(connection, value): |
67 """Convert a unicode object to a string using the default encoding. | 68 """Convert a unicode object to a string using the connection encoding.""" |
68 This is only used as a placeholder for the real function, which | 69 return connection.string_literal(value.encode(connection.character_set_name())) |
69 is connection-dependent.""" | |
70 assert isinstance(value, unicode) | |
71 return value.encode() | |
72 | 70 |
73 def float_to_sql(value, conv): | 71 def float_to_sql(connection, value): |
74 return '%.15g' % value | 72 return '%.15g' % value |
75 | 73 |
76 def None_to_sql(value, conv): | 74 def None_to_sql(connection, value): |
77 """Convert None to NULL.""" | 75 """Convert None to NULL.""" |
78 return NULL # duh | 76 return NULL # duh |
79 | 77 |
80 def object_to_quoted_sql(obj, conv): | 78 def object_to_quoted_sql(connection, obj): |
81 """Convert something into a SQL string literal. If using | 79 """Convert something into a SQL string literal.""" |
82 MySQL-3.23 or newer, string_literal() is a method of the | 80 if hasattr(obj, "__unicode__"): |
83 _mysql.MYSQL object, and this function will be overridden with | 81 return unicode_to_sql(connection, obj) |
84 that method when the connection is created.""" | 82 return connection.string_literal(str(obj)) |
85 | 83 |
86 return string_literal(obj, conv) | 84 def instance_to_sql(connection, obj): |
87 | |
88 def instance_to_sql(obj, conv): | |
89 """Convert an Instance to a string representation. If the __str__() | 85 """Convert an Instance to a string representation. If the __str__() |
90 method produces acceptable output, then you don't need to add the | 86 method produces acceptable output, then you don't need to add the |
91 class to conversions; it will be handled by the default | 87 class to conversions; it will be handled by the default |
92 converter. If the exact class is not found in conv, it will use the | 88 converter. If the exact class is not found in conv, it will use the |
93 first class it can find for which obj is an instance. | 89 first class it can find for which obj is an instance. |
99 if not classes: | 95 if not classes: |
100 return conv[types.StringType](obj, conv) | 96 return conv[types.StringType](obj, conv) |
101 conv[obj.__class__] = conv[classes[0]] | 97 conv[obj.__class__] = conv[classes[0]] |
102 return conv[classes[0]](obj, conv) | 98 return conv[classes[0]](obj, conv) |
103 | 99 |
104 def char_array(obj): | 100 def array_to_sql(connection, obj): |
105 return array.array('c', obj) | 101 return connection.string_literal(obj.tostring()) |
106 | |
107 def array_to_sql(obj, conv): | |
108 return object_to_quoted_sql(obj.tostring(), conv) | |
109 | 102 |
110 simple_type_encoders = { | 103 simple_type_encoders = { |
111 int: object_to_sql, | 104 int: object_to_sql, |
112 long: object_to_sql, | 105 long: object_to_sql, |
113 float: float_to_sql, | 106 float: float_to_sql, |
114 type(None): None_to_sql, | 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 unicode: unicode_to_sql, | 108 unicode: unicode_to_sql, |
121 object: instance_to_sql, | 109 object: instance_to_sql, |
122 bool: bool_to_sql, | 110 bool: bool_to_sql, |
123 datetime.datetime: datetime_to_sql, | 111 datetime.datetime: datetime_to_sql, |
124 datetime.timedelta: timedelta_to_sql, | 112 datetime.timedelta: timedelta_to_sql, |
154 # * A callable that given an SQL value, returns a Python object. | 142 # * A callable that given an SQL value, returns a Python object. |
155 # This can be as simple as int or str, etc. If the decoder | 143 # This can be as simple as int or str, etc. If the decoder |
156 # returns None, this decoder will be ignored and the next decoder | 144 # returns None, this decoder will be ignored and the next decoder |
157 # on the stack will be checked. | 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 def default_decoder(field): | 147 def default_decoder(field): |
167 return str | 148 return str |
168 | 149 |
150 def default_encoder(value): | |
151 return object_to_quoted_sql | |
152 | |
169 def simple_decoder(field): | 153 def simple_decoder(field): |
170 return simple_field_decoders.get(field.type, None) | 154 return simple_field_decoders.get(field.type, None) |
155 | |
156 def simple_encoder(value): | |
157 return simple_type_encoders.get(type(value), None) | |
171 | 158 |
172 character_types = [ | 159 character_types = [ |
173 FIELD_TYPE.BLOB, | 160 FIELD_TYPE.BLOB, |
174 FIELD_TYPE.STRING, | 161 FIELD_TYPE.STRING, |
175 FIELD_TYPE.VAR_STRING, | 162 FIELD_TYPE.VAR_STRING, |
193 simple_decoder, | 180 simple_decoder, |
194 default_decoder, | 181 default_decoder, |
195 ] | 182 ] |
196 | 183 |
197 default_encoders = [ | 184 default_encoders = [ |
185 simple_encoder, | |
186 default_encoder, | |
198 ] | 187 ] |
199 | 188 |
200 | 189 |
201 | 190 |
202 | 191 |