Mercurial > p > mysql-python > mysqldb-2
annotate MySQLdb/converters.py @ 15:a275593a1630 MySQLdb
More doc fixes
author | adustman |
---|---|
date | Mon, 26 Feb 2007 18:59:22 +0000 |
parents | 7773efbe9b30 |
children | e7bd07afbcb9 |
rev | line source |
---|---|
15 | 1 """ |
2 MySQLdb type conversion module | |
3 ------------------------------ | |
0 | 4 |
15 | 5 This module handles all the type conversions for MySQL. If the default type |
6 conversions aren't what you need, you can make your own. The dictionary | |
7 conversions maps some kind of type to a conversion function which returns the | |
8 corresponding value: | |
0 | 9 |
10 Key: FIELD_TYPE.* (from MySQLdb.constants) | |
11 | |
12 Conversion function: | |
13 | |
14 Arguments: string | |
15 | |
16 Returns: Python object | |
17 | |
18 Key: Python type object (from types) or class | |
19 | |
20 Conversion function: | |
21 | |
22 Arguments: Python object of indicated type or class AND | |
23 conversion dictionary | |
24 | |
25 Returns: SQL literal value | |
26 | |
15 | 27 Notes: Most conversion functions can ignore the dictionary, but it is a |
28 required parameter. It is necessary for converting things like sequences | |
29 and instances. | |
0 | 30 |
31 Don't modify conversions if you can avoid it. Instead, make copies | |
32 (with the copy() method), modify the copies, and then pass them to | |
33 MySQL.connect(). | |
34 | |
35 """ | |
36 | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
37 from _mysql import string_literal, escape_sequence, escape_dict, NULL |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
38 from MySQLdb.constants import FIELD_TYPE, FLAG |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
39 from sets import Set |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
40 from MySQLdb.times import datetime_to_sql, timedelta_to_sql, \ |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
41 timedelta_or_None, datetime_or_None, date_or_None, \ |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
42 mysql_timestamp_converter |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
43 from types import InstanceType |
0 | 44 import array |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
45 import datetime |
0 | 46 |
14 | 47 __revision__ = "$Revision$"[11:-2] |
48 __author__ = "$Author$"[9:-2] | |
0 | 49 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
50 def bool_to_sql(boolean, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
51 """Convert a Python bool to an SQL literal.""" |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
52 return str(int(boolean)) |
0 | 53 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
54 def SET_to_Set(value): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
55 """Convert MySQL SET column to Python Set.""" |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
56 return Set([ i for i in value.split(',') if i ]) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
57 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
58 def Set_to_sql(set, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
59 """Convert a Python Set to an SQL literal.""" |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
60 return string_literal(','.join(set), conv) |
0 | 61 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
62 def object_to_sql(obj, conv): |
0 | 63 """Convert something into a string via str().""" |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
64 return str(obj) |
0 | 65 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
66 def unicode_to_sql(value, conv): |
0 | 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.""" | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
70 assert isinstance(value, unicode) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
71 return value.encode() |
0 | 72 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
73 def float_to_sql(value, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
74 return '%.15g' % value |
0 | 75 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
76 def None_to_sql(value, conv): |
0 | 77 """Convert None to NULL.""" |
78 return NULL # duh | |
79 | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
80 def object_to_quoted_sql(obj, conv): |
0 | 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 | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
86 return string_literal(obj, conv) |
0 | 87 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
88 def instance_to_sql(obj, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
89 """Convert an Instance to a string representation. If the __str__() |
0 | 90 method produces acceptable output, then you don't need to add the |
91 class to conversions; it will be handled by the default | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
92 converter. If the exact class is not found in conv, it will use the |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
93 first class it can find for which obj is an instance. |
0 | 94 """ |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
95 if obj.__class__ in conv: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
96 return conv[obj.__class__](obj, conv) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
97 classes = [ key for key in conv.keys() |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
98 if isinstance(obj, key) ] |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
99 if not classes: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
100 return conv[types.StringType](obj, conv) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
101 conv[obj.__class__] = conv[classes[0]] |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
102 return conv[classes[0]](obj, conv) |
0 | 103 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
104 def char_array(obj): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
105 return array.array('c', obj) |
0 | 106 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
107 def array_to_sql(obj, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
108 return object_to_quoted_sql(obj.tostring(), conv) |
0 | 109 |
110 conversions = { | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
111 int: object_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
112 long: object_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
113 float: float_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
114 type(None): None_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
115 tuple: escape_sequence, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
116 list: escape_sequence, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
117 dict: escape_dict, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
118 InstanceType: instance_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
119 array.array: array_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
120 unicode: unicode_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
121 object: instance_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
122 bool: bool_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
123 datetime.datetime: datetime_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
124 datetime.timedelta: timedelta_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
125 Set: Set_to_sql, |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
126 str: object_to_quoted_sql, # default |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
127 |
0 | 128 FIELD_TYPE.TINY: int, |
129 FIELD_TYPE.SHORT: int, | |
130 FIELD_TYPE.LONG: long, | |
131 FIELD_TYPE.FLOAT: float, | |
132 FIELD_TYPE.DOUBLE: float, | |
133 FIELD_TYPE.DECIMAL: float, | |
134 FIELD_TYPE.NEWDECIMAL: float, | |
135 FIELD_TYPE.LONGLONG: long, | |
136 FIELD_TYPE.INT24: int, | |
137 FIELD_TYPE.YEAR: int, | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
138 FIELD_TYPE.SET: SET_to_Set, |
0 | 139 FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter, |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
140 FIELD_TYPE.DATETIME: datetime_or_None, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
141 FIELD_TYPE.TIME: timedelta_or_None, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
142 FIELD_TYPE.DATE: date_or_None, |
0 | 143 FIELD_TYPE.BLOB: [ |
4 | 144 (FLAG.BINARY, str), |
145 ], | |
0 | 146 FIELD_TYPE.STRING: [ |
4 | 147 (FLAG.BINARY, str), |
148 ], | |
0 | 149 FIELD_TYPE.VAR_STRING: [ |
4 | 150 (FLAG.BINARY, str), |
151 ], | |
152 FIELD_TYPE.VARCHAR: [ | |
153 (FLAG.BINARY, str), | |
154 ], | |
0 | 155 } |
156 | |
157 try: | |
158 from decimal import Decimal | |
159 conversions[FIELD_TYPE.DECIMAL] = Decimal | |
160 conversions[FIELD_TYPE.NEWDECIMAL] = Decimal | |
161 except ImportError: | |
162 pass | |
163 | |
164 | |
165 |