blob: 10ce2e1cede6b6a16076c011923ef0888fdc9192 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* cursor.c - the cursor type
2 *
Gerhard Häringe7ea7452008-03-29 00:45:29 +00003 * Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
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 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000083 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 }
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++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000205 /* Converter names are split at '(' and blanks.
206 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
207 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
208 * In other words, it will work as people expect it to work.*/
209 if (*pos == ' ' || *pos == '(' || *pos == 0) {
Brett Cannon40430012007-10-22 20:24:51 +0000210 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211 if (!py_decltype) {
212 return -1;
213 }
214 break;
215 }
216 }
217
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000218 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000219 Py_DECREF(py_decltype);
220 }
221 }
222
223 if (!converter) {
224 converter = Py_None;
225 }
226
227 if (PyList_Append(self->row_cast_map, converter) != 0) {
228 if (converter != Py_None) {
229 Py_DECREF(converter);
230 }
231 Py_XDECREF(self->row_cast_map);
232 self->row_cast_map = NULL;
233
234 return -1;
235 }
236 }
237
238 return 0;
239}
240
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000241PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242{
243 const char* pos;
244
245 if (!colname) {
246 Py_INCREF(Py_None);
247 return Py_None;
248 }
249
250 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000251 if (*pos == 0 || *pos == '[') {
252 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
253 pos--;
254 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000255 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256 }
257 }
258}
259
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000260PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000261{
Guido van Rossum98297ee2007-11-06 21:34:58 +0000262 return PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263}
264
265/*
266 * Returns a row from the currently active SQLite statement
267 *
268 * Precondidition:
269 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
270 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000271PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000272{
273 int i, numcols;
274 PyObject* row;
275 PyObject* item = NULL;
276 int coltype;
277 PY_LONG_LONG intval;
278 PyObject* converter;
279 PyObject* converted;
280 Py_ssize_t nbytes;
281 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000282 const char* val_str;
283 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000284 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000285 PyObject* buf_bytes;
286 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287
288 Py_BEGIN_ALLOW_THREADS
289 numcols = sqlite3_data_count(self->statement->st);
290 Py_END_ALLOW_THREADS
291
292 row = PyTuple_New(numcols);
293 if (!row) {
294 return NULL;
295 }
296
297 for (i = 0; i < numcols; i++) {
298 if (self->connection->detect_types) {
299 converter = PyList_GetItem(self->row_cast_map, i);
300 if (!converter) {
301 converter = Py_None;
302 }
303 } else {
304 converter = Py_None;
305 }
306
307 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308 nbytes = sqlite3_column_bytes(self->statement->st, i);
309 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000310 if (!val_str) {
311 Py_INCREF(Py_None);
312 converted = Py_None;
313 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000314 item = PyString_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315 if (!item) {
316 return NULL;
317 }
318 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 Py_DECREF(item);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000320 if (!converted) {
321 break;
322 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 }
324 } else {
325 Py_BEGIN_ALLOW_THREADS
326 coltype = sqlite3_column_type(self->statement->st, i);
327 Py_END_ALLOW_THREADS
328 if (coltype == SQLITE_NULL) {
329 Py_INCREF(Py_None);
330 converted = Py_None;
331 } else if (coltype == SQLITE_INTEGER) {
332 intval = sqlite3_column_int64(self->statement->st, i);
333 if (intval < INT32_MIN || intval > INT32_MAX) {
334 converted = PyLong_FromLongLong(intval);
335 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000336 converted = PyLong_FromLong((long)intval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337 }
338 } else if (coltype == SQLITE_FLOAT) {
339 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
340 } else if (coltype == SQLITE_TEXT) {
341 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
342 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000343 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000345 converted = pysqlite_unicode_from_string(val_str,
346 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347
348 if (!converted) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000349 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000350 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000351 colname = "<unknown column name>";
352 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000353 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000354 colname , val_str);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000355 buf_bytes = PyBytes_FromStringAndSize(buf, strlen(buf));
356 if (!buf_bytes) {
357 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
358 } else {
359 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
360 if (!error_obj) {
361 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000362 } else {
363 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000364 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000365 }
366 Py_DECREF(buf_bytes);
367 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 }
369 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
370 converted = PyString_FromString(val_str);
Gerhard Häring6d214562007-08-10 18:15:11 +0000371 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
372 converted = PyBytes_FromStringAndSize(val_str, strlen(val_str));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000373 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000374 converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000375 }
376 } else {
377 /* coltype == SQLITE_BLOB */
378 nbytes = sqlite3_column_bytes(self->statement->st, i);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000379 buffer = PyString_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000380 sqlite3_column_blob(self->statement->st, i), nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381 if (!buffer) {
382 break;
383 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384 converted = buffer;
385 }
386 }
387
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000388 if (converted) {
389 PyTuple_SetItem(row, i, converted);
390 } else {
391 Py_INCREF(Py_None);
392 PyTuple_SetItem(row, i, Py_None);
393 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394 }
395
396 if (PyErr_Occurred()) {
397 Py_DECREF(row);
398 row = NULL;
399 }
400
401 return row;
402}
403
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000404PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405{
406 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000407 const char* operation_cstr;
408 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 PyObject* parameters_list = NULL;
410 PyObject* parameters_iter = NULL;
411 PyObject* parameters = NULL;
412 int i;
413 int rc;
414 PyObject* func_args;
415 PyObject* result;
416 int numcols;
417 PY_LONG_LONG lastrowid;
418 int statement_type;
419 PyObject* descriptor;
420 PyObject* second_argument = NULL;
421 long rowcount = 0;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000422 int allow_8bit_chars;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000424 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425 return NULL;
426 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000427 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
428 allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
429 (self->connection->text_factory != (PyObject*)&PyUnicode_Type && pysqlite_OptimizedUnicode));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430
431 Py_XDECREF(self->next_row);
432 self->next_row = NULL;
433
434 if (multiple) {
435 /* executemany() */
436 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000437 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000438 }
439
Guido van Rossum98297ee2007-11-06 21:34:58 +0000440 if (!PyUnicode_Check(operation)) {
441 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442 return NULL;
443 }
444
445 if (PyIter_Check(second_argument)) {
446 /* iterator */
447 Py_INCREF(second_argument);
448 parameters_iter = second_argument;
449 } else {
450 /* sequence */
451 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000452 if (!parameters_iter) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000453 return NULL;
454 }
455 }
456 } else {
457 /* execute() */
458 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000459 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000460 }
461
Guido van Rossum98297ee2007-11-06 21:34:58 +0000462 if (!PyUnicode_Check(operation)) {
463 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 return NULL;
465 }
466
467 parameters_list = PyList_New(0);
468 if (!parameters_list) {
469 return NULL;
470 }
471
472 if (second_argument == NULL) {
473 second_argument = PyTuple_New(0);
474 if (!second_argument) {
475 goto error;
476 }
477 } else {
478 Py_INCREF(second_argument);
479 }
480 if (PyList_Append(parameters_list, second_argument) != 0) {
481 Py_DECREF(second_argument);
482 goto error;
483 }
484 Py_DECREF(second_argument);
485
486 parameters_iter = PyObject_GetIter(parameters_list);
487 if (!parameters_iter) {
488 goto error;
489 }
490 }
491
492 if (self->statement != NULL) {
493 /* There is an active statement */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000494 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000495 }
496
Guido van Rossumec9a4af2007-08-29 14:26:02 +0000497 operation_cstr = PyUnicode_AsStringAndSize(operation, &operation_len);
Guido van Rossumfa9a1212007-08-29 03:34:29 +0000498 if (operation == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000499 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000500
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000501 /* reset description */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502 Py_DECREF(self->description);
503 Py_INCREF(Py_None);
504 self->description = Py_None;
505
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000506 func_args = PyTuple_New(1);
507 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 goto error;
509 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000510 Py_INCREF(operation);
511 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
512 goto error;
513 }
514
515 if (self->statement) {
516 (void)pysqlite_statement_reset(self->statement);
517 Py_DECREF(self->statement);
518 }
519
520 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
521 Py_DECREF(func_args);
522
523 if (!self->statement) {
524 goto error;
525 }
526
527 if (self->statement->in_use) {
528 Py_DECREF(self->statement);
529 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
530 if (!self->statement) {
531 goto error;
532 }
533 rc = pysqlite_statement_create(self->statement, self->connection, operation);
534 if (rc != SQLITE_OK) {
535 self->statement = 0;
536 goto error;
537 }
538 }
539
540 pysqlite_statement_reset(self->statement);
541 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542
543 statement_type = detect_statement_type(operation_cstr);
544 if (self->connection->begin_statement) {
545 switch (statement_type) {
546 case STATEMENT_UPDATE:
547 case STATEMENT_DELETE:
548 case STATEMENT_INSERT:
549 case STATEMENT_REPLACE:
550 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000551 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000552 if (!result) {
553 goto error;
554 }
555 Py_DECREF(result);
556 }
557 break;
558 case STATEMENT_OTHER:
559 /* it's a DDL statement or something similar
560 - we better COMMIT first so it works for all cases */
561 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000562 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000563 if (!result) {
564 goto error;
565 }
566 Py_DECREF(result);
567 }
568 break;
569 case STATEMENT_SELECT:
570 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000571 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000572 "You cannot execute SELECT statements in executemany().");
573 goto error;
574 }
575 break;
576 }
577 }
578
579 func_args = PyTuple_New(1);
580 if (!func_args) {
581 goto error;
582 }
583 Py_INCREF(operation);
584 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
585 goto error;
586 }
587
588 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000589 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590 Py_DECREF(self->statement);
591 }
592
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000593 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000594 Py_DECREF(func_args);
595
596 if (!self->statement) {
597 goto error;
598 }
599
600 if (self->statement->in_use) {
601 Py_DECREF(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000602 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000603 if (!self->statement) {
604 goto error;
605 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000606 rc = pysqlite_statement_create(self->statement, self->connection, operation);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000607 if (rc != SQLITE_OK) {
608 self->statement = 0;
609 goto error;
610 }
611 }
612
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000613 pysqlite_statement_reset(self->statement);
614 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000615
616 while (1) {
617 parameters = PyIter_Next(parameters_iter);
618 if (!parameters) {
619 break;
620 }
621
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000622 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000624 pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000625 if (PyErr_Occurred()) {
626 goto error;
627 }
628
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000629 if (pysqlite_build_row_cast_map(self) != 0) {
630 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000631 goto error;
632 }
633
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000634 /* Keep trying the SQL statement until the schema stops changing. */
635 while (1) {
636 /* Actually execute the SQL statement. */
637 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
638 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
639 /* If it worked, let's get out of the loop */
640 break;
641 }
642 /* Something went wrong. Re-set the statement and try again. */
643 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000645 /* If this was a result of the schema changing, let's try
646 again. */
647 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000649 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000651 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000652 (void)pysqlite_statement_reset(self->statement);
653 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654 goto error;
655 }
656 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000657 if (PyErr_Occurred()) {
658 /* there was an error that occurred in a user-defined callback */
659 if (_enable_callback_tracebacks) {
660 PyErr_Print();
661 } else {
662 PyErr_Clear();
663 }
664 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000665 (void)pysqlite_statement_reset(self->statement);
666 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000667 goto error;
668 }
669 }
670
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000671 if (pysqlite_build_row_cast_map(self) != 0) {
672 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
673 goto error;
674 }
675
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000676 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
677 Py_BEGIN_ALLOW_THREADS
678 numcols = sqlite3_column_count(self->statement->st);
679 Py_END_ALLOW_THREADS
680
681 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000682 Py_BEGIN_ALLOW_THREADS
683 numcols = sqlite3_column_count(self->statement->st);
684 Py_END_ALLOW_THREADS
685
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000686 Py_DECREF(self->description);
687 self->description = PyTuple_New(numcols);
688 if (!self->description) {
689 goto error;
690 }
691 for (i = 0; i < numcols; i++) {
692 descriptor = PyTuple_New(7);
693 if (!descriptor) {
694 goto error;
695 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000696 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000697 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
698 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
699 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
700 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
701 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
702 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
703 PyTuple_SetItem(self->description, i, descriptor);
704 }
705 }
706 }
707
708 if (rc == SQLITE_ROW) {
709 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000710 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 goto error;
712 }
713
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000714 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000716 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000717 Py_DECREF(self->statement);
718 self->statement = 0;
719 }
720
721 switch (statement_type) {
722 case STATEMENT_UPDATE:
723 case STATEMENT_DELETE:
724 case STATEMENT_INSERT:
725 case STATEMENT_REPLACE:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000726 rowcount += (long)sqlite3_changes(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000727 }
728
729 Py_DECREF(self->lastrowid);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000730 if (!multiple && statement_type == STATEMENT_INSERT) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000731 Py_BEGIN_ALLOW_THREADS
732 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
733 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +0000734 self->lastrowid = PyLong_FromLong((long)lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735 } else {
736 Py_INCREF(Py_None);
737 self->lastrowid = Py_None;
738 }
739
740 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000741 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000742 }
743 Py_XDECREF(parameters);
744 }
745
746error:
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000747 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
748 * ROLLBACK could have happened */
749 #ifdef SQLITE_VERSION_NUMBER
750 #if SQLITE_VERSION_NUMBER >= 3002002
751 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
752 #endif
753 #endif
754
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000755 Py_XDECREF(parameters);
756 Py_XDECREF(parameters_iter);
757 Py_XDECREF(parameters_list);
758
759 if (PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000760 Py_DECREF(self->rowcount);
761 self->rowcount = PyLong_FromLong(-1L);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000762 return NULL;
763 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000764 Py_DECREF(self->rowcount);
765 self->rowcount = PyLong_FromLong(rowcount);
766
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767 Py_INCREF(self);
768 return (PyObject*)self;
769 }
770}
771
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000772PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000774 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775}
776
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000777PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000778{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000779 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780}
781
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000782PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000783{
784 PyObject* script_obj;
785 PyObject* script_str = NULL;
786 const char* script_cstr;
787 sqlite3_stmt* statement;
788 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000789 PyObject* result;
790 int statement_completed = 0;
791
792 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000793 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000794 }
795
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000796 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000797 return NULL;
798 }
799
Gerhard Häring6d214562007-08-10 18:15:11 +0000800 if (PyUnicode_Check(script_obj)) {
801 script_cstr = PyUnicode_AsString(script_obj);
802 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000803 return NULL;
804 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000806 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000807 return NULL;
808 }
809
810 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000811 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000812 if (!result) {
813 goto error;
814 }
815 Py_DECREF(result);
816
817 while (1) {
818 if (!sqlite3_complete(script_cstr)) {
819 break;
820 }
821 statement_completed = 1;
822
823 rc = sqlite3_prepare(self->connection->db,
824 script_cstr,
825 -1,
826 &statement,
827 &script_cstr);
828 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000829 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000830 goto error;
831 }
832
833 /* execute statement, and ignore results of SELECT statements */
834 rc = SQLITE_ROW;
835 while (rc == SQLITE_ROW) {
836 rc = _sqlite_step_with_busyhandler(statement, self->connection);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000837 /* TODO: we probably need more error handling here */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838 }
839
840 if (rc != SQLITE_DONE) {
841 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000842 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000843 goto error;
844 }
845
846 rc = sqlite3_finalize(statement);
847 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000848 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000849 goto error;
850 }
851 }
852
853error:
854 Py_XDECREF(script_str);
855
856 if (!statement_completed) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000857 PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858 }
859
860 if (PyErr_Occurred()) {
861 return NULL;
862 } else {
863 Py_INCREF(self);
864 return (PyObject*)self;
865 }
866}
867
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000868PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000869{
870 Py_INCREF(self);
871 return (PyObject*)self;
872}
873
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000874PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000875{
876 PyObject* next_row_tuple;
877 PyObject* next_row;
878 int rc;
879
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000880 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000881 return NULL;
882 }
883
884 if (!self->next_row) {
885 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000886 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000887 Py_DECREF(self->statement);
888 self->statement = NULL;
889 }
890 return NULL;
891 }
892
893 next_row_tuple = self->next_row;
894 self->next_row = NULL;
895
896 if (self->row_factory != Py_None) {
897 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
898 Py_DECREF(next_row_tuple);
899 } else {
900 next_row = next_row_tuple;
901 }
902
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000903 if (self->statement) {
904 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
905 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000906 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000907 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000908 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000909 return NULL;
910 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000911
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000912 if (rc == SQLITE_ROW) {
913 self->next_row = _pysqlite_fetch_one_row(self);
914 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000915 }
916
917 return next_row;
918}
919
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000920PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921{
922 PyObject* row;
923
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000924 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000925 if (!row && !PyErr_Occurred()) {
926 Py_INCREF(Py_None);
927 return Py_None;
928 }
929
930 return row;
931}
932
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000933PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000934{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000935 static char *kwlist[] = {"size", NULL, NULL};
936
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000937 PyObject* row;
938 PyObject* list;
939 int maxrows = self->arraysize;
940 int counter = 0;
941
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000942 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
943 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000944 }
945
946 list = PyList_New(0);
947 if (!list) {
948 return NULL;
949 }
950
951 /* just make sure we enter the loop */
952 row = Py_None;
953
954 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000955 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000956 if (row) {
957 PyList_Append(list, row);
958 Py_DECREF(row);
959 } else {
960 break;
961 }
962
963 if (++counter == maxrows) {
964 break;
965 }
966 }
967
968 if (PyErr_Occurred()) {
969 Py_DECREF(list);
970 return NULL;
971 } else {
972 return list;
973 }
974}
975
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000976PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000977{
978 PyObject* row;
979 PyObject* list;
980
981 list = PyList_New(0);
982 if (!list) {
983 return NULL;
984 }
985
986 /* just make sure we enter the loop */
987 row = (PyObject*)Py_None;
988
989 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000990 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000991 if (row) {
992 PyList_Append(list, row);
993 Py_DECREF(row);
994 }
995 }
996
997 if (PyErr_Occurred()) {
998 Py_DECREF(list);
999 return NULL;
1000 } else {
1001 return list;
1002 }
1003}
1004
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001005PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001006{
1007 /* don't care, return None */
1008 Py_INCREF(Py_None);
1009 return Py_None;
1010}
1011
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001012PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001013{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001014 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001015 return NULL;
1016 }
1017
1018 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001019 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020 Py_DECREF(self->statement);
1021 self->statement = 0;
1022 }
1023
1024 Py_INCREF(Py_None);
1025 return Py_None;
1026}
1027
1028static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001029 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001030 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001031 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001032 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001033 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001034 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001035 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001036 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001037 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001038 PyDoc_STR("Fetches several rows from the resultset.")},
1039 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1040 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001041 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001042 PyDoc_STR("Closes the cursor.")},
1043 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1044 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1045 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1046 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1047 {NULL, NULL}
1048};
1049
1050static struct PyMemberDef cursor_members[] =
1051{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001052 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1053 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001054 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001055 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
1056 {"rowcount", T_OBJECT, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001057 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001058 {NULL}
1059};
1060
Thomas Wouters477c8d52006-05-27 19:21:47 +00001061static char cursor_doc[] =
1062PyDoc_STR("SQLite database cursor class.");
1063
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001064PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001065 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001066 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001067 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001068 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001069 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001070 0, /* tp_print */
1071 0, /* tp_getattr */
1072 0, /* tp_setattr */
1073 0, /* tp_compare */
1074 0, /* tp_repr */
1075 0, /* tp_as_number */
1076 0, /* tp_as_sequence */
1077 0, /* tp_as_mapping */
1078 0, /* tp_hash */
1079 0, /* tp_call */
1080 0, /* tp_str */
1081 0, /* tp_getattro */
1082 0, /* tp_setattro */
1083 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001084 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001085 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001086 0, /* tp_traverse */
1087 0, /* tp_clear */
1088 0, /* tp_richcompare */
1089 0, /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001090 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1091 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092 cursor_methods, /* tp_methods */
1093 cursor_members, /* tp_members */
1094 0, /* tp_getset */
1095 0, /* tp_base */
1096 0, /* tp_dict */
1097 0, /* tp_descr_get */
1098 0, /* tp_descr_set */
1099 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001100 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001101 0, /* tp_alloc */
1102 0, /* tp_new */
1103 0 /* tp_free */
1104};
1105
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001106extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001107{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001108 pysqlite_CursorType.tp_new = PyType_GenericNew;
1109 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001110}