Issue #7042: Use a better mechanism for testing timers in test_signal.
[python.git] / Modules / _sqlite / cursor.c
blob6572a559a655a7b4daf3dd369f6834d33cff9977
1 /* cursor.c - the cursor type
3 * Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de>
5 * This file is part of pysqlite.
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.
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:
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.
24 #include "cursor.h"
25 #include "module.h"
26 #include "util.h"
27 #include "sqlitecompat.h"
29 /* used to decide wether to call PyInt_FromLong or PyLong_FromLongLong */
30 #ifndef INT32_MIN
31 #define INT32_MIN (-2147483647 - 1)
32 #endif
33 #ifndef INT32_MAX
34 #define INT32_MAX 2147483647
35 #endif
37 PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
39 static pysqlite_StatementKind detect_statement_type(char* statement)
41 char buf[20];
42 char* src;
43 char* dst;
45 src = statement;
46 /* skip over whitepace */
47 while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') {
48 src++;
51 if (*src == 0)
52 return STATEMENT_INVALID;
54 dst = buf;
55 *dst = 0;
56 while (isalpha(*src) && dst - buf < sizeof(buf) - 2) {
57 *dst++ = tolower(*src++);
60 *dst = 0;
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;
77 int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
79 pysqlite_Connection* connection;
81 if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
83 return -1;
86 Py_INCREF(connection);
87 self->connection = connection;
88 self->statement = NULL;
89 self->next_row = NULL;
91 self->row_cast_map = PyList_New(0);
92 if (!self->row_cast_map) {
93 return -1;
96 Py_INCREF(Py_None);
97 self->description = Py_None;
99 Py_INCREF(Py_None);
100 self->lastrowid= Py_None;
102 self->arraysize = 1;
104 self->rowcount = -1L;
106 Py_INCREF(Py_None);
107 self->row_factory = Py_None;
109 if (!pysqlite_check_thread(self->connection)) {
110 return -1;
113 return 0;
116 void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
118 int rc;
120 /* Reset the statement if the user has not closed the cursor */
121 if (self->statement) {
122 rc = pysqlite_statement_reset(self->statement);
123 Py_DECREF(self->statement);
126 Py_XDECREF(self->connection);
127 Py_XDECREF(self->row_cast_map);
128 Py_XDECREF(self->description);
129 Py_XDECREF(self->lastrowid);
130 Py_XDECREF(self->row_factory);
131 Py_XDECREF(self->next_row);
133 Py_TYPE(self)->tp_free((PyObject*)self);
136 PyObject* _pysqlite_get_converter(PyObject* key)
138 PyObject* upcase_key;
139 PyObject* retval;
141 upcase_key = PyObject_CallMethod(key, "upper", "");
142 if (!upcase_key) {
143 return NULL;
146 retval = PyDict_GetItem(converters, upcase_key);
147 Py_DECREF(upcase_key);
149 return retval;
152 int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
154 int i;
155 const char* type_start = (const char*)-1;
156 const char* pos;
158 const char* colname;
159 const char* decltype;
160 PyObject* py_decltype;
161 PyObject* converter;
162 PyObject* key;
164 if (!self->connection->detect_types) {
165 return 0;
168 Py_XDECREF(self->row_cast_map);
169 self->row_cast_map = PyList_New(0);
171 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
172 converter = NULL;
174 if (self->connection->detect_types & PARSE_COLNAMES) {
175 colname = sqlite3_column_name(self->statement->st, i);
176 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) {
181 key = PyString_FromStringAndSize(type_start, pos - type_start);
182 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;
189 converter = _pysqlite_get_converter(key);
190 Py_DECREF(key);
191 break;
197 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
198 decltype = sqlite3_column_decltype(self->statement->st, i);
199 if (decltype) {
200 for (pos = decltype;;pos++) {
201 /* 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) {
206 py_decltype = PyString_FromStringAndSize(decltype, pos - decltype);
207 if (!py_decltype) {
208 return -1;
210 break;
214 converter = _pysqlite_get_converter(py_decltype);
215 Py_DECREF(py_decltype);
219 if (!converter) {
220 converter = Py_None;
223 if (PyList_Append(self->row_cast_map, converter) != 0) {
224 if (converter != Py_None) {
225 Py_DECREF(converter);
227 Py_XDECREF(self->row_cast_map);
228 self->row_cast_map = NULL;
230 return -1;
234 return 0;
237 PyObject* _pysqlite_build_column_name(const char* colname)
239 const char* pos;
241 if (!colname) {
242 Py_INCREF(Py_None);
243 return Py_None;
246 for (pos = colname;; pos++) {
247 if (*pos == 0 || *pos == '[') {
248 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
249 pos--;
251 return PyString_FromStringAndSize(colname, pos - colname);
256 PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
258 const char* check;
259 int is_ascii = 0;
261 if (optimize) {
262 is_ascii = 1;
264 check = val_str;
265 while (*check) {
266 if (*check & 0x80) {
267 is_ascii = 0;
268 break;
271 check++;
275 if (is_ascii) {
276 return PyString_FromString(val_str);
277 } else {
278 return PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
283 * Returns a row from the currently active SQLite statement
285 * Precondidition:
286 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
288 PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
290 int i, numcols;
291 PyObject* row;
292 PyObject* item = NULL;
293 int coltype;
294 PY_LONG_LONG intval;
295 PyObject* converter;
296 PyObject* converted;
297 Py_ssize_t nbytes;
298 PyObject* buffer;
299 void* raw_buffer;
300 const char* val_str;
301 char buf[200];
302 const char* colname;
304 Py_BEGIN_ALLOW_THREADS
305 numcols = sqlite3_data_count(self->statement->st);
306 Py_END_ALLOW_THREADS
308 row = PyTuple_New(numcols);
309 if (!row) {
310 return NULL;
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;
319 } else {
320 converter = Py_None;
323 if (converter != Py_None) {
324 nbytes = sqlite3_column_bytes(self->statement->st, i);
325 val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
326 if (!val_str) {
327 Py_INCREF(Py_None);
328 converted = Py_None;
329 } else {
330 item = PyString_FromStringAndSize(val_str, nbytes);
331 if (!item) {
332 return NULL;
334 converted = PyObject_CallFunction(converter, "O", item);
335 Py_DECREF(item);
336 if (!converted) {
337 break;
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 {
352 converted = PyInt_FromLong((long)intval);
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);
358 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
359 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
361 converted = pysqlite_unicode_from_string(val_str,
362 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
364 if (!converted) {
365 colname = sqlite3_column_name(self->statement->st, i);
366 if (!colname) {
367 colname = "<unknown column name>";
369 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
370 colname , val_str);
371 PyErr_SetString(pysqlite_OperationalError, buf);
373 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
374 converted = PyString_FromString(val_str);
375 } else {
376 converted = PyObject_CallFunction(self->connection->text_factory, "s", val_str);
378 } else {
379 /* coltype == SQLITE_BLOB */
380 nbytes = sqlite3_column_bytes(self->statement->st, i);
381 buffer = PyBuffer_New(nbytes);
382 if (!buffer) {
383 break;
385 if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &nbytes)) {
386 break;
388 memcpy(raw_buffer, sqlite3_column_blob(self->statement->st, i), nbytes);
389 converted = buffer;
393 if (converted) {
394 PyTuple_SetItem(row, i, converted);
395 } else {
396 Py_INCREF(Py_None);
397 PyTuple_SetItem(row, i, Py_None);
401 if (PyErr_Occurred()) {
402 Py_DECREF(row);
403 row = NULL;
406 return row;
409 PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
411 PyObject* operation;
412 PyObject* operation_bytestr = NULL;
413 char* operation_cstr;
414 PyObject* parameters_list = NULL;
415 PyObject* parameters_iter = NULL;
416 PyObject* parameters = NULL;
417 int i;
418 int rc;
419 PyObject* func_args;
420 PyObject* result;
421 int numcols;
422 PY_LONG_LONG lastrowid;
423 int statement_type;
424 PyObject* descriptor;
425 PyObject* second_argument = NULL;
426 int allow_8bit_chars;
428 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
429 return NULL;
432 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
433 allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
434 (self->connection->text_factory != (PyObject*)&PyUnicode_Type && pysqlite_OptimizedUnicode));
436 Py_XDECREF(self->next_row);
437 self->next_row = NULL;
439 if (multiple) {
440 /* executemany() */
441 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
442 return NULL;
445 if (!PyString_Check(operation) && !PyUnicode_Check(operation)) {
446 PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode");
447 return NULL;
450 if (PyIter_Check(second_argument)) {
451 /* iterator */
452 Py_INCREF(second_argument);
453 parameters_iter = second_argument;
454 } else {
455 /* sequence */
456 parameters_iter = PyObject_GetIter(second_argument);
457 if (!parameters_iter) {
458 return NULL;
461 } else {
462 /* execute() */
463 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
464 return NULL;
467 if (!PyString_Check(operation) && !PyUnicode_Check(operation)) {
468 PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode");
469 return NULL;
472 parameters_list = PyList_New(0);
473 if (!parameters_list) {
474 return NULL;
477 if (second_argument == NULL) {
478 second_argument = PyTuple_New(0);
479 if (!second_argument) {
480 goto error;
482 } else {
483 Py_INCREF(second_argument);
485 if (PyList_Append(parameters_list, second_argument) != 0) {
486 Py_DECREF(second_argument);
487 goto error;
489 Py_DECREF(second_argument);
491 parameters_iter = PyObject_GetIter(parameters_list);
492 if (!parameters_iter) {
493 goto error;
497 if (self->statement != NULL) {
498 /* There is an active statement */
499 rc = pysqlite_statement_reset(self->statement);
502 if (PyString_Check(operation)) {
503 operation_cstr = PyString_AsString(operation);
504 } else {
505 operation_bytestr = PyUnicode_AsUTF8String(operation);
506 if (!operation_bytestr) {
507 goto error;
510 operation_cstr = PyString_AsString(operation_bytestr);
513 /* reset description and rowcount */
514 Py_DECREF(self->description);
515 Py_INCREF(Py_None);
516 self->description = Py_None;
517 self->rowcount = -1L;
519 func_args = PyTuple_New(1);
520 if (!func_args) {
521 goto error;
523 Py_INCREF(operation);
524 if (PyTuple_SetItem(func_args, 0, operation) != 0) {
525 goto error;
528 if (self->statement) {
529 (void)pysqlite_statement_reset(self->statement);
530 Py_DECREF(self->statement);
533 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
534 Py_DECREF(func_args);
536 if (!self->statement) {
537 goto error;
540 if (self->statement->in_use) {
541 Py_DECREF(self->statement);
542 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
543 if (!self->statement) {
544 goto error;
546 rc = pysqlite_statement_create(self->statement, self->connection, operation);
547 if (rc != SQLITE_OK) {
548 Py_CLEAR(self->statement);
549 goto error;
553 pysqlite_statement_reset(self->statement);
554 pysqlite_statement_mark_dirty(self->statement);
556 statement_type = detect_statement_type(operation_cstr);
557 if (self->connection->begin_statement) {
558 switch (statement_type) {
559 case STATEMENT_UPDATE:
560 case STATEMENT_DELETE:
561 case STATEMENT_INSERT:
562 case STATEMENT_REPLACE:
563 if (!self->connection->inTransaction) {
564 result = _pysqlite_connection_begin(self->connection);
565 if (!result) {
566 goto error;
568 Py_DECREF(result);
570 break;
571 case STATEMENT_OTHER:
572 /* it's a DDL statement or something similar
573 - we better COMMIT first so it works for all cases */
574 if (self->connection->inTransaction) {
575 result = pysqlite_connection_commit(self->connection, NULL);
576 if (!result) {
577 goto error;
579 Py_DECREF(result);
581 break;
582 case STATEMENT_SELECT:
583 if (multiple) {
584 PyErr_SetString(pysqlite_ProgrammingError,
585 "You cannot execute SELECT statements in executemany().");
586 goto error;
588 break;
592 while (1) {
593 parameters = PyIter_Next(parameters_iter);
594 if (!parameters) {
595 break;
598 pysqlite_statement_mark_dirty(self->statement);
600 pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
601 if (PyErr_Occurred()) {
602 goto error;
605 /* Keep trying the SQL statement until the schema stops changing. */
606 while (1) {
607 /* Actually execute the SQL statement. */
608 rc = pysqlite_step(self->statement->st, self->connection);
609 if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
610 /* If it worked, let's get out of the loop */
611 break;
613 /* Something went wrong. Re-set the statement and try again. */
614 rc = pysqlite_statement_reset(self->statement);
615 if (rc == SQLITE_SCHEMA) {
616 /* If this was a result of the schema changing, let's try
617 again. */
618 rc = pysqlite_statement_recompile(self->statement, parameters);
619 if (rc == SQLITE_OK) {
620 continue;
621 } else {
622 /* If the database gave us an error, promote it to Python. */
623 (void)pysqlite_statement_reset(self->statement);
624 _pysqlite_seterror(self->connection->db, NULL);
625 goto error;
627 } else {
628 if (PyErr_Occurred()) {
629 /* there was an error that occurred in a user-defined callback */
630 if (_enable_callback_tracebacks) {
631 PyErr_Print();
632 } else {
633 PyErr_Clear();
636 (void)pysqlite_statement_reset(self->statement);
637 _pysqlite_seterror(self->connection->db, NULL);
638 goto error;
642 if (pysqlite_build_row_cast_map(self) != 0) {
643 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
644 goto error;
647 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
648 if (self->description == Py_None) {
649 Py_BEGIN_ALLOW_THREADS
650 numcols = sqlite3_column_count(self->statement->st);
651 Py_END_ALLOW_THREADS
653 Py_DECREF(self->description);
654 self->description = PyTuple_New(numcols);
655 if (!self->description) {
656 goto error;
658 for (i = 0; i < numcols; i++) {
659 descriptor = PyTuple_New(7);
660 if (!descriptor) {
661 goto error;
663 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
664 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
665 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
666 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
667 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
668 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
669 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
670 PyTuple_SetItem(self->description, i, descriptor);
675 if (rc == SQLITE_ROW) {
676 if (multiple) {
677 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
678 goto error;
681 self->next_row = _pysqlite_fetch_one_row(self);
682 } else if (rc == SQLITE_DONE && !multiple) {
683 pysqlite_statement_reset(self->statement);
684 Py_CLEAR(self->statement);
687 switch (statement_type) {
688 case STATEMENT_UPDATE:
689 case STATEMENT_DELETE:
690 case STATEMENT_INSERT:
691 case STATEMENT_REPLACE:
692 if (self->rowcount == -1L) {
693 self->rowcount = 0L;
695 self->rowcount += (long)sqlite3_changes(self->connection->db);
698 Py_DECREF(self->lastrowid);
699 if (!multiple && statement_type == STATEMENT_INSERT) {
700 Py_BEGIN_ALLOW_THREADS
701 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
702 Py_END_ALLOW_THREADS
703 self->lastrowid = PyInt_FromLong((long)lastrowid);
704 } else {
705 Py_INCREF(Py_None);
706 self->lastrowid = Py_None;
709 if (multiple) {
710 rc = pysqlite_statement_reset(self->statement);
712 Py_XDECREF(parameters);
715 error:
716 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
717 * ROLLBACK could have happened */
718 #ifdef SQLITE_VERSION_NUMBER
719 #if SQLITE_VERSION_NUMBER >= 3002002
720 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
721 #endif
722 #endif
724 Py_XDECREF(operation_bytestr);
725 Py_XDECREF(parameters);
726 Py_XDECREF(parameters_iter);
727 Py_XDECREF(parameters_list);
729 if (PyErr_Occurred()) {
730 self->rowcount = -1L;
731 return NULL;
732 } else {
733 Py_INCREF(self);
734 return (PyObject*)self;
738 PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
740 return _pysqlite_query_execute(self, 0, args);
743 PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
745 return _pysqlite_query_execute(self, 1, args);
748 PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
750 PyObject* script_obj;
751 PyObject* script_str = NULL;
752 const char* script_cstr;
753 sqlite3_stmt* statement;
754 int rc;
755 PyObject* result;
756 int statement_completed = 0;
758 if (!PyArg_ParseTuple(args, "O", &script_obj)) {
759 return NULL;
762 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
763 return NULL;
766 if (PyString_Check(script_obj)) {
767 script_cstr = PyString_AsString(script_obj);
768 } else if (PyUnicode_Check(script_obj)) {
769 script_str = PyUnicode_AsUTF8String(script_obj);
770 if (!script_str) {
771 return NULL;
774 script_cstr = PyString_AsString(script_str);
775 } else {
776 PyErr_SetString(PyExc_ValueError, "script argument must be unicode or string.");
777 return NULL;
780 /* commit first */
781 result = pysqlite_connection_commit(self->connection, NULL);
782 if (!result) {
783 goto error;
785 Py_DECREF(result);
787 while (1) {
788 if (!sqlite3_complete(script_cstr)) {
789 break;
791 statement_completed = 1;
793 Py_BEGIN_ALLOW_THREADS
794 rc = sqlite3_prepare(self->connection->db,
795 script_cstr,
797 &statement,
798 &script_cstr);
799 Py_END_ALLOW_THREADS
800 if (rc != SQLITE_OK) {
801 _pysqlite_seterror(self->connection->db, NULL);
802 goto error;
805 /* execute statement, and ignore results of SELECT statements */
806 rc = SQLITE_ROW;
807 while (rc == SQLITE_ROW) {
808 rc = pysqlite_step(statement, self->connection);
809 /* TODO: we probably need more error handling here */
812 if (rc != SQLITE_DONE) {
813 (void)sqlite3_finalize(statement);
814 _pysqlite_seterror(self->connection->db, NULL);
815 goto error;
818 rc = sqlite3_finalize(statement);
819 if (rc != SQLITE_OK) {
820 _pysqlite_seterror(self->connection->db, NULL);
821 goto error;
825 error:
826 Py_XDECREF(script_str);
828 if (!statement_completed) {
829 PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement");
832 if (PyErr_Occurred()) {
833 return NULL;
834 } else {
835 Py_INCREF(self);
836 return (PyObject*)self;
840 PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
842 Py_INCREF(self);
843 return (PyObject*)self;
846 PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
848 PyObject* next_row_tuple;
849 PyObject* next_row;
850 int rc;
852 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
853 return NULL;
856 if (!self->next_row) {
857 if (self->statement) {
858 (void)pysqlite_statement_reset(self->statement);
859 Py_DECREF(self->statement);
860 self->statement = NULL;
862 return NULL;
865 next_row_tuple = self->next_row;
866 self->next_row = NULL;
868 if (self->row_factory != Py_None) {
869 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
870 Py_DECREF(next_row_tuple);
871 } else {
872 next_row = next_row_tuple;
875 if (self->statement) {
876 rc = pysqlite_step(self->statement->st, self->connection);
877 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
878 (void)pysqlite_statement_reset(self->statement);
879 Py_DECREF(next_row);
880 _pysqlite_seterror(self->connection->db, NULL);
881 return NULL;
884 if (rc == SQLITE_ROW) {
885 self->next_row = _pysqlite_fetch_one_row(self);
889 return next_row;
892 PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
894 PyObject* row;
896 row = pysqlite_cursor_iternext(self);
897 if (!row && !PyErr_Occurred()) {
898 Py_INCREF(Py_None);
899 return Py_None;
902 return row;
905 PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
907 static char *kwlist[] = {"size", NULL, NULL};
909 PyObject* row;
910 PyObject* list;
911 int maxrows = self->arraysize;
912 int counter = 0;
914 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
915 return NULL;
918 list = PyList_New(0);
919 if (!list) {
920 return NULL;
923 /* just make sure we enter the loop */
924 row = Py_None;
926 while (row) {
927 row = pysqlite_cursor_iternext(self);
928 if (row) {
929 PyList_Append(list, row);
930 Py_DECREF(row);
931 } else {
932 break;
935 if (++counter == maxrows) {
936 break;
940 if (PyErr_Occurred()) {
941 Py_DECREF(list);
942 return NULL;
943 } else {
944 return list;
948 PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
950 PyObject* row;
951 PyObject* list;
953 list = PyList_New(0);
954 if (!list) {
955 return NULL;
958 /* just make sure we enter the loop */
959 row = (PyObject*)Py_None;
961 while (row) {
962 row = pysqlite_cursor_iternext(self);
963 if (row) {
964 PyList_Append(list, row);
965 Py_DECREF(row);
969 if (PyErr_Occurred()) {
970 Py_DECREF(list);
971 return NULL;
972 } else {
973 return list;
977 PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
979 /* don't care, return None */
980 Py_INCREF(Py_None);
981 return Py_None;
984 PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
986 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
987 return NULL;
990 if (self->statement) {
991 (void)pysqlite_statement_reset(self->statement);
992 Py_CLEAR(self->statement);
995 Py_INCREF(Py_None);
996 return Py_None;
999 static PyMethodDef cursor_methods[] = {
1000 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
1001 PyDoc_STR("Executes a SQL statement.")},
1002 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
1003 PyDoc_STR("Repeatedly executes a SQL statement.")},
1004 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
1005 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
1006 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
1007 PyDoc_STR("Fetches one row from the resultset.")},
1008 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
1009 PyDoc_STR("Fetches several rows from the resultset.")},
1010 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1011 PyDoc_STR("Fetches all rows from the resultset.")},
1012 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
1013 PyDoc_STR("Closes the cursor.")},
1014 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1015 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1016 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1017 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1018 {NULL, NULL}
1021 static struct PyMemberDef cursor_members[] =
1023 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), RO},
1024 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), RO},
1025 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
1026 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), RO},
1027 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), RO},
1028 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
1029 {NULL}
1032 static char cursor_doc[] =
1033 PyDoc_STR("SQLite database cursor class.");
1035 PyTypeObject pysqlite_CursorType = {
1036 PyVarObject_HEAD_INIT(NULL, 0)
1037 MODULE_NAME ".Cursor", /* tp_name */
1038 sizeof(pysqlite_Cursor), /* tp_basicsize */
1039 0, /* tp_itemsize */
1040 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
1041 0, /* tp_print */
1042 0, /* tp_getattr */
1043 0, /* tp_setattr */
1044 0, /* tp_compare */
1045 0, /* tp_repr */
1046 0, /* tp_as_number */
1047 0, /* tp_as_sequence */
1048 0, /* tp_as_mapping */
1049 0, /* tp_hash */
1050 0, /* tp_call */
1051 0, /* tp_str */
1052 0, /* tp_getattro */
1053 0, /* tp_setattro */
1054 0, /* tp_as_buffer */
1055 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_ITER|Py_TPFLAGS_BASETYPE, /* tp_flags */
1056 cursor_doc, /* tp_doc */
1057 0, /* tp_traverse */
1058 0, /* tp_clear */
1059 0, /* tp_richcompare */
1060 0, /* tp_weaklistoffset */
1061 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
1062 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
1063 cursor_methods, /* tp_methods */
1064 cursor_members, /* tp_members */
1065 0, /* tp_getset */
1066 0, /* tp_base */
1067 0, /* tp_dict */
1068 0, /* tp_descr_get */
1069 0, /* tp_descr_set */
1070 0, /* tp_dictoffset */
1071 (initproc)pysqlite_cursor_init, /* tp_init */
1072 0, /* tp_alloc */
1073 0, /* tp_new */
1074 0 /* tp_free */
1077 extern int pysqlite_cursor_setup_types(void)
1079 pysqlite_CursorType.tp_new = PyType_GenericNew;
1080 return PyType_Ready(&pysqlite_CursorType);