blob: c468754e75fe2945c2a166d706f3a57ebea6b4ef [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* cursor.c - the cursor type
2 *
3 * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
4 *
5 * This file is part of pysqlite.
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "cursor.h"
25#include "module.h"
26#include "util.h"
27#include "sqlitecompat.h"
28
29/* used to decide wether to call PyInt_FromLong or PyLong_FromLongLong */
Thomas Wouters477c8d52006-05-27 19:21:47 +000030#ifndef INT32_MIN
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000031#define INT32_MIN (-2147483647 - 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +000032#endif
33#ifndef INT32_MAX
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#define INT32_MAX 2147483647
Thomas Wouters477c8d52006-05-27 19:21:47 +000035#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000037PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038
Guido van Rossum6374bb52007-06-13 16:28:25 +000039static pysqlite_StatementKind detect_statement_type(const char* statement)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040{
41 char buf[20];
Guido van Rossum6374bb52007-06-13 16:28:25 +000042 const char* src;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043 char* dst;
44
45 src = statement;
46 /* skip over whitepace */
47 while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') {
48 src++;
49 }
50
51 if (*src == 0)
52 return STATEMENT_INVALID;
53
54 dst = buf;
55 *dst = 0;
56 while (isalpha(*src) && dst - buf < sizeof(buf) - 2) {
57 *dst++ = tolower(*src++);
58 }
59
60 *dst = 0;
61
62 if (!strcmp(buf, "select")) {
63 return STATEMENT_SELECT;
64 } else if (!strcmp(buf, "insert")) {
65 return STATEMENT_INSERT;
66 } else if (!strcmp(buf, "update")) {
67 return STATEMENT_UPDATE;
68 } else if (!strcmp(buf, "delete")) {
69 return STATEMENT_DELETE;
70 } else if (!strcmp(buf, "replace")) {
71 return STATEMENT_REPLACE;
72 } else {
73 return STATEMENT_OTHER;
74 }
75}
76
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000077int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000079 pysqlite_Connection* connection;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000081 if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 {
83 return -1;
84 }
85
86 Py_INCREF(connection);
87 self->connection = connection;
88 self->statement = NULL;
89 self->next_row = NULL;
90
91 self->row_cast_map = PyList_New(0);
92 if (!self->row_cast_map) {
93 return -1;
94 }
95
96 Py_INCREF(Py_None);
97 self->description = Py_None;
98
99 Py_INCREF(Py_None);
100 self->lastrowid= Py_None;
101
102 self->arraysize = 1;
103
104 self->rowcount = PyInt_FromLong(-1L);
105 if (!self->rowcount) {
106 return -1;
107 }
108
109 Py_INCREF(Py_None);
110 self->row_factory = Py_None;
111
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000112 if (!pysqlite_check_thread(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 return -1;
114 }
115
116 return 0;
117}
118
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000119void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120{
121 int rc;
122
123 /* Reset the statement if the user has not closed the cursor */
124 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000125 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 Py_DECREF(self->statement);
127 }
128
129 Py_XDECREF(self->connection);
130 Py_XDECREF(self->row_cast_map);
131 Py_XDECREF(self->description);
132 Py_XDECREF(self->lastrowid);
133 Py_XDECREF(self->rowcount);
134 Py_XDECREF(self->row_factory);
135 Py_XDECREF(self->next_row);
136
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000137 Py_Type(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138}
139
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000140PyObject* _pysqlite_get_converter(PyObject* key)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000141{
142 PyObject* upcase_key;
143 PyObject* retval;
144
145 upcase_key = PyObject_CallMethod(key, "upper", "");
146 if (!upcase_key) {
147 return NULL;
148 }
149
150 retval = PyDict_GetItem(converters, upcase_key);
151 Py_DECREF(upcase_key);
152
153 return retval;
154}
155
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000156int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000157{
158 int i;
159 const char* type_start = (const char*)-1;
160 const char* pos;
161
162 const char* colname;
163 const char* decltype;
164 PyObject* py_decltype;
165 PyObject* converter;
166 PyObject* key;
167
168 if (!self->connection->detect_types) {
169 return 0;
170 }
171
172 Py_XDECREF(self->row_cast_map);
173 self->row_cast_map = PyList_New(0);
174
175 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
176 converter = NULL;
177
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000178 if (self->connection->detect_types & PARSE_COLNAMES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000180 if (colname) {
181 for (pos = colname; *pos != 0; pos++) {
182 if (*pos == '[') {
183 type_start = pos + 1;
184 } else if (*pos == ']' && type_start != (const char*)-1) {
185 key = PyString_FromStringAndSize(type_start, pos - type_start);
186 if (!key) {
187 /* creating a string failed, but it is too complicated
188 * to propagate the error here, we just assume there is
189 * no converter and proceed */
190 break;
191 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000192
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000193 converter = _pysqlite_get_converter(key);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000194 Py_DECREF(key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195 break;
196 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 }
199 }
200
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000201 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202 decltype = sqlite3_column_decltype(self->statement->st, i);
203 if (decltype) {
204 for (pos = decltype;;pos++) {
205 if (*pos == ' ' || *pos == 0) {
206 py_decltype = PyString_FromStringAndSize(decltype, pos - decltype);
207 if (!py_decltype) {
208 return -1;
209 }
210 break;
211 }
212 }
213
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000214 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215 Py_DECREF(py_decltype);
216 }
217 }
218
219 if (!converter) {
220 converter = Py_None;
221 }
222
223 if (PyList_Append(self->row_cast_map, converter) != 0) {
224 if (converter != Py_None) {
225 Py_DECREF(converter);
226 }
227 Py_XDECREF(self->row_cast_map);
228 self->row_cast_map = NULL;
229
230 return -1;
231 }
232 }
233
234 return 0;
235}
236
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000237PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000238{
239 const char* pos;
240
241 if (!colname) {
242 Py_INCREF(Py_None);
243 return Py_None;
244 }
245
246 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000247 if (*pos == 0 || *pos == '[') {
248 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
249 pos--;
250 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000251 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252 }
253 }
254}
255
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000256PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257{
258 const char* check;
259 int is_ascii = 0;
260
261 if (optimize) {
262 is_ascii = 1;
263
264 check = val_str;
265 while (*check) {
266 if (*check & 0x80) {
267 is_ascii = 0;
268 break;
269 }
270
271 check++;
272 }
273 }
274
275 if (is_ascii) {
276 return PyString_FromString(val_str);
277 } else {
278 return PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
279 }
280}
281
282/*
283 * Returns a row from the currently active SQLite statement
284 *
285 * Precondidition:
286 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
287 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000288PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000289{
290 int i, numcols;
291 PyObject* row;
292 PyObject* item = NULL;
293 int coltype;
294 PY_LONG_LONG intval;
295 PyObject* converter;
296 PyObject* converted;
297 Py_ssize_t nbytes;
298 PyObject* buffer;
299 void* raw_buffer;
300 const char* val_str;
301 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000302 const char* colname;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303
304 Py_BEGIN_ALLOW_THREADS
305 numcols = sqlite3_data_count(self->statement->st);
306 Py_END_ALLOW_THREADS
307
308 row = PyTuple_New(numcols);
309 if (!row) {
310 return NULL;
311 }
312
313 for (i = 0; i < numcols; i++) {
314 if (self->connection->detect_types) {
315 converter = PyList_GetItem(self->row_cast_map, i);
316 if (!converter) {
317 converter = Py_None;
318 }
319 } else {
320 converter = Py_None;
321 }
322
323 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000324 nbytes = sqlite3_column_bytes(self->statement->st, i);
325 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326 if (!val_str) {
327 Py_INCREF(Py_None);
328 converted = Py_None;
329 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000330 item = PyString_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331 if (!item) {
332 return NULL;
333 }
334 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335 Py_DECREF(item);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000336 if (!converted) {
337 break;
338 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 }
340 } else {
341 Py_BEGIN_ALLOW_THREADS
342 coltype = sqlite3_column_type(self->statement->st, i);
343 Py_END_ALLOW_THREADS
344 if (coltype == SQLITE_NULL) {
345 Py_INCREF(Py_None);
346 converted = Py_None;
347 } else if (coltype == SQLITE_INTEGER) {
348 intval = sqlite3_column_int64(self->statement->st, i);
349 if (intval < INT32_MIN || intval > INT32_MAX) {
350 converted = PyLong_FromLongLong(intval);
351 } else {
352 converted = PyInt_FromLong((long)intval);
353 }
354 } else if (coltype == SQLITE_FLOAT) {
355 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
356 } else if (coltype == SQLITE_TEXT) {
357 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
358 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000359 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000361 converted = pysqlite_unicode_from_string(val_str,
362 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363
364 if (!converted) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000365 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000366 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000367 colname = "<unknown column name>";
368 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000369 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000370 colname , val_str);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000371 PyErr_SetString(pysqlite_OperationalError, buf);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372 }
373 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
374 converted = PyString_FromString(val_str);
Gerhard Häring6d214562007-08-10 18:15:11 +0000375 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
376 converted = PyBytes_FromStringAndSize(val_str, strlen(val_str));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000378 converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379 }
380 } else {
381 /* coltype == SQLITE_BLOB */
382 nbytes = sqlite3_column_bytes(self->statement->st, i);
383 buffer = PyBuffer_New(nbytes);
384 if (!buffer) {
385 break;
386 }
387 if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &nbytes)) {
388 break;
389 }
390 memcpy(raw_buffer, sqlite3_column_blob(self->statement->st, i), nbytes);
391 converted = buffer;
392 }
393 }
394
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000395 if (converted) {
396 PyTuple_SetItem(row, i, converted);
397 } else {
398 Py_INCREF(Py_None);
399 PyTuple_SetItem(row, i, Py_None);
400 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000401 }
402
403 if (PyErr_Occurred()) {
404 Py_DECREF(row);
405 row = NULL;
406 }
407
408 return row;
409}
410
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000411PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412{
413 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000414 const char* operation_cstr;
415 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000416 PyObject* parameters_list = NULL;
417 PyObject* parameters_iter = NULL;
418 PyObject* parameters = NULL;
419 int i;
420 int rc;
421 PyObject* func_args;
422 PyObject* result;
423 int numcols;
424 PY_LONG_LONG lastrowid;
425 int statement_type;
426 PyObject* descriptor;
427 PyObject* second_argument = NULL;
428 long rowcount = 0;
429
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000430 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431 return NULL;
432 }
433
434 Py_XDECREF(self->next_row);
435 self->next_row = NULL;
436
437 if (multiple) {
438 /* executemany() */
439 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
440 return NULL;
441 }
442
443 if (!PyString_Check(operation) && !PyUnicode_Check(operation)) {
444 PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode");
445 return NULL;
446 }
447
448 if (PyIter_Check(second_argument)) {
449 /* iterator */
450 Py_INCREF(second_argument);
451 parameters_iter = second_argument;
452 } else {
453 /* sequence */
454 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000455 if (!parameters_iter) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000456 return NULL;
457 }
458 }
459 } else {
460 /* execute() */
461 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
462 return NULL;
463 }
464
465 if (!PyString_Check(operation) && !PyUnicode_Check(operation)) {
466 PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode");
467 return NULL;
468 }
469
470 parameters_list = PyList_New(0);
471 if (!parameters_list) {
472 return NULL;
473 }
474
475 if (second_argument == NULL) {
476 second_argument = PyTuple_New(0);
477 if (!second_argument) {
478 goto error;
479 }
480 } else {
481 Py_INCREF(second_argument);
482 }
483 if (PyList_Append(parameters_list, second_argument) != 0) {
484 Py_DECREF(second_argument);
485 goto error;
486 }
487 Py_DECREF(second_argument);
488
489 parameters_iter = PyObject_GetIter(parameters_list);
490 if (!parameters_iter) {
491 goto error;
492 }
493 }
494
495 if (self->statement != NULL) {
496 /* There is an active statement */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000497 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000498 }
499
Guido van Rossumec9a4af2007-08-29 14:26:02 +0000500 operation_cstr = PyUnicode_AsStringAndSize(operation, &operation_len);
Guido van Rossumfa9a1212007-08-29 03:34:29 +0000501 if (operation == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000502 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503
504 /* reset description and rowcount */
505 Py_DECREF(self->description);
506 Py_INCREF(Py_None);
507 self->description = Py_None;
508
509 Py_DECREF(self->rowcount);
510 self->rowcount = PyInt_FromLong(-1L);
511 if (!self->rowcount) {
512 goto error;
513 }
514
515 statement_type = detect_statement_type(operation_cstr);
516 if (self->connection->begin_statement) {
517 switch (statement_type) {
518 case STATEMENT_UPDATE:
519 case STATEMENT_DELETE:
520 case STATEMENT_INSERT:
521 case STATEMENT_REPLACE:
522 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000523 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000524 if (!result) {
525 goto error;
526 }
527 Py_DECREF(result);
528 }
529 break;
530 case STATEMENT_OTHER:
531 /* it's a DDL statement or something similar
532 - we better COMMIT first so it works for all cases */
533 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000534 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535 if (!result) {
536 goto error;
537 }
538 Py_DECREF(result);
539 }
540 break;
541 case STATEMENT_SELECT:
542 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000543 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544 "You cannot execute SELECT statements in executemany().");
545 goto error;
546 }
547 break;
548 }
549 }
550
551 func_args = PyTuple_New(1);
552 if (!func_args) {
553 goto error;
554 }
555 Py_INCREF(operation);
556 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
557 goto error;
558 }
559
560 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000561 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562 Py_DECREF(self->statement);
563 }
564
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000565 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000566 Py_DECREF(func_args);
567
568 if (!self->statement) {
569 goto error;
570 }
571
572 if (self->statement->in_use) {
573 Py_DECREF(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000574 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000575 if (!self->statement) {
576 goto error;
577 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000578 rc = pysqlite_statement_create(self->statement, self->connection, operation);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 if (rc != SQLITE_OK) {
580 self->statement = 0;
581 goto error;
582 }
583 }
584
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000585 pysqlite_statement_reset(self->statement);
586 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587
588 while (1) {
589 parameters = PyIter_Next(parameters_iter);
590 if (!parameters) {
591 break;
592 }
593
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000594 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000596 pysqlite_statement_bind_parameters(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597 if (PyErr_Occurred()) {
598 goto error;
599 }
600
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000601 if (pysqlite_build_row_cast_map(self) != 0) {
602 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000603 goto error;
604 }
605
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000606 /* Keep trying the SQL statement until the schema stops changing. */
607 while (1) {
608 /* Actually execute the SQL statement. */
609 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
610 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
611 /* If it worked, let's get out of the loop */
612 break;
613 }
614 /* Something went wrong. Re-set the statement and try again. */
615 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000617 /* If this was a result of the schema changing, let's try
618 again. */
619 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000621 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000623 /* If the database gave us an error, promote it to Python. */
624 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000625 goto error;
626 }
627 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000628 if (PyErr_Occurred()) {
629 /* there was an error that occurred in a user-defined callback */
630 if (_enable_callback_tracebacks) {
631 PyErr_Print();
632 } else {
633 PyErr_Clear();
634 }
635 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000636 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637 goto error;
638 }
639 }
640
641 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
642 Py_BEGIN_ALLOW_THREADS
643 numcols = sqlite3_column_count(self->statement->st);
644 Py_END_ALLOW_THREADS
645
646 if (self->description == Py_None) {
647 Py_DECREF(self->description);
648 self->description = PyTuple_New(numcols);
649 if (!self->description) {
650 goto error;
651 }
652 for (i = 0; i < numcols; i++) {
653 descriptor = PyTuple_New(7);
654 if (!descriptor) {
655 goto error;
656 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000657 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
659 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
660 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
661 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
662 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
663 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
664 PyTuple_SetItem(self->description, i, descriptor);
665 }
666 }
667 }
668
669 if (rc == SQLITE_ROW) {
670 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000671 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672 goto error;
673 }
674
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000675 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000676 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000677 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678 Py_DECREF(self->statement);
679 self->statement = 0;
680 }
681
682 switch (statement_type) {
683 case STATEMENT_UPDATE:
684 case STATEMENT_DELETE:
685 case STATEMENT_INSERT:
686 case STATEMENT_REPLACE:
687 Py_BEGIN_ALLOW_THREADS
688 rowcount += (long)sqlite3_changes(self->connection->db);
689 Py_END_ALLOW_THREADS
690 Py_DECREF(self->rowcount);
691 self->rowcount = PyInt_FromLong(rowcount);
692 }
693
694 Py_DECREF(self->lastrowid);
695 if (statement_type == STATEMENT_INSERT) {
696 Py_BEGIN_ALLOW_THREADS
697 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
698 Py_END_ALLOW_THREADS
699 self->lastrowid = PyInt_FromLong((long)lastrowid);
700 } else {
701 Py_INCREF(Py_None);
702 self->lastrowid = Py_None;
703 }
704
705 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000706 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707 }
708 Py_XDECREF(parameters);
709 }
710
711error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000712 Py_XDECREF(parameters);
713 Py_XDECREF(parameters_iter);
714 Py_XDECREF(parameters_list);
715
716 if (PyErr_Occurred()) {
717 return NULL;
718 } else {
719 Py_INCREF(self);
720 return (PyObject*)self;
721 }
722}
723
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000724PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000725{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000726 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000727}
728
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000729PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000730{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000731 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000732}
733
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000734PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735{
736 PyObject* script_obj;
737 PyObject* script_str = NULL;
738 const char* script_cstr;
739 sqlite3_stmt* statement;
740 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 PyObject* result;
742 int statement_completed = 0;
743
744 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
745 return NULL;
746 }
747
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000748 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000749 return NULL;
750 }
751
Gerhard Häring6d214562007-08-10 18:15:11 +0000752 if (PyUnicode_Check(script_obj)) {
753 script_cstr = PyUnicode_AsString(script_obj);
754 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000755 return NULL;
756 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000758 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000759 return NULL;
760 }
761
762 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000763 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000764 if (!result) {
765 goto error;
766 }
767 Py_DECREF(result);
768
769 while (1) {
770 if (!sqlite3_complete(script_cstr)) {
771 break;
772 }
773 statement_completed = 1;
774
775 rc = sqlite3_prepare(self->connection->db,
776 script_cstr,
777 -1,
778 &statement,
779 &script_cstr);
780 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000781 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782 goto error;
783 }
784
785 /* execute statement, and ignore results of SELECT statements */
786 rc = SQLITE_ROW;
787 while (rc == SQLITE_ROW) {
788 rc = _sqlite_step_with_busyhandler(statement, self->connection);
789 }
790
791 if (rc != SQLITE_DONE) {
792 (void)sqlite3_finalize(statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000793 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000794 goto error;
795 }
796
797 rc = sqlite3_finalize(statement);
798 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000799 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000800 goto error;
801 }
802 }
803
804error:
805 Py_XDECREF(script_str);
806
807 if (!statement_completed) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000808 PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000809 }
810
811 if (PyErr_Occurred()) {
812 return NULL;
813 } else {
814 Py_INCREF(self);
815 return (PyObject*)self;
816 }
817}
818
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000819PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820{
821 Py_INCREF(self);
822 return (PyObject*)self;
823}
824
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000825PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826{
827 PyObject* next_row_tuple;
828 PyObject* next_row;
829 int rc;
830
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000831 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000832 return NULL;
833 }
834
835 if (!self->next_row) {
836 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000837 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838 Py_DECREF(self->statement);
839 self->statement = NULL;
840 }
841 return NULL;
842 }
843
844 next_row_tuple = self->next_row;
845 self->next_row = NULL;
846
847 if (self->row_factory != Py_None) {
848 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
849 Py_DECREF(next_row_tuple);
850 } else {
851 next_row = next_row_tuple;
852 }
853
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000854 if (self->statement) {
855 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
856 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
857 Py_DECREF(next_row);
858 _pysqlite_seterror(self->connection->db);
859 return NULL;
860 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000862 if (rc == SQLITE_ROW) {
863 self->next_row = _pysqlite_fetch_one_row(self);
864 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865 }
866
867 return next_row;
868}
869
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000870PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000871{
872 PyObject* row;
873
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000874 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000875 if (!row && !PyErr_Occurred()) {
876 Py_INCREF(Py_None);
877 return Py_None;
878 }
879
880 return row;
881}
882
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000883PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000884{
885 PyObject* row;
886 PyObject* list;
887 int maxrows = self->arraysize;
888 int counter = 0;
889
890 if (!PyArg_ParseTuple(args, "|i", &maxrows)) {
891 return NULL;
892 }
893
894 list = PyList_New(0);
895 if (!list) {
896 return NULL;
897 }
898
899 /* just make sure we enter the loop */
900 row = Py_None;
901
902 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000903 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000904 if (row) {
905 PyList_Append(list, row);
906 Py_DECREF(row);
907 } else {
908 break;
909 }
910
911 if (++counter == maxrows) {
912 break;
913 }
914 }
915
916 if (PyErr_Occurred()) {
917 Py_DECREF(list);
918 return NULL;
919 } else {
920 return list;
921 }
922}
923
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000924PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000925{
926 PyObject* row;
927 PyObject* list;
928
929 list = PyList_New(0);
930 if (!list) {
931 return NULL;
932 }
933
934 /* just make sure we enter the loop */
935 row = (PyObject*)Py_None;
936
937 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000938 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939 if (row) {
940 PyList_Append(list, row);
941 Py_DECREF(row);
942 }
943 }
944
945 if (PyErr_Occurred()) {
946 Py_DECREF(list);
947 return NULL;
948 } else {
949 return list;
950 }
951}
952
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000953PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000954{
955 /* don't care, return None */
956 Py_INCREF(Py_None);
957 return Py_None;
958}
959
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000960PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000961{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000962 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000963 return NULL;
964 }
965
966 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000967 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000968 Py_DECREF(self->statement);
969 self->statement = 0;
970 }
971
972 Py_INCREF(Py_None);
973 return Py_None;
974}
975
976static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000977 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000978 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000979 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000980 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000981 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000982 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000983 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000984 PyDoc_STR("Fetches several rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000985 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000986 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000987 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000988 PyDoc_STR("Fetches one row from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000989 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000990 PyDoc_STR("Closes the cursor.")},
991 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
992 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
993 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
994 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
995 {NULL, NULL}
996};
997
998static struct PyMemberDef cursor_members[] =
999{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001000 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1001 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001002 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001003 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
1004 {"rowcount", T_OBJECT, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001005 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001006 {NULL}
1007};
1008
Thomas Wouters477c8d52006-05-27 19:21:47 +00001009static char cursor_doc[] =
1010PyDoc_STR("SQLite database cursor class.");
1011
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001012PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001013 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001014 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001015 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001016 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001017 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001018 0, /* tp_print */
1019 0, /* tp_getattr */
1020 0, /* tp_setattr */
1021 0, /* tp_compare */
1022 0, /* tp_repr */
1023 0, /* tp_as_number */
1024 0, /* tp_as_sequence */
1025 0, /* tp_as_mapping */
1026 0, /* tp_hash */
1027 0, /* tp_call */
1028 0, /* tp_str */
1029 0, /* tp_getattro */
1030 0, /* tp_setattro */
1031 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001032 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001033 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001034 0, /* tp_traverse */
1035 0, /* tp_clear */
1036 0, /* tp_richcompare */
1037 0, /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001038 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1039 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001040 cursor_methods, /* tp_methods */
1041 cursor_members, /* tp_members */
1042 0, /* tp_getset */
1043 0, /* tp_base */
1044 0, /* tp_dict */
1045 0, /* tp_descr_get */
1046 0, /* tp_descr_set */
1047 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001048 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001049 0, /* tp_alloc */
1050 0, /* tp_new */
1051 0 /* tp_free */
1052};
1053
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001054extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001056 pysqlite_CursorType.tp_new = PyType_GenericNew;
1057 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001058}