1 /* cursor.c - the cursor type
2  *
3  * Copyright (C) 2004-2010 Gerhard H�ring <gh@ghaering.de>
4  *
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 
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
36 
37 PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
38 
39 static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
40 
detect_statement_type(char * statement)41 static pysqlite_StatementKind detect_statement_type(char* statement)
42 {
43     char buf[20];
44     char* src;
45     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;
58     while (isalpha(*src) && dst - buf < sizeof(buf) - 2) {
59         *dst++ = tolower(*src++);
60     }
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 
pysqlite_cursor_init(pysqlite_Cursor * self,PyObject * args,PyObject * kwargs)79 static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
80 {
81     pysqlite_Connection* connection;
82 
83     if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
84     {
85         return -1;
86     }
87 
88     Py_INCREF(connection);
89     self->connection = connection;
90     self->statement = NULL;
91     self->next_row = NULL;
92     self->in_weakreflist = NULL;
93 
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;
106     self->closed = 0;
107     self->reset = 0;
108 
109     self->rowcount = -1L;
110 
111     Py_INCREF(Py_None);
112     self->row_factory = Py_None;
113 
114     if (!pysqlite_check_thread(self->connection)) {
115         return -1;
116     }
117 
118     if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
119         return -1;
120     }
121 
122     self->initialized = 1;
123 
124     return 0;
125 }
126 
pysqlite_cursor_dealloc(pysqlite_Cursor * self)127 static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
128 {
129     int rc;
130 
131     /* Reset the statement if the user has not closed the cursor */
132     if (self->statement) {
133         rc = pysqlite_statement_reset(self->statement);
134         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);
141     Py_XDECREF(self->row_factory);
142     Py_XDECREF(self->next_row);
143 
144     if (self->in_weakreflist != NULL) {
145         PyObject_ClearWeakRefs((PyObject*)self);
146     }
147 
148     self->ob_type->tp_free((PyObject*)self);
149 }
150 
_pysqlite_get_converter(PyObject * key)151 PyObject* _pysqlite_get_converter(PyObject* key)
152 {
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 
pysqlite_build_row_cast_map(pysqlite_Cursor * self)167 int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
168 {
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 
189         if (self->connection->detect_types & PARSE_COLNAMES) {
190             colname = sqlite3_column_name(self->statement->st, i);
191             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) {
196                         key = PyString_FromStringAndSize(type_start, pos - type_start);
197                         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                         }
203 
204                         converter = _pysqlite_get_converter(key);
205                         Py_DECREF(key);
206                         break;
207                     }
208                 }
209             }
210         }
211 
212         if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
213             decltype = sqlite3_column_decltype(self->statement->st, i);
214             if (decltype) {
215                 for (pos = decltype;;pos++) {
216                     /* 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) {
221                         py_decltype = PyString_FromStringAndSize(decltype, pos - decltype);
222                         if (!py_decltype) {
223                             return -1;
224                         }
225                         break;
226                     }
227                 }
228 
229                 converter = _pysqlite_get_converter(py_decltype);
230                 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 
_pysqlite_build_column_name(const char * colname)252 PyObject* _pysqlite_build_column_name(const char* colname)
253 {
254     const char* pos;
255 
256     if (!colname) {
257         Py_INCREF(Py_None);
258         return Py_None;
259     }
260 
261     for (pos = colname;; pos++) {
262         if (*pos == 0 || *pos == '[') {
263             if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
264                 pos--;
265             }
266             return PyString_FromStringAndSize(colname, pos - colname);
267         }
268     }
269 }
270 
pysqlite_unicode_from_string(const char * val_str,int optimize)271 PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
272 {
273     const char* check;
274     int is_ascii = 0;
275 
276     if (optimize) {
277         is_ascii = 1;
278 
279         check = val_str;
280         while (*check) {
281             if (*check & 0x80) {
282                 is_ascii = 0;
283                 break;
284             }
285 
286             check++;
287         }
288     }
289 
290     if (is_ascii) {
291         return PyString_FromString(val_str);
292     } else {
293         return PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
294     }
295 }
296 
297 /*
298  * Returns a row from the currently active SQLite statement
299  *
300  * Precondidition:
301  * - sqlite3_step() has been called before and it returned SQLITE_ROW.
302  */
_pysqlite_fetch_one_row(pysqlite_Cursor * self)303 PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
304 {
305     int i, numcols;
306     PyObject* row;
307     PyObject* item = NULL;
308     int coltype;
309     PY_LONG_LONG intval;
310     PyObject* converter;
311     PyObject* converted;
312     Py_ssize_t nbytes;
313     PyObject* buffer;
314     void* raw_buffer;
315     const char* val_str;
316     char buf[200];
317     const char* colname;
318 
319     if (self->reset) {
320         PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
321         return NULL;
322     }
323 
324     Py_BEGIN_ALLOW_THREADS
325     numcols = sqlite3_data_count(self->statement->st);
326     Py_END_ALLOW_THREADS
327 
328     row = PyTuple_New(numcols);
329     if (!row) {
330         return NULL;
331     }
332 
333     for (i = 0; i < numcols; i++) {
334         if (self->connection->detect_types) {
335             converter = PyList_GetItem(self->row_cast_map, i);
336             if (!converter) {
337                 converter = Py_None;
338             }
339         } else {
340             converter = Py_None;
341         }
342 
343         if (converter != Py_None) {
344             nbytes = sqlite3_column_bytes(self->statement->st, i);
345             val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
346             if (!val_str) {
347                 Py_INCREF(Py_None);
348                 converted = Py_None;
349             } else {
350                 item = PyString_FromStringAndSize(val_str, nbytes);
351                 if (!item) {
352                     return NULL;
353                 }
354                 converted = PyObject_CallFunction(converter, "O", item);
355                 Py_DECREF(item);
356                 if (!converted) {
357                     break;
358                 }
359             }
360         } else {
361             Py_BEGIN_ALLOW_THREADS
362             coltype = sqlite3_column_type(self->statement->st, i);
363             Py_END_ALLOW_THREADS
364             if (coltype == SQLITE_NULL) {
365                 Py_INCREF(Py_None);
366                 converted = Py_None;
367             } else if (coltype == SQLITE_INTEGER) {
368                 intval = sqlite3_column_int64(self->statement->st, i);
369                 if (intval < INT32_MIN || intval > INT32_MAX) {
370                     converted = PyLong_FromLongLong(intval);
371                 } else {
372                     converted = PyInt_FromLong((long)intval);
373                 }
374             } else if (coltype == SQLITE_FLOAT) {
375                 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
376             } else if (coltype == SQLITE_TEXT) {
377                 val_str = (const char*)sqlite3_column_text(self->statement->st, i);
378                 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
379                     || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
380 
381                     converted = pysqlite_unicode_from_string(val_str,
382                         self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
383 
384                     if (!converted) {
385                         colname = sqlite3_column_name(self->statement->st, i);
386                         if (!colname) {
387                             colname = "<unknown column name>";
388                         }
389                         PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
390                                      colname , val_str);
391                         PyErr_SetString(pysqlite_OperationalError, buf);
392                     }
393                 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
394                     converted = PyString_FromString(val_str);
395                 } else {
396                     converted = PyObject_CallFunction(self->connection->text_factory, "s", val_str);
397                 }
398             } else {
399                 /* coltype == SQLITE_BLOB */
400                 nbytes = sqlite3_column_bytes(self->statement->st, i);
401                 buffer = PyBuffer_New(nbytes);
402                 if (!buffer) {
403                     break;
404                 }
405                 if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &nbytes)) {
406                     break;
407                 }
408                 memcpy(raw_buffer, sqlite3_column_blob(self->statement->st, i), nbytes);
409                 converted = buffer;
410             }
411         }
412 
413         if (converted) {
414             PyTuple_SetItem(row, i, converted);
415         } else {
416             Py_INCREF(Py_None);
417             PyTuple_SetItem(row, i, Py_None);
418         }
419     }
420 
421     if (PyErr_Occurred()) {
422         Py_DECREF(row);
423         row = NULL;
424     }
425 
426     return row;
427 }
428 
429 /*
430  * Checks if a cursor object is usable.
431  *
432  * 0 => error; 1 => ok
433  */
check_cursor(pysqlite_Cursor * cur)434 static int check_cursor(pysqlite_Cursor* cur)
435 {
436     if (!cur->initialized) {
437         PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
438         return 0;
439     }
440 
441     if (cur->closed) {
442         PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
443         return 0;
444     } else {
445         return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
446     }
447 }
448 
_pysqlite_query_execute(pysqlite_Cursor * self,int multiple,PyObject * args)449 PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
450 {
451     PyObject* operation;
452     PyObject* operation_bytestr = NULL;
453     char* operation_cstr;
454     PyObject* parameters_list = NULL;
455     PyObject* parameters_iter = NULL;
456     PyObject* parameters = NULL;
457     int i;
458     int rc;
459     PyObject* func_args;
460     PyObject* result;
461     int numcols;
462     PY_LONG_LONG lastrowid;
463     int statement_type;
464     PyObject* descriptor;
465     PyObject* second_argument = NULL;
466     int allow_8bit_chars;
467 
468     if (!check_cursor(self)) {
469         return NULL;
470     }
471 
472     self->reset = 0;
473 
474     /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
475     allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
476         (self->connection->text_factory != pysqlite_OptimizedUnicode));
477 
478     Py_XDECREF(self->next_row);
479     self->next_row = NULL;
480 
481     if (multiple) {
482         /* executemany() */
483         if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
484             return NULL;
485         }
486 
487         if (!PyString_Check(operation) && !PyUnicode_Check(operation)) {
488             PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode");
489             return NULL;
490         }
491 
492         if (PyIter_Check(second_argument)) {
493             /* iterator */
494             Py_INCREF(second_argument);
495             parameters_iter = second_argument;
496         } else {
497             /* sequence */
498             parameters_iter = PyObject_GetIter(second_argument);
499             if (!parameters_iter) {
500                 return NULL;
501             }
502         }
503     } else {
504         /* execute() */
505         if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
506             return NULL;
507         }
508 
509         if (!PyString_Check(operation) && !PyUnicode_Check(operation)) {
510             PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode");
511             return NULL;
512         }
513 
514         parameters_list = PyList_New(0);
515         if (!parameters_list) {
516             return NULL;
517         }
518 
519         if (second_argument == NULL) {
520             second_argument = PyTuple_New(0);
521             if (!second_argument) {
522                 goto error;
523             }
524         } else {
525             Py_INCREF(second_argument);
526         }
527         if (PyList_Append(parameters_list, second_argument) != 0) {
528             Py_DECREF(second_argument);
529             goto error;
530         }
531         Py_DECREF(second_argument);
532 
533         parameters_iter = PyObject_GetIter(parameters_list);
534         if (!parameters_iter) {
535             goto error;
536         }
537     }
538 
539     if (self->statement != NULL) {
540         /* There is an active statement */
541         rc = pysqlite_statement_reset(self->statement);
542     }
543 
544     if (PyString_Check(operation)) {
545         operation_cstr = PyString_AsString(operation);
546     } else {
547         operation_bytestr = PyUnicode_AsUTF8String(operation);
548         if (!operation_bytestr) {
549             goto error;
550         }
551 
552         operation_cstr = PyString_AsString(operation_bytestr);
553     }
554 
555     /* reset description and rowcount */
556     Py_DECREF(self->description);
557     Py_INCREF(Py_None);
558     self->description = Py_None;
559     self->rowcount = -1L;
560 
561     func_args = PyTuple_New(1);
562     if (!func_args) {
563         goto error;
564     }
565     Py_INCREF(operation);
566     if (PyTuple_SetItem(func_args, 0, operation) != 0) {
567         goto error;
568     }
569 
570     if (self->statement) {
571         (void)pysqlite_statement_reset(self->statement);
572         Py_DECREF(self->statement);
573     }
574 
575     self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
576     Py_DECREF(func_args);
577 
578     if (!self->statement) {
579         goto error;
580     }
581 
582     if (self->statement->in_use) {
583         Py_DECREF(self->statement);
584         self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
585         if (!self->statement) {
586             goto error;
587         }
588         rc = pysqlite_statement_create(self->statement, self->connection, operation);
589         if (rc != SQLITE_OK) {
590             Py_CLEAR(self->statement);
591             goto error;
592         }
593     }
594 
595     pysqlite_statement_reset(self->statement);
596     pysqlite_statement_mark_dirty(self->statement);
597 
598     statement_type = detect_statement_type(operation_cstr);
599     if (self->connection->begin_statement) {
600         switch (statement_type) {
601             case STATEMENT_UPDATE:
602             case STATEMENT_DELETE:
603             case STATEMENT_INSERT:
604             case STATEMENT_REPLACE:
605                 if (!self->connection->inTransaction) {
606                     result = _pysqlite_connection_begin(self->connection);
607                     if (!result) {
608                         goto error;
609                     }
610                     Py_DECREF(result);
611                 }
612                 break;
613             case STATEMENT_OTHER:
614                 /* it's a DDL statement or something similar
615                    - we better COMMIT first so it works for all cases */
616                 if (self->connection->inTransaction) {
617                     result = pysqlite_connection_commit(self->connection, NULL);
618                     if (!result) {
619                         goto error;
620                     }
621                     Py_DECREF(result);
622                 }
623                 break;
624             case STATEMENT_SELECT:
625                 if (multiple) {
626                     PyErr_SetString(pysqlite_ProgrammingError,
627                                 "You cannot execute SELECT statements in executemany().");
628                     goto error;
629                 }
630                 break;
631         }
632     }
633 
634     while (1) {
635         parameters = PyIter_Next(parameters_iter);
636         if (!parameters) {
637             break;
638         }
639 
640         pysqlite_statement_mark_dirty(self->statement);
641 
642         pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
643         if (PyErr_Occurred()) {
644             goto error;
645         }
646 
647         /* Keep trying the SQL statement until the schema stops changing. */
648         while (1) {
649             /* Actually execute the SQL statement. */
650             rc = pysqlite_step(self->statement->st, self->connection);
651             if (rc == SQLITE_DONE ||  rc == SQLITE_ROW) {
652                 /* If it worked, let's get out of the loop */
653                 break;
654             }
655             /* Something went wrong.  Re-set the statement and try again. */
656             rc = pysqlite_statement_reset(self->statement);
657             if (rc == SQLITE_SCHEMA) {
658                 /* If this was a result of the schema changing, let's try
659                    again. */
660                 rc = pysqlite_statement_recompile(self->statement, parameters);
661                 if (rc == SQLITE_OK) {
662                     continue;
663                 } else {
664                     /* If the database gave us an error, promote it to Python. */
665                     (void)pysqlite_statement_reset(self->statement);
666                     _pysqlite_seterror(self->connection->db, NULL);
667                     goto error;
668                 }
669             } else {
670                 if (PyErr_Occurred()) {
671                     /* there was an error that occurred in a user-defined callback */
672                     if (_enable_callback_tracebacks) {
673                         PyErr_Print();
674                     } else {
675                         PyErr_Clear();
676                     }
677                 }
678                 (void)pysqlite_statement_reset(self->statement);
679                 _pysqlite_seterror(self->connection->db, NULL);
680                 goto error;
681             }
682         }
683 
684         if (pysqlite_build_row_cast_map(self) != 0) {
685             PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
686             goto error;
687         }
688 
689         if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
690             if (self->description == Py_None) {
691                 Py_BEGIN_ALLOW_THREADS
692                 numcols = sqlite3_column_count(self->statement->st);
693                 Py_END_ALLOW_THREADS
694 
695                 Py_DECREF(self->description);
696                 self->description = PyTuple_New(numcols);
697                 if (!self->description) {
698                     goto error;
699                 }
700                 for (i = 0; i < numcols; i++) {
701                     descriptor = PyTuple_New(7);
702                     if (!descriptor) {
703                         goto error;
704                     }
705                     PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
706                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
707                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
708                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
709                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
710                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
711                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
712                     PyTuple_SetItem(self->description, i, descriptor);
713                 }
714             }
715         }
716 
717         if (rc == SQLITE_ROW) {
718             if (multiple) {
719                 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
720                 goto error;
721             }
722 
723             self->next_row = _pysqlite_fetch_one_row(self);
724         } else if (rc == SQLITE_DONE && !multiple) {
725             pysqlite_statement_reset(self->statement);
726             Py_CLEAR(self->statement);
727         }
728 
729         switch (statement_type) {
730             case STATEMENT_UPDATE:
731             case STATEMENT_DELETE:
732             case STATEMENT_INSERT:
733             case STATEMENT_REPLACE:
734                 if (self->rowcount == -1L) {
735                     self->rowcount = 0L;
736                 }
737                 self->rowcount += (long)sqlite3_changes(self->connection->db);
738         }
739 
740         Py_DECREF(self->lastrowid);
741         if (!multiple && statement_type == STATEMENT_INSERT) {
742             Py_BEGIN_ALLOW_THREADS
743             lastrowid = sqlite3_last_insert_rowid(self->connection->db);
744             Py_END_ALLOW_THREADS
745             self->lastrowid = PyInt_FromLong((long)lastrowid);
746         } else {
747             Py_INCREF(Py_None);
748             self->lastrowid = Py_None;
749         }
750 
751         if (multiple) {
752             rc = pysqlite_statement_reset(self->statement);
753         }
754         Py_XDECREF(parameters);
755     }
756 
757 error:
758     /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
759      * ROLLBACK could have happened */
760     #ifdef SQLITE_VERSION_NUMBER
761     #if SQLITE_VERSION_NUMBER >= 3002002
762     self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
763     #endif
764     #endif
765 
766     Py_XDECREF(operation_bytestr);
767     Py_XDECREF(parameters);
768     Py_XDECREF(parameters_iter);
769     Py_XDECREF(parameters_list);
770 
771     if (PyErr_Occurred()) {
772         self->rowcount = -1L;
773         return NULL;
774     } else {
775         Py_INCREF(self);
776         return (PyObject*)self;
777     }
778 }
779 
pysqlite_cursor_execute(pysqlite_Cursor * self,PyObject * args)780 PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
781 {
782     return _pysqlite_query_execute(self, 0, args);
783 }
784 
pysqlite_cursor_executemany(pysqlite_Cursor * self,PyObject * args)785 PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
786 {
787     return _pysqlite_query_execute(self, 1, args);
788 }
789 
pysqlite_cursor_executescript(pysqlite_Cursor * self,PyObject * args)790 PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
791 {
792     PyObject* script_obj;
793     PyObject* script_str = NULL;
794     const char* script_cstr;
795     sqlite3_stmt* statement;
796     int rc;
797     PyObject* result;
798 
799     if (!PyArg_ParseTuple(args, "O", &script_obj)) {
800         return NULL;
801     }
802 
803     if (!check_cursor(self)) {
804         return NULL;
805     }
806 
807     self->reset = 0;
808 
809     if (PyString_Check(script_obj)) {
810         script_cstr = PyString_AsString(script_obj);
811     } else if (PyUnicode_Check(script_obj)) {
812         script_str = PyUnicode_AsUTF8String(script_obj);
813         if (!script_str) {
814             return NULL;
815         }
816 
817         script_cstr = PyString_AsString(script_str);
818     } else {
819         PyErr_SetString(PyExc_ValueError, "script argument must be unicode or string.");
820         return NULL;
821     }
822 
823     /* commit first */
824     result = pysqlite_connection_commit(self->connection, NULL);
825     if (!result) {
826         goto error;
827     }
828     Py_DECREF(result);
829 
830     while (1) {
831         Py_BEGIN_ALLOW_THREADS
832         rc = sqlite3_prepare(self->connection->db,
833                              script_cstr,
834                              -1,
835                              &statement,
836                              &script_cstr);
837         Py_END_ALLOW_THREADS
838         if (rc != SQLITE_OK) {
839             _pysqlite_seterror(self->connection->db, NULL);
840             goto error;
841         }
842 
843         /* execute statement, and ignore results of SELECT statements */
844         rc = SQLITE_ROW;
845         while (rc == SQLITE_ROW) {
846             rc = pysqlite_step(statement, self->connection);
847             /* TODO: we probably need more error handling here */
848         }
849 
850         if (rc != SQLITE_DONE) {
851             (void)sqlite3_finalize(statement);
852             _pysqlite_seterror(self->connection->db, NULL);
853             goto error;
854         }
855 
856         rc = sqlite3_finalize(statement);
857         if (rc != SQLITE_OK) {
858             _pysqlite_seterror(self->connection->db, NULL);
859             goto error;
860         }
861 
862         if (*script_cstr == (char)0) {
863             break;
864         }
865     }
866 
867 error:
868     Py_XDECREF(script_str);
869 
870     if (PyErr_Occurred()) {
871         return NULL;
872     } else {
873         Py_INCREF(self);
874         return (PyObject*)self;
875     }
876 }
877 
pysqlite_cursor_getiter(pysqlite_Cursor * self)878 PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
879 {
880     Py_INCREF(self);
881     return (PyObject*)self;
882 }
883 
pysqlite_cursor_iternext(pysqlite_Cursor * self)884 PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
885 {
886     PyObject* next_row_tuple;
887     PyObject* next_row;
888     int rc;
889 
890     if (!check_cursor(self)) {
891         return NULL;
892     }
893 
894     if (self->reset) {
895         PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
896         return NULL;
897     }
898 
899     if (!self->next_row) {
900          if (self->statement) {
901             (void)pysqlite_statement_reset(self->statement);
902             Py_DECREF(self->statement);
903             self->statement = NULL;
904         }
905         return NULL;
906     }
907 
908     next_row_tuple = self->next_row;
909     self->next_row = NULL;
910 
911     if (self->row_factory != Py_None) {
912         next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
913         Py_DECREF(next_row_tuple);
914     } else {
915         next_row = next_row_tuple;
916     }
917 
918     if (self->statement) {
919         rc = pysqlite_step(self->statement->st, self->connection);
920         if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
921             (void)pysqlite_statement_reset(self->statement);
922             Py_DECREF(next_row);
923             _pysqlite_seterror(self->connection->db, NULL);
924             return NULL;
925         }
926 
927         if (rc == SQLITE_ROW) {
928             self->next_row = _pysqlite_fetch_one_row(self);
929         }
930     }
931 
932     return next_row;
933 }
934 
pysqlite_cursor_fetchone(pysqlite_Cursor * self,PyObject * args)935 PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
936 {
937     PyObject* row;
938 
939     row = pysqlite_cursor_iternext(self);
940     if (!row && !PyErr_Occurred()) {
941         Py_INCREF(Py_None);
942         return Py_None;
943     }
944 
945     return row;
946 }
947 
pysqlite_cursor_fetchmany(pysqlite_Cursor * self,PyObject * args,PyObject * kwargs)948 PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
949 {
950     static char *kwlist[] = {"size", NULL, NULL};
951 
952     PyObject* row;
953     PyObject* list;
954     int maxrows = self->arraysize;
955     int counter = 0;
956 
957     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
958         return NULL;
959     }
960 
961     list = PyList_New(0);
962     if (!list) {
963         return NULL;
964     }
965 
966     /* just make sure we enter the loop */
967     row = Py_None;
968 
969     while (row) {
970         row = pysqlite_cursor_iternext(self);
971         if (row) {
972             PyList_Append(list, row);
973             Py_DECREF(row);
974         } else {
975             break;
976         }
977 
978         if (++counter == maxrows) {
979             break;
980         }
981     }
982 
983     if (PyErr_Occurred()) {
984         Py_DECREF(list);
985         return NULL;
986     } else {
987         return list;
988     }
989 }
990 
pysqlite_cursor_fetchall(pysqlite_Cursor * self,PyObject * args)991 PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
992 {
993     PyObject* row;
994     PyObject* list;
995 
996     list = PyList_New(0);
997     if (!list) {
998         return NULL;
999     }
1000 
1001     /* just make sure we enter the loop */
1002     row = (PyObject*)Py_None;
1003 
1004     while (row) {
1005         row = pysqlite_cursor_iternext(self);
1006         if (row) {
1007             PyList_Append(list, row);
1008             Py_DECREF(row);
1009         }
1010     }
1011 
1012     if (PyErr_Occurred()) {
1013         Py_DECREF(list);
1014         return NULL;
1015     } else {
1016         return list;
1017     }
1018 }
1019 
pysqlite_noop(pysqlite_Connection * self,PyObject * args)1020 PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
1021 {
1022     /* don't care, return None */
1023     Py_INCREF(Py_None);
1024     return Py_None;
1025 }
1026 
pysqlite_cursor_close(pysqlite_Cursor * self,PyObject * args)1027 PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
1028 {
1029     if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
1030         return NULL;
1031     }
1032 
1033     if (self->statement) {
1034         (void)pysqlite_statement_reset(self->statement);
1035         Py_CLEAR(self->statement);
1036     }
1037 
1038     self->closed = 1;
1039 
1040     Py_INCREF(Py_None);
1041     return Py_None;
1042 }
1043 
1044 static PyMethodDef cursor_methods[] = {
1045     {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
1046         PyDoc_STR("Executes a SQL statement.")},
1047     {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
1048         PyDoc_STR("Repeatedly executes a SQL statement.")},
1049     {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
1050         PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
1051     {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
1052         PyDoc_STR("Fetches one row from the resultset.")},
1053     {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
1054         PyDoc_STR("Fetches several rows from the resultset.")},
1055     {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
1056         PyDoc_STR("Fetches all rows from the resultset.")},
1057     {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
1058         PyDoc_STR("Closes the cursor.")},
1059     {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
1060         PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1061     {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
1062         PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1063     {NULL, NULL}
1064 };
1065 
1066 static struct PyMemberDef cursor_members[] =
1067 {
1068     {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), RO},
1069     {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), RO},
1070     {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
1071     {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), RO},
1072     {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), RO},
1073     {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
1074     {NULL}
1075 };
1076 
1077 static char cursor_doc[] =
1078 PyDoc_STR("SQLite database cursor class.");
1079 
1080 PyTypeObject pysqlite_CursorType = {
1081         PyVarObject_HEAD_INIT(NULL, 0)
1082         MODULE_NAME ".Cursor",                          /* tp_name */
1083         sizeof(pysqlite_Cursor),                        /* tp_basicsize */
1084         0,                                              /* tp_itemsize */
1085         (destructor)pysqlite_cursor_dealloc,            /* tp_dealloc */
1086         0,                                              /* tp_print */
1087         0,                                              /* tp_getattr */
1088         0,                                              /* tp_setattr */
1089         0,                                              /* tp_compare */
1090         0,                                              /* tp_repr */
1091         0,                                              /* tp_as_number */
1092         0,                                              /* tp_as_sequence */
1093         0,                                              /* tp_as_mapping */
1094         0,                                              /* tp_hash */
1095         0,                                              /* tp_call */
1096         0,                                              /* tp_str */
1097         0,                                              /* tp_getattro */
1098         0,                                              /* tp_setattro */
1099         0,                                              /* tp_as_buffer */
1100         Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_ITER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
1101         cursor_doc,                                     /* tp_doc */
1102         0,                                              /* tp_traverse */
1103         0,                                              /* tp_clear */
1104         0,                                              /* tp_richcompare */
1105         offsetof(pysqlite_Cursor, in_weakreflist),      /* tp_weaklistoffset */
1106         (getiterfunc)pysqlite_cursor_getiter,           /* tp_iter */
1107         (iternextfunc)pysqlite_cursor_iternext,         /* tp_iternext */
1108         cursor_methods,                                 /* tp_methods */
1109         cursor_members,                                 /* tp_members */
1110         0,                                              /* tp_getset */
1111         0,                                              /* tp_base */
1112         0,                                              /* tp_dict */
1113         0,                                              /* tp_descr_get */
1114         0,                                              /* tp_descr_set */
1115         0,                                              /* tp_dictoffset */
1116         (initproc)pysqlite_cursor_init,                 /* tp_init */
1117         0,                                              /* tp_alloc */
1118         0,                                              /* tp_new */
1119         0                                               /* tp_free */
1120 };
1121 
pysqlite_cursor_setup_types(void)1122 extern int pysqlite_cursor_setup_types(void)
1123 {
1124     pysqlite_CursorType.tp_new = PyType_GenericNew;
1125     return PyType_Ready(&pysqlite_CursorType);
1126 }
1127