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