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