blob: b9a4358b1a9d18237900dff5cb38d2419416afaa [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;
Antoine Pitrou1665d2c2011-10-04 13:35:28 +020058 while (Py_ISALPHA(*src) && dst - buf < sizeof(buf) - 2) {
59 *dst++ = Py_TOLOWER(*src++);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060 }
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{
129 int rc;
130
131 /* Reset the statement if the user has not closed the cursor */
132 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000133 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 Py_DECREF(self->statement);
135 }
136
137 Py_XDECREF(self->connection);
138 Py_XDECREF(self->row_cast_map);
139 Py_XDECREF(self->description);
140 Py_XDECREF(self->lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141 Py_XDECREF(self->row_factory);
142 Py_XDECREF(self->next_row);
143
Gerhard Häringf9cee222010-03-05 15:20:03 +0000144 if (self->in_weakreflist != NULL) {
145 PyObject_ClearWeakRefs((PyObject*)self);
146 }
147
Christian Heimes90aa7642007-12-19 02:45:37 +0000148 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000149}
150
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000151PyObject* _pysqlite_get_converter(PyObject* key)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000152{
153 PyObject* upcase_key;
154 PyObject* retval;
155
156 upcase_key = PyObject_CallMethod(key, "upper", "");
157 if (!upcase_key) {
158 return NULL;
159 }
160
161 retval = PyDict_GetItem(converters, upcase_key);
162 Py_DECREF(upcase_key);
163
164 return retval;
165}
166
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000167int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000168{
169 int i;
170 const char* type_start = (const char*)-1;
171 const char* pos;
172
173 const char* colname;
174 const char* decltype;
175 PyObject* py_decltype;
176 PyObject* converter;
177 PyObject* key;
178
179 if (!self->connection->detect_types) {
180 return 0;
181 }
182
183 Py_XDECREF(self->row_cast_map);
184 self->row_cast_map = PyList_New(0);
185
186 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
187 converter = NULL;
188
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000189 if (self->connection->detect_types & PARSE_COLNAMES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000191 if (colname) {
192 for (pos = colname; *pos != 0; pos++) {
193 if (*pos == '[') {
194 type_start = pos + 1;
195 } else if (*pos == ']' && type_start != (const char*)-1) {
Brett Cannon40430012007-10-22 20:24:51 +0000196 key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000197 if (!key) {
198 /* creating a string failed, but it is too complicated
199 * to propagate the error here, we just assume there is
200 * no converter and proceed */
201 break;
202 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000203
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000204 converter = _pysqlite_get_converter(key);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000205 Py_DECREF(key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206 break;
207 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209 }
210 }
211
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000212 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213 decltype = sqlite3_column_decltype(self->statement->st, i);
214 if (decltype) {
215 for (pos = decltype;;pos++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000216 /* Converter names are split at '(' and blanks.
217 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
218 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
219 * In other words, it will work as people expect it to work.*/
220 if (*pos == ' ' || *pos == '(' || *pos == 0) {
Brett Cannon40430012007-10-22 20:24:51 +0000221 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222 if (!py_decltype) {
223 return -1;
224 }
225 break;
226 }
227 }
228
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000229 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230 Py_DECREF(py_decltype);
231 }
232 }
233
234 if (!converter) {
235 converter = Py_None;
236 }
237
238 if (PyList_Append(self->row_cast_map, converter) != 0) {
239 if (converter != Py_None) {
240 Py_DECREF(converter);
241 }
242 Py_XDECREF(self->row_cast_map);
243 self->row_cast_map = NULL;
244
245 return -1;
246 }
247 }
248
249 return 0;
250}
251
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000252PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000253{
254 const char* pos;
255
256 if (!colname) {
257 Py_INCREF(Py_None);
258 return Py_None;
259 }
260
261 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262 if (*pos == 0 || *pos == '[') {
263 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
264 pos--;
265 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000266 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267 }
268 }
269}
270
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000271PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000272{
Guido van Rossum98297ee2007-11-06 21:34:58 +0000273 return PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274}
275
276/*
277 * Returns a row from the currently active SQLite statement
278 *
279 * Precondidition:
280 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
281 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000282PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283{
284 int i, numcols;
285 PyObject* row;
286 PyObject* item = NULL;
287 int coltype;
288 PY_LONG_LONG intval;
289 PyObject* converter;
290 PyObject* converted;
291 Py_ssize_t nbytes;
292 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293 const char* val_str;
294 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000295 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000296 PyObject* buf_bytes;
297 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298
Gerhard Häringf9cee222010-03-05 15:20:03 +0000299 if (self->reset) {
300 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
301 return NULL;
302 }
303
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304 Py_BEGIN_ALLOW_THREADS
305 numcols = sqlite3_data_count(self->statement->st);
306 Py_END_ALLOW_THREADS
307
308 row = PyTuple_New(numcols);
309 if (!row) {
310 return NULL;
311 }
312
313 for (i = 0; i < numcols; i++) {
314 if (self->connection->detect_types) {
315 converter = PyList_GetItem(self->row_cast_map, i);
316 if (!converter) {
317 converter = Py_None;
318 }
319 } else {
320 converter = Py_None;
321 }
322
323 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000324 nbytes = sqlite3_column_bytes(self->statement->st, i);
325 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326 if (!val_str) {
327 Py_INCREF(Py_None);
328 converted = Py_None;
329 } else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000330 item = PyBytes_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331 if (!item) {
332 return NULL;
333 }
334 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335 Py_DECREF(item);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000336 if (!converted) {
337 break;
338 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 }
340 } else {
341 Py_BEGIN_ALLOW_THREADS
342 coltype = sqlite3_column_type(self->statement->st, i);
343 Py_END_ALLOW_THREADS
344 if (coltype == SQLITE_NULL) {
345 Py_INCREF(Py_None);
346 converted = Py_None;
347 } else if (coltype == SQLITE_INTEGER) {
348 intval = sqlite3_column_int64(self->statement->st, i);
349 if (intval < INT32_MIN || intval > INT32_MAX) {
350 converted = PyLong_FromLongLong(intval);
351 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000352 converted = PyLong_FromLong((long)intval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000353 }
354 } else if (coltype == SQLITE_FLOAT) {
355 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
356 } else if (coltype == SQLITE_TEXT) {
357 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
358 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000359 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000361 converted = pysqlite_unicode_from_string(val_str,
362 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363
364 if (!converted) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000365 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000366 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000367 colname = "<unknown column name>";
368 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000369 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000370 colname , val_str);
Victor Stinner86999502010-05-19 01:27:23 +0000371 buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000372 if (!buf_bytes) {
373 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
374 } else {
375 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
376 if (!error_obj) {
377 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000378 } else {
379 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000380 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000381 }
382 Py_DECREF(buf_bytes);
383 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000385 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
386 converted = PyBytes_FromString(val_str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000387 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
388 converted = PyByteArray_FromStringAndSize(val_str, strlen(val_str));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000389 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000390 converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000391 }
392 } else {
393 /* coltype == SQLITE_BLOB */
394 nbytes = sqlite3_column_bytes(self->statement->st, i);
Christian Heimes72b710a2008-05-26 13:28:38 +0000395 buffer = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000396 sqlite3_column_blob(self->statement->st, i), nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000397 if (!buffer) {
398 break;
399 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 converted = buffer;
401 }
402 }
403
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000404 if (converted) {
405 PyTuple_SetItem(row, i, converted);
406 } else {
407 Py_INCREF(Py_None);
408 PyTuple_SetItem(row, i, Py_None);
409 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410 }
411
412 if (PyErr_Occurred()) {
413 Py_DECREF(row);
414 row = NULL;
415 }
416
417 return row;
418}
419
Gerhard Häringf9cee222010-03-05 15:20:03 +0000420/*
421 * Checks if a cursor object is usable.
422 *
423 * 0 => error; 1 => ok
424 */
425static int check_cursor(pysqlite_Cursor* cur)
426{
427 if (!cur->initialized) {
428 PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
429 return 0;
430 }
431
432 if (cur->closed) {
433 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
434 return 0;
435 } else {
436 return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
437 }
438}
439
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000440PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000441{
442 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000443 const char* operation_cstr;
444 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000445 PyObject* parameters_list = NULL;
446 PyObject* parameters_iter = NULL;
447 PyObject* parameters = NULL;
448 int i;
449 int rc;
450 PyObject* func_args;
451 PyObject* result;
452 int numcols;
453 PY_LONG_LONG lastrowid;
454 int statement_type;
455 PyObject* descriptor;
456 PyObject* second_argument = NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000457 int allow_8bit_chars;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000458
Gerhard Häringf9cee222010-03-05 15:20:03 +0000459 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000460 return NULL;
461 }
Georg Brandlf78e02b2008-06-10 17:40:04 +0000462
Gerhard Häringf9cee222010-03-05 15:20:03 +0000463 self->reset = 0;
464
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000465 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
466 allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
Gerhard Häringf9cee222010-03-05 15:20:03 +0000467 (self->connection->text_factory != pysqlite_OptimizedUnicode));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000468
469 Py_XDECREF(self->next_row);
470 self->next_row = NULL;
471
472 if (multiple) {
473 /* executemany() */
474 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000475 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000476 }
477
Guido van Rossum98297ee2007-11-06 21:34:58 +0000478 if (!PyUnicode_Check(operation)) {
479 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000480 return NULL;
481 }
482
483 if (PyIter_Check(second_argument)) {
484 /* iterator */
485 Py_INCREF(second_argument);
486 parameters_iter = second_argument;
487 } else {
488 /* sequence */
489 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000490 if (!parameters_iter) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000491 return NULL;
492 }
493 }
494 } else {
495 /* execute() */
496 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000497 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000498 }
499
Guido van Rossum98297ee2007-11-06 21:34:58 +0000500 if (!PyUnicode_Check(operation)) {
501 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502 return NULL;
503 }
504
505 parameters_list = PyList_New(0);
506 if (!parameters_list) {
507 return NULL;
508 }
509
510 if (second_argument == NULL) {
511 second_argument = PyTuple_New(0);
512 if (!second_argument) {
513 goto error;
514 }
515 } else {
516 Py_INCREF(second_argument);
517 }
518 if (PyList_Append(parameters_list, second_argument) != 0) {
519 Py_DECREF(second_argument);
520 goto error;
521 }
522 Py_DECREF(second_argument);
523
524 parameters_iter = PyObject_GetIter(parameters_list);
525 if (!parameters_iter) {
526 goto error;
527 }
528 }
529
530 if (self->statement != NULL) {
531 /* There is an active statement */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000532 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533 }
534
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000535 operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
Victor Stinner86999502010-05-19 01:27:23 +0000536 if (operation_cstr == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000537 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538
Georg Brandlf78e02b2008-06-10 17:40:04 +0000539 /* reset description and rowcount */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000540 Py_DECREF(self->description);
541 Py_INCREF(Py_None);
542 self->description = Py_None;
Georg Brandlf78e02b2008-06-10 17:40:04 +0000543 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000545 func_args = PyTuple_New(1);
546 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000547 goto error;
548 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000549 Py_INCREF(operation);
550 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
551 goto error;
552 }
553
554 if (self->statement) {
555 (void)pysqlite_statement_reset(self->statement);
556 Py_DECREF(self->statement);
557 }
558
559 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
560 Py_DECREF(func_args);
561
562 if (!self->statement) {
563 goto error;
564 }
565
566 if (self->statement->in_use) {
567 Py_DECREF(self->statement);
568 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
569 if (!self->statement) {
570 goto error;
571 }
572 rc = pysqlite_statement_create(self->statement, self->connection, operation);
573 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000574 Py_CLEAR(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000575 goto error;
576 }
577 }
578
579 pysqlite_statement_reset(self->statement);
580 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000581
582 statement_type = detect_statement_type(operation_cstr);
583 if (self->connection->begin_statement) {
584 switch (statement_type) {
585 case STATEMENT_UPDATE:
586 case STATEMENT_DELETE:
587 case STATEMENT_INSERT:
588 case STATEMENT_REPLACE:
589 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000590 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000591 if (!result) {
592 goto error;
593 }
594 Py_DECREF(result);
595 }
596 break;
597 case STATEMENT_OTHER:
598 /* it's a DDL statement or something similar
599 - we better COMMIT first so it works for all cases */
600 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000601 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602 if (!result) {
603 goto error;
604 }
605 Py_DECREF(result);
606 }
607 break;
608 case STATEMENT_SELECT:
609 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000610 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611 "You cannot execute SELECT statements in executemany().");
612 goto error;
613 }
614 break;
615 }
616 }
617
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000618
619 while (1) {
620 parameters = PyIter_Next(parameters_iter);
621 if (!parameters) {
622 break;
623 }
624
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000625 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000626
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000627 pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628 if (PyErr_Occurred()) {
629 goto error;
630 }
631
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000632 /* Keep trying the SQL statement until the schema stops changing. */
633 while (1) {
634 /* Actually execute the SQL statement. */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000635 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000636 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
637 /* If it worked, let's get out of the loop */
638 break;
639 }
640 /* Something went wrong. Re-set the statement and try again. */
641 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000642 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000643 /* If this was a result of the schema changing, let's try
644 again. */
645 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000647 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000649 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000650 (void)pysqlite_statement_reset(self->statement);
651 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652 goto error;
653 }
654 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000655 if (PyErr_Occurred()) {
656 /* there was an error that occurred in a user-defined callback */
657 if (_enable_callback_tracebacks) {
658 PyErr_Print();
659 } else {
660 PyErr_Clear();
661 }
662 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000663 (void)pysqlite_statement_reset(self->statement);
664 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000665 goto error;
666 }
667 }
668
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000669 if (pysqlite_build_row_cast_map(self) != 0) {
670 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
671 goto error;
672 }
673
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000676 Py_BEGIN_ALLOW_THREADS
677 numcols = sqlite3_column_count(self->statement->st);
678 Py_END_ALLOW_THREADS
679
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 Py_DECREF(self->description);
681 self->description = PyTuple_New(numcols);
682 if (!self->description) {
683 goto error;
684 }
685 for (i = 0; i < numcols; i++) {
686 descriptor = PyTuple_New(7);
687 if (!descriptor) {
688 goto error;
689 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000690 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000691 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
692 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
693 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
694 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
695 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
696 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
697 PyTuple_SetItem(self->description, i, descriptor);
698 }
699 }
700 }
701
702 if (rc == SQLITE_ROW) {
703 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000704 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705 goto error;
706 }
707
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000708 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000710 pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000711 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000712 }
713
714 switch (statement_type) {
715 case STATEMENT_UPDATE:
716 case STATEMENT_DELETE:
717 case STATEMENT_INSERT:
718 case STATEMENT_REPLACE:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000719 if (self->rowcount == -1L) {
720 self->rowcount = 0L;
721 }
722 self->rowcount += (long)sqlite3_changes(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000723 }
724
725 Py_DECREF(self->lastrowid);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000726 if (!multiple && statement_type == STATEMENT_INSERT) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000727 Py_BEGIN_ALLOW_THREADS
728 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
729 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +0000730 self->lastrowid = PyLong_FromLong((long)lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000731 } else {
732 Py_INCREF(Py_None);
733 self->lastrowid = Py_None;
734 }
735
736 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000737 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000738 }
739 Py_XDECREF(parameters);
740 }
741
742error:
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000743 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
744 * ROLLBACK could have happened */
745 #ifdef SQLITE_VERSION_NUMBER
746 #if SQLITE_VERSION_NUMBER >= 3002002
747 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
748 #endif
749 #endif
750
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751 Py_XDECREF(parameters);
752 Py_XDECREF(parameters_iter);
753 Py_XDECREF(parameters_list);
754
755 if (PyErr_Occurred()) {
Georg Brandlf78e02b2008-06-10 17:40:04 +0000756 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757 return NULL;
758 } else {
759 Py_INCREF(self);
760 return (PyObject*)self;
761 }
762}
763
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000764PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000765{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000766 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767}
768
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000769PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000770{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000771 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000772}
773
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000774PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775{
776 PyObject* script_obj;
777 PyObject* script_str = NULL;
778 const char* script_cstr;
779 sqlite3_stmt* statement;
780 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000781 PyObject* result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782
783 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000784 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000785 }
786
Gerhard Häringf9cee222010-03-05 15:20:03 +0000787 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000788 return NULL;
789 }
790
Gerhard Häringf9cee222010-03-05 15:20:03 +0000791 self->reset = 0;
792
Gerhard Häring6d214562007-08-10 18:15:11 +0000793 if (PyUnicode_Check(script_obj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000794 script_cstr = _PyUnicode_AsString(script_obj);
Gerhard Häring6d214562007-08-10 18:15:11 +0000795 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000796 return NULL;
797 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000799 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000800 return NULL;
801 }
802
803 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000804 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805 if (!result) {
806 goto error;
807 }
808 Py_DECREF(result);
809
810 while (1) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000811 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000812 rc = sqlite3_prepare(self->connection->db,
813 script_cstr,
814 -1,
815 &statement,
816 &script_cstr);
Benjamin Petersond7b03282008-09-13 15:58:53 +0000817 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000819 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820 goto error;
821 }
822
823 /* execute statement, and ignore results of SELECT statements */
824 rc = SQLITE_ROW;
825 while (rc == SQLITE_ROW) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000826 rc = pysqlite_step(statement, self->connection);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000827 /* TODO: we probably need more error handling here */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000828 }
829
830 if (rc != SQLITE_DONE) {
831 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000832 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833 goto error;
834 }
835
836 rc = sqlite3_finalize(statement);
837 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000838 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 goto error;
840 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000841
842 if (*script_cstr == (char)0) {
843 break;
844 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000845 }
846
847error:
848 Py_XDECREF(script_str);
849
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000850 if (PyErr_Occurred()) {
851 return NULL;
852 } else {
853 Py_INCREF(self);
854 return (PyObject*)self;
855 }
856}
857
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000858PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000859{
860 Py_INCREF(self);
861 return (PyObject*)self;
862}
863
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000864PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865{
866 PyObject* next_row_tuple;
867 PyObject* next_row;
868 int rc;
869
Gerhard Häringf9cee222010-03-05 15:20:03 +0000870 if (!check_cursor(self)) {
871 return NULL;
872 }
873
874 if (self->reset) {
875 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000876 return NULL;
877 }
878
879 if (!self->next_row) {
880 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000881 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882 Py_DECREF(self->statement);
883 self->statement = NULL;
884 }
885 return NULL;
886 }
887
888 next_row_tuple = self->next_row;
889 self->next_row = NULL;
890
891 if (self->row_factory != Py_None) {
892 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
893 Py_DECREF(next_row_tuple);
894 } else {
895 next_row = next_row_tuple;
896 }
897
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000898 if (self->statement) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000899 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000900 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000901 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000902 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000903 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000904 return NULL;
905 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000906
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000907 if (rc == SQLITE_ROW) {
908 self->next_row = _pysqlite_fetch_one_row(self);
909 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000910 }
911
912 return next_row;
913}
914
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000915PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000916{
917 PyObject* row;
918
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000919 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000920 if (!row && !PyErr_Occurred()) {
921 Py_INCREF(Py_None);
922 return Py_None;
923 }
924
925 return row;
926}
927
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000928PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000929{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000930 static char *kwlist[] = {"size", NULL, NULL};
931
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932 PyObject* row;
933 PyObject* list;
934 int maxrows = self->arraysize;
935 int counter = 0;
936
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000937 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
938 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939 }
940
941 list = PyList_New(0);
942 if (!list) {
943 return NULL;
944 }
945
946 /* just make sure we enter the loop */
947 row = Py_None;
948
949 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000950 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000951 if (row) {
952 PyList_Append(list, row);
953 Py_DECREF(row);
954 } else {
955 break;
956 }
957
958 if (++counter == maxrows) {
959 break;
960 }
961 }
962
963 if (PyErr_Occurred()) {
964 Py_DECREF(list);
965 return NULL;
966 } else {
967 return list;
968 }
969}
970
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000971PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000972{
973 PyObject* row;
974 PyObject* list;
975
976 list = PyList_New(0);
977 if (!list) {
978 return NULL;
979 }
980
981 /* just make sure we enter the loop */
982 row = (PyObject*)Py_None;
983
984 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000985 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000986 if (row) {
987 PyList_Append(list, row);
988 Py_DECREF(row);
989 }
990 }
991
992 if (PyErr_Occurred()) {
993 Py_DECREF(list);
994 return NULL;
995 } else {
996 return list;
997 }
998}
999
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001000PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001001{
1002 /* don't care, return None */
1003 Py_INCREF(Py_None);
1004 return Py_None;
1005}
1006
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001007PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001008{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001009 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001010 return NULL;
1011 }
1012
1013 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001014 (void)pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001015 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001016 }
1017
Gerhard Häringf9cee222010-03-05 15:20:03 +00001018 self->closed = 1;
1019
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020 Py_INCREF(Py_None);
1021 return Py_None;
1022}
1023
1024static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001025 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001026 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001027 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001028 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001029 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001030 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001031 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001032 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001033 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001034 PyDoc_STR("Fetches several rows from the resultset.")},
1035 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1036 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001037 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001038 PyDoc_STR("Closes the cursor.")},
1039 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1040 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1041 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1042 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1043 {NULL, NULL}
1044};
1045
1046static struct PyMemberDef cursor_members[] =
1047{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001048 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1049 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001050 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001051 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
Georg Brandlf78e02b2008-06-10 17:40:04 +00001052 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001053 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001054 {NULL}
1055};
1056
Thomas Wouters477c8d52006-05-27 19:21:47 +00001057static char cursor_doc[] =
1058PyDoc_STR("SQLite database cursor class.");
1059
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001060PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001061 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001062 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001063 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001064 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001065 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001066 0, /* tp_print */
1067 0, /* tp_getattr */
1068 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001069 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001070 0, /* tp_repr */
1071 0, /* tp_as_number */
1072 0, /* tp_as_sequence */
1073 0, /* tp_as_mapping */
1074 0, /* tp_hash */
1075 0, /* tp_call */
1076 0, /* tp_str */
1077 0, /* tp_getattro */
1078 0, /* tp_setattro */
1079 0, /* tp_as_buffer */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001080 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001081 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001082 0, /* tp_traverse */
1083 0, /* tp_clear */
1084 0, /* tp_richcompare */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001085 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001086 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1087 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001088 cursor_methods, /* tp_methods */
1089 cursor_members, /* tp_members */
1090 0, /* tp_getset */
1091 0, /* tp_base */
1092 0, /* tp_dict */
1093 0, /* tp_descr_get */
1094 0, /* tp_descr_set */
1095 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001096 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001097 0, /* tp_alloc */
1098 0, /* tp_new */
1099 0 /* tp_free */
1100};
1101
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001102extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001103{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001104 pysqlite_CursorType.tp_new = PyType_GenericNew;
1105 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001106}