blob: 7fe00e32d2fa1eed9fb73926fed90419b428591a [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
173 Py_XDECREF(self->row_cast_map);
174 self->row_cast_map = PyList_New(0);
175
176 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
177 converter = NULL;
178
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000179 if (self->connection->detect_types & PARSE_COLNAMES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000180 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000181 if (colname) {
182 for (pos = colname; *pos != 0; pos++) {
183 if (*pos == '[') {
184 type_start = pos + 1;
185 } else if (*pos == ']' && type_start != (const char*)-1) {
Brett Cannon40430012007-10-22 20:24:51 +0000186 key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000187 if (!key) {
188 /* creating a string failed, but it is too complicated
189 * to propagate the error here, we just assume there is
190 * no converter and proceed */
191 break;
192 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000194 converter = _pysqlite_get_converter(key);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000195 Py_DECREF(key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196 break;
197 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000199 }
200 }
201
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000202 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000203 decltype = sqlite3_column_decltype(self->statement->st, i);
204 if (decltype) {
205 for (pos = decltype;;pos++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000206 /* Converter names are split at '(' and blanks.
207 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
208 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
209 * In other words, it will work as people expect it to work.*/
210 if (*pos == ' ' || *pos == '(' || *pos == 0) {
Brett Cannon40430012007-10-22 20:24:51 +0000211 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212 if (!py_decltype) {
213 return -1;
214 }
215 break;
216 }
217 }
218
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000219 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220 Py_DECREF(py_decltype);
221 }
222 }
223
224 if (!converter) {
225 converter = Py_None;
226 }
227
228 if (PyList_Append(self->row_cast_map, converter) != 0) {
229 if (converter != Py_None) {
230 Py_DECREF(converter);
231 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200232 Py_CLEAR(self->row_cast_map);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000233
234 return -1;
235 }
236 }
237
238 return 0;
239}
240
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000241PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242{
243 const char* pos;
244
245 if (!colname) {
246 Py_INCREF(Py_None);
247 return Py_None;
248 }
249
250 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000251 if (*pos == 0 || *pos == '[') {
252 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
253 pos--;
254 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000255 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256 }
257 }
258}
259
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260/*
261 * Returns a row from the currently active SQLite statement
262 *
263 * Precondidition:
264 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
265 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000266PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267{
268 int i, numcols;
269 PyObject* row;
270 PyObject* item = NULL;
271 int coltype;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000272 PyObject* converter;
273 PyObject* converted;
274 Py_ssize_t nbytes;
275 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000276 const char* val_str;
277 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000278 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000279 PyObject* buf_bytes;
280 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000281
Gerhard Häringf9cee222010-03-05 15:20:03 +0000282 if (self->reset) {
283 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
284 return NULL;
285 }
286
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 Py_BEGIN_ALLOW_THREADS
288 numcols = sqlite3_data_count(self->statement->st);
289 Py_END_ALLOW_THREADS
290
291 row = PyTuple_New(numcols);
Victor Stinner297d1042014-06-26 23:32:00 +0200292 if (!row)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294
295 for (i = 0; i < numcols; i++) {
296 if (self->connection->detect_types) {
297 converter = PyList_GetItem(self->row_cast_map, i);
298 if (!converter) {
299 converter = Py_None;
300 }
301 } else {
302 converter = Py_None;
303 }
304
305 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306 nbytes = sqlite3_column_bytes(self->statement->st, i);
307 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308 if (!val_str) {
309 Py_INCREF(Py_None);
310 converted = Py_None;
311 } else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000312 item = PyBytes_FromStringAndSize(val_str, nbytes);
Victor Stinner297d1042014-06-26 23:32:00 +0200313 if (!item)
314 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316 Py_DECREF(item);
Victor Stinner297d1042014-06-26 23:32:00 +0200317 if (!converted)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000318 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 }
320 } else {
321 Py_BEGIN_ALLOW_THREADS
322 coltype = sqlite3_column_type(self->statement->st, i);
323 Py_END_ALLOW_THREADS
324 if (coltype == SQLITE_NULL) {
325 Py_INCREF(Py_None);
326 converted = Py_None;
327 } else if (coltype == SQLITE_INTEGER) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200328 converted = _pysqlite_long_from_int64(sqlite3_column_int64(self->statement->st, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000329 } else if (coltype == SQLITE_FLOAT) {
330 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
331 } else if (coltype == SQLITE_TEXT) {
332 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
Petri Lehtinen023fe332012-02-01 22:18:19 +0200333 nbytes = sqlite3_column_bytes(self->statement->st, i);
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200334 if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) {
335 converted = PyUnicode_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000336 if (!converted) {
Victor Stinner4abda5d2013-07-18 01:54:37 +0200337#ifdef Py_DEBUG
338 /* in debug mode, type_call() fails with an assertion
339 error if an exception is set when it is called */
340 PyErr_Clear();
341#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000342 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000343 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000344 colname = "<unknown column name>";
345 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000346 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000347 colname , val_str);
Victor Stinner86999502010-05-19 01:27:23 +0000348 buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000349 if (!buf_bytes) {
350 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
351 } else {
352 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
353 if (!error_obj) {
354 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000355 } else {
356 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000357 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000358 }
359 Py_DECREF(buf_bytes);
360 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000361 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000362 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200363 converted = PyBytes_FromStringAndSize(val_str, nbytes);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000364 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200365 converted = PyByteArray_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 } else {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200367 converted = PyObject_CallFunction(self->connection->text_factory, "y#", val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 }
369 } else {
370 /* coltype == SQLITE_BLOB */
371 nbytes = sqlite3_column_bytes(self->statement->st, i);
Christian Heimes72b710a2008-05-26 13:28:38 +0000372 buffer = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000373 sqlite3_column_blob(self->statement->st, i), nbytes);
Victor Stinner297d1042014-06-26 23:32:00 +0200374 if (!buffer)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000375 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000376 converted = buffer;
377 }
378 }
379
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000380 if (converted) {
381 PyTuple_SetItem(row, i, converted);
382 } else {
383 Py_INCREF(Py_None);
384 PyTuple_SetItem(row, i, Py_None);
385 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 }
387
Victor Stinner297d1042014-06-26 23:32:00 +0200388 if (PyErr_Occurred())
389 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390
391 return row;
Victor Stinner297d1042014-06-26 23:32:00 +0200392
393error:
394 Py_DECREF(row);
395 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000396}
397
Gerhard Häringf9cee222010-03-05 15:20:03 +0000398/*
399 * Checks if a cursor object is usable.
400 *
401 * 0 => error; 1 => ok
402 */
403static int check_cursor(pysqlite_Cursor* cur)
404{
405 if (!cur->initialized) {
406 PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
407 return 0;
408 }
409
410 if (cur->closed) {
411 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
412 return 0;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000413 }
Gerhard Haering936d5182011-05-09 12:24:09 +0200414
415 if (cur->locked) {
416 PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
417 return 0;
418 }
419
420 return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000421}
422
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000423PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424{
425 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000426 const char* operation_cstr;
427 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000428 PyObject* parameters_list = NULL;
429 PyObject* parameters_iter = NULL;
430 PyObject* parameters = NULL;
431 int i;
432 int rc;
433 PyObject* func_args;
434 PyObject* result;
435 int numcols;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000436 int statement_type;
437 PyObject* descriptor;
438 PyObject* second_argument = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439
Gerhard Häringf9cee222010-03-05 15:20:03 +0000440 if (!check_cursor(self)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200441 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442 }
Georg Brandlf78e02b2008-06-10 17:40:04 +0000443
Gerhard Haering936d5182011-05-09 12:24:09 +0200444 self->locked = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000445 self->reset = 0;
446
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200447 Py_CLEAR(self->next_row);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448
449 if (multiple) {
450 /* executemany() */
451 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200452 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000453 }
454
Guido van Rossum98297ee2007-11-06 21:34:58 +0000455 if (!PyUnicode_Check(operation)) {
456 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200457 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000458 }
459
460 if (PyIter_Check(second_argument)) {
461 /* iterator */
462 Py_INCREF(second_argument);
463 parameters_iter = second_argument;
464 } else {
465 /* sequence */
466 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000467 if (!parameters_iter) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200468 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469 }
470 }
471 } else {
472 /* execute() */
473 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200474 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 }
476
Guido van Rossum98297ee2007-11-06 21:34:58 +0000477 if (!PyUnicode_Check(operation)) {
478 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200479 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000480 }
481
482 parameters_list = PyList_New(0);
483 if (!parameters_list) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200484 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000485 }
486
487 if (second_argument == NULL) {
488 second_argument = PyTuple_New(0);
489 if (!second_argument) {
490 goto error;
491 }
492 } else {
493 Py_INCREF(second_argument);
494 }
495 if (PyList_Append(parameters_list, second_argument) != 0) {
496 Py_DECREF(second_argument);
497 goto error;
498 }
499 Py_DECREF(second_argument);
500
501 parameters_iter = PyObject_GetIter(parameters_list);
502 if (!parameters_iter) {
503 goto error;
504 }
505 }
506
507 if (self->statement != NULL) {
508 /* There is an active statement */
Brett Cannonb94767f2011-02-22 20:15:44 +0000509 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000510 }
511
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000512 operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
Victor Stinner86999502010-05-19 01:27:23 +0000513 if (operation_cstr == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000514 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515
Georg Brandlf78e02b2008-06-10 17:40:04 +0000516 /* reset description and rowcount */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517 Py_DECREF(self->description);
518 Py_INCREF(Py_None);
519 self->description = Py_None;
Georg Brandlf78e02b2008-06-10 17:40:04 +0000520 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000521
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000522 func_args = PyTuple_New(1);
523 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000524 goto error;
525 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000526 Py_INCREF(operation);
527 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
528 goto error;
529 }
530
531 if (self->statement) {
532 (void)pysqlite_statement_reset(self->statement);
533 Py_DECREF(self->statement);
534 }
535
536 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
537 Py_DECREF(func_args);
538
539 if (!self->statement) {
540 goto error;
541 }
542
543 if (self->statement->in_use) {
544 Py_DECREF(self->statement);
545 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
546 if (!self->statement) {
547 goto error;
548 }
549 rc = pysqlite_statement_create(self->statement, self->connection, operation);
550 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000551 Py_CLEAR(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000552 goto error;
553 }
554 }
555
556 pysqlite_statement_reset(self->statement);
557 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000558
559 statement_type = detect_statement_type(operation_cstr);
560 if (self->connection->begin_statement) {
561 switch (statement_type) {
562 case STATEMENT_UPDATE:
563 case STATEMENT_DELETE:
564 case STATEMENT_INSERT:
565 case STATEMENT_REPLACE:
566 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000567 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000568 if (!result) {
569 goto error;
570 }
571 Py_DECREF(result);
572 }
573 break;
574 case STATEMENT_OTHER:
575 /* it's a DDL statement or something similar
576 - we better COMMIT first so it works for all cases */
577 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000578 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 if (!result) {
580 goto error;
581 }
582 Py_DECREF(result);
583 }
584 break;
585 case STATEMENT_SELECT:
586 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000587 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000588 "You cannot execute SELECT statements in executemany().");
589 goto error;
590 }
591 break;
592 }
593 }
594
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595
596 while (1) {
597 parameters = PyIter_Next(parameters_iter);
598 if (!parameters) {
599 break;
600 }
601
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000602 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000603
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200604 pysqlite_statement_bind_parameters(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000605 if (PyErr_Occurred()) {
606 goto error;
607 }
608
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000609 /* Keep trying the SQL statement until the schema stops changing. */
610 while (1) {
611 /* Actually execute the SQL statement. */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000612 rc = pysqlite_step(self->statement->st, self->connection);
Victor Stinner297d1042014-06-26 23:32:00 +0200613 if (PyErr_Occurred()) {
614 (void)pysqlite_statement_reset(self->statement);
615 goto error;
616 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000617 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
618 /* If it worked, let's get out of the loop */
619 break;
620 }
621 /* Something went wrong. Re-set the statement and try again. */
622 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000624 /* If this was a result of the schema changing, let's try
625 again. */
626 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000627 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000628 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000630 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000631 (void)pysqlite_statement_reset(self->statement);
632 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000633 goto error;
634 }
635 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000636 if (PyErr_Occurred()) {
637 /* there was an error that occurred in a user-defined callback */
638 if (_enable_callback_tracebacks) {
639 PyErr_Print();
640 } else {
641 PyErr_Clear();
642 }
643 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000644 (void)pysqlite_statement_reset(self->statement);
645 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646 goto error;
647 }
648 }
649
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000650 if (pysqlite_build_row_cast_map(self) != 0) {
651 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
652 goto error;
653 }
654
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000656 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000657 Py_BEGIN_ALLOW_THREADS
658 numcols = sqlite3_column_count(self->statement->st);
659 Py_END_ALLOW_THREADS
660
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661 Py_DECREF(self->description);
662 self->description = PyTuple_New(numcols);
663 if (!self->description) {
664 goto error;
665 }
666 for (i = 0; i < numcols; i++) {
667 descriptor = PyTuple_New(7);
668 if (!descriptor) {
669 goto error;
670 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000671 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
673 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
674 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
675 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
676 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
677 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
678 PyTuple_SetItem(self->description, i, descriptor);
679 }
680 }
681 }
682
683 if (rc == SQLITE_ROW) {
684 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000685 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000686 goto error;
687 }
688
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000689 self->next_row = _pysqlite_fetch_one_row(self);
Victor Stinner297d1042014-06-26 23:32:00 +0200690 if (self->next_row == NULL)
691 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000693 pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000694 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000695 }
696
697 switch (statement_type) {
698 case STATEMENT_UPDATE:
699 case STATEMENT_DELETE:
700 case STATEMENT_INSERT:
701 case STATEMENT_REPLACE:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000702 if (self->rowcount == -1L) {
703 self->rowcount = 0L;
704 }
705 self->rowcount += (long)sqlite3_changes(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706 }
707
708 Py_DECREF(self->lastrowid);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000709 if (!multiple && statement_type == STATEMENT_INSERT) {
Serhiy Storchakad160b122013-04-28 14:10:27 +0300710 sqlite_int64 lastrowid;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 Py_BEGIN_ALLOW_THREADS
712 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
713 Py_END_ALLOW_THREADS
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200714 self->lastrowid = _pysqlite_long_from_int64(lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715 } else {
716 Py_INCREF(Py_None);
717 self->lastrowid = Py_None;
718 }
719
720 if (multiple) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000721 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722 }
723 Py_XDECREF(parameters);
724 }
725
726error:
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000727 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
728 * ROLLBACK could have happened */
729 #ifdef SQLITE_VERSION_NUMBER
730 #if SQLITE_VERSION_NUMBER >= 3002002
Gerhard Haering936d5182011-05-09 12:24:09 +0200731 if (self->connection && self->connection->db)
732 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000733 #endif
734 #endif
735
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000736 Py_XDECREF(parameters);
737 Py_XDECREF(parameters_iter);
738 Py_XDECREF(parameters_list);
739
Gerhard Haering936d5182011-05-09 12:24:09 +0200740 self->locked = 0;
741
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000742 if (PyErr_Occurred()) {
Georg Brandlf78e02b2008-06-10 17:40:04 +0000743 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744 return NULL;
745 } else {
746 Py_INCREF(self);
747 return (PyObject*)self;
748 }
749}
750
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000751PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000752{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000753 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000754}
755
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000756PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000758 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000759}
760
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000761PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000762{
763 PyObject* script_obj;
764 PyObject* script_str = NULL;
765 const char* script_cstr;
766 sqlite3_stmt* statement;
767 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768 PyObject* result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000769
770 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000771 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000772 }
773
Gerhard Häringf9cee222010-03-05 15:20:03 +0000774 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775 return NULL;
776 }
777
Gerhard Häringf9cee222010-03-05 15:20:03 +0000778 self->reset = 0;
779
Gerhard Häring6d214562007-08-10 18:15:11 +0000780 if (PyUnicode_Check(script_obj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000781 script_cstr = _PyUnicode_AsString(script_obj);
Gerhard Häring6d214562007-08-10 18:15:11 +0000782 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000783 return NULL;
784 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000785 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000786 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787 return NULL;
788 }
789
790 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000791 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000792 if (!result) {
793 goto error;
794 }
795 Py_DECREF(result);
796
797 while (1) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000798 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000799 rc = sqlite3_prepare(self->connection->db,
800 script_cstr,
801 -1,
802 &statement,
803 &script_cstr);
Benjamin Petersond7b03282008-09-13 15:58:53 +0000804 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000806 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000807 goto error;
808 }
809
810 /* execute statement, and ignore results of SELECT statements */
811 rc = SQLITE_ROW;
812 while (rc == SQLITE_ROW) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000813 rc = pysqlite_step(statement, self->connection);
Victor Stinner297d1042014-06-26 23:32:00 +0200814 if (PyErr_Occurred()) {
815 (void)sqlite3_finalize(statement);
816 goto error;
817 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818 }
819
820 if (rc != SQLITE_DONE) {
821 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000822 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000823 goto error;
824 }
825
826 rc = sqlite3_finalize(statement);
827 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000828 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000829 goto error;
830 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000831
832 if (*script_cstr == (char)0) {
833 break;
834 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835 }
836
837error:
838 Py_XDECREF(script_str);
839
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840 if (PyErr_Occurred()) {
841 return NULL;
842 } else {
843 Py_INCREF(self);
844 return (PyObject*)self;
845 }
846}
847
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000848PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000849{
850 Py_INCREF(self);
851 return (PyObject*)self;
852}
853
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000854PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000855{
856 PyObject* next_row_tuple;
857 PyObject* next_row;
858 int rc;
859
Gerhard Häringf9cee222010-03-05 15:20:03 +0000860 if (!check_cursor(self)) {
861 return NULL;
862 }
863
864 if (self->reset) {
865 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866 return NULL;
867 }
868
869 if (!self->next_row) {
870 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000871 (void)pysqlite_statement_reset(self->statement);
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200872 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000873 }
874 return NULL;
875 }
876
877 next_row_tuple = self->next_row;
Victor Stinner85a12a82013-11-05 14:50:30 +0100878 assert(next_row_tuple != NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000879 self->next_row = NULL;
880
881 if (self->row_factory != Py_None) {
882 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
Victor Stinner85a12a82013-11-05 14:50:30 +0100883 if (next_row == NULL) {
884 self->next_row = next_row_tuple;
885 return NULL;
886 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000887 Py_DECREF(next_row_tuple);
888 } else {
889 next_row = next_row_tuple;
890 }
891
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000892 if (self->statement) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000893 rc = pysqlite_step(self->statement->st, self->connection);
Victor Stinner297d1042014-06-26 23:32:00 +0200894 if (PyErr_Occurred()) {
895 (void)pysqlite_statement_reset(self->statement);
896 Py_DECREF(next_row);
897 return NULL;
898 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000899 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000900 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000901 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000902 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000903 return NULL;
904 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000906 if (rc == SQLITE_ROW) {
907 self->next_row = _pysqlite_fetch_one_row(self);
Victor Stinnerdd4b2992013-11-05 14:30:11 +0100908 if (self->next_row == NULL) {
909 (void)pysqlite_statement_reset(self->statement);
Victor Stinnerdd4b2992013-11-05 14:30:11 +0100910 return NULL;
911 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000912 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000913 }
914
915 return next_row;
916}
917
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000918PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000919{
920 PyObject* row;
921
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000922 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923 if (!row && !PyErr_Occurred()) {
924 Py_INCREF(Py_None);
925 return Py_None;
926 }
927
928 return row;
929}
930
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000931PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000933 static char *kwlist[] = {"size", NULL, NULL};
934
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000935 PyObject* row;
936 PyObject* list;
937 int maxrows = self->arraysize;
938 int counter = 0;
939
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000940 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
941 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000942 }
943
944 list = PyList_New(0);
945 if (!list) {
946 return NULL;
947 }
948
949 /* just make sure we enter the loop */
950 row = Py_None;
951
952 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000953 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000954 if (row) {
955 PyList_Append(list, row);
956 Py_DECREF(row);
957 } else {
958 break;
959 }
960
961 if (++counter == maxrows) {
962 break;
963 }
964 }
965
966 if (PyErr_Occurred()) {
967 Py_DECREF(list);
968 return NULL;
969 } else {
970 return list;
971 }
972}
973
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000974PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000975{
976 PyObject* row;
977 PyObject* list;
978
979 list = PyList_New(0);
980 if (!list) {
981 return NULL;
982 }
983
984 /* just make sure we enter the loop */
985 row = (PyObject*)Py_None;
986
987 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000988 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000989 if (row) {
990 PyList_Append(list, row);
991 Py_DECREF(row);
992 }
993 }
994
995 if (PyErr_Occurred()) {
996 Py_DECREF(list);
997 return NULL;
998 } else {
999 return list;
1000 }
1001}
1002
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001003PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001004{
1005 /* don't care, return None */
1006 Py_INCREF(Py_None);
1007 return Py_None;
1008}
1009
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001010PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001011{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001012 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001013 return NULL;
1014 }
1015
1016 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001017 (void)pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001018 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001019 }
1020
Gerhard Häringf9cee222010-03-05 15:20:03 +00001021 self->closed = 1;
1022
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001023 Py_INCREF(Py_None);
1024 return Py_None;
1025}
1026
1027static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001028 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001029 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001030 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001031 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001032 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001033 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001034 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001035 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001036 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001037 PyDoc_STR("Fetches several rows from the resultset.")},
1038 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1039 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001040 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001041 PyDoc_STR("Closes the cursor.")},
1042 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1043 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1044 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1045 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1046 {NULL, NULL}
1047};
1048
1049static struct PyMemberDef cursor_members[] =
1050{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001051 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1052 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001053 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001054 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
Georg Brandlf78e02b2008-06-10 17:40:04 +00001055 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001056 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001057 {NULL}
1058};
1059
Thomas Wouters477c8d52006-05-27 19:21:47 +00001060static char cursor_doc[] =
1061PyDoc_STR("SQLite database cursor class.");
1062
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001063PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001064 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001065 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001066 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001067 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001068 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001069 0, /* tp_print */
1070 0, /* tp_getattr */
1071 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001072 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001073 0, /* tp_repr */
1074 0, /* tp_as_number */
1075 0, /* tp_as_sequence */
1076 0, /* tp_as_mapping */
1077 0, /* tp_hash */
1078 0, /* tp_call */
1079 0, /* tp_str */
1080 0, /* tp_getattro */
1081 0, /* tp_setattro */
1082 0, /* tp_as_buffer */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001083 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001084 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085 0, /* tp_traverse */
1086 0, /* tp_clear */
1087 0, /* tp_richcompare */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001088 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001089 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1090 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001091 cursor_methods, /* tp_methods */
1092 cursor_members, /* tp_members */
1093 0, /* tp_getset */
1094 0, /* tp_base */
1095 0, /* tp_dict */
1096 0, /* tp_descr_get */
1097 0, /* tp_descr_set */
1098 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001099 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001100 0, /* tp_alloc */
1101 0, /* tp_new */
1102 0 /* tp_free */
1103};
1104
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001105extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001106{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001107 pysqlite_CursorType.tp_new = PyType_GenericNew;
1108 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001109}