blob: d723cdbca05f18604d4c7ec2326b3c1445b1e60a [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
Christian Heimes217cfd12007-12-02 14:31:20 +000029/* used to decide wether to call PyLong_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
Christian Heimes217cfd12007-12-02 14:31:20 +0000104 self->rowcount = PyLong_FromLong(-1L);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105 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
Christian Heimes90aa7642007-12-19 02:45:37 +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) {
Brett Cannon40430012007-10-22 20:24:51 +0000185 key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000186 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) {
Brett Cannon40430012007-10-22 20:24:51 +0000206 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207 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
Guido van Rossum98297ee2007-11-06 21:34:58 +0000275 return PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000276}
277
278/*
279 * Returns a row from the currently active SQLite statement
280 *
281 * Precondidition:
282 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
283 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000284PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285{
286 int i, numcols;
287 PyObject* row;
288 PyObject* item = NULL;
289 int coltype;
290 PY_LONG_LONG intval;
291 PyObject* converter;
292 PyObject* converted;
293 Py_ssize_t nbytes;
294 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000295 const char* val_str;
296 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000297 const char* colname;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298
299 Py_BEGIN_ALLOW_THREADS
300 numcols = sqlite3_data_count(self->statement->st);
301 Py_END_ALLOW_THREADS
302
303 row = PyTuple_New(numcols);
304 if (!row) {
305 return NULL;
306 }
307
308 for (i = 0; i < numcols; i++) {
309 if (self->connection->detect_types) {
310 converter = PyList_GetItem(self->row_cast_map, i);
311 if (!converter) {
312 converter = Py_None;
313 }
314 } else {
315 converter = Py_None;
316 }
317
318 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000319 nbytes = sqlite3_column_bytes(self->statement->st, i);
320 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000321 if (!val_str) {
322 Py_INCREF(Py_None);
323 converted = Py_None;
324 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000325 item = PyString_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326 if (!item) {
327 return NULL;
328 }
329 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 Py_DECREF(item);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000331 if (!converted) {
332 break;
333 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000334 }
335 } else {
336 Py_BEGIN_ALLOW_THREADS
337 coltype = sqlite3_column_type(self->statement->st, i);
338 Py_END_ALLOW_THREADS
339 if (coltype == SQLITE_NULL) {
340 Py_INCREF(Py_None);
341 converted = Py_None;
342 } else if (coltype == SQLITE_INTEGER) {
343 intval = sqlite3_column_int64(self->statement->st, i);
344 if (intval < INT32_MIN || intval > INT32_MAX) {
345 converted = PyLong_FromLongLong(intval);
346 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000347 converted = PyLong_FromLong((long)intval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000348 }
349 } else if (coltype == SQLITE_FLOAT) {
350 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
351 } else if (coltype == SQLITE_TEXT) {
352 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
353 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000354 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000356 converted = pysqlite_unicode_from_string(val_str,
357 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358
359 if (!converted) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000360 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000361 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000362 colname = "<unknown column name>";
363 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000364 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000365 colname , val_str);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000366 PyErr_SetString(pysqlite_OperationalError, buf);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367 }
368 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
369 converted = PyString_FromString(val_str);
Gerhard Häring6d214562007-08-10 18:15:11 +0000370 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
371 converted = PyBytes_FromStringAndSize(val_str, strlen(val_str));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000373 converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374 }
375 } else {
376 /* coltype == SQLITE_BLOB */
377 nbytes = sqlite3_column_bytes(self->statement->st, i);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000378 buffer = PyString_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000379 sqlite3_column_blob(self->statement->st, i), nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000380 if (!buffer) {
381 break;
382 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000383 converted = buffer;
384 }
385 }
386
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000387 if (converted) {
388 PyTuple_SetItem(row, i, converted);
389 } else {
390 Py_INCREF(Py_None);
391 PyTuple_SetItem(row, i, Py_None);
392 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000393 }
394
395 if (PyErr_Occurred()) {
396 Py_DECREF(row);
397 row = NULL;
398 }
399
400 return row;
401}
402
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000403PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404{
405 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000406 const char* operation_cstr;
407 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000408 PyObject* parameters_list = NULL;
409 PyObject* parameters_iter = NULL;
410 PyObject* parameters = NULL;
411 int i;
412 int rc;
413 PyObject* func_args;
414 PyObject* result;
415 int numcols;
416 PY_LONG_LONG lastrowid;
417 int statement_type;
418 PyObject* descriptor;
419 PyObject* second_argument = NULL;
420 long rowcount = 0;
421
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000422 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423 return NULL;
424 }
425
426 Py_XDECREF(self->next_row);
427 self->next_row = NULL;
428
429 if (multiple) {
430 /* executemany() */
431 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
432 return NULL;
433 }
434
Guido van Rossum98297ee2007-11-06 21:34:58 +0000435 if (!PyUnicode_Check(operation)) {
436 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 return NULL;
438 }
439
440 if (PyIter_Check(second_argument)) {
441 /* iterator */
442 Py_INCREF(second_argument);
443 parameters_iter = second_argument;
444 } else {
445 /* sequence */
446 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000447 if (!parameters_iter) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 return NULL;
449 }
450 }
451 } else {
452 /* execute() */
453 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
454 return NULL;
455 }
456
Guido van Rossum98297ee2007-11-06 21:34:58 +0000457 if (!PyUnicode_Check(operation)) {
458 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000459 return NULL;
460 }
461
462 parameters_list = PyList_New(0);
463 if (!parameters_list) {
464 return NULL;
465 }
466
467 if (second_argument == NULL) {
468 second_argument = PyTuple_New(0);
469 if (!second_argument) {
470 goto error;
471 }
472 } else {
473 Py_INCREF(second_argument);
474 }
475 if (PyList_Append(parameters_list, second_argument) != 0) {
476 Py_DECREF(second_argument);
477 goto error;
478 }
479 Py_DECREF(second_argument);
480
481 parameters_iter = PyObject_GetIter(parameters_list);
482 if (!parameters_iter) {
483 goto error;
484 }
485 }
486
487 if (self->statement != NULL) {
488 /* There is an active statement */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000489 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490 }
491
Guido van Rossumec9a4af2007-08-29 14:26:02 +0000492 operation_cstr = PyUnicode_AsStringAndSize(operation, &operation_len);
Guido van Rossumfa9a1212007-08-29 03:34:29 +0000493 if (operation == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000494 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000495
496 /* reset description and rowcount */
497 Py_DECREF(self->description);
498 Py_INCREF(Py_None);
499 self->description = Py_None;
500
501 Py_DECREF(self->rowcount);
Christian Heimes217cfd12007-12-02 14:31:20 +0000502 self->rowcount = PyLong_FromLong(-1L);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503 if (!self->rowcount) {
504 goto error;
505 }
506
507 statement_type = detect_statement_type(operation_cstr);
508 if (self->connection->begin_statement) {
509 switch (statement_type) {
510 case STATEMENT_UPDATE:
511 case STATEMENT_DELETE:
512 case STATEMENT_INSERT:
513 case STATEMENT_REPLACE:
514 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000515 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000516 if (!result) {
517 goto error;
518 }
519 Py_DECREF(result);
520 }
521 break;
522 case STATEMENT_OTHER:
523 /* it's a DDL statement or something similar
524 - we better COMMIT first so it works for all cases */
525 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000526 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527 if (!result) {
528 goto error;
529 }
530 Py_DECREF(result);
531 }
532 break;
533 case STATEMENT_SELECT:
534 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000535 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536 "You cannot execute SELECT statements in executemany().");
537 goto error;
538 }
539 break;
540 }
541 }
542
543 func_args = PyTuple_New(1);
544 if (!func_args) {
545 goto error;
546 }
547 Py_INCREF(operation);
548 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
549 goto error;
550 }
551
552 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000553 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554 Py_DECREF(self->statement);
555 }
556
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000557 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000558 Py_DECREF(func_args);
559
560 if (!self->statement) {
561 goto error;
562 }
563
564 if (self->statement->in_use) {
565 Py_DECREF(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000566 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567 if (!self->statement) {
568 goto error;
569 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000570 rc = pysqlite_statement_create(self->statement, self->connection, operation);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000571 if (rc != SQLITE_OK) {
572 self->statement = 0;
573 goto error;
574 }
575 }
576
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000577 pysqlite_statement_reset(self->statement);
578 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579
580 while (1) {
581 parameters = PyIter_Next(parameters_iter);
582 if (!parameters) {
583 break;
584 }
585
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000586 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000588 pysqlite_statement_bind_parameters(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589 if (PyErr_Occurred()) {
590 goto error;
591 }
592
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000593 if (pysqlite_build_row_cast_map(self) != 0) {
594 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595 goto error;
596 }
597
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000598 /* Keep trying the SQL statement until the schema stops changing. */
599 while (1) {
600 /* Actually execute the SQL statement. */
601 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
602 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
603 /* If it worked, let's get out of the loop */
604 break;
605 }
606 /* Something went wrong. Re-set the statement and try again. */
607 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000608 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000609 /* If this was a result of the schema changing, let's try
610 again. */
611 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000612 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000613 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000614 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000615 /* If the database gave us an error, promote it to Python. */
616 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000617 goto error;
618 }
619 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000620 if (PyErr_Occurred()) {
621 /* there was an error that occurred in a user-defined callback */
622 if (_enable_callback_tracebacks) {
623 PyErr_Print();
624 } else {
625 PyErr_Clear();
626 }
627 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000628 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629 goto error;
630 }
631 }
632
633 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
634 Py_BEGIN_ALLOW_THREADS
635 numcols = sqlite3_column_count(self->statement->st);
636 Py_END_ALLOW_THREADS
637
638 if (self->description == Py_None) {
639 Py_DECREF(self->description);
640 self->description = PyTuple_New(numcols);
641 if (!self->description) {
642 goto error;
643 }
644 for (i = 0; i < numcols; i++) {
645 descriptor = PyTuple_New(7);
646 if (!descriptor) {
647 goto error;
648 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000649 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
651 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
652 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
653 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
654 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
655 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
656 PyTuple_SetItem(self->description, i, descriptor);
657 }
658 }
659 }
660
661 if (rc == SQLITE_ROW) {
662 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000663 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000664 goto error;
665 }
666
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000667 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000669 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670 Py_DECREF(self->statement);
671 self->statement = 0;
672 }
673
674 switch (statement_type) {
675 case STATEMENT_UPDATE:
676 case STATEMENT_DELETE:
677 case STATEMENT_INSERT:
678 case STATEMENT_REPLACE:
679 Py_BEGIN_ALLOW_THREADS
680 rowcount += (long)sqlite3_changes(self->connection->db);
681 Py_END_ALLOW_THREADS
682 Py_DECREF(self->rowcount);
Christian Heimes217cfd12007-12-02 14:31:20 +0000683 self->rowcount = PyLong_FromLong(rowcount);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 }
685
686 Py_DECREF(self->lastrowid);
687 if (statement_type == STATEMENT_INSERT) {
688 Py_BEGIN_ALLOW_THREADS
689 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
690 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +0000691 self->lastrowid = PyLong_FromLong((long)lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692 } else {
693 Py_INCREF(Py_None);
694 self->lastrowid = Py_None;
695 }
696
697 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000698 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000699 }
700 Py_XDECREF(parameters);
701 }
702
703error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 Py_XDECREF(parameters);
705 Py_XDECREF(parameters_iter);
706 Py_XDECREF(parameters_list);
707
708 if (PyErr_Occurred()) {
709 return NULL;
710 } else {
711 Py_INCREF(self);
712 return (PyObject*)self;
713 }
714}
715
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000716PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000717{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000718 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719}
720
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000721PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000723 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000724}
725
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000726PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000727{
728 PyObject* script_obj;
729 PyObject* script_str = NULL;
730 const char* script_cstr;
731 sqlite3_stmt* statement;
732 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000733 PyObject* result;
734 int statement_completed = 0;
735
736 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
737 return NULL;
738 }
739
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000740 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 return NULL;
742 }
743
Gerhard Häring6d214562007-08-10 18:15:11 +0000744 if (PyUnicode_Check(script_obj)) {
745 script_cstr = PyUnicode_AsString(script_obj);
746 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000747 return NULL;
748 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000749 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000750 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751 return NULL;
752 }
753
754 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000755 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000756 if (!result) {
757 goto error;
758 }
759 Py_DECREF(result);
760
761 while (1) {
762 if (!sqlite3_complete(script_cstr)) {
763 break;
764 }
765 statement_completed = 1;
766
767 rc = sqlite3_prepare(self->connection->db,
768 script_cstr,
769 -1,
770 &statement,
771 &script_cstr);
772 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000773 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000774 goto error;
775 }
776
777 /* execute statement, and ignore results of SELECT statements */
778 rc = SQLITE_ROW;
779 while (rc == SQLITE_ROW) {
780 rc = _sqlite_step_with_busyhandler(statement, self->connection);
781 }
782
783 if (rc != SQLITE_DONE) {
784 (void)sqlite3_finalize(statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000785 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000786 goto error;
787 }
788
789 rc = sqlite3_finalize(statement);
790 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000791 _pysqlite_seterror(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000792 goto error;
793 }
794 }
795
796error:
797 Py_XDECREF(script_str);
798
799 if (!statement_completed) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000800 PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000801 }
802
803 if (PyErr_Occurred()) {
804 return NULL;
805 } else {
806 Py_INCREF(self);
807 return (PyObject*)self;
808 }
809}
810
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000811PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000812{
813 Py_INCREF(self);
814 return (PyObject*)self;
815}
816
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000817PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818{
819 PyObject* next_row_tuple;
820 PyObject* next_row;
821 int rc;
822
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000823 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824 return NULL;
825 }
826
827 if (!self->next_row) {
828 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000829 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000830 Py_DECREF(self->statement);
831 self->statement = NULL;
832 }
833 return NULL;
834 }
835
836 next_row_tuple = self->next_row;
837 self->next_row = NULL;
838
839 if (self->row_factory != Py_None) {
840 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
841 Py_DECREF(next_row_tuple);
842 } else {
843 next_row = next_row_tuple;
844 }
845
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000846 if (self->statement) {
847 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
848 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
849 Py_DECREF(next_row);
850 _pysqlite_seterror(self->connection->db);
851 return NULL;
852 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000853
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000854 if (rc == SQLITE_ROW) {
855 self->next_row = _pysqlite_fetch_one_row(self);
856 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000857 }
858
859 return next_row;
860}
861
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000862PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000863{
864 PyObject* row;
865
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000866 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867 if (!row && !PyErr_Occurred()) {
868 Py_INCREF(Py_None);
869 return Py_None;
870 }
871
872 return row;
873}
874
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000875PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000876{
877 PyObject* row;
878 PyObject* list;
879 int maxrows = self->arraysize;
880 int counter = 0;
881
882 if (!PyArg_ParseTuple(args, "|i", &maxrows)) {
883 return NULL;
884 }
885
886 list = PyList_New(0);
887 if (!list) {
888 return NULL;
889 }
890
891 /* just make sure we enter the loop */
892 row = Py_None;
893
894 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000895 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000896 if (row) {
897 PyList_Append(list, row);
898 Py_DECREF(row);
899 } else {
900 break;
901 }
902
903 if (++counter == maxrows) {
904 break;
905 }
906 }
907
908 if (PyErr_Occurred()) {
909 Py_DECREF(list);
910 return NULL;
911 } else {
912 return list;
913 }
914}
915
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000916PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000917{
918 PyObject* row;
919 PyObject* list;
920
921 list = PyList_New(0);
922 if (!list) {
923 return NULL;
924 }
925
926 /* just make sure we enter the loop */
927 row = (PyObject*)Py_None;
928
929 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000930 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000931 if (row) {
932 PyList_Append(list, row);
933 Py_DECREF(row);
934 }
935 }
936
937 if (PyErr_Occurred()) {
938 Py_DECREF(list);
939 return NULL;
940 } else {
941 return list;
942 }
943}
944
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000945PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000946{
947 /* don't care, return None */
948 Py_INCREF(Py_None);
949 return Py_None;
950}
951
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000952PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000953{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000954 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000955 return NULL;
956 }
957
958 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000959 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000960 Py_DECREF(self->statement);
961 self->statement = 0;
962 }
963
964 Py_INCREF(Py_None);
965 return Py_None;
966}
967
968static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000969 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000970 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000971 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000972 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000973 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000974 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000975 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000976 PyDoc_STR("Fetches several rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000977 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000978 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000979 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000980 PyDoc_STR("Fetches one row from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000981 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000982 PyDoc_STR("Closes the cursor.")},
983 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
984 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
985 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
986 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
987 {NULL, NULL}
988};
989
990static struct PyMemberDef cursor_members[] =
991{
Guido van Rossum10f07c42007-08-11 15:32:55 +0000992 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
993 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000994 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +0000995 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
996 {"rowcount", T_OBJECT, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000997 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998 {NULL}
999};
1000
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001static char cursor_doc[] =
1002PyDoc_STR("SQLite database cursor class.");
1003
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001004PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001005 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001006 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001007 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001008 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001009 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001010 0, /* tp_print */
1011 0, /* tp_getattr */
1012 0, /* tp_setattr */
1013 0, /* tp_compare */
1014 0, /* tp_repr */
1015 0, /* tp_as_number */
1016 0, /* tp_as_sequence */
1017 0, /* tp_as_mapping */
1018 0, /* tp_hash */
1019 0, /* tp_call */
1020 0, /* tp_str */
1021 0, /* tp_getattro */
1022 0, /* tp_setattro */
1023 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001024 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001026 0, /* tp_traverse */
1027 0, /* tp_clear */
1028 0, /* tp_richcompare */
1029 0, /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001030 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1031 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001032 cursor_methods, /* tp_methods */
1033 cursor_members, /* tp_members */
1034 0, /* tp_getset */
1035 0, /* tp_base */
1036 0, /* tp_dict */
1037 0, /* tp_descr_get */
1038 0, /* tp_descr_set */
1039 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001040 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001041 0, /* tp_alloc */
1042 0, /* tp_new */
1043 0 /* tp_free */
1044};
1045
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001046extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001047{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001048 pysqlite_CursorType.tp_new = PyType_GenericNew;
1049 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001050}