• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "clinic/cursor.c.h"
28 
29 /*[clinic input]
30 module _sqlite3
31 class _sqlite3.Cursor "pysqlite_Cursor *" "pysqlite_CursorType"
32 [clinic start generated code]*/
33 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b2072d8db95411d5]*/
34 
35 static const char errmsg_fetch_across_rollback[] = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
36 
37 /*[clinic input]
38 _sqlite3.Cursor.__init__ as pysqlite_cursor_init
39 
40     connection: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType')
41     /
42 
43 [clinic start generated code]*/
44 
45 static int
pysqlite_cursor_init_impl(pysqlite_Cursor * self,pysqlite_Connection * connection)46 pysqlite_cursor_init_impl(pysqlite_Cursor *self,
47                           pysqlite_Connection *connection)
48 /*[clinic end generated code: output=ac59dce49a809ca8 input=a8a4f75ac90999b2]*/
49 {
50     Py_INCREF(connection);
51     Py_XSETREF(self->connection, connection);
52     Py_CLEAR(self->statement);
53     Py_CLEAR(self->next_row);
54     Py_CLEAR(self->row_cast_map);
55 
56     Py_INCREF(Py_None);
57     Py_XSETREF(self->description, Py_None);
58 
59     Py_INCREF(Py_None);
60     Py_XSETREF(self->lastrowid, Py_None);
61 
62     self->arraysize = 1;
63     self->closed = 0;
64     self->reset = 0;
65 
66     self->rowcount = -1L;
67 
68     Py_INCREF(Py_None);
69     Py_XSETREF(self->row_factory, Py_None);
70 
71     if (!pysqlite_check_thread(self->connection)) {
72         return -1;
73     }
74 
75     if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
76         return -1;
77     }
78 
79     self->initialized = 1;
80 
81     return 0;
82 }
83 
84 static int
cursor_traverse(pysqlite_Cursor * self,visitproc visit,void * arg)85 cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
86 {
87     Py_VISIT(Py_TYPE(self));
88     Py_VISIT(self->connection);
89     Py_VISIT(self->description);
90     Py_VISIT(self->row_cast_map);
91     Py_VISIT(self->lastrowid);
92     Py_VISIT(self->row_factory);
93     Py_VISIT(self->statement);
94     Py_VISIT(self->next_row);
95     return 0;
96 }
97 
98 static int
cursor_clear(pysqlite_Cursor * self)99 cursor_clear(pysqlite_Cursor *self)
100 {
101     Py_CLEAR(self->connection);
102     Py_CLEAR(self->description);
103     Py_CLEAR(self->row_cast_map);
104     Py_CLEAR(self->lastrowid);
105     Py_CLEAR(self->row_factory);
106     if (self->statement) {
107         /* Reset the statement if the user has not closed the cursor */
108         pysqlite_statement_reset(self->statement);
109         Py_CLEAR(self->statement);
110     }
111     Py_CLEAR(self->next_row);
112 
113     return 0;
114 }
115 
116 static void
cursor_dealloc(pysqlite_Cursor * self)117 cursor_dealloc(pysqlite_Cursor *self)
118 {
119     PyTypeObject *tp = Py_TYPE(self);
120     PyObject_GC_UnTrack(self);
121     if (self->in_weakreflist != NULL) {
122         PyObject_ClearWeakRefs((PyObject*)self);
123     }
124     tp->tp_clear((PyObject *)self);
125     tp->tp_free(self);
126     Py_DECREF(tp);
127 }
128 
129 static PyObject *
_pysqlite_get_converter(const char * keystr,Py_ssize_t keylen)130 _pysqlite_get_converter(const char *keystr, Py_ssize_t keylen)
131 {
132     PyObject *key;
133     PyObject *upcase_key;
134     PyObject *retval;
135     _Py_IDENTIFIER(upper);
136 
137     key = PyUnicode_FromStringAndSize(keystr, keylen);
138     if (!key) {
139         return NULL;
140     }
141     upcase_key = _PyObject_CallMethodIdNoArgs(key, &PyId_upper);
142     Py_DECREF(key);
143     if (!upcase_key) {
144         return NULL;
145     }
146 
147     retval = PyDict_GetItemWithError(_pysqlite_converters, upcase_key);
148     Py_DECREF(upcase_key);
149 
150     return retval;
151 }
152 
153 static int
pysqlite_build_row_cast_map(pysqlite_Cursor * self)154 pysqlite_build_row_cast_map(pysqlite_Cursor* self)
155 {
156     int i;
157     const char* pos;
158     const char* decltype;
159     PyObject* converter;
160 
161     if (!self->connection->detect_types) {
162         return 0;
163     }
164 
165     Py_XSETREF(self->row_cast_map, PyList_New(0));
166     if (!self->row_cast_map) {
167         return -1;
168     }
169 
170     for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
171         converter = NULL;
172 
173         if (self->connection->detect_types & PARSE_COLNAMES) {
174             const char *colname = sqlite3_column_name(self->statement->st, i);
175             if (colname == NULL) {
176                 PyErr_NoMemory();
177                 Py_CLEAR(self->row_cast_map);
178                 return -1;
179             }
180             const char *type_start = NULL;
181             for (pos = colname; *pos != 0; pos++) {
182                 if (*pos == '[') {
183                     type_start = pos + 1;
184                 }
185                 else if (*pos == ']' && type_start != NULL) {
186                     converter = _pysqlite_get_converter(type_start, pos - type_start);
187                     if (!converter && PyErr_Occurred()) {
188                         Py_CLEAR(self->row_cast_map);
189                         return -1;
190                     }
191                     break;
192                 }
193             }
194         }
195 
196         if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
197             decltype = sqlite3_column_decltype(self->statement->st, i);
198             if (decltype) {
199                 for (pos = decltype;;pos++) {
200                     /* Converter names are split at '(' and blanks.
201                      * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
202                      * 'NUMBER(10)' to be treated as 'NUMBER', for example.
203                      * In other words, it will work as people expect it to work.*/
204                     if (*pos == ' ' || *pos == '(' || *pos == 0) {
205                         converter = _pysqlite_get_converter(decltype, pos - decltype);
206                         if (!converter && PyErr_Occurred()) {
207                             Py_CLEAR(self->row_cast_map);
208                             return -1;
209                         }
210                         break;
211                     }
212                 }
213             }
214         }
215 
216         if (!converter) {
217             converter = Py_None;
218         }
219 
220         if (PyList_Append(self->row_cast_map, converter) != 0) {
221             Py_CLEAR(self->row_cast_map);
222             return -1;
223         }
224     }
225 
226     return 0;
227 }
228 
229 static PyObject *
_pysqlite_build_column_name(pysqlite_Cursor * self,const char * colname)230 _pysqlite_build_column_name(pysqlite_Cursor *self, const char *colname)
231 {
232     const char* pos;
233     Py_ssize_t len;
234 
235     if (self->connection->detect_types & PARSE_COLNAMES) {
236         for (pos = colname; *pos; pos++) {
237             if (*pos == '[') {
238                 if ((pos != colname) && (*(pos-1) == ' ')) {
239                     pos--;
240                 }
241                 break;
242             }
243         }
244         len = pos - colname;
245     }
246     else {
247         len = strlen(colname);
248     }
249     return PyUnicode_FromStringAndSize(colname, len);
250 }
251 
252 /*
253  * Returns a row from the currently active SQLite statement
254  *
255  * Precondidition:
256  * - sqlite3_step() has been called before and it returned SQLITE_ROW.
257  */
258 static PyObject *
_pysqlite_fetch_one_row(pysqlite_Cursor * self)259 _pysqlite_fetch_one_row(pysqlite_Cursor* self)
260 {
261     int i, numcols;
262     PyObject* row;
263     int coltype;
264     PyObject* converter;
265     PyObject* converted;
266     Py_ssize_t nbytes;
267     char buf[200];
268     const char* colname;
269     PyObject* error_msg;
270 
271     if (self->reset) {
272         PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
273         return NULL;
274     }
275 
276     Py_BEGIN_ALLOW_THREADS
277     numcols = sqlite3_data_count(self->statement->st);
278     Py_END_ALLOW_THREADS
279 
280     row = PyTuple_New(numcols);
281     if (!row)
282         return NULL;
283 
284     sqlite3 *db = self->connection->db;
285     for (i = 0; i < numcols; i++) {
286         if (self->connection->detect_types
287                 && self->row_cast_map != NULL
288                 && i < PyList_GET_SIZE(self->row_cast_map))
289         {
290             converter = PyList_GET_ITEM(self->row_cast_map, i);
291         }
292         else {
293             converter = Py_None;
294         }
295 
296         /*
297          * Note, sqlite3_column_bytes() must come after sqlite3_column_blob()
298          * or sqlite3_column_text().
299          *
300          * See https://sqlite.org/c3ref/column_blob.html for details.
301          */
302         if (converter != Py_None) {
303             const void *blob = sqlite3_column_blob(self->statement->st, i);
304             if (blob == NULL) {
305                 if (sqlite3_errcode(db) == SQLITE_NOMEM) {
306                     PyErr_NoMemory();
307                     goto error;
308                 }
309                 converted = Py_NewRef(Py_None);
310             }
311             else {
312                 nbytes = sqlite3_column_bytes(self->statement->st, i);
313                 PyObject *item = PyBytes_FromStringAndSize(blob, nbytes);
314                 if (item == NULL) {
315                     goto error;
316                 }
317                 converted = PyObject_CallOneArg(converter, item);
318                 Py_DECREF(item);
319             }
320         } else {
321             Py_BEGIN_ALLOW_THREADS
322             coltype = sqlite3_column_type(self->statement->st, i);
323             Py_END_ALLOW_THREADS
324             if (coltype == SQLITE_NULL) {
325                 converted = Py_NewRef(Py_None);
326             } else if (coltype == SQLITE_INTEGER) {
327                 converted = PyLong_FromLongLong(sqlite3_column_int64(self->statement->st, i));
328             } else if (coltype == SQLITE_FLOAT) {
329                 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
330             } else if (coltype == SQLITE_TEXT) {
331                 const char *text = (const char*)sqlite3_column_text(self->statement->st, i);
332                 if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
333                     PyErr_NoMemory();
334                     goto error;
335                 }
336 
337                 nbytes = sqlite3_column_bytes(self->statement->st, i);
338                 if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) {
339                     converted = PyUnicode_FromStringAndSize(text, nbytes);
340                     if (!converted && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
341                         PyErr_Clear();
342                         colname = sqlite3_column_name(self->statement->st, i);
343                         if (colname == NULL) {
344                             PyErr_NoMemory();
345                             goto error;
346                         }
347                         PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
348                                      colname , text);
349                         error_msg = PyUnicode_Decode(buf, strlen(buf), "ascii", "replace");
350                         if (!error_msg) {
351                             PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
352                         } else {
353                             PyErr_SetObject(pysqlite_OperationalError, error_msg);
354                             Py_DECREF(error_msg);
355                         }
356                     }
357                 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
358                     converted = PyBytes_FromStringAndSize(text, nbytes);
359                 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
360                     converted = PyByteArray_FromStringAndSize(text, nbytes);
361                 } else {
362                     converted = PyObject_CallFunction(self->connection->text_factory, "y#", text, nbytes);
363                 }
364             } else {
365                 /* coltype == SQLITE_BLOB */
366                 const void *blob = sqlite3_column_blob(self->statement->st, i);
367                 if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
368                     PyErr_NoMemory();
369                     goto error;
370                 }
371 
372                 nbytes = sqlite3_column_bytes(self->statement->st, i);
373                 converted = PyBytes_FromStringAndSize(blob, nbytes);
374             }
375         }
376 
377         if (!converted) {
378             goto error;
379         }
380         PyTuple_SET_ITEM(row, i, converted);
381     }
382 
383     if (PyErr_Occurred())
384         goto error;
385 
386     return row;
387 
388 error:
389     Py_DECREF(row);
390     return NULL;
391 }
392 
393 /*
394  * Checks if a cursor object is usable.
395  *
396  * 0 => error; 1 => ok
397  */
check_cursor(pysqlite_Cursor * cur)398 static int check_cursor(pysqlite_Cursor* cur)
399 {
400     if (!cur->initialized) {
401         PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
402         return 0;
403     }
404 
405     if (cur->closed) {
406         PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
407         return 0;
408     }
409 
410     if (cur->locked) {
411         PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
412         return 0;
413     }
414 
415     return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
416 }
417 
418 static PyObject *
_pysqlite_query_execute(pysqlite_Cursor * self,int multiple,PyObject * operation,PyObject * second_argument)419 _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject* second_argument)
420 {
421     PyObject* parameters_list = NULL;
422     PyObject* parameters_iter = NULL;
423     PyObject* parameters = NULL;
424     int i;
425     int rc;
426     PyObject* func_args;
427     PyObject* result;
428     int numcols;
429     PyObject* column_name;
430     sqlite_int64 lastrowid;
431 
432     if (!check_cursor(self)) {
433         goto error;
434     }
435 
436     self->locked = 1;
437     self->reset = 0;
438 
439     Py_CLEAR(self->next_row);
440 
441     if (multiple) {
442         if (PyIter_Check(second_argument)) {
443             /* iterator */
444             parameters_iter = Py_NewRef(second_argument);
445         } else {
446             /* sequence */
447             parameters_iter = PyObject_GetIter(second_argument);
448             if (!parameters_iter) {
449                 goto error;
450             }
451         }
452     } else {
453         parameters_list = PyList_New(0);
454         if (!parameters_list) {
455             goto error;
456         }
457 
458         if (second_argument == NULL) {
459             second_argument = PyTuple_New(0);
460             if (!second_argument) {
461                 goto error;
462             }
463         } else {
464             Py_INCREF(second_argument);
465         }
466         if (PyList_Append(parameters_list, second_argument) != 0) {
467             Py_DECREF(second_argument);
468             goto error;
469         }
470         Py_DECREF(second_argument);
471 
472         parameters_iter = PyObject_GetIter(parameters_list);
473         if (!parameters_iter) {
474             goto error;
475         }
476     }
477 
478     if (self->statement != NULL) {
479         /* There is an active statement */
480         pysqlite_statement_reset(self->statement);
481     }
482 
483     /* reset description and rowcount */
484     Py_INCREF(Py_None);
485     Py_SETREF(self->description, Py_None);
486     self->rowcount = 0L;
487 
488     func_args = PyTuple_New(1);
489     if (!func_args) {
490         goto error;
491     }
492     if (PyTuple_SetItem(func_args, 0, Py_NewRef(operation)) != 0) {
493         goto error;
494     }
495 
496     if (self->statement) {
497         (void)pysqlite_statement_reset(self->statement);
498     }
499 
500     Py_XSETREF(self->statement,
501               (pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args));
502     Py_DECREF(func_args);
503 
504     if (!self->statement) {
505         goto error;
506     }
507 
508     if (self->statement->in_use) {
509         Py_SETREF(self->statement,
510                   pysqlite_statement_create(self->connection, operation));
511         if (self->statement == NULL) {
512             goto error;
513         }
514     }
515 
516     pysqlite_statement_reset(self->statement);
517     pysqlite_statement_mark_dirty(self->statement);
518 
519     /* We start a transaction implicitly before a DML statement.
520        SELECT is the only exception. See #9924. */
521     if (self->connection->begin_statement && self->statement->is_dml) {
522         if (sqlite3_get_autocommit(self->connection->db)) {
523             result = _pysqlite_connection_begin(self->connection);
524             if (!result) {
525                 goto error;
526             }
527             Py_DECREF(result);
528         }
529     }
530 
531     while (1) {
532         parameters = PyIter_Next(parameters_iter);
533         if (!parameters) {
534             break;
535         }
536 
537         pysqlite_statement_mark_dirty(self->statement);
538 
539         pysqlite_statement_bind_parameters(self->statement, parameters);
540         if (PyErr_Occurred()) {
541             goto error;
542         }
543 
544         rc = pysqlite_step(self->statement->st, self->connection);
545         if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
546             if (PyErr_Occurred()) {
547                 /* there was an error that occurred in a user-defined callback */
548                 if (_pysqlite_enable_callback_tracebacks) {
549                     PyErr_Print();
550                 } else {
551                     PyErr_Clear();
552                 }
553             }
554             (void)pysqlite_statement_reset(self->statement);
555             _pysqlite_seterror(self->connection->db, NULL);
556             goto error;
557         }
558 
559         if (pysqlite_build_row_cast_map(self) != 0) {
560             _PyErr_FormatFromCause(pysqlite_OperationalError, "Error while building row_cast_map");
561             goto error;
562         }
563 
564         assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
565         Py_BEGIN_ALLOW_THREADS
566         numcols = sqlite3_column_count(self->statement->st);
567         Py_END_ALLOW_THREADS
568         if (self->description == Py_None && numcols > 0) {
569             Py_SETREF(self->description, PyTuple_New(numcols));
570             if (!self->description) {
571                 goto error;
572             }
573             for (i = 0; i < numcols; i++) {
574                 const char *colname;
575                 colname = sqlite3_column_name(self->statement->st, i);
576                 if (colname == NULL) {
577                     PyErr_NoMemory();
578                     goto error;
579                 }
580                 column_name = _pysqlite_build_column_name(self, colname);
581                 if (column_name == NULL) {
582                     goto error;
583                 }
584                 PyObject *descriptor = PyTuple_Pack(7, column_name,
585                                                     Py_None, Py_None, Py_None,
586                                                     Py_None, Py_None, Py_None);
587                 Py_DECREF(column_name);
588                 if (descriptor == NULL) {
589                     goto error;
590                 }
591                 PyTuple_SET_ITEM(self->description, i, descriptor);
592             }
593         }
594 
595         if (self->statement->is_dml) {
596             self->rowcount += (long)sqlite3_changes(self->connection->db);
597         } else {
598             self->rowcount= -1L;
599         }
600 
601         if (!multiple) {
602             Py_BEGIN_ALLOW_THREADS
603             lastrowid = sqlite3_last_insert_rowid(self->connection->db);
604             Py_END_ALLOW_THREADS
605             Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid));
606             if (self->lastrowid == NULL) {
607                 goto error;
608             }
609         }
610 
611         if (rc == SQLITE_ROW) {
612             if (multiple) {
613                 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
614                 goto error;
615             }
616 
617             self->next_row = _pysqlite_fetch_one_row(self);
618             if (self->next_row == NULL)
619                 goto error;
620         } else if (rc == SQLITE_DONE && !multiple) {
621             pysqlite_statement_reset(self->statement);
622             Py_CLEAR(self->statement);
623         }
624 
625         if (multiple) {
626             pysqlite_statement_reset(self->statement);
627         }
628         Py_XDECREF(parameters);
629     }
630 
631 error:
632     Py_XDECREF(parameters);
633     Py_XDECREF(parameters_iter);
634     Py_XDECREF(parameters_list);
635 
636     self->locked = 0;
637 
638     if (PyErr_Occurred()) {
639         self->rowcount = -1L;
640         return NULL;
641     } else {
642         return Py_NewRef((PyObject *)self);
643     }
644 }
645 
646 /*[clinic input]
647 _sqlite3.Cursor.execute as pysqlite_cursor_execute
648 
649     sql: unicode
650     parameters: object(c_default = 'NULL') = ()
651     /
652 
653 Executes an SQL statement.
654 [clinic start generated code]*/
655 
656 static PyObject *
pysqlite_cursor_execute_impl(pysqlite_Cursor * self,PyObject * sql,PyObject * parameters)657 pysqlite_cursor_execute_impl(pysqlite_Cursor *self, PyObject *sql,
658                              PyObject *parameters)
659 /*[clinic end generated code: output=d81b4655c7c0bbad input=a8e0200a11627f94]*/
660 {
661     return _pysqlite_query_execute(self, 0, sql, parameters);
662 }
663 
664 /*[clinic input]
665 _sqlite3.Cursor.executemany as pysqlite_cursor_executemany
666 
667     sql: unicode
668     seq_of_parameters: object
669     /
670 
671 Repeatedly executes an SQL statement.
672 [clinic start generated code]*/
673 
674 static PyObject *
pysqlite_cursor_executemany_impl(pysqlite_Cursor * self,PyObject * sql,PyObject * seq_of_parameters)675 pysqlite_cursor_executemany_impl(pysqlite_Cursor *self, PyObject *sql,
676                                  PyObject *seq_of_parameters)
677 /*[clinic end generated code: output=2c65a3c4733fb5d8 input=0d0a52e5eb7ccd35]*/
678 {
679     return _pysqlite_query_execute(self, 1, sql, seq_of_parameters);
680 }
681 
682 /*[clinic input]
683 _sqlite3.Cursor.executescript as pysqlite_cursor_executescript
684 
685     sql_script as script_obj: object
686     /
687 
688 Executes multiple SQL statements at once.
689 [clinic start generated code]*/
690 
691 static PyObject *
pysqlite_cursor_executescript(pysqlite_Cursor * self,PyObject * script_obj)692 pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
693 /*[clinic end generated code: output=115a8132b0f200fe input=75270e5bcdb4d6aa]*/
694 {
695     _Py_IDENTIFIER(commit);
696     const char* script_cstr;
697     sqlite3_stmt* statement;
698     int rc;
699     PyObject* result;
700 
701     if (!check_cursor(self)) {
702         return NULL;
703     }
704 
705     self->reset = 0;
706 
707     if (PyUnicode_Check(script_obj)) {
708         script_cstr = PyUnicode_AsUTF8(script_obj);
709         if (!script_cstr) {
710             return NULL;
711         }
712     } else {
713         PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
714         return NULL;
715     }
716 
717     /* commit first */
718     result = _PyObject_CallMethodIdNoArgs((PyObject *)self->connection, &PyId_commit);
719     if (!result) {
720         goto error;
721     }
722     Py_DECREF(result);
723 
724     while (1) {
725         Py_BEGIN_ALLOW_THREADS
726         rc = sqlite3_prepare_v2(self->connection->db,
727                                 script_cstr,
728                                 -1,
729                                 &statement,
730                                 &script_cstr);
731         Py_END_ALLOW_THREADS
732         if (rc != SQLITE_OK) {
733             _pysqlite_seterror(self->connection->db, NULL);
734             goto error;
735         }
736 
737         /* execute statement, and ignore results of SELECT statements */
738         do {
739             rc = pysqlite_step(statement, self->connection);
740             if (PyErr_Occurred()) {
741                 (void)sqlite3_finalize(statement);
742                 goto error;
743             }
744         } while (rc == SQLITE_ROW);
745 
746         if (rc != SQLITE_DONE) {
747             (void)sqlite3_finalize(statement);
748             _pysqlite_seterror(self->connection->db, NULL);
749             goto error;
750         }
751 
752         rc = sqlite3_finalize(statement);
753         if (rc != SQLITE_OK) {
754             _pysqlite_seterror(self->connection->db, NULL);
755             goto error;
756         }
757 
758         if (*script_cstr == (char)0) {
759             break;
760         }
761     }
762 
763 error:
764     if (PyErr_Occurred()) {
765         return NULL;
766     } else {
767         return Py_NewRef((PyObject *)self);
768     }
769 }
770 
771 static PyObject *
pysqlite_cursor_iternext(pysqlite_Cursor * self)772 pysqlite_cursor_iternext(pysqlite_Cursor *self)
773 {
774     PyObject* next_row_tuple;
775     PyObject* next_row;
776     int rc;
777 
778     if (!check_cursor(self)) {
779         return NULL;
780     }
781 
782     if (self->reset) {
783         PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
784         return NULL;
785     }
786 
787     if (!self->next_row) {
788          if (self->statement) {
789             (void)pysqlite_statement_reset(self->statement);
790             Py_CLEAR(self->statement);
791         }
792         return NULL;
793     }
794 
795     next_row_tuple = self->next_row;
796     assert(next_row_tuple != NULL);
797     self->next_row = NULL;
798 
799     if (self->row_factory != Py_None) {
800         next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
801         if (next_row == NULL) {
802             self->next_row = next_row_tuple;
803             return NULL;
804         }
805         Py_DECREF(next_row_tuple);
806     } else {
807         next_row = next_row_tuple;
808     }
809 
810     if (self->statement) {
811         rc = pysqlite_step(self->statement->st, self->connection);
812         if (PyErr_Occurred()) {
813             (void)pysqlite_statement_reset(self->statement);
814             Py_DECREF(next_row);
815             return NULL;
816         }
817         if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
818             (void)pysqlite_statement_reset(self->statement);
819             Py_DECREF(next_row);
820             _pysqlite_seterror(self->connection->db, NULL);
821             return NULL;
822         }
823 
824         if (rc == SQLITE_ROW) {
825             self->next_row = _pysqlite_fetch_one_row(self);
826             if (self->next_row == NULL) {
827                 (void)pysqlite_statement_reset(self->statement);
828                 return NULL;
829             }
830         }
831     }
832 
833     return next_row;
834 }
835 
836 /*[clinic input]
837 _sqlite3.Cursor.fetchone as pysqlite_cursor_fetchone
838 
839 Fetches one row from the resultset.
840 [clinic start generated code]*/
841 
842 static PyObject *
pysqlite_cursor_fetchone_impl(pysqlite_Cursor * self)843 pysqlite_cursor_fetchone_impl(pysqlite_Cursor *self)
844 /*[clinic end generated code: output=4bd2eabf5baaddb0 input=e78294ec5980fdba]*/
845 {
846     PyObject* row;
847 
848     row = pysqlite_cursor_iternext(self);
849     if (!row && !PyErr_Occurred()) {
850         Py_RETURN_NONE;
851     }
852 
853     return row;
854 }
855 
856 /*[clinic input]
857 _sqlite3.Cursor.fetchmany as pysqlite_cursor_fetchmany
858 
859     size as maxrows: int(c_default='self->arraysize') = 1
860         The default value is set by the Cursor.arraysize attribute.
861 
862 Fetches several rows from the resultset.
863 [clinic start generated code]*/
864 
865 static PyObject *
pysqlite_cursor_fetchmany_impl(pysqlite_Cursor * self,int maxrows)866 pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows)
867 /*[clinic end generated code: output=a8ef31fea64d0906 input=c26e6ca3f34debd0]*/
868 {
869     PyObject* row;
870     PyObject* list;
871     int counter = 0;
872 
873     list = PyList_New(0);
874     if (!list) {
875         return NULL;
876     }
877 
878     while ((row = pysqlite_cursor_iternext(self))) {
879         if (PyList_Append(list, row) < 0) {
880             Py_DECREF(row);
881             break;
882         }
883         Py_DECREF(row);
884 
885         if (++counter == maxrows) {
886             break;
887         }
888     }
889 
890     if (PyErr_Occurred()) {
891         Py_DECREF(list);
892         return NULL;
893     } else {
894         return list;
895     }
896 }
897 
898 /*[clinic input]
899 _sqlite3.Cursor.fetchall as pysqlite_cursor_fetchall
900 
901 Fetches all rows from the resultset.
902 [clinic start generated code]*/
903 
904 static PyObject *
pysqlite_cursor_fetchall_impl(pysqlite_Cursor * self)905 pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self)
906 /*[clinic end generated code: output=d5da12aca2da4b27 input=f5d401086a8df25a]*/
907 {
908     PyObject* row;
909     PyObject* list;
910 
911     list = PyList_New(0);
912     if (!list) {
913         return NULL;
914     }
915 
916     while ((row = pysqlite_cursor_iternext(self))) {
917         if (PyList_Append(list, row) < 0) {
918             Py_DECREF(row);
919             break;
920         }
921         Py_DECREF(row);
922     }
923 
924     if (PyErr_Occurred()) {
925         Py_DECREF(list);
926         return NULL;
927     } else {
928         return list;
929     }
930 }
931 
932 /*[clinic input]
933 _sqlite3.Cursor.setinputsizes as pysqlite_cursor_setinputsizes
934 
935     sizes: object
936     /
937 
938 Required by DB-API. Does nothing in sqlite3.
939 [clinic start generated code]*/
940 
941 static PyObject *
pysqlite_cursor_setinputsizes(pysqlite_Cursor * self,PyObject * sizes)942 pysqlite_cursor_setinputsizes(pysqlite_Cursor *self, PyObject *sizes)
943 /*[clinic end generated code: output=893c817afe9d08ad input=de7950a3aec79bdf]*/
944 {
945     Py_RETURN_NONE;
946 }
947 
948 /*[clinic input]
949 _sqlite3.Cursor.setoutputsize as pysqlite_cursor_setoutputsize
950 
951     size: object
952     column: object = None
953     /
954 
955 Required by DB-API. Does nothing in sqlite3.
956 [clinic start generated code]*/
957 
958 static PyObject *
pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor * self,PyObject * size,PyObject * column)959 pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor *self, PyObject *size,
960                                    PyObject *column)
961 /*[clinic end generated code: output=018d7e9129d45efe input=607a6bece8bbb273]*/
962 {
963     Py_RETURN_NONE;
964 }
965 
966 /*[clinic input]
967 _sqlite3.Cursor.close as pysqlite_cursor_close
968 
969 Closes the cursor.
970 [clinic start generated code]*/
971 
972 static PyObject *
pysqlite_cursor_close_impl(pysqlite_Cursor * self)973 pysqlite_cursor_close_impl(pysqlite_Cursor *self)
974 /*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/
975 {
976     if (!self->connection) {
977         PyErr_SetString(pysqlite_ProgrammingError,
978                         "Base Cursor.__init__ not called.");
979         return NULL;
980     }
981     if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
982         return NULL;
983     }
984 
985     if (self->statement) {
986         (void)pysqlite_statement_reset(self->statement);
987         Py_CLEAR(self->statement);
988     }
989 
990     self->closed = 1;
991 
992     Py_RETURN_NONE;
993 }
994 
995 static PyMethodDef cursor_methods[] = {
996     PYSQLITE_CURSOR_CLOSE_METHODDEF
997     PYSQLITE_CURSOR_EXECUTEMANY_METHODDEF
998     PYSQLITE_CURSOR_EXECUTESCRIPT_METHODDEF
999     PYSQLITE_CURSOR_EXECUTE_METHODDEF
1000     PYSQLITE_CURSOR_FETCHALL_METHODDEF
1001     PYSQLITE_CURSOR_FETCHMANY_METHODDEF
1002     PYSQLITE_CURSOR_FETCHONE_METHODDEF
1003     PYSQLITE_CURSOR_SETINPUTSIZES_METHODDEF
1004     PYSQLITE_CURSOR_SETOUTPUTSIZE_METHODDEF
1005     {NULL, NULL}
1006 };
1007 
1008 static struct PyMemberDef cursor_members[] =
1009 {
1010     {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
1011     {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
1012     {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
1013     {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
1014     {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
1015     {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
1016     {"__weaklistoffset__", T_PYSSIZET, offsetof(pysqlite_Cursor, in_weakreflist), READONLY},
1017     {NULL}
1018 };
1019 
1020 static const char cursor_doc[] =
1021 PyDoc_STR("SQLite database cursor class.");
1022 
1023 static PyType_Slot cursor_slots[] = {
1024     {Py_tp_dealloc, cursor_dealloc},
1025     {Py_tp_doc, (void *)cursor_doc},
1026     {Py_tp_iter, PyObject_SelfIter},
1027     {Py_tp_iternext, pysqlite_cursor_iternext},
1028     {Py_tp_methods, cursor_methods},
1029     {Py_tp_members, cursor_members},
1030     {Py_tp_init, pysqlite_cursor_init},
1031     {Py_tp_traverse, cursor_traverse},
1032     {Py_tp_clear, cursor_clear},
1033     {0, NULL},
1034 };
1035 
1036 static PyType_Spec cursor_spec = {
1037     .name = MODULE_NAME ".Cursor",
1038     .basicsize = sizeof(pysqlite_Cursor),
1039     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
1040               Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
1041     .slots = cursor_slots,
1042 };
1043 
1044 PyTypeObject *pysqlite_CursorType = NULL;
1045 
1046 int
pysqlite_cursor_setup_types(PyObject * module)1047 pysqlite_cursor_setup_types(PyObject *module)
1048 {
1049     pysqlite_CursorType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &cursor_spec, NULL);
1050     if (pysqlite_CursorType == NULL) {
1051         return -1;
1052     }
1053     return 0;
1054 }
1055