blob: 23f30575cdec7221c08d430c85ec2c0d4a953efc [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
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020030static const char errmsg_fetch_across_rollback[] = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
Gerhard Häringf9cee222010-03-05 15:20:03 +000031
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 Storchaka48842712016-04-06 09:45:48 +0300173 Py_XSETREF(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) {
Berker Peksagfe21de92016-04-09 07:34:39 +0300245 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 }
247
248 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000249 if (*pos == 0 || *pos == '[') {
250 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
251 pos--;
252 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000253 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254 }
255 }
256}
257
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000258/*
259 * Returns a row from the currently active SQLite statement
260 *
261 * Precondidition:
262 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
263 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000264PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000265{
266 int i, numcols;
267 PyObject* row;
268 PyObject* item = NULL;
269 int coltype;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000270 PyObject* converter;
271 PyObject* converted;
272 Py_ssize_t nbytes;
273 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274 const char* val_str;
275 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000276 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000277 PyObject* buf_bytes;
278 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000279
Gerhard Häringf9cee222010-03-05 15:20:03 +0000280 if (self->reset) {
281 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
282 return NULL;
283 }
284
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285 Py_BEGIN_ALLOW_THREADS
286 numcols = sqlite3_data_count(self->statement->st);
287 Py_END_ALLOW_THREADS
288
289 row = PyTuple_New(numcols);
Victor Stinner297d1042014-06-26 23:32:00 +0200290 if (!row)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292
293 for (i = 0; i < numcols; i++) {
294 if (self->connection->detect_types) {
295 converter = PyList_GetItem(self->row_cast_map, i);
296 if (!converter) {
297 converter = Py_None;
298 }
299 } else {
300 converter = Py_None;
301 }
302
303 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000304 nbytes = sqlite3_column_bytes(self->statement->st, i);
305 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000306 if (!val_str) {
307 Py_INCREF(Py_None);
308 converted = Py_None;
309 } else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000310 item = PyBytes_FromStringAndSize(val_str, nbytes);
Victor Stinner297d1042014-06-26 23:32:00 +0200311 if (!item)
312 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000314 Py_DECREF(item);
Victor Stinner297d1042014-06-26 23:32:00 +0200315 if (!converted)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000316 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317 }
318 } else {
319 Py_BEGIN_ALLOW_THREADS
320 coltype = sqlite3_column_type(self->statement->st, i);
321 Py_END_ALLOW_THREADS
322 if (coltype == SQLITE_NULL) {
323 Py_INCREF(Py_None);
324 converted = Py_None;
325 } else if (coltype == SQLITE_INTEGER) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200326 converted = _pysqlite_long_from_int64(sqlite3_column_int64(self->statement->st, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327 } else if (coltype == SQLITE_FLOAT) {
328 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
329 } else if (coltype == SQLITE_TEXT) {
330 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
Petri Lehtinen023fe332012-02-01 22:18:19 +0200331 nbytes = sqlite3_column_bytes(self->statement->st, i);
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200332 if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) {
333 converted = PyUnicode_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000334 if (!converted) {
Victor Stinner4abda5d2013-07-18 01:54:37 +0200335 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000337 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000338 colname = "<unknown column name>";
339 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000340 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000341 colname , val_str);
Victor Stinner86999502010-05-19 01:27:23 +0000342 buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000343 if (!buf_bytes) {
344 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
345 } else {
346 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
347 if (!error_obj) {
348 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000349 } else {
350 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000351 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000352 }
353 Py_DECREF(buf_bytes);
354 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000356 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200357 converted = PyBytes_FromStringAndSize(val_str, nbytes);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000358 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200359 converted = PyByteArray_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360 } else {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200361 converted = PyObject_CallFunction(self->connection->text_factory, "y#", val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000362 }
363 } else {
364 /* coltype == SQLITE_BLOB */
365 nbytes = sqlite3_column_bytes(self->statement->st, i);
Christian Heimes72b710a2008-05-26 13:28:38 +0000366 buffer = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000367 sqlite3_column_blob(self->statement->st, i), nbytes);
Victor Stinner297d1042014-06-26 23:32:00 +0200368 if (!buffer)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 converted = buffer;
371 }
372 }
373
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000374 if (converted) {
375 PyTuple_SetItem(row, i, converted);
376 } else {
377 Py_INCREF(Py_None);
378 PyTuple_SetItem(row, i, Py_None);
379 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000380 }
381
Victor Stinner297d1042014-06-26 23:32:00 +0200382 if (PyErr_Occurred())
383 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384
385 return row;
Victor Stinner297d1042014-06-26 23:32:00 +0200386
387error:
388 Py_DECREF(row);
389 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390}
391
Gerhard Häringf9cee222010-03-05 15:20:03 +0000392/*
393 * Checks if a cursor object is usable.
394 *
395 * 0 => error; 1 => ok
396 */
397static int check_cursor(pysqlite_Cursor* cur)
398{
399 if (!cur->initialized) {
400 PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
401 return 0;
402 }
403
404 if (cur->closed) {
405 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
406 return 0;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000407 }
Gerhard Haering936d5182011-05-09 12:24:09 +0200408
409 if (cur->locked) {
410 PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
411 return 0;
412 }
413
414 return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000415}
416
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000417PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000418{
419 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000420 const char* operation_cstr;
421 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000422 PyObject* parameters_list = NULL;
423 PyObject* parameters_iter = NULL;
424 PyObject* parameters = NULL;
425 int i;
426 int rc;
427 PyObject* func_args;
428 PyObject* result;
429 int numcols;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430 int statement_type;
431 PyObject* descriptor;
432 PyObject* second_argument = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000433
Gerhard Häringf9cee222010-03-05 15:20:03 +0000434 if (!check_cursor(self)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200435 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000436 }
Georg Brandlf78e02b2008-06-10 17:40:04 +0000437
Gerhard Haering936d5182011-05-09 12:24:09 +0200438 self->locked = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000439 self->reset = 0;
440
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200441 Py_CLEAR(self->next_row);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442
443 if (multiple) {
444 /* executemany() */
445 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200446 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000447 }
448
Guido van Rossum98297ee2007-11-06 21:34:58 +0000449 if (!PyUnicode_Check(operation)) {
450 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200451 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000452 }
453
454 if (PyIter_Check(second_argument)) {
455 /* iterator */
456 Py_INCREF(second_argument);
457 parameters_iter = second_argument;
458 } else {
459 /* sequence */
460 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000461 if (!parameters_iter) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200462 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463 }
464 }
465 } else {
466 /* execute() */
467 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200468 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469 }
470
Guido van Rossum98297ee2007-11-06 21:34:58 +0000471 if (!PyUnicode_Check(operation)) {
472 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200473 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474 }
475
476 parameters_list = PyList_New(0);
477 if (!parameters_list) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200478 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000479 }
480
481 if (second_argument == NULL) {
482 second_argument = PyTuple_New(0);
483 if (!second_argument) {
484 goto error;
485 }
486 } else {
487 Py_INCREF(second_argument);
488 }
489 if (PyList_Append(parameters_list, second_argument) != 0) {
490 Py_DECREF(second_argument);
491 goto error;
492 }
493 Py_DECREF(second_argument);
494
495 parameters_iter = PyObject_GetIter(parameters_list);
496 if (!parameters_iter) {
497 goto error;
498 }
499 }
500
501 if (self->statement != NULL) {
502 /* There is an active statement */
Brett Cannonb94767f2011-02-22 20:15:44 +0000503 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000504 }
505
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000506 operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
Victor Stinner86999502010-05-19 01:27:23 +0000507 if (operation_cstr == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000508 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509
Georg Brandlf78e02b2008-06-10 17:40:04 +0000510 /* reset description and rowcount */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511 Py_INCREF(Py_None);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300512 Py_SETREF(self->description, Py_None);
Georg Brandlf78e02b2008-06-10 17:40:04 +0000513 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000514
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000515 func_args = PyTuple_New(1);
516 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517 goto error;
518 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000519 Py_INCREF(operation);
520 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
521 goto error;
522 }
523
524 if (self->statement) {
525 (void)pysqlite_statement_reset(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000526 }
527
Serhiy Storchaka48842712016-04-06 09:45:48 +0300528 Py_XSETREF(self->statement,
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200529 (pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args));
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000530 Py_DECREF(func_args);
531
532 if (!self->statement) {
533 goto error;
534 }
535
536 if (self->statement->in_use) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300537 Py_SETREF(self->statement,
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200538 PyObject_New(pysqlite_Statement, &pysqlite_StatementType));
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000539 if (!self->statement) {
540 goto error;
541 }
542 rc = pysqlite_statement_create(self->statement, self->connection, operation);
543 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000544 Py_CLEAR(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000545 goto error;
546 }
547 }
548
549 pysqlite_statement_reset(self->statement);
550 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551
552 statement_type = detect_statement_type(operation_cstr);
553 if (self->connection->begin_statement) {
554 switch (statement_type) {
555 case STATEMENT_UPDATE:
556 case STATEMENT_DELETE:
557 case STATEMENT_INSERT:
558 case STATEMENT_REPLACE:
559 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000560 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000561 if (!result) {
562 goto error;
563 }
564 Py_DECREF(result);
565 }
566 break;
567 case STATEMENT_OTHER:
568 /* it's a DDL statement or something similar
569 - we better COMMIT first so it works for all cases */
570 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000571 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000572 if (!result) {
573 goto error;
574 }
575 Py_DECREF(result);
576 }
577 break;
578 case STATEMENT_SELECT:
579 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000580 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000581 "You cannot execute SELECT statements in executemany().");
582 goto error;
583 }
584 break;
585 }
586 }
587
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000588
589 while (1) {
590 parameters = PyIter_Next(parameters_iter);
591 if (!parameters) {
592 break;
593 }
594
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000595 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200597 pysqlite_statement_bind_parameters(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598 if (PyErr_Occurred()) {
599 goto error;
600 }
601
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000602 /* Keep trying the SQL statement until the schema stops changing. */
603 while (1) {
604 /* Actually execute the SQL statement. */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000605 rc = pysqlite_step(self->statement->st, self->connection);
Victor Stinner297d1042014-06-26 23:32:00 +0200606 if (PyErr_Occurred()) {
607 (void)pysqlite_statement_reset(self->statement);
608 goto error;
609 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000610 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
611 /* If it worked, let's get out of the loop */
612 break;
613 }
614 /* Something went wrong. Re-set the statement and try again. */
615 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000617 /* If this was a result of the schema changing, let's try
618 again. */
619 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000621 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000623 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000624 (void)pysqlite_statement_reset(self->statement);
625 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000626 goto error;
627 }
628 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000629 if (PyErr_Occurred()) {
630 /* there was an error that occurred in a user-defined callback */
631 if (_enable_callback_tracebacks) {
632 PyErr_Print();
633 } else {
634 PyErr_Clear();
635 }
636 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000637 (void)pysqlite_statement_reset(self->statement);
638 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000639 goto error;
640 }
641 }
642
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000643 if (pysqlite_build_row_cast_map(self) != 0) {
644 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
645 goto error;
646 }
647
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000649 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000650 Py_BEGIN_ALLOW_THREADS
651 numcols = sqlite3_column_count(self->statement->st);
652 Py_END_ALLOW_THREADS
653
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300654 Py_SETREF(self->description, PyTuple_New(numcols));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655 if (!self->description) {
656 goto error;
657 }
658 for (i = 0; i < numcols; i++) {
659 descriptor = PyTuple_New(7);
660 if (!descriptor) {
661 goto error;
662 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000663 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000664 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
665 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
666 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
667 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
668 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
669 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
670 PyTuple_SetItem(self->description, i, descriptor);
671 }
672 }
673 }
674
675 if (rc == SQLITE_ROW) {
676 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000677 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678 goto error;
679 }
680
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000681 self->next_row = _pysqlite_fetch_one_row(self);
Victor Stinner297d1042014-06-26 23:32:00 +0200682 if (self->next_row == NULL)
683 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000685 pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000686 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000687 }
688
689 switch (statement_type) {
690 case STATEMENT_UPDATE:
691 case STATEMENT_DELETE:
692 case STATEMENT_INSERT:
693 case STATEMENT_REPLACE:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000694 if (self->rowcount == -1L) {
695 self->rowcount = 0L;
696 }
697 self->rowcount += (long)sqlite3_changes(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000698 }
699
700 Py_DECREF(self->lastrowid);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000701 if (!multiple && statement_type == STATEMENT_INSERT) {
Serhiy Storchakad160b122013-04-28 14:10:27 +0300702 sqlite_int64 lastrowid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000703 Py_BEGIN_ALLOW_THREADS
704 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
705 Py_END_ALLOW_THREADS
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200706 self->lastrowid = _pysqlite_long_from_int64(lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707 } else {
708 Py_INCREF(Py_None);
709 self->lastrowid = Py_None;
710 }
711
712 if (multiple) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000713 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000714 }
715 Py_XDECREF(parameters);
716 }
717
718error:
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000719 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
720 * ROLLBACK could have happened */
721 #ifdef SQLITE_VERSION_NUMBER
722 #if SQLITE_VERSION_NUMBER >= 3002002
Gerhard Haering936d5182011-05-09 12:24:09 +0200723 if (self->connection && self->connection->db)
724 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000725 #endif
726 #endif
727
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000728 Py_XDECREF(parameters);
729 Py_XDECREF(parameters_iter);
730 Py_XDECREF(parameters_list);
731
Gerhard Haering936d5182011-05-09 12:24:09 +0200732 self->locked = 0;
733
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734 if (PyErr_Occurred()) {
Georg Brandlf78e02b2008-06-10 17:40:04 +0000735 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000736 return NULL;
737 } else {
738 Py_INCREF(self);
739 return (PyObject*)self;
740 }
741}
742
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000743PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000745 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000746}
747
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000748PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000749{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000750 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751}
752
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000753PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000754{
755 PyObject* script_obj;
756 PyObject* script_str = NULL;
757 const char* script_cstr;
758 sqlite3_stmt* statement;
759 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000760 PyObject* result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761
762 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000763 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000764 }
765
Gerhard Häringf9cee222010-03-05 15:20:03 +0000766 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767 return NULL;
768 }
769
Gerhard Häringf9cee222010-03-05 15:20:03 +0000770 self->reset = 0;
771
Gerhard Häring6d214562007-08-10 18:15:11 +0000772 if (PyUnicode_Check(script_obj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000773 script_cstr = _PyUnicode_AsString(script_obj);
Gerhard Häring6d214562007-08-10 18:15:11 +0000774 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775 return NULL;
776 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000777 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000778 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000779 return NULL;
780 }
781
782 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000783 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000784 if (!result) {
785 goto error;
786 }
787 Py_DECREF(result);
788
789 while (1) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000790 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791 rc = sqlite3_prepare(self->connection->db,
792 script_cstr,
793 -1,
794 &statement,
795 &script_cstr);
Benjamin Petersond7b03282008-09-13 15:58:53 +0000796 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000797 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000798 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000799 goto error;
800 }
801
802 /* execute statement, and ignore results of SELECT statements */
803 rc = SQLITE_ROW;
804 while (rc == SQLITE_ROW) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000805 rc = pysqlite_step(statement, self->connection);
Victor Stinner297d1042014-06-26 23:32:00 +0200806 if (PyErr_Occurred()) {
807 (void)sqlite3_finalize(statement);
808 goto error;
809 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000810 }
811
812 if (rc != SQLITE_DONE) {
813 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000814 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815 goto error;
816 }
817
818 rc = sqlite3_finalize(statement);
819 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000820 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000821 goto error;
822 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000823
824 if (*script_cstr == (char)0) {
825 break;
826 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827 }
828
829error:
830 Py_XDECREF(script_str);
831
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000832 if (PyErr_Occurred()) {
833 return NULL;
834 } else {
835 Py_INCREF(self);
836 return (PyObject*)self;
837 }
838}
839
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000840PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000841{
842 Py_INCREF(self);
843 return (PyObject*)self;
844}
845
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000846PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000847{
848 PyObject* next_row_tuple;
849 PyObject* next_row;
850 int rc;
851
Gerhard Häringf9cee222010-03-05 15:20:03 +0000852 if (!check_cursor(self)) {
853 return NULL;
854 }
855
856 if (self->reset) {
857 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858 return NULL;
859 }
860
861 if (!self->next_row) {
862 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000863 (void)pysqlite_statement_reset(self->statement);
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200864 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865 }
866 return NULL;
867 }
868
869 next_row_tuple = self->next_row;
Victor Stinner85a12a82013-11-05 14:50:30 +0100870 assert(next_row_tuple != NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000871 self->next_row = NULL;
872
873 if (self->row_factory != Py_None) {
874 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
Victor Stinner85a12a82013-11-05 14:50:30 +0100875 if (next_row == NULL) {
876 self->next_row = next_row_tuple;
877 return NULL;
878 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000879 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);
Victor Stinner297d1042014-06-26 23:32:00 +0200886 if (PyErr_Occurred()) {
887 (void)pysqlite_statement_reset(self->statement);
888 Py_DECREF(next_row);
889 return NULL;
890 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000891 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000892 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000893 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000894 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000895 return NULL;
896 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000897
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000898 if (rc == SQLITE_ROW) {
899 self->next_row = _pysqlite_fetch_one_row(self);
Victor Stinnerdd4b2992013-11-05 14:30:11 +0100900 if (self->next_row == NULL) {
901 (void)pysqlite_statement_reset(self->statement);
Victor Stinnerdd4b2992013-11-05 14:30:11 +0100902 return NULL;
903 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000904 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905 }
906
907 return next_row;
908}
909
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000910PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000911{
912 PyObject* row;
913
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000914 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000915 if (!row && !PyErr_Occurred()) {
Berker Peksagfe21de92016-04-09 07:34:39 +0300916 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000917 }
918
919 return row;
920}
921
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000922PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000924 static char *kwlist[] = {"size", NULL, NULL};
925
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000926 PyObject* row;
927 PyObject* list;
928 int maxrows = self->arraysize;
929 int counter = 0;
930
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000931 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
932 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933 }
934
935 list = PyList_New(0);
936 if (!list) {
937 return NULL;
938 }
939
940 /* just make sure we enter the loop */
941 row = Py_None;
942
943 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000944 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000945 if (row) {
946 PyList_Append(list, row);
947 Py_DECREF(row);
948 } else {
949 break;
950 }
951
952 if (++counter == maxrows) {
953 break;
954 }
955 }
956
957 if (PyErr_Occurred()) {
958 Py_DECREF(list);
959 return NULL;
960 } else {
961 return list;
962 }
963}
964
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000965PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000966{
967 PyObject* row;
968 PyObject* list;
969
970 list = PyList_New(0);
971 if (!list) {
972 return NULL;
973 }
974
975 /* just make sure we enter the loop */
976 row = (PyObject*)Py_None;
977
978 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000979 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000980 if (row) {
981 PyList_Append(list, row);
982 Py_DECREF(row);
983 }
984 }
985
986 if (PyErr_Occurred()) {
987 Py_DECREF(list);
988 return NULL;
989 } else {
990 return list;
991 }
992}
993
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000994PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000995{
996 /* don't care, return None */
Berker Peksagfe21de92016-04-09 07:34:39 +0300997 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998}
999
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001000PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001001{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001002 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001003 return NULL;
1004 }
1005
1006 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001007 (void)pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001008 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001009 }
1010
Gerhard Häringf9cee222010-03-05 15:20:03 +00001011 self->closed = 1;
1012
Berker Peksagfe21de92016-04-09 07:34:39 +03001013 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001014}
1015
1016static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001017 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001018 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001019 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001021 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001022 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001023 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001025 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001026 PyDoc_STR("Fetches several rows from the resultset.")},
1027 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1028 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001029 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001030 PyDoc_STR("Closes the cursor.")},
1031 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1032 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1033 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1034 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1035 {NULL, NULL}
1036};
1037
1038static struct PyMemberDef cursor_members[] =
1039{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001040 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1041 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001042 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001043 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
Georg Brandlf78e02b2008-06-10 17:40:04 +00001044 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001045 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001046 {NULL}
1047};
1048
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001049static const char cursor_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001050PyDoc_STR("SQLite database cursor class.");
1051
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001052PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001053 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001054 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001055 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001056 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001057 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001058 0, /* tp_print */
1059 0, /* tp_getattr */
1060 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001061 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001062 0, /* tp_repr */
1063 0, /* tp_as_number */
1064 0, /* tp_as_sequence */
1065 0, /* tp_as_mapping */
1066 0, /* tp_hash */
1067 0, /* tp_call */
1068 0, /* tp_str */
1069 0, /* tp_getattro */
1070 0, /* tp_setattro */
1071 0, /* tp_as_buffer */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001072 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001073 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001074 0, /* tp_traverse */
1075 0, /* tp_clear */
1076 0, /* tp_richcompare */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001077 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001078 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1079 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001080 cursor_methods, /* tp_methods */
1081 cursor_members, /* tp_members */
1082 0, /* tp_getset */
1083 0, /* tp_base */
1084 0, /* tp_dict */
1085 0, /* tp_descr_get */
1086 0, /* tp_descr_set */
1087 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001088 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001089 0, /* tp_alloc */
1090 0, /* tp_new */
1091 0 /* tp_free */
1092};
1093
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001094extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001095{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001096 pysqlite_CursorType.tp_new = PyType_GenericNew;
1097 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098}