blob: 8f9bd699a6c5c771749a389d0bf71868de840ac3 [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
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000029PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030
Gerhard Häringf9cee222010-03-05 15:20:03 +000031static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
32
Guido van Rossum6374bb52007-06-13 16:28:25 +000033static pysqlite_StatementKind detect_statement_type(const char* statement)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034{
35 char buf[20];
Guido van Rossum6374bb52007-06-13 16:28:25 +000036 const char* src;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037 char* dst;
38
39 src = statement;
40 /* skip over whitepace */
41 while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') {
42 src++;
43 }
44
45 if (*src == 0)
46 return STATEMENT_INVALID;
47
48 dst = buf;
49 *dst = 0;
Antoine Pitrou1665d2c2011-10-04 13:35:28 +020050 while (Py_ISALPHA(*src) && dst - buf < sizeof(buf) - 2) {
51 *dst++ = Py_TOLOWER(*src++);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052 }
53
54 *dst = 0;
55
56 if (!strcmp(buf, "select")) {
57 return STATEMENT_SELECT;
58 } else if (!strcmp(buf, "insert")) {
59 return STATEMENT_INSERT;
60 } else if (!strcmp(buf, "update")) {
61 return STATEMENT_UPDATE;
62 } else if (!strcmp(buf, "delete")) {
63 return STATEMENT_DELETE;
64 } else if (!strcmp(buf, "replace")) {
65 return STATEMENT_REPLACE;
66 } else {
67 return STATEMENT_OTHER;
68 }
69}
70
Gerhard Häringf9cee222010-03-05 15:20:03 +000071static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000073 pysqlite_Connection* connection;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000074
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000075 if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000076 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000077 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 }
79
80 Py_INCREF(connection);
81 self->connection = connection;
82 self->statement = NULL;
83 self->next_row = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +000084 self->in_weakreflist = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000085
86 self->row_cast_map = PyList_New(0);
87 if (!self->row_cast_map) {
88 return -1;
89 }
90
91 Py_INCREF(Py_None);
92 self->description = Py_None;
93
94 Py_INCREF(Py_None);
95 self->lastrowid= Py_None;
96
97 self->arraysize = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +000098 self->closed = 0;
99 self->reset = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000100
Georg Brandlf78e02b2008-06-10 17:40:04 +0000101 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000102
103 Py_INCREF(Py_None);
104 self->row_factory = Py_None;
105
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000106 if (!pysqlite_check_thread(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107 return -1;
108 }
109
Gerhard Häringf9cee222010-03-05 15:20:03 +0000110 if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
111 return -1;
112 }
113
114 self->initialized = 1;
115
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116 return 0;
117}
118
Gerhard Häringf9cee222010-03-05 15:20:03 +0000119static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000121 /* Reset the statement if the user has not closed the cursor */
122 if (self->statement) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000123 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000124 Py_DECREF(self->statement);
125 }
126
127 Py_XDECREF(self->connection);
128 Py_XDECREF(self->row_cast_map);
129 Py_XDECREF(self->description);
130 Py_XDECREF(self->lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131 Py_XDECREF(self->row_factory);
132 Py_XDECREF(self->next_row);
133
Gerhard Häringf9cee222010-03-05 15:20:03 +0000134 if (self->in_weakreflist != NULL) {
135 PyObject_ClearWeakRefs((PyObject*)self);
136 }
137
Christian Heimes90aa7642007-12-19 02:45:37 +0000138 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139}
140
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000141PyObject* _pysqlite_get_converter(PyObject* key)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000142{
143 PyObject* upcase_key;
144 PyObject* retval;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200145 _Py_IDENTIFIER(upper);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000146
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200147 upcase_key = _PyObject_CallMethodId(key, &PyId_upper, "");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000148 if (!upcase_key) {
149 return NULL;
150 }
151
152 retval = PyDict_GetItem(converters, upcase_key);
153 Py_DECREF(upcase_key);
154
155 return retval;
156}
157
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000158int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159{
160 int i;
161 const char* type_start = (const char*)-1;
162 const char* pos;
163
164 const char* colname;
165 const char* decltype;
166 PyObject* py_decltype;
167 PyObject* converter;
168 PyObject* key;
169
170 if (!self->connection->detect_types) {
171 return 0;
172 }
173
174 Py_XDECREF(self->row_cast_map);
175 self->row_cast_map = PyList_New(0);
176
177 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
178 converter = NULL;
179
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000180 if (self->connection->detect_types & PARSE_COLNAMES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000181 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000182 if (colname) {
183 for (pos = colname; *pos != 0; pos++) {
184 if (*pos == '[') {
185 type_start = pos + 1;
186 } else if (*pos == ']' && type_start != (const char*)-1) {
Brett Cannon40430012007-10-22 20:24:51 +0000187 key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000188 if (!key) {
189 /* creating a string failed, but it is too complicated
190 * to propagate the error here, we just assume there is
191 * no converter and proceed */
192 break;
193 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000195 converter = _pysqlite_get_converter(key);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000196 Py_DECREF(key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197 break;
198 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000199 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200 }
201 }
202
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000203 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000204 decltype = sqlite3_column_decltype(self->statement->st, i);
205 if (decltype) {
206 for (pos = decltype;;pos++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000207 /* Converter names are split at '(' and blanks.
208 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
209 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
210 * In other words, it will work as people expect it to work.*/
211 if (*pos == ' ' || *pos == '(' || *pos == 0) {
Brett Cannon40430012007-10-22 20:24:51 +0000212 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213 if (!py_decltype) {
214 return -1;
215 }
216 break;
217 }
218 }
219
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000220 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221 Py_DECREF(py_decltype);
222 }
223 }
224
225 if (!converter) {
226 converter = Py_None;
227 }
228
229 if (PyList_Append(self->row_cast_map, converter) != 0) {
230 if (converter != Py_None) {
231 Py_DECREF(converter);
232 }
233 Py_XDECREF(self->row_cast_map);
234 self->row_cast_map = NULL;
235
236 return -1;
237 }
238 }
239
240 return 0;
241}
242
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000243PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244{
245 const char* pos;
246
247 if (!colname) {
248 Py_INCREF(Py_None);
249 return Py_None;
250 }
251
252 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000253 if (*pos == 0 || *pos == '[') {
254 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
255 pos--;
256 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000257 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000258 }
259 }
260}
261
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000262/*
263 * Returns a row from the currently active SQLite statement
264 *
265 * Precondidition:
266 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
267 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000268PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269{
270 int i, numcols;
271 PyObject* row;
272 PyObject* item = NULL;
273 int coltype;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274 PyObject* converter;
275 PyObject* converted;
276 Py_ssize_t nbytes;
277 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000278 const char* val_str;
279 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000281 PyObject* buf_bytes;
282 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283
Gerhard Häringf9cee222010-03-05 15:20:03 +0000284 if (self->reset) {
285 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
286 return NULL;
287 }
288
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000289 Py_BEGIN_ALLOW_THREADS
290 numcols = sqlite3_data_count(self->statement->st);
291 Py_END_ALLOW_THREADS
292
293 row = PyTuple_New(numcols);
294 if (!row) {
295 return NULL;
296 }
297
298 for (i = 0; i < numcols; i++) {
299 if (self->connection->detect_types) {
300 converter = PyList_GetItem(self->row_cast_map, i);
301 if (!converter) {
302 converter = Py_None;
303 }
304 } else {
305 converter = Py_None;
306 }
307
308 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309 nbytes = sqlite3_column_bytes(self->statement->st, i);
310 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000311 if (!val_str) {
312 Py_INCREF(Py_None);
313 converted = Py_None;
314 } else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000315 item = PyBytes_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316 if (!item) {
317 return NULL;
318 }
319 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320 Py_DECREF(item);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000321 if (!converted) {
322 break;
323 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324 }
325 } else {
326 Py_BEGIN_ALLOW_THREADS
327 coltype = sqlite3_column_type(self->statement->st, i);
328 Py_END_ALLOW_THREADS
329 if (coltype == SQLITE_NULL) {
330 Py_INCREF(Py_None);
331 converted = Py_None;
332 } else if (coltype == SQLITE_INTEGER) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200333 converted = _pysqlite_long_from_int64(sqlite3_column_int64(self->statement->st, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000334 } else if (coltype == SQLITE_FLOAT) {
335 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
336 } else if (coltype == SQLITE_TEXT) {
337 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
Petri Lehtinen023fe332012-02-01 22:18:19 +0200338 nbytes = sqlite3_column_bytes(self->statement->st, i);
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200339 if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) {
340 converted = PyUnicode_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341 if (!converted) {
Victor Stinner4abda5d2013-07-18 01:54:37 +0200342#ifdef Py_DEBUG
343 /* in debug mode, type_call() fails with an assertion
344 error if an exception is set when it is called */
345 PyErr_Clear();
346#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000347 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000348 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000349 colname = "<unknown column name>";
350 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000351 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000352 colname , val_str);
Victor Stinner86999502010-05-19 01:27:23 +0000353 buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000354 if (!buf_bytes) {
355 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
356 } else {
357 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
358 if (!error_obj) {
359 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000360 } else {
361 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000362 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000363 }
364 Py_DECREF(buf_bytes);
365 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000367 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200368 converted = PyBytes_FromStringAndSize(val_str, nbytes);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000369 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200370 converted = PyByteArray_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000371 } else {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200372 converted = PyObject_CallFunction(self->connection->text_factory, "y#", val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000373 }
374 } else {
375 /* coltype == SQLITE_BLOB */
376 nbytes = sqlite3_column_bytes(self->statement->st, i);
Christian Heimes72b710a2008-05-26 13:28:38 +0000377 buffer = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000378 sqlite3_column_blob(self->statement->st, i), nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379 if (!buffer) {
380 break;
381 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 converted = buffer;
383 }
384 }
385
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000386 if (converted) {
387 PyTuple_SetItem(row, i, converted);
388 } else {
389 Py_INCREF(Py_None);
390 PyTuple_SetItem(row, i, Py_None);
391 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 }
393
394 if (PyErr_Occurred()) {
395 Py_DECREF(row);
396 row = NULL;
397 }
398
399 return row;
400}
401
Gerhard Häringf9cee222010-03-05 15:20:03 +0000402/*
403 * Checks if a cursor object is usable.
404 *
405 * 0 => error; 1 => ok
406 */
407static int check_cursor(pysqlite_Cursor* cur)
408{
409 if (!cur->initialized) {
410 PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
411 return 0;
412 }
413
414 if (cur->closed) {
415 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
416 return 0;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000417 }
Gerhard Haering936d5182011-05-09 12:24:09 +0200418
419 if (cur->locked) {
420 PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
421 return 0;
422 }
423
424 return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000425}
426
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000427PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000428{
429 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000430 const char* operation_cstr;
431 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432 PyObject* parameters_list = NULL;
433 PyObject* parameters_iter = NULL;
434 PyObject* parameters = NULL;
435 int i;
436 int rc;
437 PyObject* func_args;
438 PyObject* result;
439 int numcols;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000440 int statement_type;
441 PyObject* descriptor;
442 PyObject* second_argument = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443
Gerhard Häringf9cee222010-03-05 15:20:03 +0000444 if (!check_cursor(self)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200445 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446 }
Georg Brandlf78e02b2008-06-10 17:40:04 +0000447
Gerhard Haering936d5182011-05-09 12:24:09 +0200448 self->locked = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000449 self->reset = 0;
450
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451 Py_XDECREF(self->next_row);
452 self->next_row = NULL;
453
454 if (multiple) {
455 /* executemany() */
456 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200457 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000458 }
459
Guido van Rossum98297ee2007-11-06 21:34:58 +0000460 if (!PyUnicode_Check(operation)) {
461 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200462 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463 }
464
465 if (PyIter_Check(second_argument)) {
466 /* iterator */
467 Py_INCREF(second_argument);
468 parameters_iter = second_argument;
469 } else {
470 /* sequence */
471 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000472 if (!parameters_iter) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200473 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474 }
475 }
476 } else {
477 /* execute() */
478 if (!PyArg_ParseTuple(args, "O|O", &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 parameters_list = PyList_New(0);
488 if (!parameters_list) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200489 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490 }
491
492 if (second_argument == NULL) {
493 second_argument = PyTuple_New(0);
494 if (!second_argument) {
495 goto error;
496 }
497 } else {
498 Py_INCREF(second_argument);
499 }
500 if (PyList_Append(parameters_list, second_argument) != 0) {
501 Py_DECREF(second_argument);
502 goto error;
503 }
504 Py_DECREF(second_argument);
505
506 parameters_iter = PyObject_GetIter(parameters_list);
507 if (!parameters_iter) {
508 goto error;
509 }
510 }
511
512 if (self->statement != NULL) {
513 /* There is an active statement */
Brett Cannonb94767f2011-02-22 20:15:44 +0000514 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515 }
516
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000517 operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
Victor Stinner86999502010-05-19 01:27:23 +0000518 if (operation_cstr == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000519 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520
Georg Brandlf78e02b2008-06-10 17:40:04 +0000521 /* reset description and rowcount */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000522 Py_DECREF(self->description);
523 Py_INCREF(Py_None);
524 self->description = Py_None;
Georg Brandlf78e02b2008-06-10 17:40:04 +0000525 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000527 func_args = PyTuple_New(1);
528 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000529 goto error;
530 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000531 Py_INCREF(operation);
532 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
533 goto error;
534 }
535
536 if (self->statement) {
537 (void)pysqlite_statement_reset(self->statement);
538 Py_DECREF(self->statement);
539 }
540
541 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
542 Py_DECREF(func_args);
543
544 if (!self->statement) {
545 goto error;
546 }
547
548 if (self->statement->in_use) {
549 Py_DECREF(self->statement);
550 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
551 if (!self->statement) {
552 goto error;
553 }
554 rc = pysqlite_statement_create(self->statement, self->connection, operation);
555 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000556 Py_CLEAR(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000557 goto error;
558 }
559 }
560
561 pysqlite_statement_reset(self->statement);
562 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000563
564 statement_type = detect_statement_type(operation_cstr);
565 if (self->connection->begin_statement) {
566 switch (statement_type) {
567 case STATEMENT_UPDATE:
568 case STATEMENT_DELETE:
569 case STATEMENT_INSERT:
570 case STATEMENT_REPLACE:
571 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000572 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000573 if (!result) {
574 goto error;
575 }
576 Py_DECREF(result);
577 }
578 break;
579 case STATEMENT_OTHER:
580 /* it's a DDL statement or something similar
581 - we better COMMIT first so it works for all cases */
582 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000583 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000584 if (!result) {
585 goto error;
586 }
587 Py_DECREF(result);
588 }
589 break;
590 case STATEMENT_SELECT:
591 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000592 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000593 "You cannot execute SELECT statements in executemany().");
594 goto error;
595 }
596 break;
597 }
598 }
599
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000600
601 while (1) {
602 parameters = PyIter_Next(parameters_iter);
603 if (!parameters) {
604 break;
605 }
606
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000607 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000608
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200609 pysqlite_statement_bind_parameters(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610 if (PyErr_Occurred()) {
611 goto error;
612 }
613
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000614 /* Keep trying the SQL statement until the schema stops changing. */
615 while (1) {
616 /* Actually execute the SQL statement. */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000617 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000618 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
619 /* If it worked, let's get out of the loop */
620 break;
621 }
622 /* Something went wrong. Re-set the statement and try again. */
623 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000625 /* If this was a result of the schema changing, let's try
626 again. */
627 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000629 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000630 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000631 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000632 (void)pysqlite_statement_reset(self->statement);
633 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634 goto error;
635 }
636 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000637 if (PyErr_Occurred()) {
638 /* there was an error that occurred in a user-defined callback */
639 if (_enable_callback_tracebacks) {
640 PyErr_Print();
641 } else {
642 PyErr_Clear();
643 }
644 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000645 (void)pysqlite_statement_reset(self->statement);
646 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647 goto error;
648 }
649 }
650
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000651 if (pysqlite_build_row_cast_map(self) != 0) {
652 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
653 goto error;
654 }
655
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000656 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000657 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000658 Py_BEGIN_ALLOW_THREADS
659 numcols = sqlite3_column_count(self->statement->st);
660 Py_END_ALLOW_THREADS
661
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000662 Py_DECREF(self->description);
663 self->description = PyTuple_New(numcols);
664 if (!self->description) {
665 goto error;
666 }
667 for (i = 0; i < numcols; i++) {
668 descriptor = PyTuple_New(7);
669 if (!descriptor) {
670 goto error;
671 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000672 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000673 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
674 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
675 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
676 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
677 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
678 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
679 PyTuple_SetItem(self->description, i, descriptor);
680 }
681 }
682 }
683
684 if (rc == SQLITE_ROW) {
685 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000686 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000687 goto error;
688 }
689
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000690 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000691 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000692 pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000693 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694 }
695
696 switch (statement_type) {
697 case STATEMENT_UPDATE:
698 case STATEMENT_DELETE:
699 case STATEMENT_INSERT:
700 case STATEMENT_REPLACE:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000701 if (self->rowcount == -1L) {
702 self->rowcount = 0L;
703 }
704 self->rowcount += (long)sqlite3_changes(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705 }
706
707 Py_DECREF(self->lastrowid);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000708 if (!multiple && statement_type == STATEMENT_INSERT) {
Serhiy Storchakad160b122013-04-28 14:10:27 +0300709 sqlite_int64 lastrowid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 Py_BEGIN_ALLOW_THREADS
711 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
712 Py_END_ALLOW_THREADS
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200713 self->lastrowid = _pysqlite_long_from_int64(lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000714 } else {
715 Py_INCREF(Py_None);
716 self->lastrowid = Py_None;
717 }
718
719 if (multiple) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000720 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000721 }
722 Py_XDECREF(parameters);
723 }
724
725error:
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000726 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
727 * ROLLBACK could have happened */
728 #ifdef SQLITE_VERSION_NUMBER
729 #if SQLITE_VERSION_NUMBER >= 3002002
Gerhard Haering936d5182011-05-09 12:24:09 +0200730 if (self->connection && self->connection->db)
731 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000732 #endif
733 #endif
734
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735 Py_XDECREF(parameters);
736 Py_XDECREF(parameters_iter);
737 Py_XDECREF(parameters_list);
738
Gerhard Haering936d5182011-05-09 12:24:09 +0200739 self->locked = 0;
740
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 if (PyErr_Occurred()) {
Georg Brandlf78e02b2008-06-10 17:40:04 +0000742 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000743 return NULL;
744 } else {
745 Py_INCREF(self);
746 return (PyObject*)self;
747 }
748}
749
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000750PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000752 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000753}
754
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000755PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000756{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000757 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000758}
759
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000760PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761{
762 PyObject* script_obj;
763 PyObject* script_str = NULL;
764 const char* script_cstr;
765 sqlite3_stmt* statement;
766 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767 PyObject* result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768
769 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000770 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000771 }
772
Gerhard Häringf9cee222010-03-05 15:20:03 +0000773 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000774 return NULL;
775 }
776
Gerhard Häringf9cee222010-03-05 15:20:03 +0000777 self->reset = 0;
778
Gerhard Häring6d214562007-08-10 18:15:11 +0000779 if (PyUnicode_Check(script_obj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000780 script_cstr = _PyUnicode_AsString(script_obj);
Gerhard Häring6d214562007-08-10 18:15:11 +0000781 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782 return NULL;
783 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000784 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000785 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000786 return NULL;
787 }
788
789 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000790 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791 if (!result) {
792 goto error;
793 }
794 Py_DECREF(result);
795
796 while (1) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000797 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798 rc = sqlite3_prepare(self->connection->db,
799 script_cstr,
800 -1,
801 &statement,
802 &script_cstr);
Benjamin Petersond7b03282008-09-13 15:58:53 +0000803 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000804 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000805 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000806 goto error;
807 }
808
809 /* execute statement, and ignore results of SELECT statements */
810 rc = SQLITE_ROW;
811 while (rc == SQLITE_ROW) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000812 rc = pysqlite_step(statement, self->connection);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000813 /* TODO: we probably need more error handling here */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000814 }
815
816 if (rc != SQLITE_DONE) {
817 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000818 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819 goto error;
820 }
821
822 rc = sqlite3_finalize(statement);
823 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000824 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825 goto error;
826 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000827
828 if (*script_cstr == (char)0) {
829 break;
830 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831 }
832
833error:
834 Py_XDECREF(script_str);
835
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000836 if (PyErr_Occurred()) {
837 return NULL;
838 } else {
839 Py_INCREF(self);
840 return (PyObject*)self;
841 }
842}
843
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000844PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000845{
846 Py_INCREF(self);
847 return (PyObject*)self;
848}
849
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000850PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000851{
852 PyObject* next_row_tuple;
853 PyObject* next_row;
854 int rc;
855
Gerhard Häringf9cee222010-03-05 15:20:03 +0000856 if (!check_cursor(self)) {
857 return NULL;
858 }
859
860 if (self->reset) {
861 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862 return NULL;
863 }
864
865 if (!self->next_row) {
866 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000867 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868 Py_DECREF(self->statement);
869 self->statement = NULL;
870 }
871 return NULL;
872 }
873
874 next_row_tuple = self->next_row;
875 self->next_row = NULL;
876
877 if (self->row_factory != Py_None) {
878 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
879 Py_DECREF(next_row_tuple);
880 } else {
881 next_row = next_row_tuple;
882 }
883
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000884 if (self->statement) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000885 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000886 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000887 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000888 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000889 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000890 return NULL;
891 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000892
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000893 if (rc == SQLITE_ROW) {
894 self->next_row = _pysqlite_fetch_one_row(self);
895 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000896 }
897
898 return next_row;
899}
900
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000901PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000902{
903 PyObject* row;
904
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000905 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000906 if (!row && !PyErr_Occurred()) {
907 Py_INCREF(Py_None);
908 return Py_None;
909 }
910
911 return row;
912}
913
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000914PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000915{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000916 static char *kwlist[] = {"size", NULL, NULL};
917
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000918 PyObject* row;
919 PyObject* list;
920 int maxrows = self->arraysize;
921 int counter = 0;
922
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000923 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
924 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000925 }
926
927 list = PyList_New(0);
928 if (!list) {
929 return NULL;
930 }
931
932 /* just make sure we enter the loop */
933 row = Py_None;
934
935 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000936 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000937 if (row) {
938 PyList_Append(list, row);
939 Py_DECREF(row);
940 } else {
941 break;
942 }
943
944 if (++counter == maxrows) {
945 break;
946 }
947 }
948
949 if (PyErr_Occurred()) {
950 Py_DECREF(list);
951 return NULL;
952 } else {
953 return list;
954 }
955}
956
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000957PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000958{
959 PyObject* row;
960 PyObject* list;
961
962 list = PyList_New(0);
963 if (!list) {
964 return NULL;
965 }
966
967 /* just make sure we enter the loop */
968 row = (PyObject*)Py_None;
969
970 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000971 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000972 if (row) {
973 PyList_Append(list, row);
974 Py_DECREF(row);
975 }
976 }
977
978 if (PyErr_Occurred()) {
979 Py_DECREF(list);
980 return NULL;
981 } else {
982 return list;
983 }
984}
985
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000986PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000987{
988 /* don't care, return None */
989 Py_INCREF(Py_None);
990 return Py_None;
991}
992
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000993PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000994{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000995 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000996 return NULL;
997 }
998
999 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001000 (void)pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001001 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001002 }
1003
Gerhard Häringf9cee222010-03-05 15:20:03 +00001004 self->closed = 1;
1005
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001006 Py_INCREF(Py_None);
1007 return Py_None;
1008}
1009
1010static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001011 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001012 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001013 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001014 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001015 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001016 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001017 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001018 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001019 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001020 PyDoc_STR("Fetches several rows from the resultset.")},
1021 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1022 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001023 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024 PyDoc_STR("Closes the cursor.")},
1025 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1026 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1027 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1028 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1029 {NULL, NULL}
1030};
1031
1032static struct PyMemberDef cursor_members[] =
1033{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001034 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1035 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001036 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001037 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
Georg Brandlf78e02b2008-06-10 17:40:04 +00001038 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001039 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001040 {NULL}
1041};
1042
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043static char cursor_doc[] =
1044PyDoc_STR("SQLite database cursor class.");
1045
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001046PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001047 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001048 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001049 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001050 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001051 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001052 0, /* tp_print */
1053 0, /* tp_getattr */
1054 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001055 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001056 0, /* tp_repr */
1057 0, /* tp_as_number */
1058 0, /* tp_as_sequence */
1059 0, /* tp_as_mapping */
1060 0, /* tp_hash */
1061 0, /* tp_call */
1062 0, /* tp_str */
1063 0, /* tp_getattro */
1064 0, /* tp_setattro */
1065 0, /* tp_as_buffer */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001066 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001067 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001068 0, /* tp_traverse */
1069 0, /* tp_clear */
1070 0, /* tp_richcompare */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001071 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001072 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1073 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001074 cursor_methods, /* tp_methods */
1075 cursor_members, /* tp_members */
1076 0, /* tp_getset */
1077 0, /* tp_base */
1078 0, /* tp_dict */
1079 0, /* tp_descr_get */
1080 0, /* tp_descr_set */
1081 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001082 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083 0, /* tp_alloc */
1084 0, /* tp_new */
1085 0 /* tp_free */
1086};
1087
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001088extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001089{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001090 pysqlite_CursorType.tp_new = PyType_GenericNew;
1091 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092}