Mercurial > p > mysql-python > mysqldb-2
annotate MySQLdb/converters.py @ 48:f4fd8c20511c MySQLdb
Read a default file in the test setUp. Since Python 2.4, int() will return longs if needed so make all long references int as in Python 3.0 there is no more long due to int/long unification (new ints are old longs).
author | adustman |
---|---|
date | Sun, 22 Feb 2009 20:01:31 +0000 |
parents | e7bd07afbcb9 |
children | 0a5e28ef7564 |
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 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
|
40 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
|
41 mysql_timestamp_converter |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
42 from types import InstanceType |
0 | 43 import array |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
44 import datetime |
0 | 45 |
35
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
46 try: |
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
47 set |
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
48 except NameError: |
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
49 from sets import Set as set |
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
50 |
14 | 51 __revision__ = "$Revision$"[11:-2] |
52 __author__ = "$Author$"[9:-2] | |
0 | 53 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
54 def bool_to_sql(boolean, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
55 """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
|
56 return str(int(boolean)) |
0 | 57 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
58 def SET_to_Set(value): |
35
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
59 """Convert MySQL SET column to Python set.""" |
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
60 return set([ i for i in value.split(',') if i ]) |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
61 |
35
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
62 def Set_to_sql(value, conv): |
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
63 """Convert a Python xet to an SQL literal.""" |
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
64 return string_literal(','.join(value), conv) |
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
65 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
66 def object_to_sql(obj, conv): |
0 | 67 """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
|
68 return str(obj) |
0 | 69 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
70 def unicode_to_sql(value, conv): |
0 | 71 """Convert a unicode object to a string using the default encoding. |
72 This is only used as a placeholder for the real function, which | |
73 is connection-dependent.""" | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
74 assert isinstance(value, unicode) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
75 return value.encode() |
0 | 76 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
77 def float_to_sql(value, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
78 return '%.15g' % value |
0 | 79 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
80 def None_to_sql(value, conv): |
0 | 81 """Convert None to NULL.""" |
82 return NULL # duh | |
83 | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
84 def object_to_quoted_sql(obj, conv): |
0 | 85 """Convert something into a SQL string literal. If using |
86 MySQL-3.23 or newer, string_literal() is a method of the | |
87 _mysql.MYSQL object, and this function will be overridden with | |
88 that method when the connection is created.""" | |
89 | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
90 return string_literal(obj, conv) |
0 | 91 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
92 def instance_to_sql(obj, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
93 """Convert an Instance to a string representation. If the __str__() |
0 | 94 method produces acceptable output, then you don't need to add the |
95 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
|
96 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
|
97 first class it can find for which obj is an instance. |
0 | 98 """ |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
99 if obj.__class__ in conv: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
100 return conv[obj.__class__](obj, conv) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
101 classes = [ key for key in conv.keys() |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
102 if isinstance(obj, key) ] |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
103 if not classes: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
104 return conv[types.StringType](obj, conv) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
105 conv[obj.__class__] = conv[classes[0]] |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
106 return conv[classes[0]](obj, conv) |
0 | 107 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
108 def char_array(obj): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
109 return array.array('c', obj) |
0 | 110 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
111 def array_to_sql(obj, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
112 return object_to_quoted_sql(obj.tostring(), conv) |
0 | 113 |
114 conversions = { | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
115 int: object_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
116 long: object_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
117 float: float_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
118 type(None): None_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
119 tuple: escape_sequence, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
120 list: escape_sequence, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
121 dict: escape_dict, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
122 InstanceType: instance_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
123 array.array: array_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
124 unicode: unicode_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
125 object: instance_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
126 bool: bool_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
127 datetime.datetime: datetime_to_sql, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
128 datetime.timedelta: timedelta_to_sql, |
35
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
129 set: Set_to_sql, |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
130 str: object_to_quoted_sql, # default |
35
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
15
diff
changeset
|
131 |
0 | 132 FIELD_TYPE.TINY: int, |
133 FIELD_TYPE.SHORT: int, | |
48
f4fd8c20511c
Read a default file in the test setUp. Since Python 2.4, int() will return longs if needed so make all long references int as in Python 3.0 there is no more long due to int/long unification (new ints are old longs).
adustman
parents:
35
diff
changeset
|
134 FIELD_TYPE.LONG: int, |
0 | 135 FIELD_TYPE.FLOAT: float, |
136 FIELD_TYPE.DOUBLE: float, | |
137 FIELD_TYPE.DECIMAL: float, | |
138 FIELD_TYPE.NEWDECIMAL: float, | |
48
f4fd8c20511c
Read a default file in the test setUp. Since Python 2.4, int() will return longs if needed so make all long references int as in Python 3.0 there is no more long due to int/long unification (new ints are old longs).
adustman
parents:
35
diff
changeset
|
139 FIELD_TYPE.LONGLONG: int, |
0 | 140 FIELD_TYPE.INT24: int, |
141 FIELD_TYPE.YEAR: int, | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
142 FIELD_TYPE.SET: SET_to_Set, |
0 | 143 FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter, |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
144 FIELD_TYPE.DATETIME: datetime_or_None, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
145 FIELD_TYPE.TIME: timedelta_or_None, |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
5
diff
changeset
|
146 FIELD_TYPE.DATE: date_or_None, |
0 | 147 FIELD_TYPE.BLOB: [ |
4 | 148 (FLAG.BINARY, str), |
149 ], | |
0 | 150 FIELD_TYPE.STRING: [ |
4 | 151 (FLAG.BINARY, str), |
152 ], | |
0 | 153 FIELD_TYPE.VAR_STRING: [ |
4 | 154 (FLAG.BINARY, str), |
155 ], | |
156 FIELD_TYPE.VARCHAR: [ | |
157 (FLAG.BINARY, str), | |
158 ], | |
0 | 159 } |
160 | |
161 try: | |
162 from decimal import Decimal | |
163 conversions[FIELD_TYPE.DECIMAL] = Decimal | |
164 conversions[FIELD_TYPE.NEWDECIMAL] = Decimal | |
165 except ImportError: | |
166 pass | |
167 | |
168 | |
169 |