Python-2.7.3/Modules/_sqlite/cursor.c

Location Tool Test ID Function Issue
/builddir/build/BUILD/Python-2.7.3/Modules/_sqlite/cursor.c:129:9 gcc unused-but-set-variable pysqlite_cursor_dealloc variable 'rc' set but not used
/builddir/build/BUILD/Python-2.7.3/Modules/_sqlite/cursor.c:129:9 gcc unused-but-set-variable pysqlite_cursor_dealloc variable 'rc' set but not used
/builddir/build/BUILD/Python-2.7.3/Modules/_sqlite/cursor.c:133:9 clang-analyzer Value stored to 'rc' is never read
/builddir/build/BUILD/Python-2.7.3/Modules/_sqlite/cursor.c:133:9 clang-analyzer Value stored to 'rc' is never read
/builddir/build/BUILD/Python-2.7.3/Modules/_sqlite/cursor.c:549:9 clang-analyzer Value stored to 'rc' is never read
/builddir/build/BUILD/Python-2.7.3/Modules/_sqlite/cursor.c:549:9 clang-analyzer Value stored to 'rc' is never read
/builddir/build/BUILD/Python-2.7.3/Modules/_sqlite/cursor.c:760:13 clang-analyzer Value stored to 'rc' is never read
/builddir/build/BUILD/Python-2.7.3/Modules/_sqlite/cursor.c:760:13 clang-analyzer Value stored to 'rc' is never read
   1 /* cursor.c - the cursor type
   2  *
   3  * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
   4  *
   5  * This file is part of pysqlite.
   6  *
   7  * This software is provided 'as-is', without any express or implied
   8  * warranty.  In no event will the authors be held liable for any damages
   9  * arising from the use of this software.
  10  *
  11  * Permission is granted to anyone to use this software for any purpose,
  12  * including commercial applications, and to alter it and redistribute it
  13  * freely, subject to the following restrictions:
  14  *
  15  * 1. The origin of this software must not be misrepresented; you must not
  16  *    claim that you wrote the original software. If you use this software
  17  *    in a product, an acknowledgment in the product documentation would be
  18  *    appreciated but is not required.
  19  * 2. Altered source versions must be plainly marked as such, and must not be
  20  *    misrepresented as being the original software.
  21  * 3. This notice may not be removed or altered from any source distribution.
  22  */
  23 
  24 #include "cursor.h"
  25 #include "module.h"
  26 #include "util.h"
  27 #include "sqlitecompat.h"
  28 
  29 /* used to decide wether to call PyInt_FromLong or PyLong_FromLongLong */
  30 #ifndef INT32_MIN
  31 #define INT32_MIN (-2147483647 - 1)
  32 #endif
  33 #ifndef INT32_MAX
  34 #define INT32_MAX 2147483647
  35 #endif
  36 
  37 PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
  38 
  39 static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
  40 
  41 static pysqlite_StatementKind detect_statement_type(char* statement)
  42 {
  43     char buf[20];
  44     char* src;
  45     char* dst;
  46 
  47     src = statement;
  48     /* skip over whitepace */
  49     while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') {
  50         src++;
  51     }
  52 
  53     if (*src == 0)
  54         return STATEMENT_INVALID;
  55 
  56     dst = buf;
  57     *dst = 0;
  58     while (Py_ISALPHA(*src) && dst - buf < sizeof(buf) - 2) {
  59         *dst++ = Py_TOLOWER(*src++);
  60     }
  61 
  62     *dst = 0;
  63 
  64     if (!strcmp(buf, "select")) {
  65         return STATEMENT_SELECT;
  66     } else if (!strcmp(buf, "insert")) {
  67         return STATEMENT_INSERT;
  68     } else if (!strcmp(buf, "update")) {
  69         return STATEMENT_UPDATE;
  70     } else if (!strcmp(buf, "delete")) {
  71         return STATEMENT_DELETE;
  72     } else if (!strcmp(buf, "replace")) {
  73         return STATEMENT_REPLACE;
  74     } else {
  75         return STATEMENT_OTHER;
  76     }
  77 }
  78 
  79 static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
  80 {
  81     pysqlite_Connection* connection;
  82 
  83     if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
  84     {
  85         return -1;
  86     }
  87 
  88     Py_INCREF(connection);
  89     self->connection = connection;
  90     self->statement = NULL;
  91     self->next_row = NULL;
  92     self->in_weakreflist = NULL;
  93 
  94     self->row_cast_map = PyList_New(0);
  95     if (!self->row_cast_map) {
  96         return -1;
  97     }
  98 
  99     Py_INCREF(Py_None);
 100     self->description = Py_None;
 101 
 102     Py_INCREF(Py_None);
 103     self->lastrowid= Py_None;
 104 
 105     self->arraysize = 1;
 106     self->closed = 0;
 107     self->reset = 0;
 108 
 109     self->rowcount = -1L;
 110 
 111     Py_INCREF(Py_None);
 112     self->row_factory = Py_None;
 113 
 114     if (!pysqlite_check_thread(self->connection)) {
 115         return -1;
 116     }
 117 
 118     if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
 119         return -1;
 120     }
 121 
 122     self->initialized = 1;
 123 
 124     return 0;
 125 }
 126 
 127 static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
 128 {
 129     int rc;
variable 'rc' set but not used
(emitted by gcc)
variable 'rc' set but not used
(emitted by gcc)
130 131 /* Reset the statement if the user has not closed the cursor */ 132 if (self->statement) { 133 rc = pysqlite_statement_reset(self->statement);
Value stored to 'rc' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Value stored to 'rc' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

134 Py_DECREF(self->statement); 135 } 136 137 Py_XDECREF(self->connection); 138 Py_XDECREF(self->row_cast_map); 139 Py_XDECREF(self->description); 140 Py_XDECREF(self->lastrowid); 141 Py_XDECREF(self->row_factory); 142 Py_XDECREF(self->next_row); 143 144 if (self->in_weakreflist != NULL) { 145 PyObject_ClearWeakRefs((PyObject*)self); 146 } 147 148 self->ob_type->tp_free((PyObject*)self); 149 } 150 151 PyObject* _pysqlite_get_converter(PyObject* key) 152 { 153 PyObject* upcase_key; 154 PyObject* retval; 155 156 upcase_key = PyObject_CallMethod(key, "upper", ""); 157 if (!upcase_key) { 158 return NULL; 159 } 160 161 retval = PyDict_GetItem(converters, upcase_key); 162 Py_DECREF(upcase_key); 163 164 return retval; 165 } 166 167 int pysqlite_build_row_cast_map(pysqlite_Cursor* self) 168 { 169 int i; 170 const char* type_start = (const char*)-1; 171 const char* pos; 172 173 const char* colname; 174 const char* decltype; 175 PyObject* py_decltype; 176 PyObject* converter; 177 PyObject* key; 178 179 if (!self->connection->detect_types) { 180 return 0; 181 } 182 183 Py_XDECREF(self->row_cast_map); 184 self->row_cast_map = PyList_New(0); 185 186 for (i = 0; i < sqlite3_column_count(self->statement->st); i++) { 187 converter = NULL; 188 189 if (self->connection->detect_types & PARSE_COLNAMES) { 190 colname = sqlite3_column_name(self->statement->st, i); 191 if (colname) { 192 for (pos = colname; *pos != 0; pos++) { 193 if (*pos == '[') { 194 type_start = pos + 1; 195 } else if (*pos == ']' && type_start != (const char*)-1) { 196 key = PyString_FromStringAndSize(type_start, pos - type_start); 197 if (!key) { 198 /* creating a string failed, but it is too complicated 199 * to propagate the error here, we just assume there is 200 * no converter and proceed */ 201 break; 202 } 203 204 converter = _pysqlite_get_converter(key); 205 Py_DECREF(key); 206 break; 207 } 208 } 209 } 210 } 211 212 if (!converter && self->connection->detect_types & PARSE_DECLTYPES) { 213 decltype = sqlite3_column_decltype(self->statement->st, i); 214 if (decltype) { 215 for (pos = decltype;;pos++) { 216 /* Converter names are split at '(' and blanks. 217 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and 218 * 'NUMBER(10)' to be treated as 'NUMBER', for example. 219 * In other words, it will work as people expect it to work.*/ 220 if (*pos == ' ' || *pos == '(' || *pos == 0) { 221 py_decltype = PyString_FromStringAndSize(decltype, pos - decltype); 222 if (!py_decltype) { 223 return -1; 224 } 225 break; 226 } 227 } 228 229 converter = _pysqlite_get_converter(py_decltype); 230 Py_DECREF(py_decltype); 231 } 232 } 233 234 if (!converter) { 235 converter = Py_None; 236 } 237 238 if (PyList_Append(self->row_cast_map, converter) != 0) { 239 if (converter != Py_None) { 240 Py_DECREF(converter); 241 } 242 Py_XDECREF(self->row_cast_map); 243 self->row_cast_map = NULL; 244 245 return -1; 246 } 247 } 248 249 return 0; 250 } 251 252 PyObject* _pysqlite_build_column_name(const char* colname) 253 { 254 const char* pos; 255 256 if (!colname) { 257 Py_INCREF(Py_None); 258 return Py_None; 259 } 260 261 for (pos = colname;; pos++) { 262 if (*pos == 0 || *pos == '[') { 263 if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) { 264 pos--; 265 } 266 return PyString_FromStringAndSize(colname, pos - colname); 267 } 268 } 269 } 270 271 PyObject* pysqlite_unicode_from_string(const char* val_str, Py_ssize_t size, int optimize) 272 { 273 const char* check; 274 Py_ssize_t pos; 275 int is_ascii = 0; 276 277 if (optimize) { 278 is_ascii = 1; 279 280 check = val_str; 281 for (pos = 0; pos < size; pos++) { 282 if (*check & 0x80) { 283 is_ascii = 0; 284 break; 285 } 286 287 check++; 288 } 289 } 290 291 if (is_ascii) { 292 return PyString_FromStringAndSize(val_str, size); 293 } else { 294 return PyUnicode_DecodeUTF8(val_str, size, NULL); 295 } 296 } 297 298 /* 299 * Returns a row from the currently active SQLite statement 300 * 301 * Precondidition: 302 * - sqlite3_step() has been called before and it returned SQLITE_ROW. 303 */ 304 PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self) 305 { 306 int i, numcols; 307 PyObject* row; 308 PyObject* item = NULL; 309 int coltype; 310 PY_LONG_LONG intval; 311 PyObject* converter; 312 PyObject* converted; 313 Py_ssize_t nbytes; 314 PyObject* buffer; 315 void* raw_buffer; 316 const char* val_str; 317 char buf[200]; 318 const char* colname; 319 320 if (self->reset) { 321 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback); 322 return NULL; 323 } 324 325 Py_BEGIN_ALLOW_THREADS 326 numcols = sqlite3_data_count(self->statement->st); 327 Py_END_ALLOW_THREADS 328 329 row = PyTuple_New(numcols); 330 if (!row) { 331 return NULL; 332 } 333 334 for (i = 0; i < numcols; i++) { 335 if (self->connection->detect_types) { 336 converter = PyList_GetItem(self->row_cast_map, i); 337 if (!converter) { 338 converter = Py_None; 339 } 340 } else { 341 converter = Py_None; 342 } 343 344 if (converter != Py_None) { 345 nbytes = sqlite3_column_bytes(self->statement->st, i); 346 val_str = (const char*)sqlite3_column_blob(self->statement->st, i); 347 if (!val_str) { 348 Py_INCREF(Py_None); 349 converted = Py_None; 350 } else { 351 item = PyString_FromStringAndSize(val_str, nbytes); 352 if (!item) { 353 return NULL; 354 } 355 converted = PyObject_CallFunction(converter, "O", item); 356 Py_DECREF(item); 357 if (!converted) { 358 break; 359 } 360 } 361 } else { 362 Py_BEGIN_ALLOW_THREADS 363 coltype = sqlite3_column_type(self->statement->st, i); 364 Py_END_ALLOW_THREADS 365 if (coltype == SQLITE_NULL) { 366 Py_INCREF(Py_None); 367 converted = Py_None; 368 } else if (coltype == SQLITE_INTEGER) { 369 intval = sqlite3_column_int64(self->statement->st, i); 370 if (intval < INT32_MIN || intval > INT32_MAX) { 371 converted = PyLong_FromLongLong(intval); 372 } else { 373 converted = PyInt_FromLong((long)intval); 374 } 375 } else if (coltype == SQLITE_FLOAT) { 376 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i)); 377 } else if (coltype == SQLITE_TEXT) { 378 val_str = (const char*)sqlite3_column_text(self->statement->st, i); 379 nbytes = sqlite3_column_bytes(self->statement->st, i); 380 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type) 381 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) { 382 383 converted = pysqlite_unicode_from_string(val_str, nbytes, 384 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0); 385 386 if (!converted) { 387 colname = sqlite3_column_name(self->statement->st, i); 388 if (!colname) { 389 colname = "<unknown column name>"; 390 } 391 PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'", 392 colname , val_str); 393 PyErr_SetString(pysqlite_OperationalError, buf); 394 } 395 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) { 396 converted = PyString_FromStringAndSize(val_str, nbytes); 397 } else { 398 converted = PyObject_CallFunction(self->connection->text_factory, "s#", val_str, nbytes); 399 } 400 } else { 401 /* coltype == SQLITE_BLOB */ 402 nbytes = sqlite3_column_bytes(self->statement->st, i); 403 buffer = PyBuffer_New(nbytes); 404 if (!buffer) { 405 break; 406 } 407 if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &nbytes)) { 408 break; 409 } 410 memcpy(raw_buffer, sqlite3_column_blob(self->statement->st, i), nbytes); 411 converted = buffer; 412 } 413 } 414 415 if (converted) { 416 PyTuple_SetItem(row, i, converted); 417 } else { 418 Py_INCREF(Py_None); 419 PyTuple_SetItem(row, i, Py_None); 420 } 421 } 422 423 if (PyErr_Occurred()) { 424 Py_DECREF(row); 425 row = NULL; 426 } 427 428 return row; 429 } 430 431 /* 432 * Checks if a cursor object is usable. 433 * 434 * 0 => error; 1 => ok 435 */ 436 static int check_cursor(pysqlite_Cursor* cur) 437 { 438 if (!cur->initialized) { 439 PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called."); 440 return 0; 441 } 442 443 if (cur->closed) { 444 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor."); 445 return 0; 446 } 447 448 if (cur->locked) { 449 PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed."); 450 return 0; 451 } 452 453 return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection); 454 } 455 456 PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) 457 { 458 PyObject* operation; 459 PyObject* operation_bytestr = NULL; 460 char* operation_cstr; 461 PyObject* parameters_list = NULL; 462 PyObject* parameters_iter = NULL; 463 PyObject* parameters = NULL; 464 int i; 465 int rc; 466 PyObject* func_args; 467 PyObject* result; 468 int numcols; 469 PY_LONG_LONG lastrowid; 470 int statement_type; 471 PyObject* descriptor; 472 PyObject* second_argument = NULL; 473 int allow_8bit_chars; 474 475 if (!check_cursor(self)) { 476 goto error; 477 } 478 479 self->locked = 1; 480 self->reset = 0; 481 482 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */ 483 allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) && 484 (self->connection->text_factory != pysqlite_OptimizedUnicode)); 485 486 Py_XDECREF(self->next_row); 487 self->next_row = NULL; 488 489 if (multiple) { 490 /* executemany() */ 491 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { 492 goto error; 493 } 494 495 if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { 496 PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); 497 goto error; 498 } 499 500 if (PyIter_Check(second_argument)) { 501 /* iterator */ 502 Py_INCREF(second_argument); 503 parameters_iter = second_argument; 504 } else { 505 /* sequence */ 506 parameters_iter = PyObject_GetIter(second_argument); 507 if (!parameters_iter) { 508 goto error; 509 } 510 } 511 } else { 512 /* execute() */ 513 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { 514 goto error; 515 } 516 517 if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { 518 PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); 519 goto error; 520 } 521 522 parameters_list = PyList_New(0); 523 if (!parameters_list) { 524 goto error; 525 } 526 527 if (second_argument == NULL) { 528 second_argument = PyTuple_New(0); 529 if (!second_argument) { 530 goto error; 531 } 532 } else { 533 Py_INCREF(second_argument); 534 } 535 if (PyList_Append(parameters_list, second_argument) != 0) { 536 Py_DECREF(second_argument); 537 goto error; 538 } 539 Py_DECREF(second_argument); 540 541 parameters_iter = PyObject_GetIter(parameters_list); 542 if (!parameters_iter) { 543 goto error; 544 } 545 } 546 547 if (self->statement != NULL) { 548 /* There is an active statement */ 549 rc = pysqlite_statement_reset(self->statement);
Value stored to 'rc' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Value stored to 'rc' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

550 } 551 552 if (PyString_Check(operation)) { 553 operation_cstr = PyString_AsString(operation); 554 } else { 555 operation_bytestr = PyUnicode_AsUTF8String(operation); 556 if (!operation_bytestr) { 557 goto error; 558 } 559 560 operation_cstr = PyString_AsString(operation_bytestr); 561 } 562 563 /* reset description and rowcount */ 564 Py_DECREF(self->description); 565 Py_INCREF(Py_None); 566 self->description = Py_None; 567 self->rowcount = -1L; 568 569 func_args = PyTuple_New(1); 570 if (!func_args) { 571 goto error; 572 } 573 Py_INCREF(operation); 574 if (PyTuple_SetItem(func_args, 0, operation) != 0) { 575 goto error; 576 } 577 578 if (self->statement) { 579 (void)pysqlite_statement_reset(self->statement); 580 Py_DECREF(self->statement); 581 } 582 583 self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args); 584 Py_DECREF(func_args); 585 586 if (!self->statement) { 587 goto error; 588 } 589 590 if (self->statement->in_use) { 591 Py_DECREF(self->statement); 592 self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); 593 if (!self->statement) { 594 goto error; 595 } 596 rc = pysqlite_statement_create(self->statement, self->connection, operation); 597 if (rc != SQLITE_OK) { 598 Py_CLEAR(self->statement); 599 goto error; 600 } 601 } 602 603 pysqlite_statement_reset(self->statement); 604 pysqlite_statement_mark_dirty(self->statement); 605 606 statement_type = detect_statement_type(operation_cstr); 607 if (self->connection->begin_statement) { 608 switch (statement_type) { 609 case STATEMENT_UPDATE: 610 case STATEMENT_DELETE: 611 case STATEMENT_INSERT: 612 case STATEMENT_REPLACE: 613 if (!self->connection->inTransaction) { 614 result = _pysqlite_connection_begin(self->connection); 615 if (!result) { 616 goto error; 617 } 618 Py_DECREF(result); 619 } 620 break; 621 case STATEMENT_OTHER: 622 /* it's a DDL statement or something similar 623 - we better COMMIT first so it works for all cases */ 624 if (self->connection->inTransaction) { 625 result = pysqlite_connection_commit(self->connection, NULL); 626 if (!result) { 627 goto error; 628 } 629 Py_DECREF(result); 630 } 631 break; 632 case STATEMENT_SELECT: 633 if (multiple) { 634 PyErr_SetString(pysqlite_ProgrammingError, 635 "You cannot execute SELECT statements in executemany()."); 636 goto error; 637 } 638 break; 639 } 640 } 641 642 while (1) { 643 parameters = PyIter_Next(parameters_iter); 644 if (!parameters) { 645 break; 646 } 647 648 pysqlite_statement_mark_dirty(self->statement); 649 650 pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars); 651 if (PyErr_Occurred()) { 652 goto error; 653 } 654 655 /* Keep trying the SQL statement until the schema stops changing. */ 656 while (1) { 657 /* Actually execute the SQL statement. */ 658 rc = pysqlite_step(self->statement->st, self->connection); 659 if (rc == SQLITE_DONE || rc == SQLITE_ROW) { 660 /* If it worked, let's get out of the loop */ 661 break; 662 } 663 /* Something went wrong. Re-set the statement and try again. */ 664 rc = pysqlite_statement_reset(self->statement); 665 if (rc == SQLITE_SCHEMA) { 666 /* If this was a result of the schema changing, let's try 667 again. */ 668 rc = pysqlite_statement_recompile(self->statement, parameters); 669 if (rc == SQLITE_OK) { 670 continue; 671 } else { 672 /* If the database gave us an error, promote it to Python. */ 673 (void)pysqlite_statement_reset(self->statement); 674 _pysqlite_seterror(self->connection->db, NULL); 675 goto error; 676 } 677 } else { 678 if (PyErr_Occurred()) { 679 /* there was an error that occurred in a user-defined callback */ 680 if (_enable_callback_tracebacks) { 681 PyErr_Print(); 682 } else { 683 PyErr_Clear(); 684 } 685 } 686 (void)pysqlite_statement_reset(self->statement); 687 _pysqlite_seterror(self->connection->db, NULL); 688 goto error; 689 } 690 } 691 692 if (pysqlite_build_row_cast_map(self) != 0) { 693 PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map"); 694 goto error; 695 } 696 697 if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) { 698 if (self->description == Py_None) { 699 Py_BEGIN_ALLOW_THREADS 700 numcols = sqlite3_column_count(self->statement->st); 701 Py_END_ALLOW_THREADS 702 703 Py_DECREF(self->description); 704 self->description = PyTuple_New(numcols); 705 if (!self->description) { 706 goto error; 707 } 708 for (i = 0; i < numcols; i++) { 709 descriptor = PyTuple_New(7); 710 if (!descriptor) { 711 goto error; 712 } 713 PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i))); 714 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None); 715 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None); 716 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None); 717 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None); 718 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None); 719 Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None); 720 PyTuple_SetItem(self->description, i, descriptor); 721 } 722 } 723 } 724 725 if (rc == SQLITE_ROW) { 726 if (multiple) { 727 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements."); 728 goto error; 729 } 730 731 self->next_row = _pysqlite_fetch_one_row(self); 732 } else if (rc == SQLITE_DONE && !multiple) { 733 pysqlite_statement_reset(self->statement); 734 Py_CLEAR(self->statement); 735 } 736 737 switch (statement_type) { 738 case STATEMENT_UPDATE: 739 case STATEMENT_DELETE: 740 case STATEMENT_INSERT: 741 case STATEMENT_REPLACE: 742 if (self->rowcount == -1L) { 743 self->rowcount = 0L; 744 } 745 self->rowcount += (long)sqlite3_changes(self->connection->db); 746 } 747 748 Py_DECREF(self->lastrowid); 749 if (!multiple && statement_type == STATEMENT_INSERT) { 750 Py_BEGIN_ALLOW_THREADS 751 lastrowid = sqlite3_last_insert_rowid(self->connection->db); 752 Py_END_ALLOW_THREADS 753 self->lastrowid = PyInt_FromLong((long)lastrowid); 754 } else { 755 Py_INCREF(Py_None); 756 self->lastrowid = Py_None; 757 } 758 759 if (multiple) { 760 rc = pysqlite_statement_reset(self->statement);
Value stored to 'rc' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Value stored to 'rc' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

761 } 762 Py_XDECREF(parameters); 763 } 764 765 error: 766 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR 767 * ROLLBACK could have happened */ 768 #ifdef SQLITE_VERSION_NUMBER 769 #if SQLITE_VERSION_NUMBER >= 3002002 770 if (self->connection && self->connection->db) 771 self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db); 772 #endif 773 #endif 774 775 Py_XDECREF(operation_bytestr); 776 Py_XDECREF(parameters); 777 Py_XDECREF(parameters_iter); 778 Py_XDECREF(parameters_list); 779 780 self->locked = 0; 781 782 if (PyErr_Occurred()) { 783 self->rowcount = -1L; 784 return NULL; 785 } else { 786 Py_INCREF(self); 787 return (PyObject*)self; 788 } 789 } 790 791 PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args) 792 { 793 return _pysqlite_query_execute(self, 0, args); 794 } 795 796 PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args) 797 { 798 return _pysqlite_query_execute(self, 1, args); 799 } 800 801 PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args) 802 { 803 PyObject* script_obj; 804 PyObject* script_str = NULL; 805 const char* script_cstr; 806 sqlite3_stmt* statement; 807 int rc; 808 PyObject* result; 809 810 if (!PyArg_ParseTuple(args, "O", &script_obj)) { 811 return NULL; 812 } 813 814 if (!check_cursor(self)) { 815 return NULL; 816 } 817 818 self->reset = 0; 819 820 if (PyString_Check(script_obj)) { 821 script_cstr = PyString_AsString(script_obj); 822 } else if (PyUnicode_Check(script_obj)) { 823 script_str = PyUnicode_AsUTF8String(script_obj); 824 if (!script_str) { 825 return NULL; 826 } 827 828 script_cstr = PyString_AsString(script_str); 829 } else { 830 PyErr_SetString(PyExc_ValueError, "script argument must be unicode or string."); 831 return NULL; 832 } 833 834 /* commit first */ 835 result = pysqlite_connection_commit(self->connection, NULL); 836 if (!result) { 837 goto error; 838 } 839 Py_DECREF(result); 840 841 while (1) { 842 Py_BEGIN_ALLOW_THREADS 843 rc = sqlite3_prepare(self->connection->db, 844 script_cstr, 845 -1, 846 &statement, 847 &script_cstr); 848 Py_END_ALLOW_THREADS 849 if (rc != SQLITE_OK) { 850 _pysqlite_seterror(self->connection->db, NULL); 851 goto error; 852 } 853 854 /* execute statement, and ignore results of SELECT statements */ 855 rc = SQLITE_ROW; 856 while (rc == SQLITE_ROW) { 857 rc = pysqlite_step(statement, self->connection); 858 /* TODO: we probably need more error handling here */ 859 } 860 861 if (rc != SQLITE_DONE) { 862 (void)sqlite3_finalize(statement); 863 _pysqlite_seterror(self->connection->db, NULL); 864 goto error; 865 } 866 867 rc = sqlite3_finalize(statement); 868 if (rc != SQLITE_OK) { 869 _pysqlite_seterror(self->connection->db, NULL); 870 goto error; 871 } 872 873 if (*script_cstr == (char)0) { 874 break; 875 } 876 } 877 878 error: 879 Py_XDECREF(script_str); 880 881 if (PyErr_Occurred()) { 882 return NULL; 883 } else { 884 Py_INCREF(self); 885 return (PyObject*)self; 886 } 887 } 888 889 PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self) 890 { 891 Py_INCREF(self); 892 return (PyObject*)self; 893 } 894 895 PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self) 896 { 897 PyObject* next_row_tuple; 898 PyObject* next_row; 899 int rc; 900 901 if (!check_cursor(self)) { 902 return NULL; 903 } 904 905 if (self->reset) { 906 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback); 907 return NULL; 908 } 909 910 if (!self->next_row) { 911 if (self->statement) { 912 (void)pysqlite_statement_reset(self->statement); 913 Py_DECREF(self->statement); 914 self->statement = NULL; 915 } 916 return NULL; 917 } 918 919 next_row_tuple = self->next_row; 920 self->next_row = NULL; 921 922 if (self->row_factory != Py_None) { 923 next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple); 924 Py_DECREF(next_row_tuple); 925 } else { 926 next_row = next_row_tuple; 927 } 928 929 if (self->statement) { 930 rc = pysqlite_step(self->statement->st, self->connection); 931 if (rc != SQLITE_DONE && rc != SQLITE_ROW) { 932 (void)pysqlite_statement_reset(self->statement); 933 Py_DECREF(next_row); 934 _pysqlite_seterror(self->connection->db, NULL); 935 return NULL; 936 } 937 938 if (rc == SQLITE_ROW) { 939 self->next_row = _pysqlite_fetch_one_row(self); 940 } 941 } 942 943 return next_row; 944 } 945 946 PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args) 947 { 948 PyObject* row; 949 950 row = pysqlite_cursor_iternext(self); 951 if (!row && !PyErr_Occurred()) { 952 Py_INCREF(Py_None); 953 return Py_None; 954 } 955 956 return row; 957 } 958 959 PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) 960 { 961 static char *kwlist[] = {"size", NULL, NULL}; 962 963 PyObject* row; 964 PyObject* list; 965 int maxrows = self->arraysize; 966 int counter = 0; 967 968 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) { 969 return NULL; 970 } 971 972 list = PyList_New(0); 973 if (!list) { 974 return NULL; 975 } 976 977 /* just make sure we enter the loop */ 978 row = Py_None; 979 980 while (row) { 981 row = pysqlite_cursor_iternext(self); 982 if (row) { 983 PyList_Append(list, row); 984 Py_DECREF(row); 985 } else { 986 break; 987 } 988 989 if (++counter == maxrows) { 990 break; 991 } 992 } 993 994 if (PyErr_Occurred()) { 995 Py_DECREF(list); 996 return NULL; 997 } else { 998 return list; 999 } 1000 } 1001 1002 PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args) 1003 { 1004 PyObject* row; 1005 PyObject* list; 1006 1007 list = PyList_New(0); 1008 if (!list) { 1009 return NULL; 1010 } 1011 1012 /* just make sure we enter the loop */ 1013 row = (PyObject*)Py_None; 1014 1015 while (row) { 1016 row = pysqlite_cursor_iternext(self); 1017 if (row) { 1018 PyList_Append(list, row); 1019 Py_DECREF(row); 1020 } 1021 } 1022 1023 if (PyErr_Occurred()) { 1024 Py_DECREF(list); 1025 return NULL; 1026 } else { 1027 return list; 1028 } 1029 } 1030 1031 PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args) 1032 { 1033 /* don't care, return None */ 1034 Py_INCREF(Py_None); 1035 return Py_None; 1036 } 1037 1038 PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) 1039 { 1040 if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { 1041 return NULL; 1042 } 1043 1044 if (self->statement) { 1045 (void)pysqlite_statement_reset(self->statement); 1046 Py_CLEAR(self->statement); 1047 } 1048 1049 self->closed = 1; 1050 1051 Py_INCREF(Py_None); 1052 return Py_None; 1053 } 1054 1055 static PyMethodDef cursor_methods[] = { 1056 {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS, 1057 PyDoc_STR("Executes a SQL statement.")}, 1058 {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS, 1059 PyDoc_STR("Repeatedly executes a SQL statement.")}, 1060 {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS, 1061 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, 1062 {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS, 1063 PyDoc_STR("Fetches one row from the resultset.")}, 1064 {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS, 1065 PyDoc_STR("Fetches several rows from the resultset.")}, 1066 {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS, 1067 PyDoc_STR("Fetches all rows from the resultset.")}, 1068 {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, 1069 PyDoc_STR("Closes the cursor.")}, 1070 {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS, 1071 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, 1072 {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS, 1073 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, 1074 {NULL, NULL} 1075 }; 1076 1077 static struct PyMemberDef cursor_members[] = 1078 { 1079 {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), RO}, 1080 {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), RO}, 1081 {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0}, 1082 {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), RO}, 1083 {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), RO}, 1084 {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0}, 1085 {NULL} 1086 }; 1087 1088 static char cursor_doc[] = 1089 PyDoc_STR("SQLite database cursor class."); 1090 1091 PyTypeObject pysqlite_CursorType = { 1092 PyVarObject_HEAD_INIT(NULL, 0) 1093 MODULE_NAME ".Cursor", /* tp_name */ 1094 sizeof(pysqlite_Cursor), /* tp_basicsize */ 1095 0, /* tp_itemsize */ 1096 (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */ 1097 0, /* tp_print */ 1098 0, /* tp_getattr */ 1099 0, /* tp_setattr */ 1100 0, /* tp_compare */ 1101 0, /* tp_repr */ 1102 0, /* tp_as_number */ 1103 0, /* tp_as_sequence */ 1104 0, /* tp_as_mapping */ 1105 0, /* tp_hash */ 1106 0, /* tp_call */ 1107 0, /* tp_str */ 1108 0, /* tp_getattro */ 1109 0, /* tp_setattro */ 1110 0, /* tp_as_buffer */ 1111 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_ITER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ 1112 cursor_doc, /* tp_doc */ 1113 0, /* tp_traverse */ 1114 0, /* tp_clear */ 1115 0, /* tp_richcompare */ 1116 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */ 1117 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */ 1118 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */ 1119 cursor_methods, /* tp_methods */ 1120 cursor_members, /* tp_members */ 1121 0, /* tp_getset */ 1122 0, /* tp_base */ 1123 0, /* tp_dict */ 1124 0, /* tp_descr_get */ 1125 0, /* tp_descr_set */ 1126 0, /* tp_dictoffset */ 1127 (initproc)pysqlite_cursor_init, /* tp_init */ 1128 0, /* tp_alloc */ 1129 0, /* tp_new */ 1130 0 /* tp_free */ 1131 }; 1132 1133 extern int pysqlite_cursor_setup_types(void) 1134 { 1135 pysqlite_CursorType.tp_new = PyType_GenericNew; 1136 return PyType_Ready(&pysqlite_CursorType); 1137 }