blob: c0c8746837d273d8fb420e98d49bd2f9b072330f [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"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000028PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000029
Gerhard Häringf9cee222010-03-05 15:20:03 +000030static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
31
Guido van Rossum6374bb52007-06-13 16:28:25 +000032static pysqlite_StatementKind detect_statement_type(const char* statement)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033{
34 char buf[20];
Guido van Rossum6374bb52007-06-13 16:28:25 +000035 const char* src;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036 char* dst;
37
38 src = statement;
39 /* skip over whitepace */
40 while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') {
41 src++;
42 }
43
44 if (*src == 0)
45 return STATEMENT_INVALID;
46
47 dst = buf;
48 *dst = 0;
Victor Stinnercfcde8c2014-08-17 21:09:30 +020049 while (Py_ISALPHA(*src) && (dst - buf) < ((Py_ssize_t)sizeof(buf) - 2)) {
Antoine Pitrou1665d2c2011-10-04 13:35:28 +020050 *dst++ = Py_TOLOWER(*src++);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000051 }
52
53 *dst = 0;
54
55 if (!strcmp(buf, "select")) {
56 return STATEMENT_SELECT;
57 } else if (!strcmp(buf, "insert")) {
58 return STATEMENT_INSERT;
59 } else if (!strcmp(buf, "update")) {
60 return STATEMENT_UPDATE;
61 } else if (!strcmp(buf, "delete")) {
62 return STATEMENT_DELETE;
63 } else if (!strcmp(buf, "replace")) {
64 return STATEMENT_REPLACE;
65 } else {
66 return STATEMENT_OTHER;
67 }
68}
69
Gerhard Häringf9cee222010-03-05 15:20:03 +000070static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000072 pysqlite_Connection* connection;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000074 if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000075 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000076 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077 }
78
79 Py_INCREF(connection);
80 self->connection = connection;
81 self->statement = NULL;
82 self->next_row = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +000083 self->in_weakreflist = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084
85 self->row_cast_map = PyList_New(0);
86 if (!self->row_cast_map) {
87 return -1;
88 }
89
90 Py_INCREF(Py_None);
91 self->description = Py_None;
92
93 Py_INCREF(Py_None);
94 self->lastrowid= Py_None;
95
96 self->arraysize = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +000097 self->closed = 0;
98 self->reset = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000099
Georg Brandlf78e02b2008-06-10 17:40:04 +0000100 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101
102 Py_INCREF(Py_None);
103 self->row_factory = Py_None;
104
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000105 if (!pysqlite_check_thread(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000106 return -1;
107 }
108
Gerhard Häringf9cee222010-03-05 15:20:03 +0000109 if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
110 return -1;
111 }
112
113 self->initialized = 1;
114
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 return 0;
116}
117
Gerhard Häringf9cee222010-03-05 15:20:03 +0000118static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120 /* Reset the statement if the user has not closed the cursor */
121 if (self->statement) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000122 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 Py_DECREF(self->statement);
124 }
125
126 Py_XDECREF(self->connection);
127 Py_XDECREF(self->row_cast_map);
128 Py_XDECREF(self->description);
129 Py_XDECREF(self->lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 Py_XDECREF(self->row_factory);
131 Py_XDECREF(self->next_row);
132
Gerhard Häringf9cee222010-03-05 15:20:03 +0000133 if (self->in_weakreflist != NULL) {
134 PyObject_ClearWeakRefs((PyObject*)self);
135 }
136
Christian Heimes90aa7642007-12-19 02:45:37 +0000137 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138}
139
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000140PyObject* _pysqlite_get_converter(PyObject* key)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000141{
142 PyObject* upcase_key;
143 PyObject* retval;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200144 _Py_IDENTIFIER(upper);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200146 upcase_key = _PyObject_CallMethodId(key, &PyId_upper, "");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000147 if (!upcase_key) {
148 return NULL;
149 }
150
151 retval = PyDict_GetItem(converters, upcase_key);
152 Py_DECREF(upcase_key);
153
154 return retval;
155}
156
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000157int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158{
159 int i;
160 const char* type_start = (const char*)-1;
161 const char* pos;
162
163 const char* colname;
164 const char* decltype;
165 PyObject* py_decltype;
166 PyObject* converter;
167 PyObject* key;
168
169 if (!self->connection->detect_types) {
170 return 0;
171 }
172
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200173 Py_SETREF(self->row_cast_map, PyList_New(0));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000174
175 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
176 converter = NULL;
177
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000178 if (self->connection->detect_types & PARSE_COLNAMES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000180 if (colname) {
181 for (pos = colname; *pos != 0; pos++) {
182 if (*pos == '[') {
183 type_start = pos + 1;
184 } else if (*pos == ']' && type_start != (const char*)-1) {
Brett Cannon40430012007-10-22 20:24:51 +0000185 key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000186 if (!key) {
187 /* creating a string failed, but it is too complicated
188 * to propagate the error here, we just assume there is
189 * no converter and proceed */
190 break;
191 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000192
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000193 converter = _pysqlite_get_converter(key);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000194 Py_DECREF(key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195 break;
196 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 }
199 }
200
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000201 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202 decltype = sqlite3_column_decltype(self->statement->st, i);
203 if (decltype) {
204 for (pos = decltype;;pos++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000205 /* Converter names are split at '(' and blanks.
206 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
207 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
208 * In other words, it will work as people expect it to work.*/
209 if (*pos == ' ' || *pos == '(' || *pos == 0) {
Brett Cannon40430012007-10-22 20:24:51 +0000210 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211 if (!py_decltype) {
212 return -1;
213 }
214 break;
215 }
216 }
217
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000218 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000219 Py_DECREF(py_decltype);
220 }
221 }
222
223 if (!converter) {
224 converter = Py_None;
225 }
226
227 if (PyList_Append(self->row_cast_map, converter) != 0) {
228 if (converter != Py_None) {
229 Py_DECREF(converter);
230 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200231 Py_CLEAR(self->row_cast_map);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232
233 return -1;
234 }
235 }
236
237 return 0;
238}
239
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000240PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241{
242 const char* pos;
243
244 if (!colname) {
245 Py_INCREF(Py_None);
246 return Py_None;
247 }
248
249 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250 if (*pos == 0 || *pos == '[') {
251 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
252 pos--;
253 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000254 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255 }
256 }
257}
258
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259/*
260 * Returns a row from the currently active SQLite statement
261 *
262 * Precondidition:
263 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
264 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000265PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266{
267 int i, numcols;
268 PyObject* row;
269 PyObject* item = NULL;
270 int coltype;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000271 PyObject* converter;
272 PyObject* converted;
273 Py_ssize_t nbytes;
274 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000275 const char* val_str;
276 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000277 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000278 PyObject* buf_bytes;
279 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000280
Gerhard Häringf9cee222010-03-05 15:20:03 +0000281 if (self->reset) {
282 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
283 return NULL;
284 }
285
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000286 Py_BEGIN_ALLOW_THREADS
287 numcols = sqlite3_data_count(self->statement->st);
288 Py_END_ALLOW_THREADS
289
290 row = PyTuple_New(numcols);
Victor Stinner297d1042014-06-26 23:32:00 +0200291 if (!row)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293
294 for (i = 0; i < numcols; i++) {
295 if (self->connection->detect_types) {
296 converter = PyList_GetItem(self->row_cast_map, i);
297 if (!converter) {
298 converter = Py_None;
299 }
300 } else {
301 converter = Py_None;
302 }
303
304 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000305 nbytes = sqlite3_column_bytes(self->statement->st, i);
306 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307 if (!val_str) {
308 Py_INCREF(Py_None);
309 converted = Py_None;
310 } else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000311 item = PyBytes_FromStringAndSize(val_str, nbytes);
Victor Stinner297d1042014-06-26 23:32:00 +0200312 if (!item)
313 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000314 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315 Py_DECREF(item);
Victor Stinner297d1042014-06-26 23:32:00 +0200316 if (!converted)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000317 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 }
319 } else {
320 Py_BEGIN_ALLOW_THREADS
321 coltype = sqlite3_column_type(self->statement->st, i);
322 Py_END_ALLOW_THREADS
323 if (coltype == SQLITE_NULL) {
324 Py_INCREF(Py_None);
325 converted = Py_None;
326 } else if (coltype == SQLITE_INTEGER) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200327 converted = _pysqlite_long_from_int64(sqlite3_column_int64(self->statement->st, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000328 } else if (coltype == SQLITE_FLOAT) {
329 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
330 } else if (coltype == SQLITE_TEXT) {
331 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
Petri Lehtinen023fe332012-02-01 22:18:19 +0200332 nbytes = sqlite3_column_bytes(self->statement->st, i);
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200333 if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) {
334 converted = PyUnicode_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335 if (!converted) {
Victor Stinner4abda5d2013-07-18 01:54:37 +0200336 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +0000337 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000338 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000339 colname = "<unknown column name>";
340 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000341 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000342 colname , val_str);
Victor Stinner86999502010-05-19 01:27:23 +0000343 buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000344 if (!buf_bytes) {
345 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
346 } else {
347 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
348 if (!error_obj) {
349 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000350 } else {
351 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000352 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000353 }
354 Py_DECREF(buf_bytes);
355 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000356 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000357 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200358 converted = PyBytes_FromStringAndSize(val_str, nbytes);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000359 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200360 converted = PyByteArray_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000361 } else {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200362 converted = PyObject_CallFunction(self->connection->text_factory, "y#", val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 }
364 } else {
365 /* coltype == SQLITE_BLOB */
366 nbytes = sqlite3_column_bytes(self->statement->st, i);
Christian Heimes72b710a2008-05-26 13:28:38 +0000367 buffer = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000368 sqlite3_column_blob(self->statement->st, i), nbytes);
Victor Stinner297d1042014-06-26 23:32:00 +0200369 if (!buffer)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000371 converted = buffer;
372 }
373 }
374
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000375 if (converted) {
376 PyTuple_SetItem(row, i, converted);
377 } else {
378 Py_INCREF(Py_None);
379 PyTuple_SetItem(row, i, Py_None);
380 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381 }
382
Victor Stinner297d1042014-06-26 23:32:00 +0200383 if (PyErr_Occurred())
384 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385
386 return row;
Victor Stinner297d1042014-06-26 23:32:00 +0200387
388error:
389 Py_DECREF(row);
390 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000391}
392
Gerhard Häringf9cee222010-03-05 15:20:03 +0000393/*
394 * Checks if a cursor object is usable.
395 *
396 * 0 => error; 1 => ok
397 */
398static int check_cursor(pysqlite_Cursor* cur)
399{
400 if (!cur->initialized) {
401 PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
402 return 0;
403 }
404
405 if (cur->closed) {
406 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
407 return 0;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000408 }
Gerhard Haering936d5182011-05-09 12:24:09 +0200409
410 if (cur->locked) {
411 PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
412 return 0;
413 }
414
415 return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000416}
417
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000418PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419{
420 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000421 const char* operation_cstr;
422 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423 PyObject* parameters_list = NULL;
424 PyObject* parameters_iter = NULL;
425 PyObject* parameters = NULL;
426 int i;
427 int rc;
428 PyObject* func_args;
429 PyObject* result;
430 int numcols;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431 int statement_type;
432 PyObject* descriptor;
433 PyObject* second_argument = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000434
Gerhard Häringf9cee222010-03-05 15:20:03 +0000435 if (!check_cursor(self)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200436 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 }
Georg Brandlf78e02b2008-06-10 17:40:04 +0000438
Gerhard Haering936d5182011-05-09 12:24:09 +0200439 self->locked = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000440 self->reset = 0;
441
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200442 Py_CLEAR(self->next_row);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443
444 if (multiple) {
445 /* executemany() */
446 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200447 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 }
449
Guido van Rossum98297ee2007-11-06 21:34:58 +0000450 if (!PyUnicode_Check(operation)) {
451 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200452 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000453 }
454
455 if (PyIter_Check(second_argument)) {
456 /* iterator */
457 Py_INCREF(second_argument);
458 parameters_iter = second_argument;
459 } else {
460 /* sequence */
461 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000462 if (!parameters_iter) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200463 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 }
465 }
466 } else {
467 /* execute() */
468 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200469 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000470 }
471
Guido van Rossum98297ee2007-11-06 21:34:58 +0000472 if (!PyUnicode_Check(operation)) {
473 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200474 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 }
476
477 parameters_list = PyList_New(0);
478 if (!parameters_list) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200479 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000480 }
481
482 if (second_argument == NULL) {
483 second_argument = PyTuple_New(0);
484 if (!second_argument) {
485 goto error;
486 }
487 } else {
488 Py_INCREF(second_argument);
489 }
490 if (PyList_Append(parameters_list, second_argument) != 0) {
491 Py_DECREF(second_argument);
492 goto error;
493 }
494 Py_DECREF(second_argument);
495
496 parameters_iter = PyObject_GetIter(parameters_list);
497 if (!parameters_iter) {
498 goto error;
499 }
500 }
501
502 if (self->statement != NULL) {
503 /* There is an active statement */
Brett Cannonb94767f2011-02-22 20:15:44 +0000504 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505 }
506
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000507 operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
Victor Stinner86999502010-05-19 01:27:23 +0000508 if (operation_cstr == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000509 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000510
Georg Brandlf78e02b2008-06-10 17:40:04 +0000511 /* reset description and rowcount */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512 Py_INCREF(Py_None);
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200513 Py_SETREF(self->description, Py_None);
Georg Brandlf78e02b2008-06-10 17:40:04 +0000514 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000516 func_args = PyTuple_New(1);
517 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000518 goto error;
519 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000520 Py_INCREF(operation);
521 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
522 goto error;
523 }
524
525 if (self->statement) {
526 (void)pysqlite_statement_reset(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000527 }
528
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200529 Py_SETREF(self->statement,
530 (pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args));
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000531 Py_DECREF(func_args);
532
533 if (!self->statement) {
534 goto error;
535 }
536
537 if (self->statement->in_use) {
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200538 Py_SETREF(self->statement,
539 PyObject_New(pysqlite_Statement, &pysqlite_StatementType));
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000540 if (!self->statement) {
541 goto error;
542 }
543 rc = pysqlite_statement_create(self->statement, self->connection, operation);
544 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000545 Py_CLEAR(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000546 goto error;
547 }
548 }
549
550 pysqlite_statement_reset(self->statement);
551 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000552
553 statement_type = detect_statement_type(operation_cstr);
554 if (self->connection->begin_statement) {
555 switch (statement_type) {
556 case STATEMENT_UPDATE:
557 case STATEMENT_DELETE:
558 case STATEMENT_INSERT:
559 case STATEMENT_REPLACE:
560 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000561 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562 if (!result) {
563 goto error;
564 }
565 Py_DECREF(result);
566 }
567 break;
568 case STATEMENT_OTHER:
569 /* it's a DDL statement or something similar
570 - we better COMMIT first so it works for all cases */
571 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000572 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000573 if (!result) {
574 goto error;
575 }
576 Py_DECREF(result);
577 }
578 break;
579 case STATEMENT_SELECT:
580 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000581 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000582 "You cannot execute SELECT statements in executemany().");
583 goto error;
584 }
585 break;
586 }
587 }
588
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589
590 while (1) {
591 parameters = PyIter_Next(parameters_iter);
592 if (!parameters) {
593 break;
594 }
595
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000596 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200598 pysqlite_statement_bind_parameters(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000599 if (PyErr_Occurred()) {
600 goto error;
601 }
602
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000603 /* Keep trying the SQL statement until the schema stops changing. */
604 while (1) {
605 /* Actually execute the SQL statement. */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000606 rc = pysqlite_step(self->statement->st, self->connection);
Victor Stinner297d1042014-06-26 23:32:00 +0200607 if (PyErr_Occurred()) {
608 (void)pysqlite_statement_reset(self->statement);
609 goto error;
610 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000611 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
612 /* If it worked, let's get out of the loop */
613 break;
614 }
615 /* Something went wrong. Re-set the statement and try again. */
616 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000617 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000618 /* If this was a result of the schema changing, let's try
619 again. */
620 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000622 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000624 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000625 (void)pysqlite_statement_reset(self->statement);
626 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000627 goto error;
628 }
629 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630 if (PyErr_Occurred()) {
631 /* there was an error that occurred in a user-defined callback */
632 if (_enable_callback_tracebacks) {
633 PyErr_Print();
634 } else {
635 PyErr_Clear();
636 }
637 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000638 (void)pysqlite_statement_reset(self->statement);
639 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000640 goto error;
641 }
642 }
643
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000644 if (pysqlite_build_row_cast_map(self) != 0) {
645 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
646 goto error;
647 }
648
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000649 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000651 Py_BEGIN_ALLOW_THREADS
652 numcols = sqlite3_column_count(self->statement->st);
653 Py_END_ALLOW_THREADS
654
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200655 Py_SETREF(self->description, PyTuple_New(numcols));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000656 if (!self->description) {
657 goto error;
658 }
659 for (i = 0; i < numcols; i++) {
660 descriptor = PyTuple_New(7);
661 if (!descriptor) {
662 goto error;
663 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000664 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000665 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
666 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
667 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
668 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
669 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
670 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
671 PyTuple_SetItem(self->description, i, descriptor);
672 }
673 }
674 }
675
676 if (rc == SQLITE_ROW) {
677 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000678 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000679 goto error;
680 }
681
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000682 self->next_row = _pysqlite_fetch_one_row(self);
Victor Stinner297d1042014-06-26 23:32:00 +0200683 if (self->next_row == NULL)
684 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000685 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000686 pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000687 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688 }
689
690 switch (statement_type) {
691 case STATEMENT_UPDATE:
692 case STATEMENT_DELETE:
693 case STATEMENT_INSERT:
694 case STATEMENT_REPLACE:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000695 if (self->rowcount == -1L) {
696 self->rowcount = 0L;
697 }
698 self->rowcount += (long)sqlite3_changes(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000699 }
700
701 Py_DECREF(self->lastrowid);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000702 if (!multiple && statement_type == STATEMENT_INSERT) {
Serhiy Storchakad160b122013-04-28 14:10:27 +0300703 sqlite_int64 lastrowid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 Py_BEGIN_ALLOW_THREADS
705 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
706 Py_END_ALLOW_THREADS
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200707 self->lastrowid = _pysqlite_long_from_int64(lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000708 } else {
709 Py_INCREF(Py_None);
710 self->lastrowid = Py_None;
711 }
712
713 if (multiple) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000714 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715 }
716 Py_XDECREF(parameters);
717 }
718
719error:
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000720 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
721 * ROLLBACK could have happened */
722 #ifdef SQLITE_VERSION_NUMBER
723 #if SQLITE_VERSION_NUMBER >= 3002002
Gerhard Haering936d5182011-05-09 12:24:09 +0200724 if (self->connection && self->connection->db)
725 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000726 #endif
727 #endif
728
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729 Py_XDECREF(parameters);
730 Py_XDECREF(parameters_iter);
731 Py_XDECREF(parameters_list);
732
Gerhard Haering936d5182011-05-09 12:24:09 +0200733 self->locked = 0;
734
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735 if (PyErr_Occurred()) {
Georg Brandlf78e02b2008-06-10 17:40:04 +0000736 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000737 return NULL;
738 } else {
739 Py_INCREF(self);
740 return (PyObject*)self;
741 }
742}
743
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000744PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000745{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000746 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000747}
748
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000749PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000750{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000751 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000752}
753
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000754PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000755{
756 PyObject* script_obj;
757 PyObject* script_str = NULL;
758 const char* script_cstr;
759 sqlite3_stmt* statement;
760 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761 PyObject* result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000762
763 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000764 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000765 }
766
Gerhard Häringf9cee222010-03-05 15:20:03 +0000767 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768 return NULL;
769 }
770
Gerhard Häringf9cee222010-03-05 15:20:03 +0000771 self->reset = 0;
772
Gerhard Häring6d214562007-08-10 18:15:11 +0000773 if (PyUnicode_Check(script_obj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000774 script_cstr = _PyUnicode_AsString(script_obj);
Gerhard Häring6d214562007-08-10 18:15:11 +0000775 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000776 return NULL;
777 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000778 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000779 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780 return NULL;
781 }
782
783 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000784 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000785 if (!result) {
786 goto error;
787 }
788 Py_DECREF(result);
789
790 while (1) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000791 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000792 rc = sqlite3_prepare(self->connection->db,
793 script_cstr,
794 -1,
795 &statement,
796 &script_cstr);
Benjamin Petersond7b03282008-09-13 15:58:53 +0000797 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000799 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000800 goto error;
801 }
802
803 /* execute statement, and ignore results of SELECT statements */
804 rc = SQLITE_ROW;
805 while (rc == SQLITE_ROW) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000806 rc = pysqlite_step(statement, self->connection);
Victor Stinner297d1042014-06-26 23:32:00 +0200807 if (PyErr_Occurred()) {
808 (void)sqlite3_finalize(statement);
809 goto error;
810 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000811 }
812
813 if (rc != SQLITE_DONE) {
814 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000815 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000816 goto error;
817 }
818
819 rc = sqlite3_finalize(statement);
820 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000821 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000822 goto error;
823 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000824
825 if (*script_cstr == (char)0) {
826 break;
827 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000828 }
829
830error:
831 Py_XDECREF(script_str);
832
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833 if (PyErr_Occurred()) {
834 return NULL;
835 } else {
836 Py_INCREF(self);
837 return (PyObject*)self;
838 }
839}
840
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000841PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000842{
843 Py_INCREF(self);
844 return (PyObject*)self;
845}
846
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000847PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848{
849 PyObject* next_row_tuple;
850 PyObject* next_row;
851 int rc;
852
Gerhard Häringf9cee222010-03-05 15:20:03 +0000853 if (!check_cursor(self)) {
854 return NULL;
855 }
856
857 if (self->reset) {
858 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000859 return NULL;
860 }
861
862 if (!self->next_row) {
863 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000864 (void)pysqlite_statement_reset(self->statement);
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200865 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866 }
867 return NULL;
868 }
869
870 next_row_tuple = self->next_row;
Victor Stinner85a12a82013-11-05 14:50:30 +0100871 assert(next_row_tuple != NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000872 self->next_row = NULL;
873
874 if (self->row_factory != Py_None) {
875 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
Victor Stinner85a12a82013-11-05 14:50:30 +0100876 if (next_row == NULL) {
877 self->next_row = next_row_tuple;
878 return NULL;
879 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880 Py_DECREF(next_row_tuple);
881 } else {
882 next_row = next_row_tuple;
883 }
884
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000885 if (self->statement) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000886 rc = pysqlite_step(self->statement->st, self->connection);
Victor Stinner297d1042014-06-26 23:32:00 +0200887 if (PyErr_Occurred()) {
888 (void)pysqlite_statement_reset(self->statement);
889 Py_DECREF(next_row);
890 return NULL;
891 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000892 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000893 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000894 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000895 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000896 return NULL;
897 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000898
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000899 if (rc == SQLITE_ROW) {
900 self->next_row = _pysqlite_fetch_one_row(self);
Victor Stinnerdd4b2992013-11-05 14:30:11 +0100901 if (self->next_row == NULL) {
902 (void)pysqlite_statement_reset(self->statement);
Victor Stinnerdd4b2992013-11-05 14:30:11 +0100903 return NULL;
904 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000905 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000906 }
907
908 return next_row;
909}
910
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000911PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000912{
913 PyObject* row;
914
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000915 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000916 if (!row && !PyErr_Occurred()) {
917 Py_INCREF(Py_None);
918 return Py_None;
919 }
920
921 return row;
922}
923
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000924PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000925{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000926 static char *kwlist[] = {"size", NULL, NULL};
927
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000928 PyObject* row;
929 PyObject* list;
930 int maxrows = self->arraysize;
931 int counter = 0;
932
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000933 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
934 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000935 }
936
937 list = PyList_New(0);
938 if (!list) {
939 return NULL;
940 }
941
942 /* just make sure we enter the loop */
943 row = Py_None;
944
945 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000946 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000947 if (row) {
948 PyList_Append(list, row);
949 Py_DECREF(row);
950 } else {
951 break;
952 }
953
954 if (++counter == maxrows) {
955 break;
956 }
957 }
958
959 if (PyErr_Occurred()) {
960 Py_DECREF(list);
961 return NULL;
962 } else {
963 return list;
964 }
965}
966
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000967PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000968{
969 PyObject* row;
970 PyObject* list;
971
972 list = PyList_New(0);
973 if (!list) {
974 return NULL;
975 }
976
977 /* just make sure we enter the loop */
978 row = (PyObject*)Py_None;
979
980 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000981 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000982 if (row) {
983 PyList_Append(list, row);
984 Py_DECREF(row);
985 }
986 }
987
988 if (PyErr_Occurred()) {
989 Py_DECREF(list);
990 return NULL;
991 } else {
992 return list;
993 }
994}
995
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000996PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000997{
998 /* don't care, return None */
999 Py_INCREF(Py_None);
1000 return Py_None;
1001}
1002
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001003PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001004{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001005 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001006 return NULL;
1007 }
1008
1009 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001010 (void)pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001011 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001012 }
1013
Gerhard Häringf9cee222010-03-05 15:20:03 +00001014 self->closed = 1;
1015
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001016 Py_INCREF(Py_None);
1017 return Py_None;
1018}
1019
1020static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001021 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001022 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001023 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001025 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001026 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001027 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001028 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001029 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001030 PyDoc_STR("Fetches several rows from the resultset.")},
1031 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1032 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001033 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001034 PyDoc_STR("Closes the cursor.")},
1035 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1036 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1037 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1038 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1039 {NULL, NULL}
1040};
1041
1042static struct PyMemberDef cursor_members[] =
1043{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001044 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1045 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001046 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001047 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
Georg Brandlf78e02b2008-06-10 17:40:04 +00001048 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001049 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001050 {NULL}
1051};
1052
Thomas Wouters477c8d52006-05-27 19:21:47 +00001053static char cursor_doc[] =
1054PyDoc_STR("SQLite database cursor class.");
1055
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001056PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001057 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001058 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001059 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001060 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001061 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001062 0, /* tp_print */
1063 0, /* tp_getattr */
1064 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001065 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001066 0, /* tp_repr */
1067 0, /* tp_as_number */
1068 0, /* tp_as_sequence */
1069 0, /* tp_as_mapping */
1070 0, /* tp_hash */
1071 0, /* tp_call */
1072 0, /* tp_str */
1073 0, /* tp_getattro */
1074 0, /* tp_setattro */
1075 0, /* tp_as_buffer */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001076 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001077 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001078 0, /* tp_traverse */
1079 0, /* tp_clear */
1080 0, /* tp_richcompare */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001081 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001082 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1083 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001084 cursor_methods, /* tp_methods */
1085 cursor_members, /* tp_members */
1086 0, /* tp_getset */
1087 0, /* tp_base */
1088 0, /* tp_dict */
1089 0, /* tp_descr_get */
1090 0, /* tp_descr_set */
1091 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001092 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001093 0, /* tp_alloc */
1094 0, /* tp_new */
1095 0 /* tp_free */
1096};
1097
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001098extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001099{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001100 pysqlite_CursorType.tp_new = PyType_GenericNew;
1101 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001102}