blob: 75ee73ae6d405ba8410bbebd7a0718a41bbddd5c [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* cursor.c - the cursor type
2 *
Gerhard Häringf9cee222010-03-05 15:20:03 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
5 * This file is part of pysqlite.
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "cursor.h"
25#include "module.h"
26#include "util.h"
27#include "sqlitecompat.h"
28
Christian Heimes217cfd12007-12-02 14:31:20 +000029/* used to decide wether to call PyLong_FromLong or PyLong_FromLongLong */
Thomas Wouters477c8d52006-05-27 19:21:47 +000030#ifndef INT32_MIN
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000031#define INT32_MIN (-2147483647 - 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +000032#endif
33#ifndef INT32_MAX
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#define INT32_MAX 2147483647
Thomas Wouters477c8d52006-05-27 19:21:47 +000035#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000037PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038
Gerhard Häringf9cee222010-03-05 15:20:03 +000039static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
40
Guido van Rossum6374bb52007-06-13 16:28:25 +000041static pysqlite_StatementKind detect_statement_type(const char* statement)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042{
43 char buf[20];
Guido van Rossum6374bb52007-06-13 16:28:25 +000044 const char* src;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000045 char* dst;
46
47 src = statement;
48 /* skip over whitepace */
49 while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') {
50 src++;
51 }
52
53 if (*src == 0)
54 return STATEMENT_INVALID;
55
56 dst = buf;
57 *dst = 0;
Antoine Pitrou1665d2c2011-10-04 13:35:28 +020058 while (Py_ISALPHA(*src) && dst - buf < sizeof(buf) - 2) {
59 *dst++ = Py_TOLOWER(*src++);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060 }
61
62 *dst = 0;
63
64 if (!strcmp(buf, "select")) {
65 return STATEMENT_SELECT;
66 } else if (!strcmp(buf, "insert")) {
67 return STATEMENT_INSERT;
68 } else if (!strcmp(buf, "update")) {
69 return STATEMENT_UPDATE;
70 } else if (!strcmp(buf, "delete")) {
71 return STATEMENT_DELETE;
72 } else if (!strcmp(buf, "replace")) {
73 return STATEMENT_REPLACE;
74 } else {
75 return STATEMENT_OTHER;
76 }
77}
78
Gerhard Häringf9cee222010-03-05 15:20:03 +000079static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000081 pysqlite_Connection* connection;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000083 if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000085 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 }
87
88 Py_INCREF(connection);
89 self->connection = connection;
90 self->statement = NULL;
91 self->next_row = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +000092 self->in_weakreflist = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093
94 self->row_cast_map = PyList_New(0);
95 if (!self->row_cast_map) {
96 return -1;
97 }
98
99 Py_INCREF(Py_None);
100 self->description = Py_None;
101
102 Py_INCREF(Py_None);
103 self->lastrowid= Py_None;
104
105 self->arraysize = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000106 self->closed = 0;
107 self->reset = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108
Georg Brandlf78e02b2008-06-10 17:40:04 +0000109 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110
111 Py_INCREF(Py_None);
112 self->row_factory = Py_None;
113
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000114 if (!pysqlite_check_thread(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 return -1;
116 }
117
Gerhard Häringf9cee222010-03-05 15:20:03 +0000118 if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
119 return -1;
120 }
121
122 self->initialized = 1;
123
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000124 return 0;
125}
126
Gerhard Häringf9cee222010-03-05 15:20:03 +0000127static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000129 /* Reset the statement if the user has not closed the cursor */
130 if (self->statement) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000131 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 Py_DECREF(self->statement);
133 }
134
135 Py_XDECREF(self->connection);
136 Py_XDECREF(self->row_cast_map);
137 Py_XDECREF(self->description);
138 Py_XDECREF(self->lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139 Py_XDECREF(self->row_factory);
140 Py_XDECREF(self->next_row);
141
Gerhard Häringf9cee222010-03-05 15:20:03 +0000142 if (self->in_weakreflist != NULL) {
143 PyObject_ClearWeakRefs((PyObject*)self);
144 }
145
Christian Heimes90aa7642007-12-19 02:45:37 +0000146 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147}
148
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000149PyObject* _pysqlite_get_converter(PyObject* key)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150{
151 PyObject* upcase_key;
152 PyObject* retval;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200153 _Py_IDENTIFIER(upper);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000154
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200155 upcase_key = _PyObject_CallMethodId(key, &PyId_upper, "");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000156 if (!upcase_key) {
157 return NULL;
158 }
159
160 retval = PyDict_GetItem(converters, upcase_key);
161 Py_DECREF(upcase_key);
162
163 return retval;
164}
165
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000166int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167{
168 int i;
169 const char* type_start = (const char*)-1;
170 const char* pos;
171
172 const char* colname;
173 const char* decltype;
174 PyObject* py_decltype;
175 PyObject* converter;
176 PyObject* key;
177
178 if (!self->connection->detect_types) {
179 return 0;
180 }
181
182 Py_XDECREF(self->row_cast_map);
183 self->row_cast_map = PyList_New(0);
184
185 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
186 converter = NULL;
187
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000188 if (self->connection->detect_types & PARSE_COLNAMES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000190 if (colname) {
191 for (pos = colname; *pos != 0; pos++) {
192 if (*pos == '[') {
193 type_start = pos + 1;
194 } else if (*pos == ']' && type_start != (const char*)-1) {
Brett Cannon40430012007-10-22 20:24:51 +0000195 key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000196 if (!key) {
197 /* creating a string failed, but it is too complicated
198 * to propagate the error here, we just assume there is
199 * no converter and proceed */
200 break;
201 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000203 converter = _pysqlite_get_converter(key);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000204 Py_DECREF(key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205 break;
206 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208 }
209 }
210
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000211 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212 decltype = sqlite3_column_decltype(self->statement->st, i);
213 if (decltype) {
214 for (pos = decltype;;pos++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000215 /* Converter names are split at '(' and blanks.
216 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
217 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
218 * In other words, it will work as people expect it to work.*/
219 if (*pos == ' ' || *pos == '(' || *pos == 0) {
Brett Cannon40430012007-10-22 20:24:51 +0000220 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221 if (!py_decltype) {
222 return -1;
223 }
224 break;
225 }
226 }
227
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000228 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229 Py_DECREF(py_decltype);
230 }
231 }
232
233 if (!converter) {
234 converter = Py_None;
235 }
236
237 if (PyList_Append(self->row_cast_map, converter) != 0) {
238 if (converter != Py_None) {
239 Py_DECREF(converter);
240 }
241 Py_XDECREF(self->row_cast_map);
242 self->row_cast_map = NULL;
243
244 return -1;
245 }
246 }
247
248 return 0;
249}
250
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000251PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252{
253 const char* pos;
254
255 if (!colname) {
256 Py_INCREF(Py_None);
257 return Py_None;
258 }
259
260 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261 if (*pos == 0 || *pos == '[') {
262 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
263 pos--;
264 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000265 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266 }
267 }
268}
269
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000270PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000271{
Guido van Rossum98297ee2007-11-06 21:34:58 +0000272 return PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000273}
274
275/*
276 * Returns a row from the currently active SQLite statement
277 *
278 * Precondidition:
279 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
280 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000281PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000282{
283 int i, numcols;
284 PyObject* row;
285 PyObject* item = NULL;
286 int coltype;
287 PY_LONG_LONG intval;
288 PyObject* converter;
289 PyObject* converted;
290 Py_ssize_t nbytes;
291 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292 const char* val_str;
293 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000294 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000295 PyObject* buf_bytes;
296 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000297
Gerhard Häringf9cee222010-03-05 15:20:03 +0000298 if (self->reset) {
299 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
300 return NULL;
301 }
302
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303 Py_BEGIN_ALLOW_THREADS
304 numcols = sqlite3_data_count(self->statement->st);
305 Py_END_ALLOW_THREADS
306
307 row = PyTuple_New(numcols);
308 if (!row) {
309 return NULL;
310 }
311
312 for (i = 0; i < numcols; i++) {
313 if (self->connection->detect_types) {
314 converter = PyList_GetItem(self->row_cast_map, i);
315 if (!converter) {
316 converter = Py_None;
317 }
318 } else {
319 converter = Py_None;
320 }
321
322 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000323 nbytes = sqlite3_column_bytes(self->statement->st, i);
324 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 if (!val_str) {
326 Py_INCREF(Py_None);
327 converted = Py_None;
328 } else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000329 item = PyBytes_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 if (!item) {
331 return NULL;
332 }
333 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000334 Py_DECREF(item);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000335 if (!converted) {
336 break;
337 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 }
339 } else {
340 Py_BEGIN_ALLOW_THREADS
341 coltype = sqlite3_column_type(self->statement->st, i);
342 Py_END_ALLOW_THREADS
343 if (coltype == SQLITE_NULL) {
344 Py_INCREF(Py_None);
345 converted = Py_None;
346 } else if (coltype == SQLITE_INTEGER) {
347 intval = sqlite3_column_int64(self->statement->st, i);
348 if (intval < INT32_MIN || intval > INT32_MAX) {
349 converted = PyLong_FromLongLong(intval);
350 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000351 converted = PyLong_FromLong((long)intval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352 }
353 } else if (coltype == SQLITE_FLOAT) {
354 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
355 } else if (coltype == SQLITE_TEXT) {
356 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
357 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000358 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000360 converted = pysqlite_unicode_from_string(val_str,
361 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000362
363 if (!converted) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000364 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000365 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000366 colname = "<unknown column name>";
367 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000368 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000369 colname , val_str);
Victor Stinner86999502010-05-19 01:27:23 +0000370 buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000371 if (!buf_bytes) {
372 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
373 } else {
374 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
375 if (!error_obj) {
376 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000377 } else {
378 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000379 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000380 }
381 Py_DECREF(buf_bytes);
382 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000383 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000384 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
385 converted = PyBytes_FromString(val_str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000386 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
387 converted = PyByteArray_FromStringAndSize(val_str, strlen(val_str));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000389 converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 }
391 } else {
392 /* coltype == SQLITE_BLOB */
393 nbytes = sqlite3_column_bytes(self->statement->st, i);
Christian Heimes72b710a2008-05-26 13:28:38 +0000394 buffer = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000395 sqlite3_column_blob(self->statement->st, i), nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000396 if (!buffer) {
397 break;
398 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399 converted = buffer;
400 }
401 }
402
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000403 if (converted) {
404 PyTuple_SetItem(row, i, converted);
405 } else {
406 Py_INCREF(Py_None);
407 PyTuple_SetItem(row, i, Py_None);
408 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 }
410
411 if (PyErr_Occurred()) {
412 Py_DECREF(row);
413 row = NULL;
414 }
415
416 return row;
417}
418
Gerhard Häringf9cee222010-03-05 15:20:03 +0000419/*
420 * Checks if a cursor object is usable.
421 *
422 * 0 => error; 1 => ok
423 */
424static int check_cursor(pysqlite_Cursor* cur)
425{
426 if (!cur->initialized) {
427 PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
428 return 0;
429 }
430
431 if (cur->closed) {
432 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
433 return 0;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000434 }
Gerhard Haering936d5182011-05-09 12:24:09 +0200435
436 if (cur->locked) {
437 PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
438 return 0;
439 }
440
441 return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000442}
443
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000444PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000445{
446 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000447 const char* operation_cstr;
448 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000449 PyObject* parameters_list = NULL;
450 PyObject* parameters_iter = NULL;
451 PyObject* parameters = NULL;
452 int i;
453 int rc;
454 PyObject* func_args;
455 PyObject* result;
456 int numcols;
457 PY_LONG_LONG lastrowid;
458 int statement_type;
459 PyObject* descriptor;
460 PyObject* second_argument = NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000461 int allow_8bit_chars;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000462
Gerhard Häringf9cee222010-03-05 15:20:03 +0000463 if (!check_cursor(self)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200464 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000465 }
Georg Brandlf78e02b2008-06-10 17:40:04 +0000466
Gerhard Haering936d5182011-05-09 12:24:09 +0200467 self->locked = 1;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000468 self->reset = 0;
469
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000470 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
471 allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
Gerhard Häringf9cee222010-03-05 15:20:03 +0000472 (self->connection->text_factory != pysqlite_OptimizedUnicode));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000473
474 Py_XDECREF(self->next_row);
475 self->next_row = NULL;
476
477 if (multiple) {
478 /* executemany() */
479 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200480 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000481 }
482
Guido van Rossum98297ee2007-11-06 21:34:58 +0000483 if (!PyUnicode_Check(operation)) {
484 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200485 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000486 }
487
488 if (PyIter_Check(second_argument)) {
489 /* iterator */
490 Py_INCREF(second_argument);
491 parameters_iter = second_argument;
492 } else {
493 /* sequence */
494 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000495 if (!parameters_iter) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200496 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000497 }
498 }
499 } else {
500 /* execute() */
501 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200502 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503 }
504
Guido van Rossum98297ee2007-11-06 21:34:58 +0000505 if (!PyUnicode_Check(operation)) {
506 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Gerhard Haering936d5182011-05-09 12:24:09 +0200507 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 }
509
510 parameters_list = PyList_New(0);
511 if (!parameters_list) {
Gerhard Haering936d5182011-05-09 12:24:09 +0200512 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513 }
514
515 if (second_argument == NULL) {
516 second_argument = PyTuple_New(0);
517 if (!second_argument) {
518 goto error;
519 }
520 } else {
521 Py_INCREF(second_argument);
522 }
523 if (PyList_Append(parameters_list, second_argument) != 0) {
524 Py_DECREF(second_argument);
525 goto error;
526 }
527 Py_DECREF(second_argument);
528
529 parameters_iter = PyObject_GetIter(parameters_list);
530 if (!parameters_iter) {
531 goto error;
532 }
533 }
534
535 if (self->statement != NULL) {
536 /* There is an active statement */
Brett Cannonb94767f2011-02-22 20:15:44 +0000537 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538 }
539
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000540 operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
Victor Stinner86999502010-05-19 01:27:23 +0000541 if (operation_cstr == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000542 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543
Georg Brandlf78e02b2008-06-10 17:40:04 +0000544 /* reset description and rowcount */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545 Py_DECREF(self->description);
546 Py_INCREF(Py_None);
547 self->description = Py_None;
Georg Brandlf78e02b2008-06-10 17:40:04 +0000548 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000550 func_args = PyTuple_New(1);
551 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000552 goto error;
553 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000554 Py_INCREF(operation);
555 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
556 goto error;
557 }
558
559 if (self->statement) {
560 (void)pysqlite_statement_reset(self->statement);
561 Py_DECREF(self->statement);
562 }
563
564 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
565 Py_DECREF(func_args);
566
567 if (!self->statement) {
568 goto error;
569 }
570
571 if (self->statement->in_use) {
572 Py_DECREF(self->statement);
573 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
574 if (!self->statement) {
575 goto error;
576 }
577 rc = pysqlite_statement_create(self->statement, self->connection, operation);
578 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000579 Py_CLEAR(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000580 goto error;
581 }
582 }
583
584 pysqlite_statement_reset(self->statement);
585 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000586
587 statement_type = detect_statement_type(operation_cstr);
588 if (self->connection->begin_statement) {
589 switch (statement_type) {
590 case STATEMENT_UPDATE:
591 case STATEMENT_DELETE:
592 case STATEMENT_INSERT:
593 case STATEMENT_REPLACE:
594 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000595 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596 if (!result) {
597 goto error;
598 }
599 Py_DECREF(result);
600 }
601 break;
602 case STATEMENT_OTHER:
603 /* it's a DDL statement or something similar
604 - we better COMMIT first so it works for all cases */
605 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000606 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000607 if (!result) {
608 goto error;
609 }
610 Py_DECREF(result);
611 }
612 break;
613 case STATEMENT_SELECT:
614 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000615 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616 "You cannot execute SELECT statements in executemany().");
617 goto error;
618 }
619 break;
620 }
621 }
622
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623
624 while (1) {
625 parameters = PyIter_Next(parameters_iter);
626 if (!parameters) {
627 break;
628 }
629
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000630 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000631
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000632 pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000633 if (PyErr_Occurred()) {
634 goto error;
635 }
636
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000637 /* Keep trying the SQL statement until the schema stops changing. */
638 while (1) {
639 /* Actually execute the SQL statement. */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000640 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000641 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
642 /* If it worked, let's get out of the loop */
643 break;
644 }
645 /* Something went wrong. Re-set the statement and try again. */
646 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000648 /* If this was a result of the schema changing, let's try
649 again. */
650 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000652 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000653 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000654 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000655 (void)pysqlite_statement_reset(self->statement);
656 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000657 goto error;
658 }
659 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000660 if (PyErr_Occurred()) {
661 /* there was an error that occurred in a user-defined callback */
662 if (_enable_callback_tracebacks) {
663 PyErr_Print();
664 } else {
665 PyErr_Clear();
666 }
667 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000668 (void)pysqlite_statement_reset(self->statement);
669 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670 goto error;
671 }
672 }
673
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000674 if (pysqlite_build_row_cast_map(self) != 0) {
675 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
676 goto error;
677 }
678
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000679 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000681 Py_BEGIN_ALLOW_THREADS
682 numcols = sqlite3_column_count(self->statement->st);
683 Py_END_ALLOW_THREADS
684
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000685 Py_DECREF(self->description);
686 self->description = PyTuple_New(numcols);
687 if (!self->description) {
688 goto error;
689 }
690 for (i = 0; i < numcols; i++) {
691 descriptor = PyTuple_New(7);
692 if (!descriptor) {
693 goto error;
694 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000695 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000696 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
697 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
698 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
699 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
700 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
701 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
702 PyTuple_SetItem(self->description, i, descriptor);
703 }
704 }
705 }
706
707 if (rc == SQLITE_ROW) {
708 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000709 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 goto error;
711 }
712
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000713 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000714 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000715 pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000716 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000717 }
718
719 switch (statement_type) {
720 case STATEMENT_UPDATE:
721 case STATEMENT_DELETE:
722 case STATEMENT_INSERT:
723 case STATEMENT_REPLACE:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000724 if (self->rowcount == -1L) {
725 self->rowcount = 0L;
726 }
727 self->rowcount += (long)sqlite3_changes(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000728 }
729
730 Py_DECREF(self->lastrowid);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000731 if (!multiple && statement_type == STATEMENT_INSERT) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000732 Py_BEGIN_ALLOW_THREADS
733 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
734 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +0000735 self->lastrowid = PyLong_FromLong((long)lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000736 } else {
737 Py_INCREF(Py_None);
738 self->lastrowid = Py_None;
739 }
740
741 if (multiple) {
Brett Cannonb94767f2011-02-22 20:15:44 +0000742 pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000743 }
744 Py_XDECREF(parameters);
745 }
746
747error:
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000748 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
749 * ROLLBACK could have happened */
750 #ifdef SQLITE_VERSION_NUMBER
751 #if SQLITE_VERSION_NUMBER >= 3002002
Gerhard Haering936d5182011-05-09 12:24:09 +0200752 if (self->connection && self->connection->db)
753 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000754 #endif
755 #endif
756
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757 Py_XDECREF(parameters);
758 Py_XDECREF(parameters_iter);
759 Py_XDECREF(parameters_list);
760
Gerhard Haering936d5182011-05-09 12:24:09 +0200761 self->locked = 0;
762
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000763 if (PyErr_Occurred()) {
Georg Brandlf78e02b2008-06-10 17:40:04 +0000764 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000765 return NULL;
766 } else {
767 Py_INCREF(self);
768 return (PyObject*)self;
769 }
770}
771
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000772PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000774 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775}
776
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000777PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000778{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000779 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780}
781
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000782PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000783{
784 PyObject* script_obj;
785 PyObject* script_str = NULL;
786 const char* script_cstr;
787 sqlite3_stmt* statement;
788 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000789 PyObject* result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790
791 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000792 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000793 }
794
Gerhard Häringf9cee222010-03-05 15:20:03 +0000795 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000796 return NULL;
797 }
798
Gerhard Häringf9cee222010-03-05 15:20:03 +0000799 self->reset = 0;
800
Gerhard Häring6d214562007-08-10 18:15:11 +0000801 if (PyUnicode_Check(script_obj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000802 script_cstr = _PyUnicode_AsString(script_obj);
Gerhard Häring6d214562007-08-10 18:15:11 +0000803 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000804 return NULL;
805 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000806 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000807 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000808 return NULL;
809 }
810
811 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000812 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813 if (!result) {
814 goto error;
815 }
816 Py_DECREF(result);
817
818 while (1) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000819 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820 rc = sqlite3_prepare(self->connection->db,
821 script_cstr,
822 -1,
823 &statement,
824 &script_cstr);
Benjamin Petersond7b03282008-09-13 15:58:53 +0000825 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000827 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000828 goto error;
829 }
830
831 /* execute statement, and ignore results of SELECT statements */
832 rc = SQLITE_ROW;
833 while (rc == SQLITE_ROW) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000834 rc = pysqlite_step(statement, self->connection);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000835 /* TODO: we probably need more error handling here */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000836 }
837
838 if (rc != SQLITE_DONE) {
839 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000840 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000841 goto error;
842 }
843
844 rc = sqlite3_finalize(statement);
845 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000846 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000847 goto error;
848 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000849
850 if (*script_cstr == (char)0) {
851 break;
852 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000853 }
854
855error:
856 Py_XDECREF(script_str);
857
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858 if (PyErr_Occurred()) {
859 return NULL;
860 } else {
861 Py_INCREF(self);
862 return (PyObject*)self;
863 }
864}
865
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000866PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867{
868 Py_INCREF(self);
869 return (PyObject*)self;
870}
871
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000872PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000873{
874 PyObject* next_row_tuple;
875 PyObject* next_row;
876 int rc;
877
Gerhard Häringf9cee222010-03-05 15:20:03 +0000878 if (!check_cursor(self)) {
879 return NULL;
880 }
881
882 if (self->reset) {
883 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000884 return NULL;
885 }
886
887 if (!self->next_row) {
888 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000889 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000890 Py_DECREF(self->statement);
891 self->statement = NULL;
892 }
893 return NULL;
894 }
895
896 next_row_tuple = self->next_row;
897 self->next_row = NULL;
898
899 if (self->row_factory != Py_None) {
900 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
901 Py_DECREF(next_row_tuple);
902 } else {
903 next_row = next_row_tuple;
904 }
905
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000906 if (self->statement) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000907 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000908 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000909 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000910 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000911 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000912 return NULL;
913 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000914
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000915 if (rc == SQLITE_ROW) {
916 self->next_row = _pysqlite_fetch_one_row(self);
917 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000918 }
919
920 return next_row;
921}
922
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000923PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000924{
925 PyObject* row;
926
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000927 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000928 if (!row && !PyErr_Occurred()) {
929 Py_INCREF(Py_None);
930 return Py_None;
931 }
932
933 return row;
934}
935
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000936PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000937{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000938 static char *kwlist[] = {"size", NULL, NULL};
939
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000940 PyObject* row;
941 PyObject* list;
942 int maxrows = self->arraysize;
943 int counter = 0;
944
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000945 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
946 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000947 }
948
949 list = PyList_New(0);
950 if (!list) {
951 return NULL;
952 }
953
954 /* just make sure we enter the loop */
955 row = Py_None;
956
957 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000958 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000959 if (row) {
960 PyList_Append(list, row);
961 Py_DECREF(row);
962 } else {
963 break;
964 }
965
966 if (++counter == maxrows) {
967 break;
968 }
969 }
970
971 if (PyErr_Occurred()) {
972 Py_DECREF(list);
973 return NULL;
974 } else {
975 return list;
976 }
977}
978
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000979PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000980{
981 PyObject* row;
982 PyObject* list;
983
984 list = PyList_New(0);
985 if (!list) {
986 return NULL;
987 }
988
989 /* just make sure we enter the loop */
990 row = (PyObject*)Py_None;
991
992 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000993 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000994 if (row) {
995 PyList_Append(list, row);
996 Py_DECREF(row);
997 }
998 }
999
1000 if (PyErr_Occurred()) {
1001 Py_DECREF(list);
1002 return NULL;
1003 } else {
1004 return list;
1005 }
1006}
1007
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001008PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001009{
1010 /* don't care, return None */
1011 Py_INCREF(Py_None);
1012 return Py_None;
1013}
1014
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001015PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001016{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001017 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001018 return NULL;
1019 }
1020
1021 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001022 (void)pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001023 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024 }
1025
Gerhard Häringf9cee222010-03-05 15:20:03 +00001026 self->closed = 1;
1027
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001028 Py_INCREF(Py_None);
1029 return Py_None;
1030}
1031
1032static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001033 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001034 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001035 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001036 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001037 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001038 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001039 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001040 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001041 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001042 PyDoc_STR("Fetches several rows from the resultset.")},
1043 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1044 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001045 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001046 PyDoc_STR("Closes the cursor.")},
1047 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1048 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1049 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1050 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1051 {NULL, NULL}
1052};
1053
1054static struct PyMemberDef cursor_members[] =
1055{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001056 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1057 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001058 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001059 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
Georg Brandlf78e02b2008-06-10 17:40:04 +00001060 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001061 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001062 {NULL}
1063};
1064
Thomas Wouters477c8d52006-05-27 19:21:47 +00001065static char cursor_doc[] =
1066PyDoc_STR("SQLite database cursor class.");
1067
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001068PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001069 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001070 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001071 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001072 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001073 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001074 0, /* tp_print */
1075 0, /* tp_getattr */
1076 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001077 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001078 0, /* tp_repr */
1079 0, /* tp_as_number */
1080 0, /* tp_as_sequence */
1081 0, /* tp_as_mapping */
1082 0, /* tp_hash */
1083 0, /* tp_call */
1084 0, /* tp_str */
1085 0, /* tp_getattro */
1086 0, /* tp_setattro */
1087 0, /* tp_as_buffer */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001088 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001089 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001090 0, /* tp_traverse */
1091 0, /* tp_clear */
1092 0, /* tp_richcompare */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001093 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001094 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1095 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001096 cursor_methods, /* tp_methods */
1097 cursor_members, /* tp_members */
1098 0, /* tp_getset */
1099 0, /* tp_base */
1100 0, /* tp_dict */
1101 0, /* tp_descr_get */
1102 0, /* tp_descr_set */
1103 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001104 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001105 0, /* tp_alloc */
1106 0, /* tp_new */
1107 0 /* tp_free */
1108};
1109
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001110extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001111{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001112 pysqlite_CursorType.tp_new = PyType_GenericNew;
1113 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001114}