Mercurial > p > mysql-python > mysqldb-2
annotate _mysql_results.c @ 18:d55bfb1a4701 MySQLdb
Tons of changes from major refactoring/cleanup. This is all really broken
right now. In particular, all results are returned as strings.
author | adustman |
---|---|
date | Fri, 14 Mar 2008 23:06:29 +0000 |
parents | c0d1fc0429ce |
children | 9863f08a337c |
rev | line source |
---|---|
2
c0d1fc0429ce
Smashed _mysql.c with a great big hammer and got some smaller,
adustman
parents:
1
diff
changeset
|
1 #include "_mysql.h" |
0 | 2 |
3 static char _mysql_ResultObject__doc__[] = | |
4 "result(connection, use=0, converter={}) -- Result set from a query.\n\ | |
5 \n\ | |
6 Creating instances of this class directly is an excellent way to\n\ | |
7 shoot yourself in the foot. If using _mysql.connection directly,\n\ | |
8 use connection.store_result() or connection.use_result() instead.\n\ | |
9 If using MySQLdb.Connection, this is done by the cursor class.\n\ | |
10 Just forget you ever saw this. Forget... FOR-GET..."; | |
11 | |
2
c0d1fc0429ce
Smashed _mysql.c with a great big hammer and got some smaller,
adustman
parents:
1
diff
changeset
|
12 int |
0 | 13 _mysql_ResultObject_Initialize( |
14 _mysql_ResultObject *self, | |
15 PyObject *args, | |
16 PyObject *kwargs) | |
17 { | |
18 static char *kwlist[] = {"connection", "use", "converter", NULL}; | |
19 MYSQL_RES *result; | |
20 _mysql_ConnectionObject *conn=NULL; | |
21 int use=0; | |
22 PyObject *conv=NULL; | |
23 int n, i; | |
24 MYSQL_FIELD *fields; | |
25 | |
26 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iO", kwlist, | |
27 &conn, &use, &conv)) | |
28 return -1; | |
29 if (!conv) conv = PyDict_New(); | |
30 if (!conv) return -1; | |
31 self->conn = (PyObject *) conn; | |
32 Py_INCREF(conn); | |
33 self->use = use; | |
34 Py_BEGIN_ALLOW_THREADS ; | |
35 if (use) | |
36 result = mysql_use_result(&(conn->connection)); | |
37 else | |
38 result = mysql_store_result(&(conn->connection)); | |
39 self->result = result; | |
40 Py_END_ALLOW_THREADS ; | |
41 if (!result) { | |
42 self->converter = PyTuple_New(0); | |
43 return 0; | |
44 } | |
45 n = mysql_num_fields(result); | |
46 self->nfields = n; | |
47 if (!(self->converter = PyTuple_New(n))) return -1; | |
48 fields = mysql_fetch_fields(result); | |
49 for (i=0; i<n; i++) { | |
50 PyObject *tmp, *fun; | |
51 tmp = PyInt_FromLong((long) fields[i].type); | |
52 if (!tmp) return -1; | |
53 fun = PyObject_GetItem(conv, tmp); | |
54 Py_DECREF(tmp); | |
55 if (!fun) { | |
56 PyErr_Clear(); | |
57 fun = Py_None; | |
58 Py_INCREF(Py_None); | |
59 } | |
60 if (PySequence_Check(fun)) { | |
61 int j, n2=PySequence_Size(fun); | |
62 PyObject *fun2=NULL; | |
63 for (j=0; j<n2; j++) { | |
64 PyObject *t = PySequence_GetItem(fun, j); | |
65 if (!t) continue; | |
66 if (!PyTuple_Check(t)) goto cleanup; | |
67 if (PyTuple_GET_SIZE(t) == 2) { | |
68 long mask; | |
69 PyObject *pmask=NULL; | |
70 pmask = PyTuple_GET_ITEM(t, 0); | |
71 fun2 = PyTuple_GET_ITEM(t, 1); | |
72 if (PyInt_Check(pmask)) { | |
73 mask = PyInt_AS_LONG(pmask); | |
74 if (mask & fields[i].flags) { | |
75 break; | |
76 } | |
77 else { | |
78 continue; | |
79 } | |
80 } else { | |
81 break; | |
82 } | |
83 } | |
84 cleanup: | |
85 Py_DECREF(t); | |
86 } | |
87 if (!fun2) fun2 = Py_None; | |
88 Py_INCREF(fun2); | |
89 Py_DECREF(fun); | |
90 fun = fun2; | |
91 } | |
92 PyTuple_SET_ITEM(self->converter, i, fun); | |
93 } | |
94 return 0; | |
95 } | |
96 | |
97 #if PY_VERSION_HEX >= 0x02020000 | |
98 static int _mysql_ResultObject_traverse( | |
99 _mysql_ResultObject *self, | |
100 visitproc visit, | |
101 void *arg) | |
102 { | |
103 int r; | |
104 if (self->converter) { | |
105 if (!(r = visit(self->converter, arg))) return r; | |
106 } | |
107 if (self->conn) | |
108 return visit(self->conn, arg); | |
109 return 0; | |
110 } | |
111 #endif | |
112 | |
113 static int _mysql_ResultObject_clear( | |
114 _mysql_ResultObject *self) | |
115 { | |
116 Py_XDECREF(self->converter); | |
117 self->converter = NULL; | |
118 Py_XDECREF(self->conn); | |
119 self->conn = NULL; | |
120 return 0; | |
121 } | |
122 | |
123 static char _mysql_ResultObject_describe__doc__[] = | |
124 "Returns the sequence of 7-tuples required by the DB-API for\n\ | |
125 the Cursor.description attribute.\n\ | |
126 "; | |
127 | |
128 static PyObject * | |
129 _mysql_ResultObject_describe( | |
130 _mysql_ResultObject *self, | |
131 PyObject *args) | |
132 { | |
133 PyObject *d; | |
134 MYSQL_FIELD *fields; | |
135 unsigned int i, n; | |
136 if (!PyArg_ParseTuple(args, "")) return NULL; | |
137 check_result_connection(self); | |
138 n = mysql_num_fields(self->result); | |
139 fields = mysql_fetch_fields(self->result); | |
140 if (!(d = PyTuple_New(n))) return NULL; | |
141 for (i=0; i<n; i++) { | |
142 PyObject *t; | |
143 t = Py_BuildValue("(siiiiii)", | |
144 fields[i].name, | |
145 (long) fields[i].type, | |
146 (long) fields[i].max_length, | |
147 (long) fields[i].length, | |
148 (long) fields[i].length, | |
149 (long) fields[i].decimals, | |
150 (long) !(IS_NOT_NULL(fields[i].flags))); | |
151 if (!t) goto error; | |
152 PyTuple_SET_ITEM(d, i, t); | |
153 } | |
154 return d; | |
155 error: | |
156 Py_XDECREF(d); | |
157 return NULL; | |
158 } | |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
159 |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
160 static char _mysql_ResultObject_fields__doc__[] = |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
161 "Returns the sequence of 7-tuples required by the DB-API for\n\ |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
162 the Cursor.description attribute.\n\ |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
163 "; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
164 |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
165 static PyObject * |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
166 _mysql_ResultObject_fields( |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
167 _mysql_ResultObject *self, |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
168 PyObject *args) |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
169 { |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
170 PyObject *arglist=NULL, *kwarglist=NULL; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
171 PyObject *fields=NULL; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
172 _mysql_FieldObject *field=NULL; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
173 unsigned int i, n; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
174 if (!PyArg_ParseTuple(args, "")) return NULL; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
175 check_result_connection(self); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
176 kwarglist = PyDict_New(); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
177 if (!kwarglist) goto error; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
178 n = mysql_num_fields(self->result); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
179 if (!(fields = PyTuple_New(n))) return NULL; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
180 for (i=0; i<n; i++) { |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
181 arglist = Py_BuildValue("(Oi)", self, i); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
182 if (!arglist) goto error; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
183 field = MyAlloc(_mysql_FieldObject, _mysql_FieldObject_Type); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
184 if (!field) goto error; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
185 if (_mysql_FieldObject_Initialize(field, arglist, kwarglist)) |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
186 goto error; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
187 Py_DECREF(arglist); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
188 PyTuple_SET_ITEM(fields, i, (PyObject *) field); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
189 } |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
190 Py_DECREF(kwarglist); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
191 return fields; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
192 error: |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
193 Py_XDECREF(arglist); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
194 Py_XDECREF(kwarglist); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
195 Py_XDECREF(fields); |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
196 return NULL; |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
197 } |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
198 |
0 | 199 static char _mysql_ResultObject_field_flags__doc__[] = |
200 "Returns a tuple of field flags, one for each column in the result.\n\ | |
201 " ; | |
202 | |
203 static PyObject * | |
204 _mysql_ResultObject_field_flags( | |
205 _mysql_ResultObject *self, | |
206 PyObject *args) | |
207 { | |
208 PyObject *d; | |
209 MYSQL_FIELD *fields; | |
210 unsigned int i, n; | |
211 if (!PyArg_ParseTuple(args, "")) return NULL; | |
212 check_result_connection(self); | |
213 n = mysql_num_fields(self->result); | |
214 fields = mysql_fetch_fields(self->result); | |
215 if (!(d = PyTuple_New(n))) return NULL; | |
216 for (i=0; i<n; i++) { | |
217 PyObject *f; | |
218 if (!(f = PyInt_FromLong((long)fields[i].flags))) goto error; | |
219 PyTuple_SET_ITEM(d, i, f); | |
220 } | |
221 return d; | |
222 error: | |
223 Py_XDECREF(d); | |
224 return NULL; | |
225 } | |
226 | |
227 static PyObject * | |
228 _mysql_field_to_python( | |
229 PyObject *converter, | |
230 char *rowitem, | |
231 unsigned long length) | |
232 { | |
233 PyObject *v; | |
234 if (rowitem) { | |
235 if (converter != Py_None) | |
236 v = PyObject_CallFunction(converter, | |
237 "s#", | |
238 rowitem, | |
239 (int)length); | |
240 else | |
241 v = PyString_FromStringAndSize(rowitem, | |
242 (int)length); | |
243 if (!v) | |
244 return NULL; | |
245 } else { | |
246 Py_INCREF(Py_None); | |
247 v = Py_None; | |
248 } | |
249 return v; | |
250 } | |
251 | |
252 static PyObject * | |
253 _mysql_row_to_tuple( | |
254 _mysql_ResultObject *self, | |
255 MYSQL_ROW row) | |
256 { | |
257 unsigned int n, i; | |
258 unsigned long *length; | |
259 PyObject *r, *c; | |
260 | |
261 n = mysql_num_fields(self->result); | |
262 if (!(r = PyTuple_New(n))) return NULL; | |
263 length = mysql_fetch_lengths(self->result); | |
264 for (i=0; i<n; i++) { | |
265 PyObject *v; | |
266 c = PyTuple_GET_ITEM(self->converter, i); | |
267 v = _mysql_field_to_python(c, row[i], length[i]); | |
268 if (!v) goto error; | |
269 PyTuple_SET_ITEM(r, i, v); | |
270 } | |
271 return r; | |
272 error: | |
273 Py_XDECREF(r); | |
274 return NULL; | |
275 } | |
276 | |
277 static PyObject * | |
278 _mysql_row_to_dict( | |
279 _mysql_ResultObject *self, | |
280 MYSQL_ROW row) | |
281 { | |
282 unsigned int n, i; | |
283 unsigned long *length; | |
284 PyObject *r, *c; | |
285 MYSQL_FIELD *fields; | |
286 | |
287 n = mysql_num_fields(self->result); | |
288 if (!(r = PyDict_New())) return NULL; | |
289 length = mysql_fetch_lengths(self->result); | |
290 fields = mysql_fetch_fields(self->result); | |
291 for (i=0; i<n; i++) { | |
292 PyObject *v; | |
293 c = PyTuple_GET_ITEM(self->converter, i); | |
294 v = _mysql_field_to_python(c, row[i], length[i]); | |
295 if (!v) goto error; | |
296 if (!PyMapping_HasKeyString(r, fields[i].name)) { | |
297 PyMapping_SetItemString(r, fields[i].name, v); | |
298 } else { | |
299 int len; | |
300 char buf[256]; | |
301 strncpy(buf, fields[i].table, 256); | |
302 len = strlen(buf); | |
303 strncat(buf, ".", 256-len); | |
304 len = strlen(buf); | |
305 strncat(buf, fields[i].name, 256-len); | |
306 PyMapping_SetItemString(r, buf, v); | |
307 } | |
308 Py_DECREF(v); | |
309 } | |
310 return r; | |
311 error: | |
312 Py_XDECREF(r); | |
313 return NULL; | |
314 } | |
315 | |
316 static PyObject * | |
317 _mysql_row_to_dict_old( | |
318 _mysql_ResultObject *self, | |
319 MYSQL_ROW row) | |
320 { | |
321 unsigned int n, i; | |
322 unsigned long *length; | |
323 PyObject *r, *c; | |
324 MYSQL_FIELD *fields; | |
325 | |
326 n = mysql_num_fields(self->result); | |
327 if (!(r = PyDict_New())) return NULL; | |
328 length = mysql_fetch_lengths(self->result); | |
329 fields = mysql_fetch_fields(self->result); | |
330 for (i=0; i<n; i++) { | |
331 PyObject *v; | |
332 c = PyTuple_GET_ITEM(self->converter, i); | |
333 v = _mysql_field_to_python(c, row[i], length[i]); | |
334 if (!v) goto error; | |
335 { | |
336 int len=0; | |
337 char buf[256]=""; | |
338 if (strlen(fields[i].table)) { | |
339 strncpy(buf, fields[i].table, 256); | |
340 len = strlen(buf); | |
341 strncat(buf, ".", 256-len); | |
342 len = strlen(buf); | |
343 } | |
344 strncat(buf, fields[i].name, 256-len); | |
345 PyMapping_SetItemString(r, buf, v); | |
346 } | |
347 Py_DECREF(v); | |
348 } | |
349 return r; | |
350 error: | |
351 Py_XDECREF(r); | |
352 return NULL; | |
353 } | |
354 | |
355 typedef PyObject *_PYFUNC(_mysql_ResultObject *, MYSQL_ROW); | |
356 | |
357 int | |
358 _mysql__fetch_row( | |
359 _mysql_ResultObject *self, | |
360 PyObject **r, | |
361 int skiprows, | |
362 int maxrows, | |
363 _PYFUNC *convert_row) | |
364 { | |
365 unsigned int i; | |
366 MYSQL_ROW row; | |
367 | |
368 for (i = skiprows; i<(skiprows+maxrows); i++) { | |
369 PyObject *v; | |
370 if (!self->use) | |
371 row = mysql_fetch_row(self->result); | |
372 else { | |
373 Py_BEGIN_ALLOW_THREADS; | |
374 row = mysql_fetch_row(self->result); | |
375 Py_END_ALLOW_THREADS; | |
376 } | |
377 if (!row && mysql_errno(&(((_mysql_ConnectionObject *)(self->conn))->connection))) { | |
378 _mysql_Exception((_mysql_ConnectionObject *)self->conn); | |
379 goto error; | |
380 } | |
381 if (!row) { | |
382 if (MyTuple_Resize(r, i, 0) == -1) goto error; | |
383 break; | |
384 } | |
385 v = convert_row(self, row); | |
386 if (!v) goto error; | |
387 PyTuple_SET_ITEM(*r, i, v); | |
388 } | |
389 return i-skiprows; | |
390 error: | |
391 return -1; | |
392 } | |
393 | |
394 static char _mysql_ResultObject_fetch_row__doc__[] = | |
395 "fetch_row([maxrows, how]) -- Fetches up to maxrows as a tuple.\n\ | |
396 The rows are formatted according to how:\n\ | |
397 \n\ | |
398 0 -- tuples (default)\n\ | |
399 1 -- dictionaries, key=column or table.column if duplicated\n\ | |
400 2 -- dictionaries, key=table.column\n\ | |
401 "; | |
402 | |
403 static PyObject * | |
404 _mysql_ResultObject_fetch_row( | |
405 _mysql_ResultObject *self, | |
406 PyObject *args, | |
407 PyObject *kwargs) | |
408 { | |
409 typedef PyObject *_PYFUNC(_mysql_ResultObject *, MYSQL_ROW); | |
410 static char *kwlist[] = { "maxrows", "how", NULL }; | |
411 static _PYFUNC *row_converters[] = | |
412 { | |
413 _mysql_row_to_tuple, | |
414 _mysql_row_to_dict, | |
415 _mysql_row_to_dict_old | |
416 }; | |
417 _PYFUNC *convert_row; | |
418 unsigned int maxrows=1, how=0, skiprows=0, rowsadded; | |
419 PyObject *r=NULL; | |
420 | |
421 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii:fetch_row", kwlist, | |
422 &maxrows, &how)) | |
423 return NULL; | |
424 check_result_connection(self); | |
425 if (how < 0 || how >= sizeof(row_converters)) { | |
426 PyErr_SetString(PyExc_ValueError, "how out of range"); | |
427 return NULL; | |
428 } | |
429 convert_row = row_converters[how]; | |
430 if (maxrows) { | |
431 if (!(r = PyTuple_New(maxrows))) goto error; | |
432 rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows, | |
433 convert_row); | |
434 if (rowsadded == -1) goto error; | |
435 } else { | |
436 if (self->use) { | |
437 maxrows = 1000; | |
438 if (!(r = PyTuple_New(maxrows))) goto error; | |
439 while (1) { | |
440 rowsadded = _mysql__fetch_row(self, &r, skiprows, | |
441 maxrows, convert_row); | |
442 if (rowsadded == -1) goto error; | |
443 skiprows += rowsadded; | |
444 if (rowsadded < maxrows) break; | |
445 if (MyTuple_Resize(&r, skiprows+maxrows, 0) == -1) | |
446 goto error; | |
447 } | |
448 } else { | |
449 /* XXX if overflow, maxrows<0? */ | |
450 maxrows = (int) mysql_num_rows(self->result); | |
451 if (!(r = PyTuple_New(maxrows))) goto error; | |
452 rowsadded = _mysql__fetch_row(self, &r, 0, | |
453 maxrows, convert_row); | |
454 if (rowsadded == -1) goto error; | |
455 } | |
456 } | |
457 return r; | |
458 error: | |
459 Py_XDECREF(r); | |
460 return NULL; | |
461 } | |
462 | |
463 | |
464 static char _mysql_ResultObject_num_fields__doc__[] = | |
465 "Returns the number of fields (column) in the result." ; | |
466 | |
467 static PyObject * | |
468 _mysql_ResultObject_num_fields( | |
469 _mysql_ResultObject *self, | |
470 PyObject *args) | |
471 { | |
472 if (!PyArg_ParseTuple(args, "")) return NULL; | |
473 check_result_connection(self); | |
474 return PyInt_FromLong((long)mysql_num_fields(self->result)); | |
475 } | |
476 | |
477 static char _mysql_ResultObject_num_rows__doc__[] = | |
478 "Returns the number of rows in the result set. Note that if\n\ | |
479 use=1, this will not return a valid value until the entire result\n\ | |
480 set has been read.\n\ | |
481 "; | |
482 | |
483 static PyObject * | |
484 _mysql_ResultObject_num_rows( | |
485 _mysql_ResultObject *self, | |
486 PyObject *args) | |
487 { | |
488 if (!PyArg_ParseTuple(args, "")) return NULL; | |
489 check_result_connection(self); | |
490 return PyLong_FromUnsignedLongLong(mysql_num_rows(self->result)); | |
491 } | |
492 | |
493 | |
494 static char _mysql_ResultObject_data_seek__doc__[] = | |
495 "data_seek(n) -- seek to row n of result set"; | |
496 static PyObject * | |
497 _mysql_ResultObject_data_seek( | |
498 _mysql_ResultObject *self, | |
499 PyObject *args) | |
500 { | |
501 unsigned int row; | |
502 if (!PyArg_ParseTuple(args, "i:data_seek", &row)) return NULL; | |
503 check_result_connection(self); | |
504 mysql_data_seek(self->result, row); | |
505 Py_INCREF(Py_None); | |
506 return Py_None; | |
507 } | |
508 | |
509 static char _mysql_ResultObject_row_seek__doc__[] = | |
510 "row_seek(n) -- seek by offset n rows of result set"; | |
511 static PyObject * | |
512 _mysql_ResultObject_row_seek( | |
513 _mysql_ResultObject *self, | |
514 PyObject *args) | |
515 { | |
516 int offset; | |
517 MYSQL_ROW_OFFSET r; | |
518 if (!PyArg_ParseTuple(args, "i:row_seek", &offset)) return NULL; | |
519 check_result_connection(self); | |
520 if (self->use) { | |
521 PyErr_SetString(_mysql_ProgrammingError, | |
522 "cannot be used with connection.use_result()"); | |
523 return NULL; | |
524 } | |
525 r = mysql_row_tell(self->result); | |
526 mysql_row_seek(self->result, r+offset); | |
527 Py_INCREF(Py_None); | |
528 return Py_None; | |
529 } | |
530 | |
531 static char _mysql_ResultObject_row_tell__doc__[] = | |
532 "row_tell() -- return the current row number of the result set."; | |
533 static PyObject * | |
534 _mysql_ResultObject_row_tell( | |
535 _mysql_ResultObject *self, | |
536 PyObject *args) | |
537 { | |
538 MYSQL_ROW_OFFSET r; | |
539 if (!PyArg_ParseTuple(args, "")) return NULL; | |
540 check_result_connection(self); | |
541 if (self->use) { | |
542 PyErr_SetString(_mysql_ProgrammingError, | |
543 "cannot be used with connection.use_result()"); | |
544 return NULL; | |
545 } | |
546 r = mysql_row_tell(self->result); | |
547 return PyInt_FromLong(r-self->result->data->data); | |
548 } | |
549 | |
550 static void | |
551 _mysql_ResultObject_dealloc( | |
552 _mysql_ResultObject *self) | |
553 { | |
554 PyObject_GC_UnTrack((PyObject *)self); | |
555 mysql_free_result(self->result); | |
556 _mysql_ResultObject_clear(self); | |
557 MyFree(self); | |
558 } | |
559 | |
560 static PyObject * | |
561 _mysql_ResultObject_repr( | |
562 _mysql_ResultObject *self) | |
563 { | |
564 char buf[300]; | |
565 sprintf(buf, "<_mysql.result object at %lx>", | |
566 (long)self); | |
567 return PyString_FromString(buf); | |
568 } | |
569 | |
570 static PyMethodDef _mysql_ResultObject_methods[] = { | |
571 { | |
572 "data_seek", | |
573 (PyCFunction)_mysql_ResultObject_data_seek, | |
574 METH_VARARGS, | |
575 _mysql_ResultObject_data_seek__doc__ | |
576 }, | |
577 { | |
578 "row_seek", | |
579 (PyCFunction)_mysql_ResultObject_row_seek, | |
580 METH_VARARGS, | |
581 _mysql_ResultObject_row_seek__doc__ | |
582 }, | |
583 { | |
584 "row_tell", | |
585 (PyCFunction)_mysql_ResultObject_row_tell, | |
586 METH_VARARGS, | |
587 _mysql_ResultObject_row_tell__doc__ | |
588 }, | |
589 { | |
590 "describe", | |
591 (PyCFunction)_mysql_ResultObject_describe, | |
592 METH_VARARGS, | |
593 _mysql_ResultObject_describe__doc__ | |
594 }, | |
595 { | |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
596 "fields", |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
597 (PyCFunction)_mysql_ResultObject_fields, |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
598 METH_VARARGS, |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
599 _mysql_ResultObject_fields__doc__ |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
600 }, |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
2
diff
changeset
|
601 { |
0 | 602 "fetch_row", |
603 (PyCFunction)_mysql_ResultObject_fetch_row, | |
604 METH_VARARGS | METH_KEYWORDS, | |
605 _mysql_ResultObject_fetch_row__doc__ | |
606 }, | |
607 { | |
608 "field_flags", | |
609 (PyCFunction)_mysql_ResultObject_field_flags, | |
610 METH_VARARGS, | |
611 _mysql_ResultObject_field_flags__doc__ | |
612 }, | |
613 { | |
614 "num_fields", | |
615 (PyCFunction)_mysql_ResultObject_num_fields, | |
616 METH_VARARGS, | |
617 _mysql_ResultObject_num_fields__doc__ | |
618 }, | |
619 { | |
620 "num_rows", | |
621 (PyCFunction)_mysql_ResultObject_num_rows, | |
622 METH_VARARGS, | |
623 _mysql_ResultObject_num_rows__doc__ | |
624 }, | |
625 {NULL, NULL} /* sentinel */ | |
626 }; | |
627 | |
628 static MyMemberlist(_mysql_ResultObject_memberlist)[] = { | |
629 MyMember( | |
630 "converter", | |
631 T_OBJECT, | |
632 offsetof(_mysql_ResultObject,converter), | |
633 RO, | |
634 "Type conversion mapping" | |
635 ), | |
636 {NULL} /* Sentinel */ | |
637 }; | |
638 | |
639 static PyObject * | |
640 _mysql_ResultObject_getattr( | |
641 _mysql_ResultObject *self, | |
642 char *name) | |
643 { | |
644 PyObject *res; | |
645 | |
646 res = Py_FindMethod(_mysql_ResultObject_methods, (PyObject *)self, name); | |
647 if (res != NULL) | |
648 return res; | |
649 PyErr_Clear(); | |
650 #if PY_VERSION_HEX < 0x02020000 | |
651 return PyMember_Get((char *)self, _mysql_ResultObject_memberlist, name); | |
652 #else | |
653 { | |
654 MyMemberlist(*l); | |
655 for (l = _mysql_ResultObject_memberlist; l->name != NULL; l++) { | |
656 if (strcmp(l->name, name) == 0) | |
657 return PyMember_GetOne((char *)self, l); | |
658 } | |
659 PyErr_SetString(PyExc_AttributeError, name); | |
660 return NULL; | |
661 } | |
662 #endif | |
663 } | |
664 | |
665 static int | |
666 _mysql_ResultObject_setattr( | |
667 _mysql_ResultObject *self, | |
668 char *name, | |
669 PyObject *v) | |
670 { | |
671 if (v == NULL) { | |
672 PyErr_SetString(PyExc_AttributeError, | |
673 "can't delete connection attributes"); | |
674 return -1; | |
675 } | |
676 #if PY_VERSION_HEX < 0x02020000 | |
677 return PyMember_Set((char *)self, _mysql_ResultObject_memberlist, name, v); | |
678 #else | |
679 { | |
680 MyMemberlist(*l); | |
681 for (l = _mysql_ResultObject_memberlist; l->name != NULL; l++) | |
682 if (strcmp(l->name, name) == 0) | |
683 return PyMember_SetOne((char *)self, l, v); | |
684 } | |
685 PyErr_SetString(PyExc_AttributeError, name); | |
686 return -1; | |
687 #endif | |
688 } | |
689 | |
690 PyTypeObject _mysql_ResultObject_Type = { | |
691 PyObject_HEAD_INIT(NULL) | |
692 0, | |
693 "_mysql.result", | |
694 sizeof(_mysql_ResultObject), | |
695 0, | |
696 (destructor)_mysql_ResultObject_dealloc, /* tp_dealloc */ | |
697 0, /*tp_print*/ | |
698 (getattrfunc)_mysql_ResultObject_getattr, /* tp_getattr */ | |
699 (setattrfunc)_mysql_ResultObject_setattr, /* tp_setattr */ | |
700 0, /*tp_compare*/ | |
701 (reprfunc)_mysql_ResultObject_repr, /* tp_repr */ | |
702 | |
703 /* Method suites for standard classes */ | |
704 | |
705 0, /* (PyNumberMethods *) tp_as_number */ | |
706 0, /* (PySequenceMethods *) tp_as_sequence */ | |
707 0, /* (PyMappingMethods *) tp_as_mapping */ | |
708 | |
709 /* More standard operations (here for binary compatibility) */ | |
710 | |
711 0, /* (hashfunc) tp_hash */ | |
712 0, /* (ternaryfunc) tp_call */ | |
713 0, /* (reprfunc) tp_str */ | |
714 0, /* (getattrofunc) tp_getattro */ | |
715 0, /* (setattrofunc) tp_setattro */ | |
716 | |
717 /* Functions to access object as input/output buffer */ | |
718 0, /* (PyBufferProcs *) tp_as_buffer */ | |
719 | |
720 /* Flags to define presence of optional/expanded features */ | |
721 #if PY_VERSION_HEX < 0x02020000 | |
722 Py_TPFLAGS_DEFAULT, /* (long) tp_flags */ | |
723 #else | |
724 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, | |
725 #endif | |
726 | |
727 _mysql_ResultObject__doc__, /* (char *) tp_doc Documentation string */ | |
728 #if PY_VERSION_HEX >= 0x02000000 | |
729 /* Assigned meaning in release 2.0 */ | |
730 #if PY_VERSION_HEX >= 0x02020000 | |
731 /* call function for all accessible objects */ | |
732 (traverseproc) _mysql_ResultObject_traverse, /* tp_traverse */ | |
733 | |
734 /* delete references to contained objects */ | |
735 (inquiry) _mysql_ResultObject_clear, /* tp_clear */ | |
736 #else | |
737 /* not supporting pre-2.2 GC */ | |
738 0, | |
739 0, | |
740 #endif | |
741 #if PY_VERSION_HEX >= 0x02010000 | |
742 /* Assigned meaning in release 2.1 */ | |
743 /* rich comparisons */ | |
744 0, /* (richcmpfunc) tp_richcompare */ | |
745 | |
746 /* weak reference enabler */ | |
747 0, /* (long) tp_weaklistoffset */ | |
748 #if PY_VERSION_HEX >= 0x02020000 | |
749 /* Added in release 2.2 */ | |
750 /* Iterators */ | |
751 0, /* (getiterfunc) tp_iter */ | |
752 0, /* (iternextfunc) tp_iternext */ | |
753 | |
754 /* Attribute descriptor and subclassing stuff */ | |
755 (struct PyMethodDef *) _mysql_ResultObject_methods, /* tp_methods */ | |
756 (MyMemberlist(*)) _mysql_ResultObject_memberlist, /*tp_members */ | |
757 0, /* (struct getsetlist *) tp_getset; */ | |
758 0, /* (struct _typeobject *) tp_base; */ | |
759 0, /* (PyObject *) tp_dict */ | |
760 0, /* (descrgetfunc) tp_descr_get */ | |
761 0, /* (descrsetfunc) tp_descr_set */ | |
762 0, /* (long) tp_dictoffset */ | |
763 (initproc)_mysql_ResultObject_Initialize, /* tp_init */ | |
764 NULL, /* tp_alloc */ | |
765 NULL, /* tp_new */ | |
766 NULL, /* tp_free Low-level free-memory routine */ | |
767 0, /* (PyObject *) tp_bases */ | |
768 0, /* (PyObject *) tp_mro method resolution order */ | |
769 0, /* (PyObject *) tp_defined */ | |
770 #endif /* python 2.2 */ | |
771 #endif /* python 2.1 */ | |
772 #endif /* python 2.0 */ | |
773 }; |