1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 __docformat__ = "epytext en"
56
57 """
58 This datasource module a datasource class for MySQL databases.
59 It relies on the Module to connect to the backend. See
60 U{http://sourceforge.net/projects/mysql-python} for details.
61 """
62
63
64 from types import *
65 from string import *
66
67
68 import MySQLdb
69 from _mysql import *
70
71
72 import orm2.datasource
73 from orm2 import sql
74 from orm2.datatypes import common_serial
75 import datatypes
76
78
79 escaped_chars = ( ('"', r'\"',),
80 ("'", r'\"',),
81 ("%", "%%",), )
82
83 encodings = {"usa7": "us-ascii",
84 "big5": "big5",
85 "gbk": "gbk",
86 "sjis": "sjis",
87 "cp932": "sjis",
88 "cp932": "sjis",
89 "cp932": "sjis",
90 "gb2312": "euc-cn",
91 "ujis": "euc-jp",
92 "euc-kr": "euc-kr",
93 "latin1": "iso8859-1",
94 "latin1-de": "iso8859-1",
95 "german1": "iso8859-1",
96 "danish": "iso8859-1",
97 "latin2": "iso8859-2",
98 "czech": "iso8859-2",
99 "hungarian": "iso8859-2",
100 "croat": "iso8859-2",
101 "greek": "iso8859-7",
102 "hebrew": "iso8859-8",
103 "latin5": "iso8859-9",
104 "latvian": "iso8859-13",
105 "latvian1": "iso8859-13",
106 "estonia": "iso8859-13",
107 "dos": "cp437",
108 "pclatin2": "cp852",
109 "cp866": "cp866",
110 "koi8-ru": "koi8-r",
111 "tis620": "tis620",
112 "win1250": "cp1250",
113 "win1250ch": "cp1250",
114 "win1251": "cp1251",
115 "cp1251": "cp1251",
116 "win1251ukr": "cp1251",
117 "cp1257": "cp1257",
118 "macroman": "macroman",
119 "macce": "maccentraleurope",
120 "utf8": "utf-8",
121 "ucs2": "unicodebig"}
122
124 """
125 This constructor accepts all those keyword args the MySQL module's
126 connect function knows about. They're passed on 'as is'.
127
128 The only exception is 'encoding' which lets you determine which
129 encoding orm uses to send data to the backend. It defaults to
130 utf-8. The set_character_set() function is used on the database
131 connection to set this. Encodings must be referred to by their
132 MySQL name.
133
134 From the MySQL-Python documentation:
135
136 Create a connection to the database. It is strongly recommended
137 that you only use keyword parameters. 'NULL pointer' indicates that
138 NULL will be passed to mysql_real_connect(); the value in parenthesis
139 indicates how MySQL interprets the NULL. Consult the MySQL C API
140 documentation for more information.
141
142 - host -- string, host to connect to or NULL pointer (localhost)
143 - user -- string, user to connect as or NULL pointer (your username)
144 - passwd -- string, password to use or NULL pointer (no password)
145 - db -- string, database to use or NULL (no DB selected)
146 - port -- integer, TCP/IP port to connect to or default MySQL port
147 - unix_socket -- string, location of unix_socket to use or use TCP
148 - client_flags -- integer, flags to use or 0 (see MySQL docs)
149 - conv -- conversion dictionary, see MySQLdb.converters
150 - connect_time -- number of seconds to wait before the connection
151 attempt fails.
152 - compress -- if set, compression is enabled
153 - init_command -- command which is run once the connection is created
154 - read_default_file -- see the MySQL documentation for
155 mysql_options()
156 - read_default_group -- see the MySQL documentation for
157 mysql_options()
158 - cursorclass -- class object, used to create cursors or
159 cursors.Cursor.
160 This parameter MUST be specified as a keyword parameter.
161
162 """
163 orm2.datasource.datasource_base.__init__(self)
164 if kwargs.has_key("encoding"):
165 encoding = kwargs["encoding"]
166 del kwargs["encoding"]
167 else:
168 encoding = "UTF8"
169
170 if encoding == "utf-8":
171 encoding = "UTF8"
172
173 kwargs["charset"] = encoding
174
175 self._connectionSpec = kwargs
176 self._conn = MySQLdb.connect(**kwargs)
177 self._update_cursor = self._conn.cursor()
178
180 """
181 Construct a mysql datasource object from an orm connection
182 string.
183
184 A connection string for the mysql adapter may contain all
185 keywords defined in orm2.datasource plus all those that are
186 described as keyword parameters for __init__ above.
187 """
188
189 if params.has_key("password"):
190 passwd = params["password"]
191 del params["password"]
192 params["passwd"] = passwd
193
194 if params.has_key("dbname"):
195 dbname = params["dbname"]
196 del params["dbname"]
197 params["db"] = dbname
198
199 return datasource(**params)
200 from_params = staticmethod(_from_params)
201
203 return '`%s`' % name
204
205
206
207
208
209
210
212 name = self._conn.character_set_name()
213 return self.encodings[lower(name)]
214
216 if dbobj.__primary_key__ is None: raise PrimaryKeyNotKnown()
217
218 primary_key_attributes = tuple(dbobj.__primary_key__.attributes())
219
220 if len(primary_key_attributes) == 1:
221 primary_key_attribute = primary_key_attributes[0]
222 else:
223 primary_key_attribute = None
224
225 if isinstance(primary_key_attribute, datatypes.auto_increment) or \
226 isinstance(primary_key_attribute, common_serial):
227 where = sql.where(primary_key_attribute.column,
228 " = LAST_INSERT_ID()")
229
230 elif dbobj.__primary_key__.isset():
231
232
233 where = dbobj.__primary_key__.where()
234
235 else:
236 raise PrimaryKeyNotKnown()
237
238 return where
239