Mercurial > p > mysql-python > mysqldb-2
comparison MySQLdb/converters.py @ 57:9ea2b0e9302e MySQLdb
The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
author | adustman |
---|---|
date | Sat, 28 Feb 2009 04:06:44 +0000 |
parents | 6122b2cacd20 |
children | 5db99d9be0fb |
comparison
equal
deleted
inserted
replaced
56:89b07ce2a788 | 57:9ea2b0e9302e |
---|---|
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 | 127 |
128 } | |
129 | |
130 # This is for MySQL column types that can be converted directly | |
131 # into Python types without having to look at metadata (flags, | |
132 # character sets, etc.). This should always be used as the last | |
133 # resort. | |
134 simple_sql_to_python_conversions = { | |
128 FIELD_TYPE.TINY: int, | 135 FIELD_TYPE.TINY: int, |
129 FIELD_TYPE.SHORT: int, | 136 FIELD_TYPE.SHORT: int, |
130 FIELD_TYPE.LONG: int, | 137 FIELD_TYPE.LONG: int, |
131 FIELD_TYPE.FLOAT: float, | 138 FIELD_TYPE.FLOAT: float, |
132 FIELD_TYPE.DOUBLE: float, | 139 FIELD_TYPE.DOUBLE: float, |
137 FIELD_TYPE.YEAR: int, | 144 FIELD_TYPE.YEAR: int, |
138 FIELD_TYPE.SET: SET_to_Set, | 145 FIELD_TYPE.SET: SET_to_Set, |
139 FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter, | 146 FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter, |
140 FIELD_TYPE.DATETIME: datetime_or_None, | 147 FIELD_TYPE.DATETIME: datetime_or_None, |
141 FIELD_TYPE.TIME: timedelta_or_None, | 148 FIELD_TYPE.TIME: timedelta_or_None, |
142 FIELD_TYPE.DATE: date_or_None, | 149 FIELD_TYPE.DATE: date_or_None, |
143 FIELD_TYPE.BLOB: [ | |
144 (FLAG.BINARY, str), | |
145 ], | |
146 FIELD_TYPE.STRING: [ | |
147 (FLAG.BINARY, str), | |
148 ], | |
149 FIELD_TYPE.VAR_STRING: [ | |
150 (FLAG.BINARY, str), | |
151 ], | |
152 FIELD_TYPE.VARCHAR: [ | |
153 (FLAG.BINARY, str), | |
154 ], | |
155 } | 150 } |
156 | 151 |
157 | 152 # Converter plugin protocol |
158 | 153 # Each plugin is passed a cursor object and a field object. |
159 | 154 # The plugin returns a single value: |
155 # 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 | |
157 # returns None, this plugin will be ignored and the next plugin | |
158 # on the stack will be checked. | |
159 | |
160 def filter_NULL(f): | |
161 def _filter_NULL(o): | |
162 if o is None: return o | |
163 return f(o) | |
164 _filter_NULL.__name__ = f.__name__ | |
165 return _filter_NULL | |
166 | |
167 def sql_to_python_last_resort_plugin(cursor, field): | |
168 return str | |
169 | |
170 def simple_sql_to_python_plugin(cursor, field): | |
171 return simple_sql_to_python_conversions.get(field.type, None) | |
172 | |
173 character_types = [ | |
174 FIELD_TYPE.BLOB, | |
175 FIELD_TYPE.STRING, | |
176 FIELD_TYPE.VAR_STRING, | |
177 FIELD_TYPE.VARCHAR, | |
178 ] | |
179 | |
180 def character_sql_to_python_plugin(cursor, field): | |
181 if field.type not in character_types: | |
182 return None | |
183 if field.flags & FLAG.BINARY: | |
184 return str | |
185 | |
186 charset = cursor.connection.character_set_name() | |
187 def char_to_unicode(s): | |
188 return s.decode(charset) | |
189 | |
190 return char_to_unicode | |
191 | |
192 sql_to_python_plugins = [ | |
193 character_sql_to_python_plugin, | |
194 simple_sql_to_python_plugin, | |
195 sql_to_python_last_resort_plugin, | |
196 ] | |
197 | |
198 def lookup_converter(cursor, field): | |
199 for plugin in sql_to_python_plugins: | |
200 f = plugin(cursor, field) | |
201 if f: | |
202 return filter_NULL(f) | |
203 return None # this should never happen | |
204 | |
205 | |
206 | |
207 |