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 #ifndef Py_BUILD_CORE_BUILTIN
25 # define Py_BUILD_CORE_MODULE 1
26 #endif
27
28 #include "cursor.h"
29 #include "microprotocols.h"
30 #include "module.h"
31 #include "util.h"
32
33 #include "pycore_pyerrors.h" // _PyErr_FormatFromCause()
34
35 typedef enum {
36 TYPE_LONG,
37 TYPE_FLOAT,
38 TYPE_UNICODE,
39 TYPE_BUFFER,
40 TYPE_UNKNOWN
41 } parameter_type;
42
43 #define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
44 #include "clinic/cursor.c.h"
45 #undef clinic_state
46
47 static inline int
check_cursor_locked(pysqlite_Cursor * cur)48 check_cursor_locked(pysqlite_Cursor *cur)
49 {
50 if (cur->locked) {
51 PyErr_SetString(cur->connection->ProgrammingError,
52 "Recursive use of cursors not allowed.");
53 return 0;
54 }
55 return 1;
56 }
57
58 /*[clinic input]
59 module _sqlite3
60 class _sqlite3.Cursor "pysqlite_Cursor *" "clinic_state()->CursorType"
61 [clinic start generated code]*/
62 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c5b8115c5cf30f1]*/
63
64 /*
65 * Registers a cursor with the connection.
66 *
67 * 0 => error; 1 => ok
68 */
69 static int
register_cursor(pysqlite_Connection * connection,PyObject * cursor)70 register_cursor(pysqlite_Connection *connection, PyObject *cursor)
71 {
72 PyObject *weakref = PyWeakref_NewRef((PyObject *)cursor, NULL);
73 if (weakref == NULL) {
74 return 0;
75 }
76
77 if (PyList_Append(connection->cursors, weakref) < 0) {
78 Py_CLEAR(weakref);
79 return 0;
80 }
81
82 Py_DECREF(weakref);
83 return 1;
84 }
85
86 /*[clinic input]
87 _sqlite3.Cursor.__init__ as pysqlite_cursor_init
88
89 connection: object(type='pysqlite_Connection *', subclass_of='clinic_state()->ConnectionType')
90 /
91
92 [clinic start generated code]*/
93
94 static int
pysqlite_cursor_init_impl(pysqlite_Cursor * self,pysqlite_Connection * connection)95 pysqlite_cursor_init_impl(pysqlite_Cursor *self,
96 pysqlite_Connection *connection)
97 /*[clinic end generated code: output=ac59dce49a809ca8 input=23d4265b534989fb]*/
98 {
99 if (!check_cursor_locked(self)) {
100 return -1;
101 }
102
103 Py_INCREF(connection);
104 Py_XSETREF(self->connection, connection);
105 Py_CLEAR(self->statement);
106 Py_CLEAR(self->row_cast_map);
107
108 Py_INCREF(Py_None);
109 Py_XSETREF(self->description, Py_None);
110
111 Py_INCREF(Py_None);
112 Py_XSETREF(self->lastrowid, Py_None);
113
114 self->arraysize = 1;
115 self->closed = 0;
116 self->rowcount = -1L;
117
118 Py_INCREF(Py_None);
119 Py_XSETREF(self->row_factory, Py_None);
120
121 if (!pysqlite_check_thread(self->connection)) {
122 return -1;
123 }
124
125 if (!register_cursor(connection, (PyObject *)self)) {
126 return -1;
127 }
128
129 self->initialized = 1;
130
131 return 0;
132 }
133
134 static inline int
stmt_reset(pysqlite_Statement * self)135 stmt_reset(pysqlite_Statement *self)
136 {
137 int rc = SQLITE_OK;
138
139 if (self->st != NULL) {
140 Py_BEGIN_ALLOW_THREADS
141 rc = sqlite3_reset(self->st);
142 Py_END_ALLOW_THREADS
143 }
144
145 return rc;
146 }
147
148 static int
cursor_traverse(pysqlite_Cursor * self,visitproc visit,void * arg)149 cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
150 {
151 Py_VISIT(Py_TYPE(self));
152 Py_VISIT(self->connection);
153 Py_VISIT(self->description);
154 Py_VISIT(self->row_cast_map);
155 Py_VISIT(self->lastrowid);
156 Py_VISIT(self->row_factory);
157 Py_VISIT(self->statement);
158 return 0;
159 }
160
161 static int
cursor_clear(pysqlite_Cursor * self)162 cursor_clear(pysqlite_Cursor *self)
163 {
164 Py_CLEAR(self->connection);
165 Py_CLEAR(self->description);
166 Py_CLEAR(self->row_cast_map);
167 Py_CLEAR(self->lastrowid);
168 Py_CLEAR(self->row_factory);
169 if (self->statement) {
170 /* Reset the statement if the user has not closed the cursor */
171 stmt_reset(self->statement);
172 Py_CLEAR(self->statement);
173 }
174
175 return 0;
176 }
177
178 static void
cursor_dealloc(pysqlite_Cursor * self)179 cursor_dealloc(pysqlite_Cursor *self)
180 {
181 PyTypeObject *tp = Py_TYPE(self);
182 PyObject_GC_UnTrack(self);
183 if (self->in_weakreflist != NULL) {
184 PyObject_ClearWeakRefs((PyObject*)self);
185 }
186 tp->tp_clear((PyObject *)self);
187 tp->tp_free(self);
188 Py_DECREF(tp);
189 }
190
191 static PyObject *
_pysqlite_get_converter(pysqlite_state * state,const char * keystr,Py_ssize_t keylen)192 _pysqlite_get_converter(pysqlite_state *state, const char *keystr,
193 Py_ssize_t keylen)
194 {
195 PyObject *key;
196 PyObject *upcase_key;
197 PyObject *retval;
198
199 key = PyUnicode_FromStringAndSize(keystr, keylen);
200 if (!key) {
201 return NULL;
202 }
203 upcase_key = PyObject_CallMethodNoArgs(key, state->str_upper);
204 Py_DECREF(key);
205 if (!upcase_key) {
206 return NULL;
207 }
208
209 retval = PyDict_GetItemWithError(state->converters, upcase_key);
210 Py_DECREF(upcase_key);
211
212 return retval;
213 }
214
215 static int
pysqlite_build_row_cast_map(pysqlite_Cursor * self)216 pysqlite_build_row_cast_map(pysqlite_Cursor* self)
217 {
218 int i;
219 const char* pos;
220 const char* decltype;
221 PyObject* converter;
222
223 if (!self->connection->detect_types) {
224 return 0;
225 }
226
227 Py_XSETREF(self->row_cast_map, PyList_New(0));
228 if (!self->row_cast_map) {
229 return -1;
230 }
231
232 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
233 converter = NULL;
234
235 if (self->connection->detect_types & PARSE_COLNAMES) {
236 const char *colname = sqlite3_column_name(self->statement->st, i);
237 if (colname == NULL) {
238 PyErr_NoMemory();
239 Py_CLEAR(self->row_cast_map);
240 return -1;
241 }
242 const char *type_start = NULL;
243 for (pos = colname; *pos != 0; pos++) {
244 if (*pos == '[') {
245 type_start = pos + 1;
246 }
247 else if (*pos == ']' && type_start != NULL) {
248 pysqlite_state *state = self->connection->state;
249 converter = _pysqlite_get_converter(state, type_start,
250 pos - type_start);
251 if (!converter && PyErr_Occurred()) {
252 Py_CLEAR(self->row_cast_map);
253 return -1;
254 }
255 break;
256 }
257 }
258 }
259
260 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
261 decltype = sqlite3_column_decltype(self->statement->st, i);
262 if (decltype) {
263 for (pos = decltype;;pos++) {
264 /* Converter names are split at '(' and blanks.
265 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
266 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
267 * In other words, it will work as people expect it to work.*/
268 if (*pos == ' ' || *pos == '(' || *pos == 0) {
269 pysqlite_state *state = self->connection->state;
270 converter = _pysqlite_get_converter(state, decltype,
271 pos - decltype);
272 if (!converter && PyErr_Occurred()) {
273 Py_CLEAR(self->row_cast_map);
274 return -1;
275 }
276 break;
277 }
278 }
279 }
280 }
281
282 if (!converter) {
283 converter = Py_None;
284 }
285
286 if (PyList_Append(self->row_cast_map, converter) != 0) {
287 Py_CLEAR(self->row_cast_map);
288 return -1;
289 }
290 }
291
292 return 0;
293 }
294
295 static PyObject *
_pysqlite_build_column_name(pysqlite_Cursor * self,const char * colname)296 _pysqlite_build_column_name(pysqlite_Cursor *self, const char *colname)
297 {
298 const char* pos;
299 Py_ssize_t len;
300
301 if (self->connection->detect_types & PARSE_COLNAMES) {
302 for (pos = colname; *pos; pos++) {
303 if (*pos == '[') {
304 if ((pos != colname) && (*(pos-1) == ' ')) {
305 pos--;
306 }
307 break;
308 }
309 }
310 len = pos - colname;
311 }
312 else {
313 len = strlen(colname);
314 }
315 return PyUnicode_FromStringAndSize(colname, len);
316 }
317
318 /*
319 * Returns a row from the currently active SQLite statement
320 *
321 * Precondidition:
322 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
323 */
324 static PyObject *
_pysqlite_fetch_one_row(pysqlite_Cursor * self)325 _pysqlite_fetch_one_row(pysqlite_Cursor* self)
326 {
327 int i, numcols;
328 PyObject* row;
329 int coltype;
330 PyObject* converter;
331 PyObject* converted;
332 Py_ssize_t nbytes;
333 char buf[200];
334 const char* colname;
335 PyObject* error_msg;
336
337 Py_BEGIN_ALLOW_THREADS
338 numcols = sqlite3_data_count(self->statement->st);
339 Py_END_ALLOW_THREADS
340
341 row = PyTuple_New(numcols);
342 if (!row)
343 return NULL;
344
345 sqlite3 *db = self->connection->db;
346 for (i = 0; i < numcols; i++) {
347 if (self->connection->detect_types
348 && self->row_cast_map != NULL
349 && i < PyList_GET_SIZE(self->row_cast_map))
350 {
351 converter = PyList_GET_ITEM(self->row_cast_map, i);
352 }
353 else {
354 converter = Py_None;
355 }
356
357 /*
358 * Note, sqlite3_column_bytes() must come after sqlite3_column_blob()
359 * or sqlite3_column_text().
360 *
361 * See https://sqlite.org/c3ref/column_blob.html for details.
362 */
363 if (converter != Py_None) {
364 const void *blob = sqlite3_column_blob(self->statement->st, i);
365 if (blob == NULL) {
366 if (sqlite3_errcode(db) == SQLITE_NOMEM) {
367 PyErr_NoMemory();
368 goto error;
369 }
370 converted = Py_NewRef(Py_None);
371 }
372 else {
373 nbytes = sqlite3_column_bytes(self->statement->st, i);
374 PyObject *item = PyBytes_FromStringAndSize(blob, nbytes);
375 if (item == NULL) {
376 goto error;
377 }
378 converted = PyObject_CallOneArg(converter, item);
379 Py_DECREF(item);
380 }
381 } else {
382 Py_BEGIN_ALLOW_THREADS
383 coltype = sqlite3_column_type(self->statement->st, i);
384 Py_END_ALLOW_THREADS
385 if (coltype == SQLITE_NULL) {
386 converted = Py_NewRef(Py_None);
387 } else if (coltype == SQLITE_INTEGER) {
388 converted = PyLong_FromLongLong(sqlite3_column_int64(self->statement->st, i));
389 } else if (coltype == SQLITE_FLOAT) {
390 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
391 } else if (coltype == SQLITE_TEXT) {
392 const char *text = (const char*)sqlite3_column_text(self->statement->st, i);
393 if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
394 PyErr_NoMemory();
395 goto error;
396 }
397
398 nbytes = sqlite3_column_bytes(self->statement->st, i);
399 if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) {
400 converted = PyUnicode_FromStringAndSize(text, nbytes);
401 if (!converted && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
402 PyErr_Clear();
403 colname = sqlite3_column_name(self->statement->st, i);
404 if (colname == NULL) {
405 PyErr_NoMemory();
406 goto error;
407 }
408 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
409 colname , text);
410 error_msg = PyUnicode_Decode(buf, strlen(buf), "ascii", "replace");
411
412 PyObject *exc = self->connection->OperationalError;
413 if (!error_msg) {
414 PyErr_SetString(exc, "Could not decode to UTF-8");
415 } else {
416 PyErr_SetObject(exc, error_msg);
417 Py_DECREF(error_msg);
418 }
419 }
420 } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
421 converted = PyBytes_FromStringAndSize(text, nbytes);
422 } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
423 converted = PyByteArray_FromStringAndSize(text, nbytes);
424 } else {
425 converted = PyObject_CallFunction(self->connection->text_factory, "y#", text, nbytes);
426 }
427 } else {
428 /* coltype == SQLITE_BLOB */
429 const void *blob = sqlite3_column_blob(self->statement->st, i);
430 if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
431 PyErr_NoMemory();
432 goto error;
433 }
434
435 nbytes = sqlite3_column_bytes(self->statement->st, i);
436 converted = PyBytes_FromStringAndSize(blob, nbytes);
437 }
438 }
439
440 if (!converted) {
441 goto error;
442 }
443 PyTuple_SET_ITEM(row, i, converted);
444 }
445
446 if (PyErr_Occurred())
447 goto error;
448
449 return row;
450
451 error:
452 Py_DECREF(row);
453 return NULL;
454 }
455
456 /*
457 * Checks if a cursor object is usable.
458 *
459 * 0 => error; 1 => ok
460 */
check_cursor(pysqlite_Cursor * cur)461 static int check_cursor(pysqlite_Cursor* cur)
462 {
463 if (!cur->initialized) {
464 pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(cur));
465 PyErr_SetString(state->ProgrammingError,
466 "Base Cursor.__init__ not called.");
467 return 0;
468 }
469
470 if (cur->closed) {
471 PyErr_SetString(cur->connection->state->ProgrammingError,
472 "Cannot operate on a closed cursor.");
473 return 0;
474 }
475
476 return (pysqlite_check_thread(cur->connection)
477 && pysqlite_check_connection(cur->connection)
478 && check_cursor_locked(cur));
479 }
480
481 static int
begin_transaction(pysqlite_Connection * self)482 begin_transaction(pysqlite_Connection *self)
483 {
484 assert(self->isolation_level != NULL);
485 int rc;
486
487 Py_BEGIN_ALLOW_THREADS
488 sqlite3_stmt *statement;
489 char begin_stmt[16] = "BEGIN ";
490 #ifdef Py_DEBUG
491 size_t len = strlen(self->isolation_level);
492 assert(len <= 9);
493 #endif
494 (void)strcat(begin_stmt, self->isolation_level);
495 rc = sqlite3_prepare_v2(self->db, begin_stmt, -1, &statement, NULL);
496 if (rc == SQLITE_OK) {
497 (void)sqlite3_step(statement);
498 rc = sqlite3_finalize(statement);
499 }
500 Py_END_ALLOW_THREADS
501
502 if (rc != SQLITE_OK) {
503 (void)_pysqlite_seterror(self->state, self->db);
504 return -1;
505 }
506
507 return 0;
508 }
509
510 static PyObject *
get_statement_from_cache(pysqlite_Cursor * self,PyObject * operation)511 get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation)
512 {
513 PyObject *args[] = { NULL, operation, }; // Borrowed ref.
514 PyObject *cache = self->connection->statement_cache;
515 size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
516 return PyObject_Vectorcall(cache, args + 1, nargsf, NULL);
517 }
518
519 static inline int
stmt_step(sqlite3_stmt * statement)520 stmt_step(sqlite3_stmt *statement)
521 {
522 int rc;
523
524 Py_BEGIN_ALLOW_THREADS
525 rc = sqlite3_step(statement);
526 Py_END_ALLOW_THREADS
527
528 return rc;
529 }
530
531 static int
bind_param(pysqlite_state * state,pysqlite_Statement * self,int pos,PyObject * parameter)532 bind_param(pysqlite_state *state, pysqlite_Statement *self, int pos,
533 PyObject *parameter)
534 {
535 int rc = SQLITE_OK;
536 const char *string;
537 Py_ssize_t buflen;
538 parameter_type paramtype;
539
540 if (parameter == Py_None) {
541 rc = sqlite3_bind_null(self->st, pos);
542 goto final;
543 }
544
545 if (PyLong_CheckExact(parameter)) {
546 paramtype = TYPE_LONG;
547 } else if (PyFloat_CheckExact(parameter)) {
548 paramtype = TYPE_FLOAT;
549 } else if (PyUnicode_CheckExact(parameter)) {
550 paramtype = TYPE_UNICODE;
551 } else if (PyLong_Check(parameter)) {
552 paramtype = TYPE_LONG;
553 } else if (PyFloat_Check(parameter)) {
554 paramtype = TYPE_FLOAT;
555 } else if (PyUnicode_Check(parameter)) {
556 paramtype = TYPE_UNICODE;
557 } else if (PyObject_CheckBuffer(parameter)) {
558 paramtype = TYPE_BUFFER;
559 } else {
560 paramtype = TYPE_UNKNOWN;
561 }
562
563 switch (paramtype) {
564 case TYPE_LONG: {
565 sqlite_int64 value = _pysqlite_long_as_int64(parameter);
566 if (value == -1 && PyErr_Occurred())
567 rc = -1;
568 else
569 rc = sqlite3_bind_int64(self->st, pos, value);
570 break;
571 }
572 case TYPE_FLOAT: {
573 double value = PyFloat_AsDouble(parameter);
574 if (value == -1 && PyErr_Occurred()) {
575 rc = -1;
576 }
577 else {
578 rc = sqlite3_bind_double(self->st, pos, value);
579 }
580 break;
581 }
582 case TYPE_UNICODE:
583 string = PyUnicode_AsUTF8AndSize(parameter, &buflen);
584 if (string == NULL)
585 return -1;
586 if (buflen > INT_MAX) {
587 PyErr_SetString(PyExc_OverflowError,
588 "string longer than INT_MAX bytes");
589 return -1;
590 }
591 rc = sqlite3_bind_text(self->st, pos, string, (int)buflen, SQLITE_TRANSIENT);
592 break;
593 case TYPE_BUFFER: {
594 Py_buffer view;
595 if (PyObject_GetBuffer(parameter, &view, PyBUF_SIMPLE) != 0) {
596 return -1;
597 }
598 if (view.len > INT_MAX) {
599 PyErr_SetString(PyExc_OverflowError,
600 "BLOB longer than INT_MAX bytes");
601 PyBuffer_Release(&view);
602 return -1;
603 }
604 rc = sqlite3_bind_blob(self->st, pos, view.buf, (int)view.len, SQLITE_TRANSIENT);
605 PyBuffer_Release(&view);
606 break;
607 }
608 case TYPE_UNKNOWN:
609 PyErr_Format(state->ProgrammingError,
610 "Error binding parameter %d: type '%s' is not supported",
611 pos, Py_TYPE(parameter)->tp_name);
612 rc = -1;
613 }
614
615 final:
616 return rc;
617 }
618
619 /* returns 0 if the object is one of Python's internal ones that don't need to be adapted */
620 static inline int
need_adapt(pysqlite_state * state,PyObject * obj)621 need_adapt(pysqlite_state *state, PyObject *obj)
622 {
623 if (state->BaseTypeAdapted) {
624 return 1;
625 }
626
627 if (PyLong_CheckExact(obj) || PyFloat_CheckExact(obj)
628 || PyUnicode_CheckExact(obj) || PyByteArray_CheckExact(obj)) {
629 return 0;
630 } else {
631 return 1;
632 }
633 }
634
635 static void
bind_parameters(pysqlite_state * state,pysqlite_Statement * self,PyObject * parameters)636 bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
637 PyObject *parameters)
638 {
639 PyObject* current_param;
640 PyObject* adapted;
641 const char* binding_name;
642 int i;
643 int rc;
644 int num_params_needed;
645 Py_ssize_t num_params;
646
647 Py_BEGIN_ALLOW_THREADS
648 num_params_needed = sqlite3_bind_parameter_count(self->st);
649 Py_END_ALLOW_THREADS
650
651 if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) {
652 /* parameters passed as sequence */
653 if (PyTuple_CheckExact(parameters)) {
654 num_params = PyTuple_GET_SIZE(parameters);
655 } else if (PyList_CheckExact(parameters)) {
656 num_params = PyList_GET_SIZE(parameters);
657 } else {
658 num_params = PySequence_Size(parameters);
659 if (num_params == -1) {
660 return;
661 }
662 }
663 if (num_params != num_params_needed) {
664 PyErr_Format(state->ProgrammingError,
665 "Incorrect number of bindings supplied. The current "
666 "statement uses %d, and there are %zd supplied.",
667 num_params_needed, num_params);
668 return;
669 }
670 for (i = 0; i < num_params; i++) {
671 const char *name = sqlite3_bind_parameter_name(self->st, i+1);
672 if (name != NULL && name[0] != '?') {
673 int ret = PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
674 "Binding %d ('%s') is a named parameter, but you "
675 "supplied a sequence which requires nameless (qmark) "
676 "placeholders. Starting with Python 3.14 an "
677 "sqlite3.ProgrammingError will be raised.",
678 i+1, name);
679 if (ret < 0) {
680 return;
681 }
682 }
683
684 if (PyTuple_CheckExact(parameters)) {
685 PyObject *item = PyTuple_GET_ITEM(parameters, i);
686 current_param = Py_NewRef(item);
687 } else if (PyList_CheckExact(parameters)) {
688 PyObject *item = PyList_GetItem(parameters, i);
689 current_param = Py_XNewRef(item);
690 } else {
691 current_param = PySequence_GetItem(parameters, i);
692 }
693 if (!current_param) {
694 return;
695 }
696
697 if (!need_adapt(state, current_param)) {
698 adapted = current_param;
699 } else {
700 PyObject *protocol = (PyObject *)state->PrepareProtocolType;
701 adapted = pysqlite_microprotocols_adapt(state, current_param,
702 protocol,
703 current_param);
704 Py_DECREF(current_param);
705 if (!adapted) {
706 return;
707 }
708 }
709
710 rc = bind_param(state, self, i + 1, adapted);
711 Py_DECREF(adapted);
712
713 if (rc != SQLITE_OK) {
714 PyObject *exc = PyErr_GetRaisedException();
715 sqlite3 *db = sqlite3_db_handle(self->st);
716 _pysqlite_seterror(state, db);
717 _PyErr_ChainExceptions1(exc);
718 return;
719 }
720 }
721 } else if (PyDict_Check(parameters)) {
722 /* parameters passed as dictionary */
723 for (i = 1; i <= num_params_needed; i++) {
724 Py_BEGIN_ALLOW_THREADS
725 binding_name = sqlite3_bind_parameter_name(self->st, i);
726 Py_END_ALLOW_THREADS
727 if (!binding_name) {
728 PyErr_Format(state->ProgrammingError,
729 "Binding %d has no name, but you supplied a "
730 "dictionary (which has only names).", i);
731 return;
732 }
733
734 binding_name++; /* skip first char (the colon) */
735 PyObject *current_param;
736 (void)PyMapping_GetOptionalItemString(parameters, binding_name, ¤t_param);
737 if (!current_param) {
738 if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_LookupError)) {
739 PyErr_Format(state->ProgrammingError,
740 "You did not supply a value for binding "
741 "parameter :%s.", binding_name);
742 }
743 return;
744 }
745
746 if (!need_adapt(state, current_param)) {
747 adapted = current_param;
748 } else {
749 PyObject *protocol = (PyObject *)state->PrepareProtocolType;
750 adapted = pysqlite_microprotocols_adapt(state, current_param,
751 protocol,
752 current_param);
753 Py_DECREF(current_param);
754 if (!adapted) {
755 return;
756 }
757 }
758
759 rc = bind_param(state, self, i, adapted);
760 Py_DECREF(adapted);
761
762 if (rc != SQLITE_OK) {
763 PyObject *exc = PyErr_GetRaisedException();
764 sqlite3 *db = sqlite3_db_handle(self->st);
765 _pysqlite_seterror(state, db);
766 _PyErr_ChainExceptions1(exc);
767 return;
768 }
769 }
770 } else {
771 PyErr_SetString(state->ProgrammingError,
772 "parameters are of unsupported type");
773 }
774 }
775
776 PyObject *
_pysqlite_query_execute(pysqlite_Cursor * self,int multiple,PyObject * operation,PyObject * second_argument)777 _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject* second_argument)
778 {
779 PyObject* parameters_list = NULL;
780 PyObject* parameters_iter = NULL;
781 PyObject* parameters = NULL;
782 int i;
783 int rc;
784 int numcols;
785 PyObject* column_name;
786
787 if (!check_cursor(self)) {
788 goto error;
789 }
790
791 self->locked = 1;
792
793 if (multiple) {
794 if (PyIter_Check(second_argument)) {
795 /* iterator */
796 parameters_iter = Py_NewRef(second_argument);
797 } else {
798 /* sequence */
799 parameters_iter = PyObject_GetIter(second_argument);
800 if (!parameters_iter) {
801 goto error;
802 }
803 }
804 } else {
805 parameters_list = PyList_New(0);
806 if (!parameters_list) {
807 goto error;
808 }
809
810 if (second_argument == NULL) {
811 second_argument = PyTuple_New(0);
812 if (!second_argument) {
813 goto error;
814 }
815 } else {
816 Py_INCREF(second_argument);
817 }
818 if (PyList_Append(parameters_list, second_argument) != 0) {
819 Py_DECREF(second_argument);
820 goto error;
821 }
822 Py_DECREF(second_argument);
823
824 parameters_iter = PyObject_GetIter(parameters_list);
825 if (!parameters_iter) {
826 goto error;
827 }
828 }
829
830 /* reset description */
831 Py_INCREF(Py_None);
832 Py_SETREF(self->description, Py_None);
833
834 if (self->statement) {
835 // Reset pending statements on this cursor.
836 (void)stmt_reset(self->statement);
837 }
838
839 PyObject *stmt = get_statement_from_cache(self, operation);
840 Py_XSETREF(self->statement, (pysqlite_Statement *)stmt);
841 if (!self->statement) {
842 goto error;
843 }
844
845 pysqlite_state *state = self->connection->state;
846 if (multiple && sqlite3_stmt_readonly(self->statement->st)) {
847 PyErr_SetString(state->ProgrammingError,
848 "executemany() can only execute DML statements.");
849 goto error;
850 }
851
852 if (sqlite3_stmt_busy(self->statement->st)) {
853 Py_SETREF(self->statement,
854 pysqlite_statement_create(self->connection, operation));
855 if (self->statement == NULL) {
856 goto error;
857 }
858 }
859
860 (void)stmt_reset(self->statement);
861 self->rowcount = self->statement->is_dml ? 0L : -1L;
862
863 /* We start a transaction implicitly before a DML statement.
864 SELECT is the only exception. See #9924. */
865 if (self->connection->autocommit == AUTOCOMMIT_LEGACY
866 && self->connection->isolation_level
867 && self->statement->is_dml
868 && sqlite3_get_autocommit(self->connection->db))
869 {
870 if (begin_transaction(self->connection) < 0) {
871 goto error;
872 }
873 }
874
875 assert(!sqlite3_stmt_busy(self->statement->st));
876 while (1) {
877 parameters = PyIter_Next(parameters_iter);
878 if (!parameters) {
879 break;
880 }
881
882 bind_parameters(state, self->statement, parameters);
883 if (PyErr_Occurred()) {
884 goto error;
885 }
886
887 rc = stmt_step(self->statement->st);
888 if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
889 if (PyErr_Occurred()) {
890 /* there was an error that occurred in a user-defined callback */
891 if (state->enable_callback_tracebacks) {
892 PyErr_Print();
893 } else {
894 PyErr_Clear();
895 }
896 }
897 _pysqlite_seterror(state, self->connection->db);
898 goto error;
899 }
900
901 if (pysqlite_build_row_cast_map(self) != 0) {
902 _PyErr_FormatFromCause(state->OperationalError,
903 "Error while building row_cast_map");
904 goto error;
905 }
906
907 assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
908 Py_BEGIN_ALLOW_THREADS
909 numcols = sqlite3_column_count(self->statement->st);
910 Py_END_ALLOW_THREADS
911 if (self->description == Py_None && numcols > 0) {
912 Py_SETREF(self->description, PyTuple_New(numcols));
913 if (!self->description) {
914 goto error;
915 }
916 for (i = 0; i < numcols; i++) {
917 const char *colname;
918 colname = sqlite3_column_name(self->statement->st, i);
919 if (colname == NULL) {
920 PyErr_NoMemory();
921 goto error;
922 }
923 column_name = _pysqlite_build_column_name(self, colname);
924 if (column_name == NULL) {
925 goto error;
926 }
927 PyObject *descriptor = PyTuple_Pack(7, column_name,
928 Py_None, Py_None, Py_None,
929 Py_None, Py_None, Py_None);
930 Py_DECREF(column_name);
931 if (descriptor == NULL) {
932 goto error;
933 }
934 PyTuple_SET_ITEM(self->description, i, descriptor);
935 }
936 }
937
938 if (rc == SQLITE_DONE) {
939 if (self->statement->is_dml) {
940 self->rowcount += (long)sqlite3_changes(self->connection->db);
941 }
942 stmt_reset(self->statement);
943 }
944 Py_XDECREF(parameters);
945 }
946
947 if (!multiple) {
948 sqlite_int64 lastrowid;
949
950 Py_BEGIN_ALLOW_THREADS
951 lastrowid = sqlite3_last_insert_rowid(self->connection->db);
952 Py_END_ALLOW_THREADS
953
954 Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid));
955 // Fall through on error.
956 }
957
958 error:
959 Py_XDECREF(parameters);
960 Py_XDECREF(parameters_iter);
961 Py_XDECREF(parameters_list);
962
963 self->locked = 0;
964
965 if (PyErr_Occurred()) {
966 if (self->statement) {
967 (void)stmt_reset(self->statement);
968 Py_CLEAR(self->statement);
969 }
970 self->rowcount = -1L;
971 return NULL;
972 }
973 if (self->statement && !sqlite3_stmt_busy(self->statement->st)) {
974 Py_CLEAR(self->statement);
975 }
976 return Py_NewRef((PyObject *)self);
977 }
978
979 /*[clinic input]
980 _sqlite3.Cursor.execute as pysqlite_cursor_execute
981
982 sql: unicode
983 parameters: object(c_default = 'NULL') = ()
984 /
985
986 Executes an SQL statement.
987 [clinic start generated code]*/
988
989 static PyObject *
pysqlite_cursor_execute_impl(pysqlite_Cursor * self,PyObject * sql,PyObject * parameters)990 pysqlite_cursor_execute_impl(pysqlite_Cursor *self, PyObject *sql,
991 PyObject *parameters)
992 /*[clinic end generated code: output=d81b4655c7c0bbad input=a8e0200a11627f94]*/
993 {
994 return _pysqlite_query_execute(self, 0, sql, parameters);
995 }
996
997 /*[clinic input]
998 _sqlite3.Cursor.executemany as pysqlite_cursor_executemany
999
1000 sql: unicode
1001 seq_of_parameters: object
1002 /
1003
1004 Repeatedly executes an SQL statement.
1005 [clinic start generated code]*/
1006
1007 static PyObject *
pysqlite_cursor_executemany_impl(pysqlite_Cursor * self,PyObject * sql,PyObject * seq_of_parameters)1008 pysqlite_cursor_executemany_impl(pysqlite_Cursor *self, PyObject *sql,
1009 PyObject *seq_of_parameters)
1010 /*[clinic end generated code: output=2c65a3c4733fb5d8 input=0d0a52e5eb7ccd35]*/
1011 {
1012 return _pysqlite_query_execute(self, 1, sql, seq_of_parameters);
1013 }
1014
1015 /*[clinic input]
1016 _sqlite3.Cursor.executescript as pysqlite_cursor_executescript
1017
1018 sql_script: str
1019 /
1020
1021 Executes multiple SQL statements at once.
1022 [clinic start generated code]*/
1023
1024 static PyObject *
pysqlite_cursor_executescript_impl(pysqlite_Cursor * self,const char * sql_script)1025 pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
1026 const char *sql_script)
1027 /*[clinic end generated code: output=8fd726dde1c65164 input=78f093be415a8a2c]*/
1028 {
1029 if (!check_cursor(self)) {
1030 return NULL;
1031 }
1032
1033 size_t sql_len = strlen(sql_script);
1034 int max_length = sqlite3_limit(self->connection->db,
1035 SQLITE_LIMIT_SQL_LENGTH, -1);
1036 if (sql_len > (unsigned)max_length) {
1037 PyErr_SetString(self->connection->DataError,
1038 "query string is too large");
1039 return NULL;
1040 }
1041
1042 // Commit if needed
1043 sqlite3 *db = self->connection->db;
1044 if (self->connection->autocommit == AUTOCOMMIT_LEGACY
1045 && !sqlite3_get_autocommit(db))
1046 {
1047 int rc = SQLITE_OK;
1048
1049 Py_BEGIN_ALLOW_THREADS
1050 rc = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
1051 Py_END_ALLOW_THREADS
1052
1053 if (rc != SQLITE_OK) {
1054 goto error;
1055 }
1056 }
1057
1058 while (1) {
1059 int rc;
1060 const char *tail;
1061
1062 Py_BEGIN_ALLOW_THREADS
1063 sqlite3_stmt *stmt;
1064 rc = sqlite3_prepare_v2(db, sql_script, (int)sql_len + 1, &stmt,
1065 &tail);
1066 if (rc == SQLITE_OK) {
1067 do {
1068 rc = sqlite3_step(stmt);
1069 } while (rc == SQLITE_ROW);
1070 rc = sqlite3_finalize(stmt);
1071 }
1072 Py_END_ALLOW_THREADS
1073
1074 if (rc != SQLITE_OK) {
1075 goto error;
1076 }
1077
1078 if (*tail == (char)0) {
1079 break;
1080 }
1081 sql_len -= (tail - sql_script);
1082 sql_script = tail;
1083 }
1084
1085 return Py_NewRef((PyObject *)self);
1086
1087 error:
1088 _pysqlite_seterror(self->connection->state, db);
1089 return NULL;
1090 }
1091
1092 static PyObject *
pysqlite_cursor_iternext(pysqlite_Cursor * self)1093 pysqlite_cursor_iternext(pysqlite_Cursor *self)
1094 {
1095 if (!check_cursor(self)) {
1096 return NULL;
1097 }
1098
1099 if (self->statement == NULL) {
1100 return NULL;
1101 }
1102
1103 sqlite3_stmt *stmt = self->statement->st;
1104 assert(stmt != NULL);
1105 assert(sqlite3_data_count(stmt) != 0);
1106
1107 self->locked = 1; // GH-80254: Prevent recursive use of cursors.
1108 PyObject *row = _pysqlite_fetch_one_row(self);
1109 self->locked = 0;
1110 if (row == NULL) {
1111 return NULL;
1112 }
1113 int rc = stmt_step(stmt);
1114 if (rc == SQLITE_DONE) {
1115 if (self->statement->is_dml) {
1116 self->rowcount = (long)sqlite3_changes(self->connection->db);
1117 }
1118 (void)stmt_reset(self->statement);
1119 Py_CLEAR(self->statement);
1120 }
1121 else if (rc != SQLITE_ROW) {
1122 (void)_pysqlite_seterror(self->connection->state,
1123 self->connection->db);
1124 (void)stmt_reset(self->statement);
1125 Py_CLEAR(self->statement);
1126 Py_DECREF(row);
1127 return NULL;
1128 }
1129 if (!Py_IsNone(self->row_factory)) {
1130 PyObject *factory = self->row_factory;
1131 PyObject *args[] = { (PyObject *)self, row, };
1132 PyObject *new_row = PyObject_Vectorcall(factory, args, 2, NULL);
1133 Py_SETREF(row, new_row);
1134 }
1135 return row;
1136 }
1137
1138 /*[clinic input]
1139 _sqlite3.Cursor.fetchone as pysqlite_cursor_fetchone
1140
1141 Fetches one row from the resultset.
1142 [clinic start generated code]*/
1143
1144 static PyObject *
pysqlite_cursor_fetchone_impl(pysqlite_Cursor * self)1145 pysqlite_cursor_fetchone_impl(pysqlite_Cursor *self)
1146 /*[clinic end generated code: output=4bd2eabf5baaddb0 input=e78294ec5980fdba]*/
1147 {
1148 PyObject* row;
1149
1150 row = pysqlite_cursor_iternext(self);
1151 if (!row && !PyErr_Occurred()) {
1152 Py_RETURN_NONE;
1153 }
1154
1155 return row;
1156 }
1157
1158 /*[clinic input]
1159 _sqlite3.Cursor.fetchmany as pysqlite_cursor_fetchmany
1160
1161 size as maxrows: int(c_default='self->arraysize') = 1
1162 The default value is set by the Cursor.arraysize attribute.
1163
1164 Fetches several rows from the resultset.
1165 [clinic start generated code]*/
1166
1167 static PyObject *
pysqlite_cursor_fetchmany_impl(pysqlite_Cursor * self,int maxrows)1168 pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows)
1169 /*[clinic end generated code: output=a8ef31fea64d0906 input=c26e6ca3f34debd0]*/
1170 {
1171 PyObject* row;
1172 PyObject* list;
1173 int counter = 0;
1174
1175 list = PyList_New(0);
1176 if (!list) {
1177 return NULL;
1178 }
1179
1180 while ((row = pysqlite_cursor_iternext(self))) {
1181 if (PyList_Append(list, row) < 0) {
1182 Py_DECREF(row);
1183 break;
1184 }
1185 Py_DECREF(row);
1186
1187 if (++counter == maxrows) {
1188 break;
1189 }
1190 }
1191
1192 if (PyErr_Occurred()) {
1193 Py_DECREF(list);
1194 return NULL;
1195 } else {
1196 return list;
1197 }
1198 }
1199
1200 /*[clinic input]
1201 _sqlite3.Cursor.fetchall as pysqlite_cursor_fetchall
1202
1203 Fetches all rows from the resultset.
1204 [clinic start generated code]*/
1205
1206 static PyObject *
pysqlite_cursor_fetchall_impl(pysqlite_Cursor * self)1207 pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self)
1208 /*[clinic end generated code: output=d5da12aca2da4b27 input=f5d401086a8df25a]*/
1209 {
1210 PyObject* row;
1211 PyObject* list;
1212
1213 list = PyList_New(0);
1214 if (!list) {
1215 return NULL;
1216 }
1217
1218 while ((row = pysqlite_cursor_iternext(self))) {
1219 if (PyList_Append(list, row) < 0) {
1220 Py_DECREF(row);
1221 break;
1222 }
1223 Py_DECREF(row);
1224 }
1225
1226 if (PyErr_Occurred()) {
1227 Py_DECREF(list);
1228 return NULL;
1229 } else {
1230 return list;
1231 }
1232 }
1233
1234 /*[clinic input]
1235 _sqlite3.Cursor.setinputsizes as pysqlite_cursor_setinputsizes
1236
1237 sizes: object
1238 /
1239
1240 Required by DB-API. Does nothing in sqlite3.
1241 [clinic start generated code]*/
1242
1243 static PyObject *
pysqlite_cursor_setinputsizes(pysqlite_Cursor * self,PyObject * sizes)1244 pysqlite_cursor_setinputsizes(pysqlite_Cursor *self, PyObject *sizes)
1245 /*[clinic end generated code: output=893c817afe9d08ad input=de7950a3aec79bdf]*/
1246 {
1247 Py_RETURN_NONE;
1248 }
1249
1250 /*[clinic input]
1251 _sqlite3.Cursor.setoutputsize as pysqlite_cursor_setoutputsize
1252
1253 size: object
1254 column: object = None
1255 /
1256
1257 Required by DB-API. Does nothing in sqlite3.
1258 [clinic start generated code]*/
1259
1260 static PyObject *
pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor * self,PyObject * size,PyObject * column)1261 pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor *self, PyObject *size,
1262 PyObject *column)
1263 /*[clinic end generated code: output=018d7e9129d45efe input=607a6bece8bbb273]*/
1264 {
1265 Py_RETURN_NONE;
1266 }
1267
1268 /*[clinic input]
1269 _sqlite3.Cursor.close as pysqlite_cursor_close
1270
1271 Closes the cursor.
1272 [clinic start generated code]*/
1273
1274 static PyObject *
pysqlite_cursor_close_impl(pysqlite_Cursor * self)1275 pysqlite_cursor_close_impl(pysqlite_Cursor *self)
1276 /*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/
1277 {
1278 if (!check_cursor_locked(self)) {
1279 return NULL;
1280 }
1281
1282 if (!self->connection) {
1283 PyTypeObject *tp = Py_TYPE(self);
1284 pysqlite_state *state = pysqlite_get_state_by_type(tp);
1285 PyErr_SetString(state->ProgrammingError,
1286 "Base Cursor.__init__ not called.");
1287 return NULL;
1288 }
1289 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
1290 return NULL;
1291 }
1292
1293 if (self->statement) {
1294 (void)stmt_reset(self->statement);
1295 Py_CLEAR(self->statement);
1296 }
1297
1298 self->closed = 1;
1299
1300 Py_RETURN_NONE;
1301 }
1302
1303 static PyMethodDef cursor_methods[] = {
1304 PYSQLITE_CURSOR_CLOSE_METHODDEF
1305 PYSQLITE_CURSOR_EXECUTEMANY_METHODDEF
1306 PYSQLITE_CURSOR_EXECUTESCRIPT_METHODDEF
1307 PYSQLITE_CURSOR_EXECUTE_METHODDEF
1308 PYSQLITE_CURSOR_FETCHALL_METHODDEF
1309 PYSQLITE_CURSOR_FETCHMANY_METHODDEF
1310 PYSQLITE_CURSOR_FETCHONE_METHODDEF
1311 PYSQLITE_CURSOR_SETINPUTSIZES_METHODDEF
1312 PYSQLITE_CURSOR_SETOUTPUTSIZE_METHODDEF
1313 {NULL, NULL}
1314 };
1315
1316 static struct PyMemberDef cursor_members[] =
1317 {
1318 {"connection", _Py_T_OBJECT, offsetof(pysqlite_Cursor, connection), Py_READONLY},
1319 {"description", _Py_T_OBJECT, offsetof(pysqlite_Cursor, description), Py_READONLY},
1320 {"arraysize", Py_T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
1321 {"lastrowid", _Py_T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), Py_READONLY},
1322 {"rowcount", Py_T_LONG, offsetof(pysqlite_Cursor, rowcount), Py_READONLY},
1323 {"row_factory", _Py_T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
1324 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(pysqlite_Cursor, in_weakreflist), Py_READONLY},
1325 {NULL}
1326 };
1327
1328 static const char cursor_doc[] =
1329 PyDoc_STR("SQLite database cursor class.");
1330
1331 static PyType_Slot cursor_slots[] = {
1332 {Py_tp_dealloc, cursor_dealloc},
1333 {Py_tp_doc, (void *)cursor_doc},
1334 {Py_tp_iter, PyObject_SelfIter},
1335 {Py_tp_iternext, pysqlite_cursor_iternext},
1336 {Py_tp_methods, cursor_methods},
1337 {Py_tp_members, cursor_members},
1338 {Py_tp_init, pysqlite_cursor_init},
1339 {Py_tp_traverse, cursor_traverse},
1340 {Py_tp_clear, cursor_clear},
1341 {0, NULL},
1342 };
1343
1344 static PyType_Spec cursor_spec = {
1345 .name = MODULE_NAME ".Cursor",
1346 .basicsize = sizeof(pysqlite_Cursor),
1347 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
1348 Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
1349 .slots = cursor_slots,
1350 };
1351
1352 int
pysqlite_cursor_setup_types(PyObject * module)1353 pysqlite_cursor_setup_types(PyObject *module)
1354 {
1355 PyObject *type = PyType_FromModuleAndSpec(module, &cursor_spec, NULL);
1356 if (type == NULL) {
1357 return -1;
1358 }
1359 pysqlite_state *state = pysqlite_get_state(module);
1360 state->CursorType = (PyTypeObject *)type;
1361 return 0;
1362 }
1363