blob: 763322ff1b64231ba4338cd747a6ce7ec7ce25db [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{
129 int rc;
130
131 /* Reset the statement if the user has not closed the cursor */
132 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000133 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 Py_DECREF(self->statement);
135 }
136
137 Py_XDECREF(self->connection);
138 Py_XDECREF(self->row_cast_map);
139 Py_XDECREF(self->description);
140 Py_XDECREF(self->lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141 Py_XDECREF(self->row_factory);
142 Py_XDECREF(self->next_row);
143
Gerhard Häringf9cee222010-03-05 15:20:03 +0000144 if (self->in_weakreflist != NULL) {
145 PyObject_ClearWeakRefs((PyObject*)self);
146 }
147
Christian Heimes90aa7642007-12-19 02:45:37 +0000148 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000149}
150
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000151PyObject* _pysqlite_get_converter(PyObject* key)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000152{
153 PyObject* upcase_key;
154 PyObject* retval;
155
156 upcase_key = PyObject_CallMethod(key, "upper", "");
157 if (!upcase_key) {
158 return NULL;
159 }
160
161 retval = PyDict_GetItem(converters, upcase_key);
162 Py_DECREF(upcase_key);
163
164 return retval;
165}
166
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000167int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000168{
169 int i;
170 const char* type_start = (const char*)-1;
171 const char* pos;
172
173 const char* colname;
174 const char* decltype;
175 PyObject* py_decltype;
176 PyObject* converter;
177 PyObject* key;
178
179 if (!self->connection->detect_types) {
180 return 0;
181 }
182
183 Py_XDECREF(self->row_cast_map);
184 self->row_cast_map = PyList_New(0);
185
186 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
187 converter = NULL;
188
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000189 if (self->connection->detect_types & PARSE_COLNAMES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000191 if (colname) {
192 for (pos = colname; *pos != 0; pos++) {
193 if (*pos == '[') {
194 type_start = pos + 1;
195 } else if (*pos == ']' && type_start != (const char*)-1) {
Brett Cannon40430012007-10-22 20:24:51 +0000196 key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000197 if (!key) {
198 /* creating a string failed, but it is too complicated
199 * to propagate the error here, we just assume there is
200 * no converter and proceed */
201 break;
202 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000203
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000204 converter = _pysqlite_get_converter(key);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000205 Py_DECREF(key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206 break;
207 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209 }
210 }
211
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000212 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213 decltype = sqlite3_column_decltype(self->statement->st, i);
214 if (decltype) {
215 for (pos = decltype;;pos++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000216 /* Converter names are split at '(' and blanks.
217 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
218 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
219 * In other words, it will work as people expect it to work.*/
220 if (*pos == ' ' || *pos == '(' || *pos == 0) {
Brett Cannon40430012007-10-22 20:24:51 +0000221 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222 if (!py_decltype) {
223 return -1;
224 }
225 break;
226 }
227 }
228
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000229 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230 Py_DECREF(py_decltype);
231 }
232 }
233
234 if (!converter) {
235 converter = Py_None;
236 }
237
238 if (PyList_Append(self->row_cast_map, converter) != 0) {
239 if (converter != Py_None) {
240 Py_DECREF(converter);
241 }
242 Py_XDECREF(self->row_cast_map);
243 self->row_cast_map = NULL;
244
245 return -1;
246 }
247 }
248
249 return 0;
250}
251
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000252PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000253{
254 const char* pos;
255
256 if (!colname) {
257 Py_INCREF(Py_None);
258 return Py_None;
259 }
260
261 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262 if (*pos == 0 || *pos == '[') {
263 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
264 pos--;
265 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000266 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267 }
268 }
269}
270
Petri Lehtinen023fe332012-02-01 22:18:19 +0200271PyObject* pysqlite_unicode_from_string(const char* val_str, Py_ssize_t size, int optimize)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000272{
Petri Lehtinen023fe332012-02-01 22:18:19 +0200273 return PyUnicode_FromStringAndSize(val_str, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274}
275
276/*
277 * Returns a row from the currently active SQLite statement
278 *
279 * Precondidition:
280 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
281 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000282PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283{
284 int i, numcols;
285 PyObject* row;
286 PyObject* item = NULL;
287 int coltype;
288 PY_LONG_LONG intval;
289 PyObject* converter;
290 PyObject* converted;
291 Py_ssize_t nbytes;
292 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293 const char* val_str;
294 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000295 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000296 PyObject* buf_bytes;
297 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298
Gerhard Häringf9cee222010-03-05 15:20:03 +0000299 if (self->reset) {
300 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
301 return NULL;
302 }
303
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304 Py_BEGIN_ALLOW_THREADS
305 numcols = sqlite3_data_count(self->statement->st);
306 Py_END_ALLOW_THREADS
307
308 row = PyTuple_New(numcols);
309 if (!row) {
310 return NULL;
311 }
312
313 for (i = 0; i < numcols; i++) {
314 if (self->connection->detect_types) {
315 converter = PyList_GetItem(self->row_cast_map, i);
316 if (!converter) {
317 converter = Py_None;
318 }
319 } else {
320 converter = Py_None;
321 }
322
323 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000324 nbytes = sqlite3_column_bytes(self->statement->st, i);
325 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326 if (!val_str) {
327 Py_INCREF(Py_None);
328 converted = Py_None;
329 } else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000330 item = PyBytes_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331 if (!item) {
332 return NULL;
333 }
334 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335 Py_DECREF(item);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000336 if (!converted) {
337 break;
338 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 }
340 } else {
341 Py_BEGIN_ALLOW_THREADS
342 coltype = sqlite3_column_type(self->statement->st, i);
343 Py_END_ALLOW_THREADS
344 if (coltype == SQLITE_NULL) {
345 Py_INCREF(Py_None);
346 converted = Py_None;
347 } else if (coltype == SQLITE_INTEGER) {
348 intval = sqlite3_column_int64(self->statement->st, i);
349 if (intval < INT32_MIN || intval > INT32_MAX) {
350 converted = PyLong_FromLongLong(intval);
351 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000352 converted = PyLong_FromLong((long)intval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000353 }
354 } else if (coltype == SQLITE_FLOAT) {
355 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
356 } else if (coltype == SQLITE_TEXT) {
357 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
Petri Lehtinen023fe332012-02-01 22:18:19 +0200358 nbytes = sqlite3_column_bytes(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000360 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000361
Petri Lehtinen023fe332012-02-01 22:18:19 +0200362 converted = pysqlite_unicode_from_string(val_str, nbytes,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000363 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364
365 if (!converted) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000366 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000367 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000368 colname = "<unknown column name>";
369 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000370 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000371 colname , val_str);
Victor Stinner86999502010-05-19 01:27:23 +0000372 buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000373 if (!buf_bytes) {
374 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
375 } else {
376 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
377 if (!error_obj) {
378 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000379 } else {
380 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000381 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000382 }
383 Py_DECREF(buf_bytes);
384 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000386 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200387 converted = PyBytes_FromStringAndSize(val_str, nbytes);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000388 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200389 converted = PyByteArray_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 } else {
Petri Lehtinen023fe332012-02-01 22:18:19 +0200391 converted = PyObject_CallFunction(self->connection->text_factory, "y#", val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 }
393 } else {
394 /* coltype == SQLITE_BLOB */
395 nbytes = sqlite3_column_bytes(self->statement->st, i);
Christian Heimes72b710a2008-05-26 13:28:38 +0000396 buffer = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000397 sqlite3_column_blob(self->statement->st, i), nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000398 if (!buffer) {
399 break;
400 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000401 converted = buffer;
402 }
403 }
404
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000405 if (converted) {
406 PyTuple_SetItem(row, i, converted);
407 } else {
408 Py_INCREF(Py_None);
409 PyTuple_SetItem(row, i, Py_None);
410 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000411 }
412
413 if (PyErr_Occurred()) {
414 Py_DECREF(row);
415 row = NULL;
416 }
417
418 return row;
419}
420
Gerhard Häringf9cee222010-03-05 15:20:03 +0000421/*
422 * Checks if a cursor object is usable.
423 *
424 * 0 => error; 1 => ok
425 */
426static int check_cursor(pysqlite_Cursor* cur)
427{
428 if (!cur->initialized) {
429 PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
430 return 0;
431 }
432
433 if (cur->closed) {
434 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
435 return 0;
436 } else {
437 return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
438 }
439}
440
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000441PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442{
443 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000444 const char* operation_cstr;
445 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446 PyObject* parameters_list = NULL;
447 PyObject* parameters_iter = NULL;
448 PyObject* parameters = NULL;
449 int i;
450 int rc;
451 PyObject* func_args;
452 PyObject* result;
453 int numcols;
454 PY_LONG_LONG lastrowid;
455 int statement_type;
456 PyObject* descriptor;
457 PyObject* second_argument = NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000458 int allow_8bit_chars;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000459
Gerhard Häringf9cee222010-03-05 15:20:03 +0000460 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461 return NULL;
462 }
Georg Brandlf78e02b2008-06-10 17:40:04 +0000463
Gerhard Häringf9cee222010-03-05 15:20:03 +0000464 self->reset = 0;
465
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000466 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
467 allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
Gerhard Häringf9cee222010-03-05 15:20:03 +0000468 (self->connection->text_factory != pysqlite_OptimizedUnicode));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469
470 Py_XDECREF(self->next_row);
471 self->next_row = NULL;
472
473 if (multiple) {
474 /* executemany() */
475 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000476 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000477 }
478
Guido van Rossum98297ee2007-11-06 21:34:58 +0000479 if (!PyUnicode_Check(operation)) {
480 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000481 return NULL;
482 }
483
484 if (PyIter_Check(second_argument)) {
485 /* iterator */
486 Py_INCREF(second_argument);
487 parameters_iter = second_argument;
488 } else {
489 /* sequence */
490 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000491 if (!parameters_iter) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492 return NULL;
493 }
494 }
495 } else {
496 /* execute() */
497 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000498 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499 }
500
Guido van Rossum98297ee2007-11-06 21:34:58 +0000501 if (!PyUnicode_Check(operation)) {
502 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503 return NULL;
504 }
505
506 parameters_list = PyList_New(0);
507 if (!parameters_list) {
508 return NULL;
509 }
510
511 if (second_argument == NULL) {
512 second_argument = PyTuple_New(0);
513 if (!second_argument) {
514 goto error;
515 }
516 } else {
517 Py_INCREF(second_argument);
518 }
519 if (PyList_Append(parameters_list, second_argument) != 0) {
520 Py_DECREF(second_argument);
521 goto error;
522 }
523 Py_DECREF(second_argument);
524
525 parameters_iter = PyObject_GetIter(parameters_list);
526 if (!parameters_iter) {
527 goto error;
528 }
529 }
530
531 if (self->statement != NULL) {
532 /* There is an active statement */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000533 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000534 }
535
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000536 operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
Victor Stinner86999502010-05-19 01:27:23 +0000537 if (operation_cstr == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000538 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539
Georg Brandlf78e02b2008-06-10 17:40:04 +0000540 /* reset description and rowcount */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541 Py_DECREF(self->description);
542 Py_INCREF(Py_None);
543 self->description = Py_None;
Georg Brandlf78e02b2008-06-10 17:40:04 +0000544 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000546 func_args = PyTuple_New(1);
547 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548 goto error;
549 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000550 Py_INCREF(operation);
551 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
552 goto error;
553 }
554
555 if (self->statement) {
556 (void)pysqlite_statement_reset(self->statement);
557 Py_DECREF(self->statement);
558 }
559
560 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
561 Py_DECREF(func_args);
562
563 if (!self->statement) {
564 goto error;
565 }
566
567 if (self->statement->in_use) {
568 Py_DECREF(self->statement);
569 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
570 if (!self->statement) {
571 goto error;
572 }
573 rc = pysqlite_statement_create(self->statement, self->connection, operation);
574 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000575 Py_CLEAR(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000576 goto error;
577 }
578 }
579
580 pysqlite_statement_reset(self->statement);
581 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000582
583 statement_type = detect_statement_type(operation_cstr);
584 if (self->connection->begin_statement) {
585 switch (statement_type) {
586 case STATEMENT_UPDATE:
587 case STATEMENT_DELETE:
588 case STATEMENT_INSERT:
589 case STATEMENT_REPLACE:
590 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000591 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592 if (!result) {
593 goto error;
594 }
595 Py_DECREF(result);
596 }
597 break;
598 case STATEMENT_OTHER:
599 /* it's a DDL statement or something similar
600 - we better COMMIT first so it works for all cases */
601 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000602 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000603 if (!result) {
604 goto error;
605 }
606 Py_DECREF(result);
607 }
608 break;
609 case STATEMENT_SELECT:
610 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000611 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000612 "You cannot execute SELECT statements in executemany().");
613 goto error;
614 }
615 break;
616 }
617 }
618
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000619
620 while (1) {
621 parameters = PyIter_Next(parameters_iter);
622 if (!parameters) {
623 break;
624 }
625
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000626 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000627
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000628 pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629 if (PyErr_Occurred()) {
630 goto error;
631 }
632
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000633 /* Keep trying the SQL statement until the schema stops changing. */
634 while (1) {
635 /* Actually execute the SQL statement. */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000636 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000637 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
638 /* If it worked, let's get out of the loop */
639 break;
640 }
641 /* Something went wrong. Re-set the statement and try again. */
642 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000644 /* If this was a result of the schema changing, let's try
645 again. */
646 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000648 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000649 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000650 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000651 (void)pysqlite_statement_reset(self->statement);
652 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000653 goto error;
654 }
655 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000656 if (PyErr_Occurred()) {
657 /* there was an error that occurred in a user-defined callback */
658 if (_enable_callback_tracebacks) {
659 PyErr_Print();
660 } else {
661 PyErr_Clear();
662 }
663 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000664 (void)pysqlite_statement_reset(self->statement);
665 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666 goto error;
667 }
668 }
669
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000670 if (pysqlite_build_row_cast_map(self) != 0) {
671 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
672 goto error;
673 }
674
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000676 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000677 Py_BEGIN_ALLOW_THREADS
678 numcols = sqlite3_column_count(self->statement->st);
679 Py_END_ALLOW_THREADS
680
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000681 Py_DECREF(self->description);
682 self->description = PyTuple_New(numcols);
683 if (!self->description) {
684 goto error;
685 }
686 for (i = 0; i < numcols; i++) {
687 descriptor = PyTuple_New(7);
688 if (!descriptor) {
689 goto error;
690 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000691 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
693 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
694 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
695 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
696 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
697 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
698 PyTuple_SetItem(self->description, i, descriptor);
699 }
700 }
701 }
702
703 if (rc == SQLITE_ROW) {
704 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000705 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706 goto error;
707 }
708
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000709 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000711 pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000712 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713 }
714
715 switch (statement_type) {
716 case STATEMENT_UPDATE:
717 case STATEMENT_DELETE:
718 case STATEMENT_INSERT:
719 case STATEMENT_REPLACE:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000720 if (self->rowcount == -1L) {
721 self->rowcount = 0L;
722 }
723 self->rowcount += (long)sqlite3_changes(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000724 }
725
726 Py_DECREF(self->lastrowid);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000727 if (!multiple && statement_type == STATEMENT_INSERT) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000728 Py_BEGIN_ALLOW_THREADS
729 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
730 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +0000731 self->lastrowid = PyLong_FromLong((long)lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000732 } else {
733 Py_INCREF(Py_None);
734 self->lastrowid = Py_None;
735 }
736
737 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000738 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000739 }
740 Py_XDECREF(parameters);
741 }
742
743error:
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000744 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
745 * ROLLBACK could have happened */
746 #ifdef SQLITE_VERSION_NUMBER
747 #if SQLITE_VERSION_NUMBER >= 3002002
748 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
749 #endif
750 #endif
751
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000752 Py_XDECREF(parameters);
753 Py_XDECREF(parameters_iter);
754 Py_XDECREF(parameters_list);
755
756 if (PyErr_Occurred()) {
Georg Brandlf78e02b2008-06-10 17:40:04 +0000757 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000758 return NULL;
759 } else {
760 Py_INCREF(self);
761 return (PyObject*)self;
762 }
763}
764
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000765PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000767 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768}
769
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000770PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000771{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000772 return _pysqlite_query_execute(self, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773}
774
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000775PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000776{
777 PyObject* script_obj;
778 PyObject* script_str = NULL;
779 const char* script_cstr;
780 sqlite3_stmt* statement;
781 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782 PyObject* result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000783
784 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000785 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000786 }
787
Gerhard Häringf9cee222010-03-05 15:20:03 +0000788 if (!check_cursor(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000789 return NULL;
790 }
791
Gerhard Häringf9cee222010-03-05 15:20:03 +0000792 self->reset = 0;
793
Gerhard Häring6d214562007-08-10 18:15:11 +0000794 if (PyUnicode_Check(script_obj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000795 script_cstr = _PyUnicode_AsString(script_obj);
Gerhard Häring6d214562007-08-10 18:15:11 +0000796 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000797 return NULL;
798 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000799 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000800 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000801 return NULL;
802 }
803
804 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000805 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000806 if (!result) {
807 goto error;
808 }
809 Py_DECREF(result);
810
811 while (1) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000812 Py_BEGIN_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813 rc = sqlite3_prepare(self->connection->db,
814 script_cstr,
815 -1,
816 &statement,
817 &script_cstr);
Benjamin Petersond7b03282008-09-13 15:58:53 +0000818 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000820 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000821 goto error;
822 }
823
824 /* execute statement, and ignore results of SELECT statements */
825 rc = SQLITE_ROW;
826 while (rc == SQLITE_ROW) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000827 rc = pysqlite_step(statement, self->connection);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000828 /* TODO: we probably need more error handling here */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000829 }
830
831 if (rc != SQLITE_DONE) {
832 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000833 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834 goto error;
835 }
836
837 rc = sqlite3_finalize(statement);
838 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000839 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840 goto error;
841 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000842
843 if (*script_cstr == (char)0) {
844 break;
845 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846 }
847
848error:
849 Py_XDECREF(script_str);
850
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000851 if (PyErr_Occurred()) {
852 return NULL;
853 } else {
854 Py_INCREF(self);
855 return (PyObject*)self;
856 }
857}
858
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000859PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000860{
861 Py_INCREF(self);
862 return (PyObject*)self;
863}
864
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000865PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866{
867 PyObject* next_row_tuple;
868 PyObject* next_row;
869 int rc;
870
Gerhard Häringf9cee222010-03-05 15:20:03 +0000871 if (!check_cursor(self)) {
872 return NULL;
873 }
874
875 if (self->reset) {
876 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000877 return NULL;
878 }
879
880 if (!self->next_row) {
881 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000882 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883 Py_DECREF(self->statement);
884 self->statement = NULL;
885 }
886 return NULL;
887 }
888
889 next_row_tuple = self->next_row;
890 self->next_row = NULL;
891
892 if (self->row_factory != Py_None) {
893 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
894 Py_DECREF(next_row_tuple);
895 } else {
896 next_row = next_row_tuple;
897 }
898
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000899 if (self->statement) {
Benjamin Petersond7b03282008-09-13 15:58:53 +0000900 rc = pysqlite_step(self->statement->st, self->connection);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000901 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000902 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000903 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000904 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000905 return NULL;
906 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000907
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000908 if (rc == SQLITE_ROW) {
909 self->next_row = _pysqlite_fetch_one_row(self);
910 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000911 }
912
913 return next_row;
914}
915
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000916PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000917{
918 PyObject* row;
919
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000920 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921 if (!row && !PyErr_Occurred()) {
922 Py_INCREF(Py_None);
923 return Py_None;
924 }
925
926 return row;
927}
928
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000929PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000930{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000931 static char *kwlist[] = {"size", NULL, NULL};
932
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933 PyObject* row;
934 PyObject* list;
935 int maxrows = self->arraysize;
936 int counter = 0;
937
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000938 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
939 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000940 }
941
942 list = PyList_New(0);
943 if (!list) {
944 return NULL;
945 }
946
947 /* just make sure we enter the loop */
948 row = Py_None;
949
950 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000951 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000952 if (row) {
953 PyList_Append(list, row);
954 Py_DECREF(row);
955 } else {
956 break;
957 }
958
959 if (++counter == maxrows) {
960 break;
961 }
962 }
963
964 if (PyErr_Occurred()) {
965 Py_DECREF(list);
966 return NULL;
967 } else {
968 return list;
969 }
970}
971
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000972PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000973{
974 PyObject* row;
975 PyObject* list;
976
977 list = PyList_New(0);
978 if (!list) {
979 return NULL;
980 }
981
982 /* just make sure we enter the loop */
983 row = (PyObject*)Py_None;
984
985 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000986 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000987 if (row) {
988 PyList_Append(list, row);
989 Py_DECREF(row);
990 }
991 }
992
993 if (PyErr_Occurred()) {
994 Py_DECREF(list);
995 return NULL;
996 } else {
997 return list;
998 }
999}
1000
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001001PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001002{
1003 /* don't care, return None */
1004 Py_INCREF(Py_None);
1005 return Py_None;
1006}
1007
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001008PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001009{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001010 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001011 return NULL;
1012 }
1013
1014 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001015 (void)pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001016 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001017 }
1018
Gerhard Häringf9cee222010-03-05 15:20:03 +00001019 self->closed = 1;
1020
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001021 Py_INCREF(Py_None);
1022 return Py_None;
1023}
1024
1025static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001026 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001027 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001028 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001029 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001030 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001031 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001032 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001033 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001034 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001035 PyDoc_STR("Fetches several rows from the resultset.")},
1036 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1037 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001038 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001039 PyDoc_STR("Closes the cursor.")},
1040 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1041 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1042 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1043 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1044 {NULL, NULL}
1045};
1046
1047static struct PyMemberDef cursor_members[] =
1048{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001049 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1050 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001051 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001052 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
Georg Brandlf78e02b2008-06-10 17:40:04 +00001053 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001054 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055 {NULL}
1056};
1057
Thomas Wouters477c8d52006-05-27 19:21:47 +00001058static char cursor_doc[] =
1059PyDoc_STR("SQLite database cursor class.");
1060
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001061PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001062 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001063 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001064 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001065 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001066 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001067 0, /* tp_print */
1068 0, /* tp_getattr */
1069 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001070 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001071 0, /* tp_repr */
1072 0, /* tp_as_number */
1073 0, /* tp_as_sequence */
1074 0, /* tp_as_mapping */
1075 0, /* tp_hash */
1076 0, /* tp_call */
1077 0, /* tp_str */
1078 0, /* tp_getattro */
1079 0, /* tp_setattro */
1080 0, /* tp_as_buffer */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001081 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001082 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083 0, /* tp_traverse */
1084 0, /* tp_clear */
1085 0, /* tp_richcompare */
Gerhard Häringf9cee222010-03-05 15:20:03 +00001086 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001087 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1088 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001089 cursor_methods, /* tp_methods */
1090 cursor_members, /* tp_members */
1091 0, /* tp_getset */
1092 0, /* tp_base */
1093 0, /* tp_dict */
1094 0, /* tp_descr_get */
1095 0, /* tp_descr_set */
1096 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001097 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098 0, /* tp_alloc */
1099 0, /* tp_new */
1100 0 /* tp_free */
1101};
1102
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001103extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001104{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001105 pysqlite_CursorType.tp_new = PyType_GenericNew;
1106 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001107}