blob: 7f5c21341115895e36ce494014af355758ba2a0f [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* cursor.c - the cursor type
2 *
Gerhard Häringf9cee222010-03-05 15:20:03 +00003 * Copyright (C) 2004-2010 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
Gerhard Häringf9cee222010-03-05 15:20:03 +000039static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
40
Guido van Rossum6374bb52007-06-13 16:28:25 +000041static pysqlite_StatementKind detect_statement_type(const char* statement)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042{
43 char buf[20];
Guido van Rossum6374bb52007-06-13 16:28:25 +000044 const char* src;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000045 char* dst;
46
47 src = statement;
48 /* skip over whitepace */
49 while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') {
50 src++;
51 }
52
53 if (*src == 0)
54 return STATEMENT_INVALID;
55
56 dst = buf;
57 *dst = 0;
58 while (isalpha(*src) && dst - buf < sizeof(buf) - 2) {
59 *dst++ = tolower(*src++);
60 }
61
62 *dst = 0;
63
64 if (!strcmp(buf, "select")) {
65 return STATEMENT_SELECT;
66 } else if (!strcmp(buf, "insert")) {
67 return STATEMENT_INSERT;
68 } else if (!strcmp(buf, "update")) {
69 return STATEMENT_UPDATE;
70 } else if (!strcmp(buf, "delete")) {
71 return STATEMENT_DELETE;
72 } else if (!strcmp(buf, "replace")) {
73 return STATEMENT_REPLACE;
74 } else {
75 return STATEMENT_OTHER;
76 }
77}
78
Gerhard Häringf9cee222010-03-05 15:20:03 +000079static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000081 pysqlite_Connection* connection;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000083 if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000085 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 }
87
88 Py_INCREF(connection);
89 self->connection = connection;
90 self->statement = NULL;
91 self->next_row = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +000092 self->in_weakreflist = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093
94 self->row_cast_map = PyList_New(0);
95 if (!self->row_cast_map) {
96 return -1;
97 }
98
99 Py_INCREF(Py_None);
100 self->description = Py_None;
101
102 Py_INCREF(Py_None);
103 self->lastrowid= Py_None;
104
105 self->arraysize = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000106 self->closed = 0;
107 self->reset = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108
Georg Brandlf78e02b2008-06-10 17:40:04 +0000109 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110
111 Py_INCREF(Py_None);
112 self->row_factory = Py_None;
113
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000114 if (!pysqlite_check_thread(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 return -1;
116 }
117
Gerhard Häringf9cee222010-03-05 15:20:03 +0000118 if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
119 return -1;
120 }
121
122 self->initialized = 1;
123
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000124 return 0;
125}
126
Gerhard Häringf9cee222010-03-05 15:20:03 +0000127static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000129 /* Reset the statement if the user has not closed the cursor */
130 if (self->statement) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000131 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 Py_DECREF(self->statement);
133 }
134
135 Py_XDECREF(self->connection);
136 Py_XDECREF(self->row_cast_map);
137 Py_XDECREF(self->description);
138 Py_XDECREF(self->lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139 Py_XDECREF(self->row_factory);
140 Py_XDECREF(self->next_row);
141
Gerhard Häringf9cee222010-03-05 15:20:03 +0000142 if (self->in_weakreflist != NULL) {
143 PyObject_ClearWeakRefs((PyObject*)self);
144 }
145
Christian Heimes90aa7642007-12-19 02:45:37 +0000146 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147}
148
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000149PyObject* _pysqlite_get_converter(PyObject* key)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150{
151 PyObject* upcase_key;
152 PyObject* retval;
153
154 upcase_key = PyObject_CallMethod(key, "upper", "");
155 if (!upcase_key) {
156 return NULL;
157 }
158
159 retval = PyDict_GetItem(converters, upcase_key);
160 Py_DECREF(upcase_key);
161
162 return retval;
163}
164
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000165int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000166{
167 int i;
168 const char* type_start = (const char*)-1;
169 const char* pos;
170
171 const char* colname;
172 const char* decltype;
173 PyObject* py_decltype;
174 PyObject* converter;
175 PyObject* key;
176
177 if (!self->connection->detect_types) {
178 return 0;
179 }
180
181 Py_XDECREF(self->row_cast_map);
182 self->row_cast_map = PyList_New(0);
183
184 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
185 converter = NULL;
186
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000187 if (self->connection->detect_types & PARSE_COLNAMES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000188 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000189 if (colname) {
190 for (pos = colname; *pos != 0; pos++) {
191 if (*pos == '[') {
192 type_start = pos + 1;
193 } else if (*pos == ']' && type_start != (const char*)-1) {
Brett Cannon40430012007-10-22 20:24:51 +0000194 key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000195 if (!key) {
196 /* creating a string failed, but it is too complicated
197 * to propagate the error here, we just assume there is
198 * no converter and proceed */
199 break;
200 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000202 converter = _pysqlite_get_converter(key);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000203 Py_DECREF(key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000204 break;
205 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207 }
208 }
209
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000210 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211 decltype = sqlite3_column_decltype(self->statement->st, i);
212 if (decltype) {
213 for (pos = decltype;;pos++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000214 /* Converter names are split at '(' and blanks.
215 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
216 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
217 * In other words, it will work as people expect it to work.*/
218 if (*pos == ' ' || *pos == '(' || *pos == 0) {
Brett Cannon40430012007-10-22 20:24:51 +0000219 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220 if (!py_decltype) {
221 return -1;
222 }
223 break;
224 }
225 }
226
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000227 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000228 Py_DECREF(py_decltype);
229 }
230 }
231
232 if (!converter) {
233 converter = Py_None;
234 }
235
236 if (PyList_Append(self->row_cast_map, converter) != 0) {
237 if (converter != Py_None) {
238 Py_DECREF(converter);
239 }
240 Py_XDECREF(self->row_cast_map);
241 self->row_cast_map = NULL;
242
243 return -1;
244 }
245 }
246
247 return 0;
248}
249
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000250PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000251{
252 const char* pos;
253
254 if (!colname) {
255 Py_INCREF(Py_None);
256 return Py_None;
257 }
258
259 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000260 if (*pos == 0 || *pos == '[') {
261 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
262 pos--;
263 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000264 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000265 }
266 }
267}
268
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000269PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000270{
Guido van Rossum98297ee2007-11-06 21:34:58 +0000271 return PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000272}
273
274/*
275 * Returns a row from the currently active SQLite statement
276 *
277 * Precondidition:
278 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
279 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000280PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000281{
282 int i, numcols;
283 PyObject* row;
284 PyObject* item = NULL;
285 int coltype;
286 PY_LONG_LONG intval;
287 PyObject* converter;
288 PyObject* converted;
289 Py_ssize_t nbytes;
290 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291 const char* val_str;
292 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000293 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000294 PyObject* buf_bytes;
295 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296
Gerhard Häringf9cee222010-03-05 15:20:03 +0000297 if (self->reset) {
298 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
299 return NULL;
300 }
301
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302 Py_BEGIN_ALLOW_THREADS
303 numcols = sqlite3_data_count(self->statement->st);
304 Py_END_ALLOW_THREADS
305
306 row = PyTuple_New(numcols);
307 if (!row) {
308 return NULL;
309 }
310
311 for (i = 0; i < numcols; i++) {
312 if (self->connection->detect_types) {
313 converter = PyList_GetItem(self->row_cast_map, i);
314 if (!converter) {
315 converter = Py_None;
316 }
317 } else {
318 converter = Py_None;
319 }
320
321 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000322 nbytes = sqlite3_column_bytes(self->statement->st, i);
323 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324 if (!val_str) {
325 Py_INCREF(Py_None);
326 converted = Py_None;
327 } else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000328 item = PyBytes_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000329 if (!item) {
330 return NULL;
331 }
332 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333 Py_DECREF(item);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000334 if (!converted) {
335 break;
336 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337 }
338 } else {
339 Py_BEGIN_ALLOW_THREADS
340 coltype = sqlite3_column_type(self->statement->st, i);
341 Py_END_ALLOW_THREADS
342 if (coltype == SQLITE_NULL) {
343 Py_INCREF(Py_None);
344 converted = Py_None;
345 } else if (coltype == SQLITE_INTEGER) {
346 intval = sqlite3_column_int64(self->statement->st, i);
347 if (intval < INT32_MIN || intval > INT32_MAX) {
348 converted = PyLong_FromLongLong(intval);
349 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000350 converted = PyLong_FromLong((long)intval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351 }
352 } else if (coltype == SQLITE_FLOAT) {
353 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
354 } else if (coltype == SQLITE_TEXT) {
355 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
356 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000357 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000359 converted = pysqlite_unicode_from_string(val_str,
360 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000361
362 if (!converted) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000363 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000364 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000365 colname = "<unknown column name>";
366 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000367 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000368 colname , val_str);
Victor Stinner86999502010-05-19 01:27:23 +0000369 buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000370 if (!buf_bytes) {
371 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
372 } else {
373 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
374 if (!error_obj) {
375 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000376 } else {
377 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000378 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000379 }
380 Py_DECREF(buf_bytes);
381 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000383 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
384 converted = PyBytes_FromString(val_str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000385 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
386 converted = PyByteArray_FromStringAndSize(val_str, strlen(val_str));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000387 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000388 converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000389 }
390 } else {
391 /* coltype == SQLITE_BLOB */
392 nbytes = sqlite3_column_bytes(self->statement->st, i);
Christian Heimes72b710a2008-05-26 13:28:38 +0000393 buffer = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000394 sqlite3_column_blob(self->statement->st, i), nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000395 if (!buffer) {
396 break;
397 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000398 converted = buffer;
399 }
400 }
401
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000402 if (converted) {
403 PyTuple_SetItem(row, i, converted);
404 } else {
405 Py_INCREF(Py_None);
406 PyTuple_SetItem(row, i, Py_None);
407 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000408 }
409
410 if (PyErr_Occurred()) {
411 Py_DECREF(row);
412 row = NULL;
413 }
414
415 return row;
416}
417
Gerhard Häringf9cee222010-03-05 15:20:03 +0000418/*
419 * Checks if a cursor object is usable.
420 *
421 * 0 => error; 1 => ok
422 */
423static int check_cursor(pysqlite_Cursor* cur)
424{
425 if (!cur->initialized) {
426 PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
427 return 0;
428 }
429
430 if (cur->closed) {
431 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
432 return 0;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000433 }
Gerhard Haering936d5182011-05-09 12:24:09 +0200434
435 if (cur->locked) {
436 PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
437 return 0;
438 }
439
440 return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000441}
442
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000443PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000444{
445 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000446 const char* operation_cstr;
447 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 PyObject* parameters_list = NULL;
449 PyObject* parameters_iter = NULL;
450 PyObject* parameters = NULL;
451 int i;
452 int rc;
453 PyObject* func_args;
454 PyObject* result;
455 int numcols;
456 PY_LONG_LONG lastrowid;
457 int statement_type;
458 PyObject* descriptor;
459 PyObject* second_argument = NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000460 int allow_8bit_chars;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461
Gerhard Häringf9cee222010-03-05 15:20:03 +0000462 if (!check_cursor(self)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200463 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 }
Georg Brandlf78e02b2008-06-10 17:40:04 +0000465
Gerhard Haering936d5182011-05-09 12:24:09 +0200466 self->locked = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000467 self->reset = 0;
468
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000469 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
470 allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
Gerhard Häringf9cee222010-03-05 15:20:03 +0000471 (self->connection->text_factory != pysqlite_OptimizedUnicode));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000472
473 Py_XDECREF(self->next_row);
474 self->next_row = NULL;
475
476 if (multiple) {
477 /* executemany() */
478 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200479 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000480 }
481
Guido van Rossum98297ee2007-11-06 21:34:58 +0000482 if (!PyUnicode_Check(operation)) {
483 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200484 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000485 }
486
487 if (PyIter_Check(second_argument)) {
488 /* iterator */
489 Py_INCREF(second_argument);
490 parameters_iter = second_argument;
491 } else {
492 /* sequence */
493 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000494 if (!parameters_iter) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200495 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000496 }
497 }
498 } else {
499 /* execute() */
500 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200501 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502 }
503
Guido van Rossum98297ee2007-11-06 21:34:58 +0000504 if (!PyUnicode_Check(operation)) {
505 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200506 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507 }
508
509 parameters_list = PyList_New(0);
510 if (!parameters_list) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200511 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512 }
513
514 if (second_argument == NULL) {
515 second_argument = PyTuple_New(0);
516 if (!second_argument) {
517 goto error;
518 }
519 } else {
520 Py_INCREF(second_argument);
521 }
522 if (PyList_Append(parameters_list, second_argument) != 0) {
523 Py_DECREF(second_argument);
524 goto error;
525 }
526 Py_DECREF(second_argument);
527
528 parameters_iter = PyObject_GetIter(parameters_list);
529 if (!parameters_iter) {
530 goto error;
531 }
532 }
533
534 if (self->statement != NULL) {
535 /* There is an active statement */
Brett Cannonb94767f2011-02-22 20:15:44 +0000536 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537 }
538
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000539 operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
Victor Stinner86999502010-05-19 01:27:23 +0000540 if (operation_cstr == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000541 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542
Georg Brandlf78e02b2008-06-10 17:40:04 +0000543 /* reset description and rowcount */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544 Py_DECREF(self->description);
545 Py_INCREF(Py_None);
546 self->description = Py_None;
Georg Brandlf78e02b2008-06-10 17:40:04 +0000547 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000549 func_args = PyTuple_New(1);
550 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551 goto error;
552 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000553 Py_INCREF(operation);
554 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
555 goto error;
556 }
557
558 if (self->statement) {
559 (void)pysqlite_statement_reset(self->statement);
560 Py_DECREF(self->statement);
561 }
562
563 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
564 Py_DECREF(func_args);
565
566 if (!self->statement) {
567 goto error;
568 }
569
570 if (self->statement->in_use) {
571 Py_DECREF(self->statement);
572 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
573 if (!self->statement) {
574 goto error;
575 }
576 rc = pysqlite_statement_create(self->statement, self->connection, operation);
577 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000578 Py_CLEAR(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000579 goto error;
580 }
581 }
582
583 pysqlite_statement_reset(self->statement);
584 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000585
586 statement_type = detect_statement_type(operation_cstr);
587 if (self->connection->begin_statement) {
588 switch (statement_type) {
589 case STATEMENT_UPDATE:
590 case STATEMENT_DELETE:
591 case STATEMENT_INSERT:
592 case STATEMENT_REPLACE:
593 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000594 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595 if (!result) {
596 goto error;
597 }
598 Py_DECREF(result);
599 }
600 break;
601 case STATEMENT_OTHER:
602 /* it's a DDL statement or something similar
603 - we better COMMIT first so it works for all cases */
604 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000605 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606 if (!result) {
607 goto error;
608 }
609 Py_DECREF(result);
610 }
611 break;
612 case STATEMENT_SELECT:
613 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000614 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000615 "You cannot execute SELECT statements in executemany().");
616 goto error;
617 }
618 break;
619 }
620 }
621
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622
623 while (1) {
624 parameters = PyIter_Next(parameters_iter);
625 if (!parameters) {
626 break;
627 }
628
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000629 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000630
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000631 pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000632 if (PyErr_Occurred()) {
633 goto error;
634 }
635
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000636 /* Keep trying the SQL statement until the schema stops changing. */
637 while (1) {
638 /* Actually execute the SQL statement. */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000639 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000640 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
641 /* If it worked, let's get out of the loop */
642 break;
643 }
644 /* Something went wrong. Re-set the statement and try again. */
645 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000647 /* If this was a result of the schema changing, let's try
648 again. */
649 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000651 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000653 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000654 (void)pysqlite_statement_reset(self->statement);
655 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000656 goto error;
657 }
658 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000659 if (PyErr_Occurred()) {
660 /* there was an error that occurred in a user-defined callback */
661 if (_enable_callback_tracebacks) {
662 PyErr_Print();
663 } else {
664 PyErr_Clear();
665 }
666 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000667 (void)pysqlite_statement_reset(self->statement);
668 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000669 goto error;
670 }
671 }
672
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000673 if (pysqlite_build_row_cast_map(self) != 0) {
674 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
675 goto error;
676 }
677
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000679 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000680 Py_BEGIN_ALLOW_THREADS
681 numcols = sqlite3_column_count(self->statement->st);
682 Py_END_ALLOW_THREADS
683
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 Py_DECREF(self->description);
685 self->description = PyTuple_New(numcols);
686 if (!self->description) {
687 goto error;
688 }
689 for (i = 0; i < numcols; i++) {
690 descriptor = PyTuple_New(7);
691 if (!descriptor) {
692 goto error;
693 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000694 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000695 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
696 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
697 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
698 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
699 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
700 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
701 PyTuple_SetItem(self->description, i, descriptor);
702 }
703 }
704 }
705
706 if (rc == SQLITE_ROW) {
707 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000708 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 goto error;
710 }
711
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000712 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000714 pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000715 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000716 }
717
718 switch (statement_type) {
719 case STATEMENT_UPDATE:
720 case STATEMENT_DELETE:
721 case STATEMENT_INSERT:
722 case STATEMENT_REPLACE:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000723 if (self->rowcount == -1L) {
724 self->rowcount = 0L;
725 }
726 self->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) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000741 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
Gerhard Haering936d5182011-05-09 12:24:09 +0200751 if (self->connection && self->connection->db)
752 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000753 #endif
754 #endif
755
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000756 Py_XDECREF(parameters);
757 Py_XDECREF(parameters_iter);
758 Py_XDECREF(parameters_list);
759
Gerhard Haering936d5182011-05-09 12:24:09 +0200760 self->locked = 0;
761
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000762 if (PyErr_Occurred()) {
Georg Brandlf78e02b2008-06-10 17:40:04 +0000763 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000764 return NULL;
765 } else {
766 Py_INCREF(self);
767 return (PyObject*)self;
768 }
769}
770
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000771PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000772{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000773 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000774}
775
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000776PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000777{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000778 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000779}
780
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000781PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782{
783 PyObject* script_obj;
784 PyObject* script_str = NULL;
785 const char* script_cstr;
786 sqlite3_stmt* statement;
787 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000788 PyObject* result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000789
790 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000791 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000792 }
793
Gerhard Häringf9cee222010-03-05 15:20:03 +0000794 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000795 return NULL;
796 }
797
Gerhard Häringf9cee222010-03-05 15:20:03 +0000798 self->reset = 0;
799
Gerhard Häring6d214562007-08-10 18:15:11 +0000800 if (PyUnicode_Check(script_obj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000801 script_cstr = _PyUnicode_AsString(script_obj);
Gerhard Häring6d214562007-08-10 18:15:11 +0000802 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) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000818 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819 rc = sqlite3_prepare(self->connection->db,
820 script_cstr,
821 -1,
822 &statement,
823 &script_cstr);
Benjamin Petersond7b03282008-09-13 15:58:53 +0000824 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000826 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827 goto error;
828 }
829
830 /* execute statement, and ignore results of SELECT statements */
831 rc = SQLITE_ROW;
832 while (rc == SQLITE_ROW) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000833 rc = pysqlite_step(statement, self->connection);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000834 /* TODO: we probably need more error handling here */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835 }
836
837 if (rc != SQLITE_DONE) {
838 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000839 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840 goto error;
841 }
842
843 rc = sqlite3_finalize(statement);
844 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000845 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846 goto error;
847 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000848
849 if (*script_cstr == (char)0) {
850 break;
851 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000852 }
853
854error:
855 Py_XDECREF(script_str);
856
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000857 if (PyErr_Occurred()) {
858 return NULL;
859 } else {
860 Py_INCREF(self);
861 return (PyObject*)self;
862 }
863}
864
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000865PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866{
867 Py_INCREF(self);
868 return (PyObject*)self;
869}
870
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000871PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000872{
873 PyObject* next_row_tuple;
874 PyObject* next_row;
875 int rc;
876
Gerhard Häringf9cee222010-03-05 15:20:03 +0000877 if (!check_cursor(self)) {
878 return NULL;
879 }
880
881 if (self->reset) {
882 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883 return NULL;
884 }
885
886 if (!self->next_row) {
887 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000888 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000889 Py_DECREF(self->statement);
890 self->statement = NULL;
891 }
892 return NULL;
893 }
894
895 next_row_tuple = self->next_row;
896 self->next_row = NULL;
897
898 if (self->row_factory != Py_None) {
899 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
900 Py_DECREF(next_row_tuple);
901 } else {
902 next_row = next_row_tuple;
903 }
904
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000905 if (self->statement) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000906 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000907 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000908 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000909 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000910 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000911 return NULL;
912 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000913
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000914 if (rc == SQLITE_ROW) {
915 self->next_row = _pysqlite_fetch_one_row(self);
916 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000917 }
918
919 return next_row;
920}
921
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000922PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923{
924 PyObject* row;
925
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000926 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000927 if (!row && !PyErr_Occurred()) {
928 Py_INCREF(Py_None);
929 return Py_None;
930 }
931
932 return row;
933}
934
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000935PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000936{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000937 static char *kwlist[] = {"size", NULL, NULL};
938
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939 PyObject* row;
940 PyObject* list;
941 int maxrows = self->arraysize;
942 int counter = 0;
943
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000944 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
945 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000946 }
947
948 list = PyList_New(0);
949 if (!list) {
950 return NULL;
951 }
952
953 /* just make sure we enter the loop */
954 row = Py_None;
955
956 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000957 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000958 if (row) {
959 PyList_Append(list, row);
960 Py_DECREF(row);
961 } else {
962 break;
963 }
964
965 if (++counter == maxrows) {
966 break;
967 }
968 }
969
970 if (PyErr_Occurred()) {
971 Py_DECREF(list);
972 return NULL;
973 } else {
974 return list;
975 }
976}
977
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000978PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000979{
980 PyObject* row;
981 PyObject* list;
982
983 list = PyList_New(0);
984 if (!list) {
985 return NULL;
986 }
987
988 /* just make sure we enter the loop */
989 row = (PyObject*)Py_None;
990
991 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000992 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000993 if (row) {
994 PyList_Append(list, row);
995 Py_DECREF(row);
996 }
997 }
998
999 if (PyErr_Occurred()) {
1000 Py_DECREF(list);
1001 return NULL;
1002 } else {
1003 return list;
1004 }
1005}
1006
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001007PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001008{
1009 /* don't care, return None */
1010 Py_INCREF(Py_None);
1011 return Py_None;
1012}
1013
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001014PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001015{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001016 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001017 return NULL;
1018 }
1019
1020 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001021 (void)pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001022 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001023 }
1024
Gerhard Häringf9cee222010-03-05 15:20:03 +00001025 self->closed = 1;
1026
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001027 Py_INCREF(Py_None);
1028 return Py_None;
1029}
1030
1031static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001032 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001033 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001034 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001035 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001036 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001037 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001038 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001039 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001040 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001041 PyDoc_STR("Fetches several rows from the resultset.")},
1042 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1043 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001044 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001045 PyDoc_STR("Closes the cursor.")},
1046 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1047 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1048 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1049 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1050 {NULL, NULL}
1051};
1052
1053static struct PyMemberDef cursor_members[] =
1054{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001055 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1056 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001057 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001058 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
Georg Brandlf78e02b2008-06-10 17:40:04 +00001059 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001060 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001061 {NULL}
1062};
1063
Thomas Wouters477c8d52006-05-27 19:21:47 +00001064static char cursor_doc[] =
1065PyDoc_STR("SQLite database cursor class.");
1066
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001067PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001068 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001069 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001070 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001071 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001072 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001073 0, /* tp_print */
1074 0, /* tp_getattr */
1075 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001076 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001077 0, /* tp_repr */
1078 0, /* tp_as_number */
1079 0, /* tp_as_sequence */
1080 0, /* tp_as_mapping */
1081 0, /* tp_hash */
1082 0, /* tp_call */
1083 0, /* tp_str */
1084 0, /* tp_getattro */
1085 0, /* tp_setattro */
1086 0, /* tp_as_buffer */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001087 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001088 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001089 0, /* tp_traverse */
1090 0, /* tp_clear */
1091 0, /* tp_richcompare */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001092 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001093 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1094 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001095 cursor_methods, /* tp_methods */
1096 cursor_members, /* tp_members */
1097 0, /* tp_getset */
1098 0, /* tp_base */
1099 0, /* tp_dict */
1100 0, /* tp_descr_get */
1101 0, /* tp_descr_set */
1102 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001103 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001104 0, /* tp_alloc */
1105 0, /* tp_new */
1106 0 /* tp_free */
1107};
1108
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001109extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001110{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001111 pysqlite_CursorType.tp_new = PyType_GenericNew;
1112 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001113}