blob: 9ac25f5cc07d7571fe0ca108ada7e9ce5c24f154 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* cursor.c - the cursor type
2 *
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00003 * Copyright (C) 2004-2007 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
Guido van Rossum6374bb52007-06-13 16:28:25 +000039static pysqlite_StatementKind detect_statement_type(const char* statement)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040{
41 char buf[20];
Guido van Rossum6374bb52007-06-13 16:28:25 +000042 const char* src;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043 char* dst;
44
45 src = statement;
46 /* skip over whitepace */
47 while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') {
48 src++;
49 }
50
51 if (*src == 0)
52 return STATEMENT_INVALID;
53
54 dst = buf;
55 *dst = 0;
56 while (isalpha(*src) && dst - buf < sizeof(buf) - 2) {
57 *dst++ = tolower(*src++);
58 }
59
60 *dst = 0;
61
62 if (!strcmp(buf, "select")) {
63 return STATEMENT_SELECT;
64 } else if (!strcmp(buf, "insert")) {
65 return STATEMENT_INSERT;
66 } else if (!strcmp(buf, "update")) {
67 return STATEMENT_UPDATE;
68 } else if (!strcmp(buf, "delete")) {
69 return STATEMENT_DELETE;
70 } else if (!strcmp(buf, "replace")) {
71 return STATEMENT_REPLACE;
72 } else {
73 return STATEMENT_OTHER;
74 }
75}
76
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000077int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000079 pysqlite_Connection* connection;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000081 if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000083 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 }
85
86 Py_INCREF(connection);
87 self->connection = connection;
88 self->statement = NULL;
89 self->next_row = NULL;
90
91 self->row_cast_map = PyList_New(0);
92 if (!self->row_cast_map) {
93 return -1;
94 }
95
96 Py_INCREF(Py_None);
97 self->description = Py_None;
98
99 Py_INCREF(Py_None);
100 self->lastrowid= Py_None;
101
102 self->arraysize = 1;
103
Georg Brandlf78e02b2008-06-10 17:40:04 +0000104 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105
106 Py_INCREF(Py_None);
107 self->row_factory = Py_None;
108
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000109 if (!pysqlite_check_thread(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110 return -1;
111 }
112
113 return 0;
114}
115
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000116void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117{
118 int rc;
119
120 /* Reset the statement if the user has not closed the cursor */
121 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000122 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 Py_DECREF(self->statement);
124 }
125
126 Py_XDECREF(self->connection);
127 Py_XDECREF(self->row_cast_map);
128 Py_XDECREF(self->description);
129 Py_XDECREF(self->lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 Py_XDECREF(self->row_factory);
131 Py_XDECREF(self->next_row);
132
Christian Heimes90aa7642007-12-19 02:45:37 +0000133 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134}
135
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000136PyObject* _pysqlite_get_converter(PyObject* key)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000137{
138 PyObject* upcase_key;
139 PyObject* retval;
140
141 upcase_key = PyObject_CallMethod(key, "upper", "");
142 if (!upcase_key) {
143 return NULL;
144 }
145
146 retval = PyDict_GetItem(converters, upcase_key);
147 Py_DECREF(upcase_key);
148
149 return retval;
150}
151
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000152int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000153{
154 int i;
155 const char* type_start = (const char*)-1;
156 const char* pos;
157
158 const char* colname;
159 const char* decltype;
160 PyObject* py_decltype;
161 PyObject* converter;
162 PyObject* key;
163
164 if (!self->connection->detect_types) {
165 return 0;
166 }
167
168 Py_XDECREF(self->row_cast_map);
169 self->row_cast_map = PyList_New(0);
170
171 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
172 converter = NULL;
173
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000174 if (self->connection->detect_types & PARSE_COLNAMES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000176 if (colname) {
177 for (pos = colname; *pos != 0; pos++) {
178 if (*pos == '[') {
179 type_start = pos + 1;
180 } else if (*pos == ']' && type_start != (const char*)-1) {
Brett Cannon40430012007-10-22 20:24:51 +0000181 key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000182 if (!key) {
183 /* creating a string failed, but it is too complicated
184 * to propagate the error here, we just assume there is
185 * no converter and proceed */
186 break;
187 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000188
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000189 converter = _pysqlite_get_converter(key);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000190 Py_DECREF(key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191 break;
192 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194 }
195 }
196
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000197 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 decltype = sqlite3_column_decltype(self->statement->st, i);
199 if (decltype) {
200 for (pos = decltype;;pos++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000201 /* Converter names are split at '(' and blanks.
202 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
203 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
204 * In other words, it will work as people expect it to work.*/
205 if (*pos == ' ' || *pos == '(' || *pos == 0) {
Brett Cannon40430012007-10-22 20:24:51 +0000206 py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207 if (!py_decltype) {
208 return -1;
209 }
210 break;
211 }
212 }
213
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000214 converter = _pysqlite_get_converter(py_decltype);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215 Py_DECREF(py_decltype);
216 }
217 }
218
219 if (!converter) {
220 converter = Py_None;
221 }
222
223 if (PyList_Append(self->row_cast_map, converter) != 0) {
224 if (converter != Py_None) {
225 Py_DECREF(converter);
226 }
227 Py_XDECREF(self->row_cast_map);
228 self->row_cast_map = NULL;
229
230 return -1;
231 }
232 }
233
234 return 0;
235}
236
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000237PyObject* _pysqlite_build_column_name(const char* colname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000238{
239 const char* pos;
240
241 if (!colname) {
242 Py_INCREF(Py_None);
243 return Py_None;
244 }
245
246 for (pos = colname;; pos++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000247 if (*pos == 0 || *pos == '[') {
248 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
249 pos--;
250 }
Gerhard Häring6d214562007-08-10 18:15:11 +0000251 return PyUnicode_FromStringAndSize(colname, pos - colname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252 }
253 }
254}
255
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000256PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257{
Guido van Rossum98297ee2007-11-06 21:34:58 +0000258 return PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259}
260
261/*
262 * Returns a row from the currently active SQLite statement
263 *
264 * Precondidition:
265 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
266 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000267PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000268{
269 int i, numcols;
270 PyObject* row;
271 PyObject* item = NULL;
272 int coltype;
273 PY_LONG_LONG intval;
274 PyObject* converter;
275 PyObject* converted;
276 Py_ssize_t nbytes;
277 PyObject* buffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000278 const char* val_str;
279 char buf[200];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280 const char* colname;
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000281 PyObject* buf_bytes;
282 PyObject* error_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283
284 Py_BEGIN_ALLOW_THREADS
285 numcols = sqlite3_data_count(self->statement->st);
286 Py_END_ALLOW_THREADS
287
288 row = PyTuple_New(numcols);
289 if (!row) {
290 return NULL;
291 }
292
293 for (i = 0; i < numcols; i++) {
294 if (self->connection->detect_types) {
295 converter = PyList_GetItem(self->row_cast_map, i);
296 if (!converter) {
297 converter = Py_None;
298 }
299 } else {
300 converter = Py_None;
301 }
302
303 if (converter != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000304 nbytes = sqlite3_column_bytes(self->statement->st, i);
305 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000306 if (!val_str) {
307 Py_INCREF(Py_None);
308 converted = Py_None;
309 } else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000310 item = PyBytes_FromStringAndSize(val_str, nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000311 if (!item) {
312 return NULL;
313 }
314 converted = PyObject_CallFunction(converter, "O", item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315 Py_DECREF(item);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000316 if (!converted) {
317 break;
318 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 }
320 } else {
321 Py_BEGIN_ALLOW_THREADS
322 coltype = sqlite3_column_type(self->statement->st, i);
323 Py_END_ALLOW_THREADS
324 if (coltype == SQLITE_NULL) {
325 Py_INCREF(Py_None);
326 converted = Py_None;
327 } else if (coltype == SQLITE_INTEGER) {
328 intval = sqlite3_column_int64(self->statement->st, i);
329 if (intval < INT32_MIN || intval > INT32_MAX) {
330 converted = PyLong_FromLongLong(intval);
331 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000332 converted = PyLong_FromLong((long)intval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333 }
334 } else if (coltype == SQLITE_FLOAT) {
335 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
336 } else if (coltype == SQLITE_TEXT) {
337 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
338 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000339 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000340
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000341 converted = pysqlite_unicode_from_string(val_str,
342 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343
344 if (!converted) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000345 colname = sqlite3_column_name(self->statement->st, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000346 if (!colname) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000347 colname = "<unknown column name>";
348 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000349 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000350 colname , val_str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000351 buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000352 if (!buf_bytes) {
353 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
354 } else {
355 error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
356 if (!error_obj) {
357 PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000358 } else {
359 PyErr_SetObject(pysqlite_OperationalError, error_obj);
Gerhard Häringe871ad592008-03-29 14:11:55 +0000360 Py_DECREF(error_obj);
Gerhard Häring873d9ff2008-02-29 22:22:09 +0000361 }
362 Py_DECREF(buf_bytes);
363 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000365 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
366 converted = PyBytes_FromString(val_str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000367 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
368 converted = PyByteArray_FromStringAndSize(val_str, strlen(val_str));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000370 converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000371 }
372 } else {
373 /* coltype == SQLITE_BLOB */
374 nbytes = sqlite3_column_bytes(self->statement->st, i);
Christian Heimes72b710a2008-05-26 13:28:38 +0000375 buffer = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000376 sqlite3_column_blob(self->statement->st, i), nbytes);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377 if (!buffer) {
378 break;
379 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000380 converted = buffer;
381 }
382 }
383
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000384 if (converted) {
385 PyTuple_SetItem(row, i, converted);
386 } else {
387 Py_INCREF(Py_None);
388 PyTuple_SetItem(row, i, Py_None);
389 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 }
391
392 if (PyErr_Occurred()) {
393 Py_DECREF(row);
394 row = NULL;
395 }
396
397 return row;
398}
399
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000400PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000401{
402 PyObject* operation;
Guido van Rossum83857e32007-05-09 23:37:01 +0000403 const char* operation_cstr;
404 Py_ssize_t operation_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405 PyObject* parameters_list = NULL;
406 PyObject* parameters_iter = NULL;
407 PyObject* parameters = NULL;
408 int i;
409 int rc;
410 PyObject* func_args;
411 PyObject* result;
412 int numcols;
413 PY_LONG_LONG lastrowid;
414 int statement_type;
415 PyObject* descriptor;
416 PyObject* second_argument = NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000417 int allow_8bit_chars;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000418
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000419 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420 return NULL;
421 }
Georg Brandlf78e02b2008-06-10 17:40:04 +0000422
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000423 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
424 allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
425 (self->connection->text_factory != (PyObject*)&PyUnicode_Type && pysqlite_OptimizedUnicode));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426
427 Py_XDECREF(self->next_row);
428 self->next_row = NULL;
429
430 if (multiple) {
431 /* executemany() */
432 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000433 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000434 }
435
Guido van Rossum98297ee2007-11-06 21:34:58 +0000436 if (!PyUnicode_Check(operation)) {
437 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000438 return NULL;
439 }
440
441 if (PyIter_Check(second_argument)) {
442 /* iterator */
443 Py_INCREF(second_argument);
444 parameters_iter = second_argument;
445 } else {
446 /* sequence */
447 parameters_iter = PyObject_GetIter(second_argument);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000448 if (!parameters_iter) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000449 return NULL;
450 }
451 }
452 } else {
453 /* execute() */
454 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000455 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000456 }
457
Guido van Rossum98297ee2007-11-06 21:34:58 +0000458 if (!PyUnicode_Check(operation)) {
459 PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000460 return NULL;
461 }
462
463 parameters_list = PyList_New(0);
464 if (!parameters_list) {
465 return NULL;
466 }
467
468 if (second_argument == NULL) {
469 second_argument = PyTuple_New(0);
470 if (!second_argument) {
471 goto error;
472 }
473 } else {
474 Py_INCREF(second_argument);
475 }
476 if (PyList_Append(parameters_list, second_argument) != 0) {
477 Py_DECREF(second_argument);
478 goto error;
479 }
480 Py_DECREF(second_argument);
481
482 parameters_iter = PyObject_GetIter(parameters_list);
483 if (!parameters_iter) {
484 goto error;
485 }
486 }
487
488 if (self->statement != NULL) {
489 /* There is an active statement */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000490 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000491 }
492
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000493 operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
Guido van Rossumfa9a1212007-08-29 03:34:29 +0000494 if (operation == NULL)
Guido van Rossum83857e32007-05-09 23:37:01 +0000495 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000496
Georg Brandlf78e02b2008-06-10 17:40:04 +0000497 /* reset description and rowcount */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000498 Py_DECREF(self->description);
499 Py_INCREF(Py_None);
500 self->description = Py_None;
Georg Brandlf78e02b2008-06-10 17:40:04 +0000501 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000503 func_args = PyTuple_New(1);
504 if (!func_args) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505 goto error;
506 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000507 Py_INCREF(operation);
508 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
509 goto error;
510 }
511
512 if (self->statement) {
513 (void)pysqlite_statement_reset(self->statement);
514 Py_DECREF(self->statement);
515 }
516
517 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
518 Py_DECREF(func_args);
519
520 if (!self->statement) {
521 goto error;
522 }
523
524 if (self->statement->in_use) {
525 Py_DECREF(self->statement);
526 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
527 if (!self->statement) {
528 goto error;
529 }
530 rc = pysqlite_statement_create(self->statement, self->connection, operation);
531 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000532 Py_CLEAR(self->statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000533 goto error;
534 }
535 }
536
537 pysqlite_statement_reset(self->statement);
538 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539
540 statement_type = detect_statement_type(operation_cstr);
541 if (self->connection->begin_statement) {
542 switch (statement_type) {
543 case STATEMENT_UPDATE:
544 case STATEMENT_DELETE:
545 case STATEMENT_INSERT:
546 case STATEMENT_REPLACE:
547 if (!self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000548 result = _pysqlite_connection_begin(self->connection);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549 if (!result) {
550 goto error;
551 }
552 Py_DECREF(result);
553 }
554 break;
555 case STATEMENT_OTHER:
556 /* it's a DDL statement or something similar
557 - we better COMMIT first so it works for all cases */
558 if (self->connection->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000559 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000560 if (!result) {
561 goto error;
562 }
563 Py_DECREF(result);
564 }
565 break;
566 case STATEMENT_SELECT:
567 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000568 PyErr_SetString(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569 "You cannot execute SELECT statements in executemany().");
570 goto error;
571 }
572 break;
573 }
574 }
575
576 func_args = PyTuple_New(1);
577 if (!func_args) {
578 goto error;
579 }
580 Py_INCREF(operation);
581 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
582 goto error;
583 }
584
585 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000586 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587 Py_DECREF(self->statement);
588 }
589
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000590 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000591 Py_DECREF(func_args);
592
593 if (!self->statement) {
594 goto error;
595 }
596
597 if (self->statement->in_use) {
598 Py_DECREF(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000599 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000600 if (!self->statement) {
601 goto error;
602 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000603 rc = pysqlite_statement_create(self->statement, self->connection, operation);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604 if (rc != SQLITE_OK) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000605 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606 goto error;
607 }
608 }
609
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000610 pysqlite_statement_reset(self->statement);
611 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000612
613 while (1) {
614 parameters = PyIter_Next(parameters_iter);
615 if (!parameters) {
616 break;
617 }
618
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000619 pysqlite_statement_mark_dirty(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000621 pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622 if (PyErr_Occurred()) {
623 goto error;
624 }
625
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000626 if (pysqlite_build_row_cast_map(self) != 0) {
627 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628 goto error;
629 }
630
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000631 /* Keep trying the SQL statement until the schema stops changing. */
632 while (1) {
633 /* Actually execute the SQL statement. */
634 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
635 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
636 /* If it worked, let's get out of the loop */
637 break;
638 }
639 /* Something went wrong. Re-set the statement and try again. */
640 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000641 if (rc == SQLITE_SCHEMA) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000642 /* If this was a result of the schema changing, let's try
643 again. */
644 rc = pysqlite_statement_recompile(self->statement, parameters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000645 if (rc == SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000646 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000648 /* If the database gave us an error, promote it to Python. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000649 (void)pysqlite_statement_reset(self->statement);
650 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651 goto error;
652 }
653 } else {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000654 if (PyErr_Occurred()) {
655 /* there was an error that occurred in a user-defined callback */
656 if (_enable_callback_tracebacks) {
657 PyErr_Print();
658 } else {
659 PyErr_Clear();
660 }
661 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000662 (void)pysqlite_statement_reset(self->statement);
663 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000664 goto error;
665 }
666 }
667
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000668 if (pysqlite_build_row_cast_map(self) != 0) {
669 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
670 goto error;
671 }
672
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000673 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
674 Py_BEGIN_ALLOW_THREADS
675 numcols = sqlite3_column_count(self->statement->st);
676 Py_END_ALLOW_THREADS
677
678 if (self->description == Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000679 Py_BEGIN_ALLOW_THREADS
680 numcols = sqlite3_column_count(self->statement->st);
681 Py_END_ALLOW_THREADS
682
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683 Py_DECREF(self->description);
684 self->description = PyTuple_New(numcols);
685 if (!self->description) {
686 goto error;
687 }
688 for (i = 0; i < numcols; i++) {
689 descriptor = PyTuple_New(7);
690 if (!descriptor) {
691 goto error;
692 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000693 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
695 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
696 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
697 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
698 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
699 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
700 PyTuple_SetItem(self->description, i, descriptor);
701 }
702 }
703 }
704
705 if (rc == SQLITE_ROW) {
706 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000707 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000708 goto error;
709 }
710
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000711 self->next_row = _pysqlite_fetch_one_row(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000712 } else if (rc == SQLITE_DONE && !multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000713 pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000714 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715 }
716
717 switch (statement_type) {
718 case STATEMENT_UPDATE:
719 case STATEMENT_DELETE:
720 case STATEMENT_INSERT:
721 case STATEMENT_REPLACE:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000722 if (self->rowcount == -1L) {
723 self->rowcount = 0L;
724 }
725 self->rowcount += (long)sqlite3_changes(self->connection->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000726 }
727
728 Py_DECREF(self->lastrowid);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000729 if (!multiple && statement_type == STATEMENT_INSERT) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000730 Py_BEGIN_ALLOW_THREADS
731 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
732 Py_END_ALLOW_THREADS
Christian Heimes217cfd12007-12-02 14:31:20 +0000733 self->lastrowid = PyLong_FromLong((long)lastrowid);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734 } else {
735 Py_INCREF(Py_None);
736 self->lastrowid = Py_None;
737 }
738
739 if (multiple) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000740 rc = pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 }
742 Py_XDECREF(parameters);
743 }
744
745error:
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000746 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
747 * ROLLBACK could have happened */
748 #ifdef SQLITE_VERSION_NUMBER
749 #if SQLITE_VERSION_NUMBER >= 3002002
750 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
751 #endif
752 #endif
753
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000754 Py_XDECREF(parameters);
755 Py_XDECREF(parameters_iter);
756 Py_XDECREF(parameters_list);
757
758 if (PyErr_Occurred()) {
Georg Brandlf78e02b2008-06-10 17:40:04 +0000759 self->rowcount = -1L;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000760 return NULL;
761 } else {
762 Py_INCREF(self);
763 return (PyObject*)self;
764 }
765}
766
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000767PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000769 return _pysqlite_query_execute(self, 0, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000770}
771
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000772PyObject* pysqlite_cursor_executemany(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, 1, args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775}
776
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000777PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000778{
779 PyObject* script_obj;
780 PyObject* script_str = NULL;
781 const char* script_cstr;
782 sqlite3_stmt* statement;
783 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000784 PyObject* result;
785 int statement_completed = 0;
786
787 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000788 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000789 }
790
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000791 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000792 return NULL;
793 }
794
Gerhard Häring6d214562007-08-10 18:15:11 +0000795 if (PyUnicode_Check(script_obj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000796 script_cstr = _PyUnicode_AsString(script_obj);
Gerhard Häring6d214562007-08-10 18:15:11 +0000797 if (!script_cstr) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798 return NULL;
799 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000800 } else {
Gerhard Häring6d214562007-08-10 18:15:11 +0000801 PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000802 return NULL;
803 }
804
805 /* commit first */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000806 result = pysqlite_connection_commit(self->connection, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000807 if (!result) {
808 goto error;
809 }
810 Py_DECREF(result);
811
812 while (1) {
813 if (!sqlite3_complete(script_cstr)) {
814 break;
815 }
816 statement_completed = 1;
817
818 rc = sqlite3_prepare(self->connection->db,
819 script_cstr,
820 -1,
821 &statement,
822 &script_cstr);
823 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000824 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825 goto error;
826 }
827
828 /* execute statement, and ignore results of SELECT statements */
829 rc = SQLITE_ROW;
830 while (rc == SQLITE_ROW) {
831 rc = _sqlite_step_with_busyhandler(statement, self->connection);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000832 /* TODO: we probably need more error handling here */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833 }
834
835 if (rc != SQLITE_DONE) {
836 (void)sqlite3_finalize(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000837 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838 goto error;
839 }
840
841 rc = sqlite3_finalize(statement);
842 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000843 _pysqlite_seterror(self->connection->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000844 goto error;
845 }
846 }
847
848error:
849 Py_XDECREF(script_str);
850
851 if (!statement_completed) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000852 PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000853 }
854
855 if (PyErr_Occurred()) {
856 return NULL;
857 } else {
858 Py_INCREF(self);
859 return (PyObject*)self;
860 }
861}
862
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000863PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864{
865 Py_INCREF(self);
866 return (PyObject*)self;
867}
868
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000869PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000870{
871 PyObject* next_row_tuple;
872 PyObject* next_row;
873 int rc;
874
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000875 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000876 return NULL;
877 }
878
879 if (!self->next_row) {
880 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000881 (void)pysqlite_statement_reset(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882 Py_DECREF(self->statement);
883 self->statement = NULL;
884 }
885 return NULL;
886 }
887
888 next_row_tuple = self->next_row;
889 self->next_row = NULL;
890
891 if (self->row_factory != Py_None) {
892 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
893 Py_DECREF(next_row_tuple);
894 } else {
895 next_row = next_row_tuple;
896 }
897
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000898 if (self->statement) {
899 rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection);
900 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000901 (void)pysqlite_statement_reset(self->statement);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000902 Py_DECREF(next_row);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000903 _pysqlite_seterror(self->connection->db, NULL);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000904 return NULL;
905 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000906
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000907 if (rc == SQLITE_ROW) {
908 self->next_row = _pysqlite_fetch_one_row(self);
909 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000910 }
911
912 return next_row;
913}
914
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000915PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000916{
917 PyObject* row;
918
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000919 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000920 if (!row && !PyErr_Occurred()) {
921 Py_INCREF(Py_None);
922 return Py_None;
923 }
924
925 return row;
926}
927
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000928PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000929{
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000930 static char *kwlist[] = {"size", NULL, NULL};
931
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932 PyObject* row;
933 PyObject* list;
934 int maxrows = self->arraysize;
935 int counter = 0;
936
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000937 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
938 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939 }
940
941 list = PyList_New(0);
942 if (!list) {
943 return NULL;
944 }
945
946 /* just make sure we enter the loop */
947 row = Py_None;
948
949 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000950 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000951 if (row) {
952 PyList_Append(list, row);
953 Py_DECREF(row);
954 } else {
955 break;
956 }
957
958 if (++counter == maxrows) {
959 break;
960 }
961 }
962
963 if (PyErr_Occurred()) {
964 Py_DECREF(list);
965 return NULL;
966 } else {
967 return list;
968 }
969}
970
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000971PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000972{
973 PyObject* row;
974 PyObject* list;
975
976 list = PyList_New(0);
977 if (!list) {
978 return NULL;
979 }
980
981 /* just make sure we enter the loop */
982 row = (PyObject*)Py_None;
983
984 while (row) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000985 row = pysqlite_cursor_iternext(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000986 if (row) {
987 PyList_Append(list, row);
988 Py_DECREF(row);
989 }
990 }
991
992 if (PyErr_Occurred()) {
993 Py_DECREF(list);
994 return NULL;
995 } else {
996 return list;
997 }
998}
999
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001000PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001001{
1002 /* don't care, return None */
1003 Py_INCREF(Py_None);
1004 return Py_None;
1005}
1006
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001007PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001008{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001009 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001010 return NULL;
1011 }
1012
1013 if (self->statement) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001014 (void)pysqlite_statement_reset(self->statement);
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001015 Py_CLEAR(self->statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001016 }
1017
1018 Py_INCREF(Py_None);
1019 return Py_None;
1020}
1021
1022static PyMethodDef cursor_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001023 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024 PyDoc_STR("Executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001025 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001026 PyDoc_STR("Repeatedly executes a SQL statement.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001027 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001028 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001029 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001030 PyDoc_STR("Fetches one row from the resultset.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001031 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
Christian Heimesfdab48e2008-01-20 09:06:41 +00001032 PyDoc_STR("Fetches several rows from the resultset.")},
1033 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1034 PyDoc_STR("Fetches all rows from the resultset.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001035 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001036 PyDoc_STR("Closes the cursor.")},
1037 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1038 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1039 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1040 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1041 {NULL, NULL}
1042};
1043
1044static struct PyMemberDef cursor_members[] =
1045{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001046 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1047 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001048 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
Guido van Rossum10f07c42007-08-11 15:32:55 +00001049 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
Georg Brandlf78e02b2008-06-10 17:40:04 +00001050 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001051 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001052 {NULL}
1053};
1054
Thomas Wouters477c8d52006-05-27 19:21:47 +00001055static char cursor_doc[] =
1056PyDoc_STR("SQLite database cursor class.");
1057
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001058PyTypeObject pysqlite_CursorType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001059 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001060 MODULE_NAME ".Cursor", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001061 sizeof(pysqlite_Cursor), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001062 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001063 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001064 0, /* tp_print */
1065 0, /* tp_getattr */
1066 0, /* tp_setattr */
1067 0, /* tp_compare */
1068 0, /* tp_repr */
1069 0, /* tp_as_number */
1070 0, /* tp_as_sequence */
1071 0, /* tp_as_mapping */
1072 0, /* tp_hash */
1073 0, /* tp_call */
1074 0, /* tp_str */
1075 0, /* tp_getattro */
1076 0, /* tp_setattro */
1077 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001078 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001079 cursor_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001080 0, /* tp_traverse */
1081 0, /* tp_clear */
1082 0, /* tp_richcompare */
1083 0, /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001084 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1085 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001086 cursor_methods, /* tp_methods */
1087 cursor_members, /* tp_members */
1088 0, /* tp_getset */
1089 0, /* tp_base */
1090 0, /* tp_dict */
1091 0, /* tp_descr_get */
1092 0, /* tp_descr_set */
1093 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001094 (initproc)pysqlite_cursor_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001095 0, /* tp_alloc */
1096 0, /* tp_new */
1097 0 /* tp_free */
1098};
1099
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001100extern int pysqlite_cursor_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001101{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001102 pysqlite_CursorType.tp_new = PyType_GenericNew;
1103 return PyType_Ready(&pysqlite_CursorType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001104}