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