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
|
|
35 from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
|
|
36 from constants import FIELD_TYPE, FLAG
|
|
37 from sets import BaseSet, Set
|
|
38 from times import *
|
|
39 import types
|
|
40 import array
|
|
41
|
|
42 def Bool2Str(s, d): return str(int(s))
|
|
43
|
|
44 def Str2Set(s):
|
|
45 return Set([ i for i in s.split(',') if i ])
|
|
46
|
|
47 def Set2Str(s, d):
|
|
48 return string_literal(','.join(s), d)
|
|
49
|
|
50 def Thing2Str(s, d):
|
|
51 """Convert something into a string via str()."""
|
|
52 return str(s)
|
|
53
|
|
54 def Unicode2Str(s, d):
|
|
55 """Convert a unicode object to a string using the default encoding.
|
|
56 This is only used as a placeholder for the real function, which
|
|
57 is connection-dependent."""
|
|
58 return s.encode()
|
|
59
|
|
60 Long2Int = Thing2Str
|
|
61
|
|
62 def Float2Str(o, d):
|
|
63 return '%.15g' % o
|
|
64
|
|
65 def None2NULL(o, d):
|
|
66 """Convert None to NULL."""
|
|
67 return NULL # duh
|
|
68
|
|
69 def Thing2Literal(o, d):
|
|
70
|
|
71 """Convert something into a SQL string literal. If using
|
|
72 MySQL-3.23 or newer, string_literal() is a method of the
|
|
73 _mysql.MYSQL object, and this function will be overridden with
|
|
74 that method when the connection is created."""
|
|
75
|
|
76 return string_literal(o, d)
|
|
77
|
|
78
|
|
79 def Instance2Str(o, d):
|
|
80
|
|
81 """
|
|
82
|
|
83 Convert an Instance to a string representation. If the __str__()
|
|
84 method produces acceptable output, then you don't need to add the
|
|
85 class to conversions; it will be handled by the default
|
|
86 converter. If the exact class is not found in d, it will use the
|
|
87 first class it can find for which o is an instance.
|
|
88
|
|
89 """
|
|
90
|
|
91 if d.has_key(o.__class__):
|
|
92 return d[o.__class__](o, d)
|
|
93 cl = filter(lambda x,o=o:
|
|
94 type(x) is types.ClassType
|
|
95 and isinstance(o, x), d.keys())
|
|
96 if not cl and hasattr(types, 'ObjectType'):
|
|
97 cl = filter(lambda x,o=o:
|
|
98 type(x) is types.TypeType
|
|
99 and isinstance(o, x)
|
|
100 and d[x] is not Instance2Str,
|
|
101 d.keys())
|
|
102 if not cl:
|
|
103 return d[types.StringType](o,d)
|
|
104 d[o.__class__] = d[cl[0]]
|
|
105 return d[cl[0]](o, d)
|
|
106
|
|
107 def char_array(s):
|
|
108 return array.array('c', s)
|
|
109
|
|
110 def array2Str(o, d):
|
|
111 return Thing2Literal(o.tostring(), d)
|
|
112
|
|
113 conversions = {
|
|
114 types.IntType: Thing2Str,
|
|
115 types.LongType: Long2Int,
|
|
116 types.FloatType: Float2Str,
|
|
117 types.NoneType: None2NULL,
|
|
118 types.TupleType: escape_sequence,
|
|
119 types.ListType: escape_sequence,
|
|
120 types.DictType: escape_dict,
|
|
121 types.InstanceType: Instance2Str,
|
|
122 array.ArrayType: array2Str,
|
|
123 types.StringType: Thing2Literal, # default
|
|
124 types.UnicodeType: Unicode2Str,
|
|
125 types.ObjectType: Instance2Str,
|
|
126 types.BooleanType: Bool2Str,
|
|
127 DateTimeType: DateTime2literal,
|
|
128 DateTimeDeltaType: DateTimeDelta2literal,
|
|
129 Set: Set2Str,
|
|
130 FIELD_TYPE.TINY: int,
|
|
131 FIELD_TYPE.SHORT: int,
|
|
132 FIELD_TYPE.LONG: long,
|
|
133 FIELD_TYPE.FLOAT: float,
|
|
134 FIELD_TYPE.DOUBLE: float,
|
|
135 FIELD_TYPE.DECIMAL: float,
|
|
136 FIELD_TYPE.NEWDECIMAL: float,
|
|
137 FIELD_TYPE.LONGLONG: long,
|
|
138 FIELD_TYPE.INT24: int,
|
|
139 FIELD_TYPE.YEAR: int,
|
|
140 FIELD_TYPE.SET: Str2Set,
|
|
141 FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter,
|
|
142 FIELD_TYPE.DATETIME: DateTime_or_None,
|
|
143 FIELD_TYPE.TIME: TimeDelta_or_None,
|
|
144 FIELD_TYPE.DATE: Date_or_None,
|
|
145 FIELD_TYPE.BLOB: [
|
4
|
146 (FLAG.BINARY, str),
|
|
147 ],
|
0
|
148 FIELD_TYPE.STRING: [
|
4
|
149 (FLAG.BINARY, str),
|
0
|
150 (FLAG.SET, Str2Set),
|
4
|
151 ],
|
0
|
152 FIELD_TYPE.VAR_STRING: [
|
4
|
153 (FLAG.BINARY, str),
|
|
154 ],
|
|
155 FIELD_TYPE.VARCHAR: [
|
|
156 (FLAG.BINARY, str),
|
|
157 ],
|
0
|
158 }
|
|
159
|
|
160 try:
|
|
161 from decimal import Decimal
|
|
162 conversions[FIELD_TYPE.DECIMAL] = Decimal
|
|
163 conversions[FIELD_TYPE.NEWDECIMAL] = Decimal
|
|
164 except ImportError:
|
|
165 pass
|
|
166
|
|
167
|
|
168
|