Mercurial > p > mysql-python > mysqldb-2
comparison MySQLdb/converters.py @ 64:2d6a35051f64 MySQLdb
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
author | adustman |
---|---|
date | Sat, 28 Mar 2009 13:37:58 +0000 |
parents | 5db99d9be0fb |
children | 5a7c30cd9de2 |
comparison
equal
deleted
inserted
replaced
63:34176b94a93f | 64:2d6a35051f64 |
---|---|
105 return array.array('c', obj) | 105 return array.array('c', obj) |
106 | 106 |
107 def array_to_sql(obj, conv): | 107 def array_to_sql(obj, conv): |
108 return object_to_quoted_sql(obj.tostring(), conv) | 108 return object_to_quoted_sql(obj.tostring(), conv) |
109 | 109 |
110 conversions = { | 110 simple_type_encoders = { |
111 int: object_to_sql, | 111 int: object_to_sql, |
112 long: object_to_sql, | 112 long: object_to_sql, |
113 float: float_to_sql, | 113 float: float_to_sql, |
114 type(None): None_to_sql, | 114 type(None): None_to_sql, |
115 tuple: escape_sequence, | 115 tuple: escape_sequence, |
122 bool: bool_to_sql, | 122 bool: bool_to_sql, |
123 datetime.datetime: datetime_to_sql, | 123 datetime.datetime: datetime_to_sql, |
124 datetime.timedelta: timedelta_to_sql, | 124 datetime.timedelta: timedelta_to_sql, |
125 set: Set_to_sql, | 125 set: Set_to_sql, |
126 str: object_to_quoted_sql, # default | 126 str: object_to_quoted_sql, # default |
127 | |
128 } | 127 } |
129 | 128 |
130 # This is for MySQL column types that can be converted directly | 129 # This is for MySQL column types that can be converted directly |
131 # into Python types without having to look at metadata (flags, | 130 # into Python types without having to look at metadata (flags, |
132 # character sets, etc.). This should always be used as the last | 131 # character sets, etc.). This should always be used as the last |
133 # resort. | 132 # resort. |
134 simple_sql_to_python_conversions = { | 133 simple_field_decoders = { |
135 FIELD_TYPE.TINY: int, | 134 FIELD_TYPE.TINY: int, |
136 FIELD_TYPE.SHORT: int, | 135 FIELD_TYPE.SHORT: int, |
137 FIELD_TYPE.LONG: int, | 136 FIELD_TYPE.LONG: int, |
138 FIELD_TYPE.FLOAT: float, | 137 FIELD_TYPE.FLOAT: float, |
139 FIELD_TYPE.DOUBLE: float, | 138 FIELD_TYPE.DOUBLE: float, |
147 FIELD_TYPE.DATETIME: datetime_or_None, | 146 FIELD_TYPE.DATETIME: datetime_or_None, |
148 FIELD_TYPE.TIME: timedelta_or_None, | 147 FIELD_TYPE.TIME: timedelta_or_None, |
149 FIELD_TYPE.DATE: date_or_None, | 148 FIELD_TYPE.DATE: date_or_None, |
150 } | 149 } |
151 | 150 |
152 # Converter plugin protocol | 151 # Decoder protocol |
153 # Each plugin is passed a cursor object and a field object. | 152 # Each decoder is passed a cursor object and a field object. |
154 # The plugin returns a single value: | 153 # The decoder returns a single value: |
155 # A callable that given an SQL value, returns a Python object. | 154 # * A callable that given an SQL value, returns a Python object. |
156 # This can be as simple as int or str, etc. If the plugin | 155 # This can be as simple as int or str, etc. If the decoder |
157 # returns None, this plugin will be ignored and the next plugin | 156 # returns None, this decoder will be ignored and the next decoder |
158 # on the stack will be checked. | 157 # on the stack will be checked. |
159 | 158 |
160 def filter_NULL(f): | 159 def filter_NULL(f): |
161 def _filter_NULL(o): | 160 def _filter_NULL(o): |
162 if o is None: return o | 161 if o is None: return o |
163 return f(o) | 162 return f(o) |
164 _filter_NULL.__name__ = f.__name__ | 163 _filter_NULL.__name__ = f.__name__ |
165 return _filter_NULL | 164 return _filter_NULL |
166 | 165 |
167 def sql_to_python_last_resort_plugin(cursor, field): | 166 def default_decoder(cursor, field): |
168 return str | 167 return str |
169 | 168 |
170 def simple_sql_to_python_plugin(cursor, field): | 169 def simple_decoder(cursor, field): |
171 return simple_sql_to_python_conversions.get(field.type, None) | 170 return simple_field_decoders.get(field.type, None) |
172 | 171 |
173 character_types = [ | 172 character_types = [ |
174 FIELD_TYPE.BLOB, | 173 FIELD_TYPE.BLOB, |
175 FIELD_TYPE.STRING, | 174 FIELD_TYPE.STRING, |
176 FIELD_TYPE.VAR_STRING, | 175 FIELD_TYPE.VAR_STRING, |
177 FIELD_TYPE.VARCHAR, | 176 FIELD_TYPE.VARCHAR, |
178 ] | 177 ] |
179 | 178 |
180 def character_sql_to_python_plugin(cursor, field): | 179 def character_decoder(cursor, field): |
181 if field.type not in character_types: | 180 if field.type not in character_types: |
182 return None | 181 return None |
183 if field.charsetnr == 63: | 182 if field.charsetnr == 63: # BINARY |
184 return str | 183 return str |
185 | 184 |
186 charset = cursor.connection.character_set_name() | 185 charset = cursor.connection.character_set_name() |
187 def char_to_unicode(s): | 186 def char_to_unicode(s): |
188 return s.decode(charset) | 187 return s.decode(charset) |
189 | 188 |
190 return char_to_unicode | 189 return char_to_unicode |
191 | 190 |
192 sql_to_python_plugins = [ | 191 default_decoders = [ |
193 character_sql_to_python_plugin, | 192 character_decoder, |
194 simple_sql_to_python_plugin, | 193 simple_decoder, |
195 sql_to_python_last_resort_plugin, | 194 default_decoder, |
196 ] | 195 ] |
197 | 196 |
198 def lookup_converter(cursor, field): | 197 default_encoders = [ |
199 for plugin in sql_to_python_plugins: | 198 ] |
200 f = plugin(cursor, field) | 199 |
201 if f: | 200 |
202 return filter_NULL(f) | 201 |
203 return None # this should never happen | 202 |
204 | 203 |
205 | 204 |
206 | |
207 |