annotate MySQLdb/converters.py @ 0:e48810735f11 MySQLdb

Copying 1.2.1 to be the new trunk
author adustman
date Sun, 02 Apr 2006 18:20:53 +0000
parents
children b5a377255eea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
1 """MySQLdb type conversion module
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
2
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
3 This module handles all the type conversions for MySQL. If the default
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
4 type conversions aren't what you need, you can make your own. The
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
5 dictionary conversions maps some kind of type to a conversion function
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
6 which returns the corresponding value:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
7
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
8 Key: FIELD_TYPE.* (from MySQLdb.constants)
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 Conversion function:
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 Arguments: string
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 Returns: Python object
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 Key: Python type object (from types) or class
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 Conversion function:
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 Arguments: Python object of indicated type or class AND
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
21 conversion dictionary
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
22
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
23 Returns: SQL literal value
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 Notes: Most conversion functions can ignore the dictionary, but
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
26 it is a required parameter. It is necessary for converting
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
27 things like sequences and instances.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
28
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
29 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
30 (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
31 MySQL.connect().
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
32
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
33 """
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 from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
36 from constants import FIELD_TYPE, FLAG
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
37 from sets import BaseSet, Set
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
38 from times import *
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
39 import types
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
40 import array
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
41
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
42 def Bool2Str(s, d): return str(int(s))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
43
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
44 def Str2Set(s):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
45 return Set([ i for i in s.split(',') if i ])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
46
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
47 def Set2Str(s, d):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
48 return string_literal(','.join(s), d)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
49
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
50 def Thing2Str(s, d):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
51 """Convert something into a string via str()."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
52 return str(s)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
53
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
54 def Unicode2Str(s, d):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
55 """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
56 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
57 is connection-dependent."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
58 return s.encode()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
59
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
60 Long2Int = Thing2Str
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
61
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
62 def Float2Str(o, d):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
63 return '%.15g' % o
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
64
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
65 def None2NULL(o, d):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
66 """Convert None to NULL."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
67 return NULL # duh
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
68
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
69 def Thing2Literal(o, d):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
70
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
71 """Convert something into a SQL string literal. If using
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
72 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
73 _mysql.MYSQL object, and this function will be overridden with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
74 that method when the connection is created."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
75
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
76 return string_literal(o, d)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
77
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
78
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
79 def Instance2Str(o, d):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
80
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
81 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
82
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
83 Convert an Instance to a string representation. If the __str__()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
84 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
85 class to conversions; it will be handled by the default
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
86 converter. If the exact class is not found in d, it will use the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
87 first class it can find for which o is an instance.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
88
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
89 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
90
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
91 if d.has_key(o.__class__):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
92 return d[o.__class__](o, d)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
93 cl = filter(lambda x,o=o:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
94 type(x) is types.ClassType
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
95 and isinstance(o, x), d.keys())
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
96 if not cl and hasattr(types, 'ObjectType'):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
97 cl = filter(lambda x,o=o:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
98 type(x) is types.TypeType
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
99 and isinstance(o, x)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
100 and d[x] is not Instance2Str,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
101 d.keys())
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
102 if not cl:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
103 return d[types.StringType](o,d)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
104 d[o.__class__] = d[cl[0]]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
105 return d[cl[0]](o, d)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
106
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
107 def char_array(s):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
108 return array.array('c', s)
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 def array2Str(o, d):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
111 return Thing2Literal(o.tostring(), d)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
112
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
113 conversions = {
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
114 types.IntType: Thing2Str,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
115 types.LongType: Long2Int,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
116 types.FloatType: Float2Str,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
117 types.NoneType: None2NULL,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
118 types.TupleType: escape_sequence,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
119 types.ListType: escape_sequence,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
120 types.DictType: escape_dict,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
121 types.InstanceType: Instance2Str,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
122 array.ArrayType: array2Str,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
123 types.StringType: Thing2Literal, # default
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
124 types.UnicodeType: Unicode2Str,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
125 types.ObjectType: Instance2Str,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
126 types.BooleanType: Bool2Str,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
127 DateTimeType: DateTime2literal,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
128 DateTimeDeltaType: DateTimeDelta2literal,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
129 Set: Set2Str,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
130 FIELD_TYPE.TINY: int,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
131 FIELD_TYPE.SHORT: int,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
132 FIELD_TYPE.LONG: long,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
133 FIELD_TYPE.FLOAT: float,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
134 FIELD_TYPE.DOUBLE: float,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
135 FIELD_TYPE.DECIMAL: float,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
136 FIELD_TYPE.NEWDECIMAL: float,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
137 FIELD_TYPE.LONGLONG: long,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
138 FIELD_TYPE.INT24: int,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
139 FIELD_TYPE.YEAR: int,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
140 FIELD_TYPE.SET: Str2Set,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
141 FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
142 FIELD_TYPE.DATETIME: DateTime_or_None,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
143 FIELD_TYPE.TIME: TimeDelta_or_None,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
144 FIELD_TYPE.DATE: Date_or_None,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
145 FIELD_TYPE.BLOB: [
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
146 (FLAG.BINARY, char_array),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
147 (None, None),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
148 ],
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
149 FIELD_TYPE.STRING: [
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
150 (FLAG.SET, Str2Set),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
151 (None, None),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
152 ],
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
153 FIELD_TYPE.VAR_STRING: [
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
154 (FLAG.SET, Str2Set),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
155 (None, None),
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 try:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
160 from decimal import Decimal
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
161 conversions[FIELD_TYPE.DECIMAL] = Decimal
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
162 conversions[FIELD_TYPE.NEWDECIMAL] = Decimal
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
163 except ImportError:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
164 pass
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
165
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
166
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
167