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