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