annotate MySQLdb/converters.py @ 51:6122b2cacd20 MySQLdb

set and frozenset always available in Python 2.4 and newer
author adustman
date Sun, 22 Feb 2009 20:09:56 +0000
parents 0f9808c4799c
children 9ea2b0e9302e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
1 """
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
2 MySQLdb type conversion module
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
3 ------------------------------
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
4
15
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
5 This module handles all the type conversions for MySQL. If the default type
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
6 conversions aren't what you need, you can make your own. The dictionary
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
7 conversions maps some kind of type to a conversion function which returns the
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
8 corresponding value:
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
9
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
10 Key: FIELD_TYPE.* (from MySQLdb.constants)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
11
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
12 Conversion function:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
13
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
14 Arguments: string
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
15
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
16 Returns: Python object
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
17
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
18 Key: Python type object (from types) or class
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
19
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
20 Conversion function:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
21
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
22 Arguments: Python object of indicated type or class AND
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
23 conversion dictionary
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
24
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
25 Returns: SQL literal value
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
26
15
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
27 Notes: Most conversion functions can ignore the dictionary, but it is a
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
28 required parameter. It is necessary for converting things like sequences
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
29 and instances.
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
30
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
31 Don't modify conversions if you can avoid it. Instead, make copies
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
32 (with the copy() method), modify the copies, and then pass them to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
33 MySQL.connect().
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
34
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
35 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
43 import array
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
44 import datetime
49
0a5e28ef7564 decimal module always available in Python 2.4 and newer
adustman
parents: 48
diff changeset
45 from decimal import Decimal
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
46
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 12
diff changeset
47 __revision__ = "$Revision$"[11:-2]
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 12
diff changeset
48 __author__ = "$Author$"[9:-2]
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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):
35
e7bd07afbcb9 Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents: 15
diff changeset
55 """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
56 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
57
35
e7bd07afbcb9 Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents: 15
diff changeset
58 def Set_to_sql(value, conv):
50
0f9808c4799c Fix docstring typo
adustman
parents: 49
diff changeset
59 """Convert a Python set to an SQL literal."""
35
e7bd07afbcb9 Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents: 15
diff changeset
60 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
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
67 """Convert a unicode object to a string using the default encoding.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
68 This is only used as a placeholder for the real function, which
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
77 """Convert None to NULL."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
78 return NULL # duh
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
81 """Convert something into a SQL string literal. If using
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
82 MySQL-3.23 or newer, string_literal() is a method of the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
83 _mysql.MYSQL object, and this function will be overridden with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
84 that method when the connection is created."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
90 method produces acceptable output, then you don't need to add the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
109
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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,
35
e7bd07afbcb9 Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents: 15
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
35
e7bd07afbcb9 Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents: 15
diff changeset
127
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
128 FIELD_TYPE.TINY: int,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
129 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
130 FIELD_TYPE.LONG: int,
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
131 FIELD_TYPE.FLOAT: float,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
132 FIELD_TYPE.DOUBLE: float,
49
0a5e28ef7564 decimal module always available in Python 2.4 and newer
adustman
parents: 48
diff changeset
133 FIELD_TYPE.DECIMAL: Decimal,
0a5e28ef7564 decimal module always available in Python 2.4 and newer
adustman
parents: 48
diff changeset
134 FIELD_TYPE.NEWDECIMAL: Decimal,
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
135 FIELD_TYPE.LONGLONG: int,
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
136 FIELD_TYPE.INT24: int,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
143 FIELD_TYPE.BLOB: [
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
144 (FLAG.BINARY, str),
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
145 ],
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
146 FIELD_TYPE.STRING: [
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
147 (FLAG.BINARY, str),
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
148 ],
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
149 FIELD_TYPE.VAR_STRING: [
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
150 (FLAG.BINARY, str),
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
151 ],
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
152 FIELD_TYPE.VARCHAR: [
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
153 (FLAG.BINARY, str),
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
154 ],
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
155 }
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
156
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
157
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
158
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
159