blob: 412e38679e8447dcd52a503dda6a994e1cf89813 [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 Rossum83857e32007-05-09 23:37:01 +0000500 if (PyObject_AsCharBuffer(operation, &operation_cstr, &operation_len) < 0)
501 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502
503 /* reset description and rowcount */
504 Py_DECREF(self->description);
505 Py_INCREF(Py_None);
506 self->description = Py_None;
507
508 Py_DECREF(self->rowcount);
509 self->rowcount = PyInt_FromLong(-1L);
510 if (!self->rowcount) {
511 goto error;
512 }
513
514 statement_type = detect_statement_type(operation_cstr);
515 if (self->connection->begin_statement) {
516 switch (statement_type) {
517 case STATEMENT_UPDATE:
518 case STATEMENT_DELETE:
519 case STATEMENT_INSERT:
520 case STATEMENT_REPLACE:
521 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000522 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000523 if (!result) {
524 goto error;
525 }
526 Py_DECREF(result);
527 }
528 break;
529 case STATEMENT_OTHER:
530 /* it's a DDL statement or something similar
531 - we better COMMIT first so it works for all cases */
532 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000533 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000534 if (!result) {
535 goto error;
536 }
537 Py_DECREF(result);
538 }
539 break;
540 case STATEMENT_SELECT:
541 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000542 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 "You cannot execute SELECT statements in executemany().");
544 goto error;
545 }
546 break;
547 }
548 }
549
550 func_args = PyTuple_New(1);
551 if (!func_args) {
552 goto error;
553 }
554 Py_INCREF(operation);
555 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
556 goto error;
557 }
558
559 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000560 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000561 Py_DECREF(self->statement);
562 }
563
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000564 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000565 Py_DECREF(func_args);
566
567 if (!self->statement) {
568 goto error;
569 }
570
571 if (self->statement->in_use) {
572 Py_DECREF(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000573 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000574 if (!self->statement) {
575 goto error;
576 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000577 rc = pysqlite_statement_create(self->statement, self->connection, operation);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000578 if (rc != SQLITE_OK) {
579 self->statement = 0;
580 goto error;
581 }
582 }
583
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000584 pysqlite_statement_reset(self->statement);
585 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000586
587 while (1) {
588 parameters = PyIter_Next(parameters_iter);
589 if (!parameters) {
590 break;
591 }
592
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000593 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000594
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000595 pysqlite_statement_bind_parameters(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596 if (PyErr_Occurred()) {
597 goto error;
598 }
599
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000600 if (pysqlite_build_row_cast_map(self) != 0) {
601 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602 goto error;
603 }
604
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000605 /* Keep trying the SQL statement until the schema stops changing. */
606 while (1) {
607 /* Actually execute the SQL statement. */
608 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
609 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
610 /* If it worked, let's get out of the loop */
611 break;
612 }
613 /* Something went wrong. Re-set the statement and try again. */
614 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000615 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000616 /* If this was a result of the schema changing, let's try
617 again. */
618 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000619 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000620 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000622 /* If the database gave us an error, promote it to Python. */
623 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624 goto error;
625 }
626 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000627 if (PyErr_Occurred()) {
628 /* there was an error that occurred in a user-defined callback */
629 if (_enable_callback_tracebacks) {
630 PyErr_Print();
631 } else {
632 PyErr_Clear();
633 }
634 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000635 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000636 goto error;
637 }
638 }
639
640 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
641 Py_BEGIN_ALLOW_THREADS
642 numcols = sqlite3_column_count(self->statement->st);
643 Py_END_ALLOW_THREADS
644
645 if (self->description == Py_None) {
646 Py_DECREF(self->description);
647 self->description = PyTuple_New(numcols);
648 if (!self->description) {
649 goto error;
650 }
651 for (i = 0; i < numcols; i++) {
652 descriptor = PyTuple_New(7);
653 if (!descriptor) {
654 goto error;
655 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000656 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000657 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
658 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
659 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
660 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
661 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
662 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
663 PyTuple_SetItem(self->description, i, descriptor);
664 }
665 }
666 }
667
668 if (rc == SQLITE_ROW) {
669 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000670 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671 goto error;
672 }
673
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000674 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000676 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677 Py_DECREF(self->statement);
678 self->statement = 0;
679 }
680
681 switch (statement_type) {
682 case STATEMENT_UPDATE:
683 case STATEMENT_DELETE:
684 case STATEMENT_INSERT:
685 case STATEMENT_REPLACE:
686 Py_BEGIN_ALLOW_THREADS
687 rowcount += (long)sqlite3_changes(self->connection->db);
688 Py_END_ALLOW_THREADS
689 Py_DECREF(self->rowcount);
690 self->rowcount = PyInt_FromLong(rowcount);
691 }
692
693 Py_DECREF(self->lastrowid);
694 if (statement_type == STATEMENT_INSERT) {
695 Py_BEGIN_ALLOW_THREADS
696 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
697 Py_END_ALLOW_THREADS
698 self->lastrowid = PyInt_FromLong((long)lastrowid);
699 } else {
700 Py_INCREF(Py_None);
701 self->lastrowid = Py_None;
702 }
703
704 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000705 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706 }
707 Py_XDECREF(parameters);
708 }
709
710error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 Py_XDECREF(parameters);
712 Py_XDECREF(parameters_iter);
713 Py_XDECREF(parameters_list);
714
715 if (PyErr_Occurred()) {
716 return NULL;
717 } else {
718 Py_INCREF(self);
719 return (PyObject*)self;
720 }
721}
722
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000723PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000724{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000725 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000726}
727
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000728PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000730 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000731}
732
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000733PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734{
735 PyObject* script_obj;
736 PyObject* script_str = NULL;
737 const char* script_cstr;
738 sqlite3_stmt* statement;
739 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000740 PyObject* result;
741 int statement_completed = 0;
742
743 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
744 return NULL;
745 }
746
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000747 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000748 return NULL;
749 }
750
Gerhard Häring6d214562007-08-10 18:15:11 +0000751 if (PyUnicode_Check(script_obj)) {
752 script_cstr = PyUnicode_AsString(script_obj);
753 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000754 return NULL;
755 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000756 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000757 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000758 return NULL;
759 }
760
761 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000762 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000763 if (!result) {
764 goto error;
765 }
766 Py_DECREF(result);
767
768 while (1) {
769 if (!sqlite3_complete(script_cstr)) {
770 break;
771 }
772 statement_completed = 1;
773
774 rc = sqlite3_prepare(self->connection->db,
775 script_cstr,
776 -1,
777 &statement,
778 &script_cstr);
779 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000780 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000781 goto error;
782 }
783
784 /* execute statement, and ignore results of SELECT statements */
785 rc = SQLITE_ROW;
786 while (rc == SQLITE_ROW) {
787 rc = _sqlite_step_with_busyhandler(statement, self->connection);
788 }
789
790 if (rc != SQLITE_DONE) {
791 (void)sqlite3_finalize(statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000792 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000793 goto error;
794 }
795
796 rc = sqlite3_finalize(statement);
797 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000798 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000799 goto error;
800 }
801 }
802
803error:
804 Py_XDECREF(script_str);
805
806 if (!statement_completed) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000807 PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000808 }
809
810 if (PyErr_Occurred()) {
811 return NULL;
812 } else {
813 Py_INCREF(self);
814 return (PyObject*)self;
815 }
816}
817
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000818PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819{
820 Py_INCREF(self);
821 return (PyObject*)self;
822}
823
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000824PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825{
826 PyObject* next_row_tuple;
827 PyObject* next_row;
828 int rc;
829
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000830 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831 return NULL;
832 }
833
834 if (!self->next_row) {
835 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000836 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837 Py_DECREF(self->statement);
838 self->statement = NULL;
839 }
840 return NULL;
841 }
842
843 next_row_tuple = self->next_row;
844 self->next_row = NULL;
845
846 if (self->row_factory != Py_None) {
847 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
848 Py_DECREF(next_row_tuple);
849 } else {
850 next_row = next_row_tuple;
851 }
852
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000853 if (self->statement) {
854 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
855 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
856 Py_DECREF(next_row);
857 _pysqlite_seterror(self->connection->db);
858 return NULL;
859 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000860
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000861 if (rc == SQLITE_ROW) {
862 self->next_row = _pysqlite_fetch_one_row(self);
863 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864 }
865
866 return next_row;
867}
868
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000869PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000870{
871 PyObject* row;
872
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000873 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000874 if (!row && !PyErr_Occurred()) {
875 Py_INCREF(Py_None);
876 return Py_None;
877 }
878
879 return row;
880}
881
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000882PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883{
884 PyObject* row;
885 PyObject* list;
886 int maxrows = self->arraysize;
887 int counter = 0;
888
889 if (!PyArg_ParseTuple(args, "|i", &maxrows)) {
890 return NULL;
891 }
892
893 list = PyList_New(0);
894 if (!list) {
895 return NULL;
896 }
897
898 /* just make sure we enter the loop */
899 row = Py_None;
900
901 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000902 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000903 if (row) {
904 PyList_Append(list, row);
905 Py_DECREF(row);
906 } else {
907 break;
908 }
909
910 if (++counter == maxrows) {
911 break;
912 }
913 }
914
915 if (PyErr_Occurred()) {
916 Py_DECREF(list);
917 return NULL;
918 } else {
919 return list;
920 }
921}
922
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000923PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000924{
925 PyObject* row;
926 PyObject* list;
927
928 list = PyList_New(0);
929 if (!list) {
930 return NULL;
931 }
932
933 /* just make sure we enter the loop */
934 row = (PyObject*)Py_None;
935
936 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000937 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000938 if (row) {
939 PyList_Append(list, row);
940 Py_DECREF(row);
941 }
942 }
943
944 if (PyErr_Occurred()) {
945 Py_DECREF(list);
946 return NULL;
947 } else {
948 return list;
949 }
950}
951
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000952PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000953{
954 /* don't care, return None */
955 Py_INCREF(Py_None);
956 return Py_None;
957}
958
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000959PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000960{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000961 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000962 return NULL;
963 }
964
965 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000966 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000967 Py_DECREF(self->statement);
968 self->statement = 0;
969 }
970
971 Py_INCREF(Py_None);
972 return Py_None;
973}
974
975static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000976 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000977 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000978 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000979 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000980 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000981 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000982 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000983 PyDoc_STR("Fetches several rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000984 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000985 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000986 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000987 PyDoc_STR("Fetches one row from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000988 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000989 PyDoc_STR("Closes the cursor.")},
990 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
991 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
992 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
993 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
994 {NULL, NULL}
995};
996
997static struct PyMemberDef cursor_members[] =
998{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000999 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), RO},
1000 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), RO},
1001 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
1002 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), RO},
1003 {"rowcount", T_OBJECT, offsetof(pysqlite_Cursor, rowcount), RO},
1004 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001005 {NULL}
1006};
1007
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008static char cursor_doc[] =
1009PyDoc_STR("SQLite database cursor class.");
1010
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001011PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001012 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001013 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001014 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001015 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001016 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001017 0, /* tp_print */
1018 0, /* tp_getattr */
1019 0, /* tp_setattr */
1020 0, /* tp_compare */
1021 0, /* tp_repr */
1022 0, /* tp_as_number */
1023 0, /* tp_as_sequence */
1024 0, /* tp_as_mapping */
1025 0, /* tp_hash */
1026 0, /* tp_call */
1027 0, /* tp_str */
1028 0, /* tp_getattro */
1029 0, /* tp_setattro */
1030 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001031 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001033 0, /* tp_traverse */
1034 0, /* tp_clear */
1035 0, /* tp_richcompare */
1036 0, /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001037 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1038 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001039 cursor_methods, /* tp_methods */
1040 cursor_members, /* tp_members */
1041 0, /* tp_getset */
1042 0, /* tp_base */
1043 0, /* tp_dict */
1044 0, /* tp_descr_get */
1045 0, /* tp_descr_set */
1046 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001047 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001048 0, /* tp_alloc */
1049 0, /* tp_new */
1050 0 /* tp_free */
1051};
1052
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001053extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001054{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001055 pysqlite_CursorType.tp_new = PyType_GenericNew;
1056 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001057}