annotate MySQLdb/converters.py @ 74:80164eb2f090 MySQLdb

This passes all test, yet is still broken and ugly in many ways. However, a lot of ugliness has been removed.
author adustman
date Sat, 20 Feb 2010 04:27:21 +0000
parents c0c00294239b
children 3b03cb566032
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
5
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
6
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
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
9 from _mysql import NULL
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
10 from MySQLdb.constants import FIELD_TYPE, FLAG
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
11 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
12 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
13 mysql_timestamp_converter
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
14 from types import InstanceType
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
15 import array
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
16 import datetime
49
0a5e28ef7564 decimal module always available in Python 2.4 and newer
adustman
parents: 48
diff changeset
17 from decimal import Decimal
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
18 from itertools import izip
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
19
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 12
diff changeset
20 __revision__ = "$Revision$"[11:-2]
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 12
diff changeset
21 __author__ = "$Author$"[9:-2]
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
22
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
23 def bool_to_sql(connection, boolean):
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
24 """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
25 return str(int(boolean))
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
26
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
27 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
28 """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
29 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
30
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
31 def Set_to_sql(connection, value):
50
0f9808c4799c Fix docstring typo
adustman
parents: 49
diff changeset
32 """Convert a Python set to an SQL literal."""
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
33 return connection.string_literal(','.join(value))
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
34
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
35 def object_to_sql(connection, obj):
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
36 """Convert something into a string via str().
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
37 The result will not be quoted."""
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
38 return connection.escape_string(str(obj))
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
39
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
40 def unicode_to_sql(connection, value):
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
41 """Convert a unicode object to a string using the connection encoding."""
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
42 return connection.string_literal(value.encode(connection.character_set_name()))
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
43
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
44 def float_to_sql(connection, value):
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
45 return '%.15g' % value
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
46
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
47 def None_to_sql(connection, value):
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
48 """Convert None to NULL."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
49 return NULL # duh
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
50
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
51 def object_to_quoted_sql(connection, obj):
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
52 """Convert something into a SQL string literal."""
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
53 if hasattr(obj, "__unicode__"):
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
54 return unicode_to_sql(connection, obj)
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
55 return connection.string_literal(str(obj))
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
56
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
57 def instance_to_sql(connection, obj):
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
58 """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
59 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
60 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
61 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
62 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
63 """
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
64 if obj.__class__ in conv:
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
65 return conv[obj.__class__](obj, conv)
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
66 classes = [ key for key in conv.keys()
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
67 if isinstance(obj, key) ]
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
68 if not classes:
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
69 return conv[types.StringType](obj, conv)
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
70 conv[obj.__class__] = conv[classes[0]]
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
71 return conv[classes[0]](obj, conv)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
72
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
73 def array_to_sql(connection, obj):
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
74 return connection.string_literal(obj.tostring())
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
75
64
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
76 simple_type_encoders = {
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
77 int: object_to_sql,
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
78 long: object_to_sql,
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
79 float: float_to_sql,
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
80 type(None): None_to_sql,
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
81 unicode: unicode_to_sql,
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
82 object: instance_to_sql,
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
83 bool: bool_to_sql,
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
84 datetime.datetime: datetime_to_sql,
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
85 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
86 set: Set_to_sql,
12
d68fe80ce1c3 More docstrings and doctests for times and other cleanups.
adustman
parents: 11
diff changeset
87 str: object_to_quoted_sql, # default
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
88 }
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
89
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
90 # This is for MySQL column types that can be converted directly
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
91 # into Python types without having to look at metadata (flags,
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
92 # character sets, etc.). This should always be used as the last
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
93 # resort.
64
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
94 simple_field_decoders = {
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
95 FIELD_TYPE.TINY: int,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
96 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
97 FIELD_TYPE.LONG: int,
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
98 FIELD_TYPE.FLOAT: float,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
99 FIELD_TYPE.DOUBLE: float,
49
0a5e28ef7564 decimal module always available in Python 2.4 and newer
adustman
parents: 48
diff changeset
100 FIELD_TYPE.DECIMAL: Decimal,
0a5e28ef7564 decimal module always available in Python 2.4 and newer
adustman
parents: 48
diff changeset
101 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
102 FIELD_TYPE.LONGLONG: int,
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
103 FIELD_TYPE.INT24: int,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
104 FIELD_TYPE.YEAR: int,
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
105 FIELD_TYPE.SET: SET_to_Set,
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
106 FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter,
11
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
107 FIELD_TYPE.DATETIME: datetime_or_None,
aef6ea6a9737 More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents: 5
diff changeset
108 FIELD_TYPE.TIME: timedelta_or_None,
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
109 FIELD_TYPE.DATE: date_or_None,
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
110 }
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
111
64
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
112 # Decoder protocol
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
113 # Each decoder is passed a cursor object and a field object.
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
114 # The decoder returns a single value:
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
115 # * A callable that given an SQL value, returns a Python object.
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
116 # This can be as simple as int or str, etc. If the decoder
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
117 # returns None, this decoder will be ignored and the next decoder
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
118 # on the stack will be checked.
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
119
66
5a7c30cd9de2 By exposing connection on result, decoders can be simplified: Only the field object is needed. Turned on leak testing and found a leak writing BLOBs. removed one of the old stubs in Cursor.
adustman
parents: 64
diff changeset
120 def default_decoder(field):
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
121 return str
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
122
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
123 def default_encoder(value):
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
124 return object_to_quoted_sql
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
125
66
5a7c30cd9de2 By exposing connection on result, decoders can be simplified: Only the field object is needed. Turned on leak testing and found a leak writing BLOBs. removed one of the old stubs in Cursor.
adustman
parents: 64
diff changeset
126 def simple_decoder(field):
64
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
127 return simple_field_decoders.get(field.type, None)
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
128
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
129 def simple_encoder(value):
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
130 return simple_type_encoders.get(type(value), None)
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
131
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
132 character_types = [
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
133 FIELD_TYPE.BLOB,
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
134 FIELD_TYPE.STRING,
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
135 FIELD_TYPE.VAR_STRING,
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
136 FIELD_TYPE.VARCHAR,
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
137 ]
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
138
66
5a7c30cd9de2 By exposing connection on result, decoders can be simplified: Only the field object is needed. Turned on leak testing and found a leak writing BLOBs. removed one of the old stubs in Cursor.
adustman
parents: 64
diff changeset
139 def character_decoder(field):
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
140 if field.type not in character_types:
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
141 return None
64
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
142 if field.charsetnr == 63: # BINARY
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
143 return str
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
144
66
5a7c30cd9de2 By exposing connection on result, decoders can be simplified: Only the field object is needed. Turned on leak testing and found a leak writing BLOBs. removed one of the old stubs in Cursor.
adustman
parents: 64
diff changeset
145 charset = field.result.connection.character_set_name()
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
146 def char_to_unicode(s):
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
147 if s is None:
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
148 return s
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
149 return s.decode(charset)
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
150
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
151 return char_to_unicode
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
152
64
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
153 default_decoders = [
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
154 character_decoder,
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
155 simple_decoder,
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
156 default_decoder,
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
157 ]
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
158
64
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
159 default_encoders = [
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
160 simple_encoder,
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 66
diff changeset
161 default_encoder,
64
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
162 ]
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 51
diff changeset
163
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
164 def get_codec(field, codecs):
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
165 for c in codecs:
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
166 func = c(field)
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
167 if func:
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
168 return func
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
169 # the default codec is guaranteed to work
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
170
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
171 def iter_row_decoder(decoders, row):
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
172 if row is None:
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
173 return None
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
174 return ( d(col) for d, col in izip(decoders, row) )
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
175
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
176 def tuple_row_decoder(decoders, row):
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
177 if row is None:
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
178 return None
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
179 return tuple(iter_row_decoder(decoders, row))
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
180
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
181
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
182
64
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
183
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 59
diff changeset
184
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
185