| 1 | n/a | /* cursor.c - the cursor type |
|---|
| 2 | n/a | * |
|---|
| 3 | n/a | * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> |
|---|
| 4 | n/a | * |
|---|
| 5 | n/a | * This file is part of pysqlite. |
|---|
| 6 | n/a | * |
|---|
| 7 | n/a | * This software is provided 'as-is', without any express or implied |
|---|
| 8 | n/a | * warranty. In no event will the authors be held liable for any damages |
|---|
| 9 | n/a | * arising from the use of this software. |
|---|
| 10 | n/a | * |
|---|
| 11 | n/a | * Permission is granted to anyone to use this software for any purpose, |
|---|
| 12 | n/a | * including commercial applications, and to alter it and redistribute it |
|---|
| 13 | n/a | * freely, subject to the following restrictions: |
|---|
| 14 | n/a | * |
|---|
| 15 | n/a | * 1. The origin of this software must not be misrepresented; you must not |
|---|
| 16 | n/a | * claim that you wrote the original software. If you use this software |
|---|
| 17 | n/a | * in a product, an acknowledgment in the product documentation would be |
|---|
| 18 | n/a | * appreciated but is not required. |
|---|
| 19 | n/a | * 2. Altered source versions must be plainly marked as such, and must not be |
|---|
| 20 | n/a | * misrepresented as being the original software. |
|---|
| 21 | n/a | * 3. This notice may not be removed or altered from any source distribution. |
|---|
| 22 | n/a | */ |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | #include "cursor.h" |
|---|
| 25 | n/a | #include "module.h" |
|---|
| 26 | n/a | #include "util.h" |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self); |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | static const char errmsg_fetch_across_rollback[] = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from."; |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) |
|---|
| 33 | n/a | { |
|---|
| 34 | n/a | pysqlite_Connection* connection; |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection)) |
|---|
| 37 | n/a | { |
|---|
| 38 | n/a | return -1; |
|---|
| 39 | n/a | } |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | Py_INCREF(connection); |
|---|
| 42 | n/a | self->connection = connection; |
|---|
| 43 | n/a | self->statement = NULL; |
|---|
| 44 | n/a | self->next_row = NULL; |
|---|
| 45 | n/a | self->in_weakreflist = NULL; |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | self->row_cast_map = PyList_New(0); |
|---|
| 48 | n/a | if (!self->row_cast_map) { |
|---|
| 49 | n/a | return -1; |
|---|
| 50 | n/a | } |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | Py_INCREF(Py_None); |
|---|
| 53 | n/a | self->description = Py_None; |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | Py_INCREF(Py_None); |
|---|
| 56 | n/a | self->lastrowid= Py_None; |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | self->arraysize = 1; |
|---|
| 59 | n/a | self->closed = 0; |
|---|
| 60 | n/a | self->reset = 0; |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | self->rowcount = -1L; |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | Py_INCREF(Py_None); |
|---|
| 65 | n/a | self->row_factory = Py_None; |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | if (!pysqlite_check_thread(self->connection)) { |
|---|
| 68 | n/a | return -1; |
|---|
| 69 | n/a | } |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) { |
|---|
| 72 | n/a | return -1; |
|---|
| 73 | n/a | } |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | self->initialized = 1; |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | return 0; |
|---|
| 78 | n/a | } |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | static void pysqlite_cursor_dealloc(pysqlite_Cursor* self) |
|---|
| 81 | n/a | { |
|---|
| 82 | n/a | /* Reset the statement if the user has not closed the cursor */ |
|---|
| 83 | n/a | if (self->statement) { |
|---|
| 84 | n/a | pysqlite_statement_reset(self->statement); |
|---|
| 85 | n/a | Py_DECREF(self->statement); |
|---|
| 86 | n/a | } |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | Py_XDECREF(self->connection); |
|---|
| 89 | n/a | Py_XDECREF(self->row_cast_map); |
|---|
| 90 | n/a | Py_XDECREF(self->description); |
|---|
| 91 | n/a | Py_XDECREF(self->lastrowid); |
|---|
| 92 | n/a | Py_XDECREF(self->row_factory); |
|---|
| 93 | n/a | Py_XDECREF(self->next_row); |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | if (self->in_weakreflist != NULL) { |
|---|
| 96 | n/a | PyObject_ClearWeakRefs((PyObject*)self); |
|---|
| 97 | n/a | } |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | Py_TYPE(self)->tp_free((PyObject*)self); |
|---|
| 100 | n/a | } |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | PyObject* _pysqlite_get_converter(PyObject* key) |
|---|
| 103 | n/a | { |
|---|
| 104 | n/a | PyObject* upcase_key; |
|---|
| 105 | n/a | PyObject* retval; |
|---|
| 106 | n/a | _Py_IDENTIFIER(upper); |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | upcase_key = _PyObject_CallMethodId(key, &PyId_upper, NULL); |
|---|
| 109 | n/a | if (!upcase_key) { |
|---|
| 110 | n/a | return NULL; |
|---|
| 111 | n/a | } |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | retval = PyDict_GetItem(converters, upcase_key); |
|---|
| 114 | n/a | Py_DECREF(upcase_key); |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | return retval; |
|---|
| 117 | n/a | } |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | int pysqlite_build_row_cast_map(pysqlite_Cursor* self) |
|---|
| 120 | n/a | { |
|---|
| 121 | n/a | int i; |
|---|
| 122 | n/a | const char* type_start = (const char*)-1; |
|---|
| 123 | n/a | const char* pos; |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | const char* colname; |
|---|
| 126 | n/a | const char* decltype; |
|---|
| 127 | n/a | PyObject* py_decltype; |
|---|
| 128 | n/a | PyObject* converter; |
|---|
| 129 | n/a | PyObject* key; |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | if (!self->connection->detect_types) { |
|---|
| 132 | n/a | return 0; |
|---|
| 133 | n/a | } |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | Py_XSETREF(self->row_cast_map, PyList_New(0)); |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | for (i = 0; i < sqlite3_column_count(self->statement->st); i++) { |
|---|
| 138 | n/a | converter = NULL; |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | if (self->connection->detect_types & PARSE_COLNAMES) { |
|---|
| 141 | n/a | colname = sqlite3_column_name(self->statement->st, i); |
|---|
| 142 | n/a | if (colname) { |
|---|
| 143 | n/a | for (pos = colname; *pos != 0; pos++) { |
|---|
| 144 | n/a | if (*pos == '[') { |
|---|
| 145 | n/a | type_start = pos + 1; |
|---|
| 146 | n/a | } else if (*pos == ']' && type_start != (const char*)-1) { |
|---|
| 147 | n/a | key = PyUnicode_FromStringAndSize(type_start, pos - type_start); |
|---|
| 148 | n/a | if (!key) { |
|---|
| 149 | n/a | /* creating a string failed, but it is too complicated |
|---|
| 150 | n/a | * to propagate the error here, we just assume there is |
|---|
| 151 | n/a | * no converter and proceed */ |
|---|
| 152 | n/a | break; |
|---|
| 153 | n/a | } |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | converter = _pysqlite_get_converter(key); |
|---|
| 156 | n/a | Py_DECREF(key); |
|---|
| 157 | n/a | break; |
|---|
| 158 | n/a | } |
|---|
| 159 | n/a | } |
|---|
| 160 | n/a | } |
|---|
| 161 | n/a | } |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | if (!converter && self->connection->detect_types & PARSE_DECLTYPES) { |
|---|
| 164 | n/a | decltype = sqlite3_column_decltype(self->statement->st, i); |
|---|
| 165 | n/a | if (decltype) { |
|---|
| 166 | n/a | for (pos = decltype;;pos++) { |
|---|
| 167 | n/a | /* Converter names are split at '(' and blanks. |
|---|
| 168 | n/a | * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and |
|---|
| 169 | n/a | * 'NUMBER(10)' to be treated as 'NUMBER', for example. |
|---|
| 170 | n/a | * In other words, it will work as people expect it to work.*/ |
|---|
| 171 | n/a | if (*pos == ' ' || *pos == '(' || *pos == 0) { |
|---|
| 172 | n/a | py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype); |
|---|
| 173 | n/a | if (!py_decltype) { |
|---|
| 174 | n/a | return -1; |
|---|
| 175 | n/a | } |
|---|
| 176 | n/a | break; |
|---|
| 177 | n/a | } |
|---|
| 178 | n/a | } |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | converter = _pysqlite_get_converter(py_decltype); |
|---|
| 181 | n/a | Py_DECREF(py_decltype); |
|---|
| 182 | n/a | } |
|---|
| 183 | n/a | } |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | if (!converter) { |
|---|
| 186 | n/a | converter = Py_None; |
|---|
| 187 | n/a | } |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | if (PyList_Append(self->row_cast_map, converter) != 0) { |
|---|
| 190 | n/a | if (converter != Py_None) { |
|---|
| 191 | n/a | Py_DECREF(converter); |
|---|
| 192 | n/a | } |
|---|
| 193 | n/a | Py_CLEAR(self->row_cast_map); |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | return -1; |
|---|
| 196 | n/a | } |
|---|
| 197 | n/a | } |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | return 0; |
|---|
| 200 | n/a | } |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | PyObject* _pysqlite_build_column_name(const char* colname) |
|---|
| 203 | n/a | { |
|---|
| 204 | n/a | const char* pos; |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | if (!colname) { |
|---|
| 207 | n/a | Py_RETURN_NONE; |
|---|
| 208 | n/a | } |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | for (pos = colname;; pos++) { |
|---|
| 211 | n/a | if (*pos == 0 || *pos == '[') { |
|---|
| 212 | n/a | if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) { |
|---|
| 213 | n/a | pos--; |
|---|
| 214 | n/a | } |
|---|
| 215 | n/a | return PyUnicode_FromStringAndSize(colname, pos - colname); |
|---|
| 216 | n/a | } |
|---|
| 217 | n/a | } |
|---|
| 218 | n/a | } |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | /* |
|---|
| 221 | n/a | * Returns a row from the currently active SQLite statement |
|---|
| 222 | n/a | * |
|---|
| 223 | n/a | * Precondidition: |
|---|
| 224 | n/a | * - sqlite3_step() has been called before and it returned SQLITE_ROW. |
|---|
| 225 | n/a | */ |
|---|
| 226 | n/a | PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self) |
|---|
| 227 | n/a | { |
|---|
| 228 | n/a | int i, numcols; |
|---|
| 229 | n/a | PyObject* row; |
|---|
| 230 | n/a | PyObject* item = NULL; |
|---|
| 231 | n/a | int coltype; |
|---|
| 232 | n/a | PyObject* converter; |
|---|
| 233 | n/a | PyObject* converted; |
|---|
| 234 | n/a | Py_ssize_t nbytes; |
|---|
| 235 | n/a | PyObject* buffer; |
|---|
| 236 | n/a | const char* val_str; |
|---|
| 237 | n/a | char buf[200]; |
|---|
| 238 | n/a | const char* colname; |
|---|
| 239 | n/a | PyObject* buf_bytes; |
|---|
| 240 | n/a | PyObject* error_obj; |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | if (self->reset) { |
|---|
| 243 | n/a | PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback); |
|---|
| 244 | n/a | return NULL; |
|---|
| 245 | n/a | } |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 248 | n/a | numcols = sqlite3_data_count(self->statement->st); |
|---|
| 249 | n/a | Py_END_ALLOW_THREADS |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | row = PyTuple_New(numcols); |
|---|
| 252 | n/a | if (!row) |
|---|
| 253 | n/a | return NULL; |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | for (i = 0; i < numcols; i++) { |
|---|
| 256 | n/a | if (self->connection->detect_types) { |
|---|
| 257 | n/a | converter = PyList_GetItem(self->row_cast_map, i); |
|---|
| 258 | n/a | if (!converter) { |
|---|
| 259 | n/a | converter = Py_None; |
|---|
| 260 | n/a | } |
|---|
| 261 | n/a | } else { |
|---|
| 262 | n/a | converter = Py_None; |
|---|
| 263 | n/a | } |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | if (converter != Py_None) { |
|---|
| 266 | n/a | nbytes = sqlite3_column_bytes(self->statement->st, i); |
|---|
| 267 | n/a | val_str = (const char*)sqlite3_column_blob(self->statement->st, i); |
|---|
| 268 | n/a | if (!val_str) { |
|---|
| 269 | n/a | Py_INCREF(Py_None); |
|---|
| 270 | n/a | converted = Py_None; |
|---|
| 271 | n/a | } else { |
|---|
| 272 | n/a | item = PyBytes_FromStringAndSize(val_str, nbytes); |
|---|
| 273 | n/a | if (!item) |
|---|
| 274 | n/a | goto error; |
|---|
| 275 | n/a | converted = PyObject_CallFunction(converter, "O", item); |
|---|
| 276 | n/a | Py_DECREF(item); |
|---|
| 277 | n/a | if (!converted) |
|---|
| 278 | n/a | break; |
|---|
| 279 | n/a | } |
|---|
| 280 | n/a | } else { |
|---|
| 281 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 282 | n/a | coltype = sqlite3_column_type(self->statement->st, i); |
|---|
| 283 | n/a | Py_END_ALLOW_THREADS |
|---|
| 284 | n/a | if (coltype == SQLITE_NULL) { |
|---|
| 285 | n/a | Py_INCREF(Py_None); |
|---|
| 286 | n/a | converted = Py_None; |
|---|
| 287 | n/a | } else if (coltype == SQLITE_INTEGER) { |
|---|
| 288 | n/a | converted = _pysqlite_long_from_int64(sqlite3_column_int64(self->statement->st, i)); |
|---|
| 289 | n/a | } else if (coltype == SQLITE_FLOAT) { |
|---|
| 290 | n/a | converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i)); |
|---|
| 291 | n/a | } else if (coltype == SQLITE_TEXT) { |
|---|
| 292 | n/a | val_str = (const char*)sqlite3_column_text(self->statement->st, i); |
|---|
| 293 | n/a | nbytes = sqlite3_column_bytes(self->statement->st, i); |
|---|
| 294 | n/a | if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) { |
|---|
| 295 | n/a | converted = PyUnicode_FromStringAndSize(val_str, nbytes); |
|---|
| 296 | n/a | if (!converted) { |
|---|
| 297 | n/a | PyErr_Clear(); |
|---|
| 298 | n/a | colname = sqlite3_column_name(self->statement->st, i); |
|---|
| 299 | n/a | if (!colname) { |
|---|
| 300 | n/a | colname = "<unknown column name>"; |
|---|
| 301 | n/a | } |
|---|
| 302 | n/a | PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'", |
|---|
| 303 | n/a | colname , val_str); |
|---|
| 304 | n/a | buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf)); |
|---|
| 305 | n/a | if (!buf_bytes) { |
|---|
| 306 | n/a | PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8"); |
|---|
| 307 | n/a | } else { |
|---|
| 308 | n/a | error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace"); |
|---|
| 309 | n/a | if (!error_obj) { |
|---|
| 310 | n/a | PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8"); |
|---|
| 311 | n/a | } else { |
|---|
| 312 | n/a | PyErr_SetObject(pysqlite_OperationalError, error_obj); |
|---|
| 313 | n/a | Py_DECREF(error_obj); |
|---|
| 314 | n/a | } |
|---|
| 315 | n/a | Py_DECREF(buf_bytes); |
|---|
| 316 | n/a | } |
|---|
| 317 | n/a | } |
|---|
| 318 | n/a | } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) { |
|---|
| 319 | n/a | converted = PyBytes_FromStringAndSize(val_str, nbytes); |
|---|
| 320 | n/a | } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) { |
|---|
| 321 | n/a | converted = PyByteArray_FromStringAndSize(val_str, nbytes); |
|---|
| 322 | n/a | } else { |
|---|
| 323 | n/a | converted = PyObject_CallFunction(self->connection->text_factory, "y#", val_str, nbytes); |
|---|
| 324 | n/a | } |
|---|
| 325 | n/a | } else { |
|---|
| 326 | n/a | /* coltype == SQLITE_BLOB */ |
|---|
| 327 | n/a | nbytes = sqlite3_column_bytes(self->statement->st, i); |
|---|
| 328 | n/a | buffer = PyBytes_FromStringAndSize( |
|---|
| 329 | n/a | sqlite3_column_blob(self->statement->st, i), nbytes); |
|---|
| 330 | n/a | if (!buffer) |
|---|
| 331 | n/a | break; |
|---|
| 332 | n/a | converted = buffer; |
|---|
| 333 | n/a | } |
|---|
| 334 | n/a | } |
|---|
| 335 | n/a | |
|---|
| 336 | n/a | if (converted) { |
|---|
| 337 | n/a | PyTuple_SetItem(row, i, converted); |
|---|
| 338 | n/a | } else { |
|---|
| 339 | n/a | Py_INCREF(Py_None); |
|---|
| 340 | n/a | PyTuple_SetItem(row, i, Py_None); |
|---|
| 341 | n/a | } |
|---|
| 342 | n/a | } |
|---|
| 343 | n/a | |
|---|
| 344 | n/a | if (PyErr_Occurred()) |
|---|
| 345 | n/a | goto error; |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | return row; |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | error: |
|---|
| 350 | n/a | Py_DECREF(row); |
|---|
| 351 | n/a | return NULL; |
|---|
| 352 | n/a | } |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | /* |
|---|
| 355 | n/a | * Checks if a cursor object is usable. |
|---|
| 356 | n/a | * |
|---|
| 357 | n/a | * 0 => error; 1 => ok |
|---|
| 358 | n/a | */ |
|---|
| 359 | n/a | static int check_cursor(pysqlite_Cursor* cur) |
|---|
| 360 | n/a | { |
|---|
| 361 | n/a | if (!cur->initialized) { |
|---|
| 362 | n/a | PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called."); |
|---|
| 363 | n/a | return 0; |
|---|
| 364 | n/a | } |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | if (cur->closed) { |
|---|
| 367 | n/a | PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor."); |
|---|
| 368 | n/a | return 0; |
|---|
| 369 | n/a | } |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | if (cur->locked) { |
|---|
| 372 | n/a | PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed."); |
|---|
| 373 | n/a | return 0; |
|---|
| 374 | n/a | } |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection); |
|---|
| 377 | n/a | } |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) |
|---|
| 380 | n/a | { |
|---|
| 381 | n/a | PyObject* operation; |
|---|
| 382 | n/a | const char* operation_cstr; |
|---|
| 383 | n/a | Py_ssize_t operation_len; |
|---|
| 384 | n/a | PyObject* parameters_list = NULL; |
|---|
| 385 | n/a | PyObject* parameters_iter = NULL; |
|---|
| 386 | n/a | PyObject* parameters = NULL; |
|---|
| 387 | n/a | int i; |
|---|
| 388 | n/a | int rc; |
|---|
| 389 | n/a | PyObject* func_args; |
|---|
| 390 | n/a | PyObject* result; |
|---|
| 391 | n/a | int numcols; |
|---|
| 392 | n/a | PyObject* descriptor; |
|---|
| 393 | n/a | PyObject* second_argument = NULL; |
|---|
| 394 | n/a | sqlite_int64 lastrowid; |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | if (!check_cursor(self)) { |
|---|
| 397 | n/a | goto error; |
|---|
| 398 | n/a | } |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | self->locked = 1; |
|---|
| 401 | n/a | self->reset = 0; |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | Py_CLEAR(self->next_row); |
|---|
| 404 | n/a | |
|---|
| 405 | n/a | if (multiple) { |
|---|
| 406 | n/a | /* executemany() */ |
|---|
| 407 | n/a | if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { |
|---|
| 408 | n/a | goto error; |
|---|
| 409 | n/a | } |
|---|
| 410 | n/a | |
|---|
| 411 | n/a | if (!PyUnicode_Check(operation)) { |
|---|
| 412 | n/a | PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); |
|---|
| 413 | n/a | goto error; |
|---|
| 414 | n/a | } |
|---|
| 415 | n/a | |
|---|
| 416 | n/a | if (PyIter_Check(second_argument)) { |
|---|
| 417 | n/a | /* iterator */ |
|---|
| 418 | n/a | Py_INCREF(second_argument); |
|---|
| 419 | n/a | parameters_iter = second_argument; |
|---|
| 420 | n/a | } else { |
|---|
| 421 | n/a | /* sequence */ |
|---|
| 422 | n/a | parameters_iter = PyObject_GetIter(second_argument); |
|---|
| 423 | n/a | if (!parameters_iter) { |
|---|
| 424 | n/a | goto error; |
|---|
| 425 | n/a | } |
|---|
| 426 | n/a | } |
|---|
| 427 | n/a | } else { |
|---|
| 428 | n/a | /* execute() */ |
|---|
| 429 | n/a | if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { |
|---|
| 430 | n/a | goto error; |
|---|
| 431 | n/a | } |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | if (!PyUnicode_Check(operation)) { |
|---|
| 434 | n/a | PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); |
|---|
| 435 | n/a | goto error; |
|---|
| 436 | n/a | } |
|---|
| 437 | n/a | |
|---|
| 438 | n/a | parameters_list = PyList_New(0); |
|---|
| 439 | n/a | if (!parameters_list) { |
|---|
| 440 | n/a | goto error; |
|---|
| 441 | n/a | } |
|---|
| 442 | n/a | |
|---|
| 443 | n/a | if (second_argument == NULL) { |
|---|
| 444 | n/a | second_argument = PyTuple_New(0); |
|---|
| 445 | n/a | if (!second_argument) { |
|---|
| 446 | n/a | goto error; |
|---|
| 447 | n/a | } |
|---|
| 448 | n/a | } else { |
|---|
| 449 | n/a | Py_INCREF(second_argument); |
|---|
| 450 | n/a | } |
|---|
| 451 | n/a | if (PyList_Append(parameters_list, second_argument) != 0) { |
|---|
| 452 | n/a | Py_DECREF(second_argument); |
|---|
| 453 | n/a | goto error; |
|---|
| 454 | n/a | } |
|---|
| 455 | n/a | Py_DECREF(second_argument); |
|---|
| 456 | n/a | |
|---|
| 457 | n/a | parameters_iter = PyObject_GetIter(parameters_list); |
|---|
| 458 | n/a | if (!parameters_iter) { |
|---|
| 459 | n/a | goto error; |
|---|
| 460 | n/a | } |
|---|
| 461 | n/a | } |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | if (self->statement != NULL) { |
|---|
| 464 | n/a | /* There is an active statement */ |
|---|
| 465 | n/a | pysqlite_statement_reset(self->statement); |
|---|
| 466 | n/a | } |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | operation_cstr = PyUnicode_AsUTF8AndSize(operation, &operation_len); |
|---|
| 469 | n/a | if (operation_cstr == NULL) |
|---|
| 470 | n/a | goto error; |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | /* reset description and rowcount */ |
|---|
| 473 | n/a | Py_INCREF(Py_None); |
|---|
| 474 | n/a | Py_SETREF(self->description, Py_None); |
|---|
| 475 | n/a | self->rowcount = 0L; |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | func_args = PyTuple_New(1); |
|---|
| 478 | n/a | if (!func_args) { |
|---|
| 479 | n/a | goto error; |
|---|
| 480 | n/a | } |
|---|
| 481 | n/a | Py_INCREF(operation); |
|---|
| 482 | n/a | if (PyTuple_SetItem(func_args, 0, operation) != 0) { |
|---|
| 483 | n/a | goto error; |
|---|
| 484 | n/a | } |
|---|
| 485 | n/a | |
|---|
| 486 | n/a | if (self->statement) { |
|---|
| 487 | n/a | (void)pysqlite_statement_reset(self->statement); |
|---|
| 488 | n/a | } |
|---|
| 489 | n/a | |
|---|
| 490 | n/a | Py_XSETREF(self->statement, |
|---|
| 491 | n/a | (pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args)); |
|---|
| 492 | n/a | Py_DECREF(func_args); |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | if (!self->statement) { |
|---|
| 495 | n/a | goto error; |
|---|
| 496 | n/a | } |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | if (self->statement->in_use) { |
|---|
| 499 | n/a | Py_SETREF(self->statement, |
|---|
| 500 | n/a | PyObject_New(pysqlite_Statement, &pysqlite_StatementType)); |
|---|
| 501 | n/a | if (!self->statement) { |
|---|
| 502 | n/a | goto error; |
|---|
| 503 | n/a | } |
|---|
| 504 | n/a | rc = pysqlite_statement_create(self->statement, self->connection, operation); |
|---|
| 505 | n/a | if (rc != SQLITE_OK) { |
|---|
| 506 | n/a | Py_CLEAR(self->statement); |
|---|
| 507 | n/a | goto error; |
|---|
| 508 | n/a | } |
|---|
| 509 | n/a | } |
|---|
| 510 | n/a | |
|---|
| 511 | n/a | pysqlite_statement_reset(self->statement); |
|---|
| 512 | n/a | pysqlite_statement_mark_dirty(self->statement); |
|---|
| 513 | n/a | |
|---|
| 514 | n/a | /* For backwards compatibility reasons, do not start a transaction if a |
|---|
| 515 | n/a | DDL statement is encountered. If anybody wants transactional DDL, |
|---|
| 516 | n/a | they can issue a BEGIN statement manually. */ |
|---|
| 517 | n/a | if (self->connection->begin_statement && !sqlite3_stmt_readonly(self->statement->st) && !self->statement->is_ddl) { |
|---|
| 518 | n/a | if (sqlite3_get_autocommit(self->connection->db)) { |
|---|
| 519 | n/a | result = _pysqlite_connection_begin(self->connection); |
|---|
| 520 | n/a | if (!result) { |
|---|
| 521 | n/a | goto error; |
|---|
| 522 | n/a | } |
|---|
| 523 | n/a | Py_DECREF(result); |
|---|
| 524 | n/a | } |
|---|
| 525 | n/a | } |
|---|
| 526 | n/a | |
|---|
| 527 | n/a | while (1) { |
|---|
| 528 | n/a | parameters = PyIter_Next(parameters_iter); |
|---|
| 529 | n/a | if (!parameters) { |
|---|
| 530 | n/a | break; |
|---|
| 531 | n/a | } |
|---|
| 532 | n/a | |
|---|
| 533 | n/a | pysqlite_statement_mark_dirty(self->statement); |
|---|
| 534 | n/a | |
|---|
| 535 | n/a | pysqlite_statement_bind_parameters(self->statement, parameters); |
|---|
| 536 | n/a | if (PyErr_Occurred()) { |
|---|
| 537 | n/a | goto error; |
|---|
| 538 | n/a | } |
|---|
| 539 | n/a | |
|---|
| 540 | n/a | /* Keep trying the SQL statement until the schema stops changing. */ |
|---|
| 541 | n/a | while (1) { |
|---|
| 542 | n/a | /* Actually execute the SQL statement. */ |
|---|
| 543 | n/a | rc = pysqlite_step(self->statement->st, self->connection); |
|---|
| 544 | n/a | if (PyErr_Occurred()) { |
|---|
| 545 | n/a | (void)pysqlite_statement_reset(self->statement); |
|---|
| 546 | n/a | goto error; |
|---|
| 547 | n/a | } |
|---|
| 548 | n/a | if (rc == SQLITE_DONE || rc == SQLITE_ROW) { |
|---|
| 549 | n/a | /* If it worked, let's get out of the loop */ |
|---|
| 550 | n/a | break; |
|---|
| 551 | n/a | } |
|---|
| 552 | n/a | /* Something went wrong. Re-set the statement and try again. */ |
|---|
| 553 | n/a | rc = pysqlite_statement_reset(self->statement); |
|---|
| 554 | n/a | if (rc == SQLITE_SCHEMA) { |
|---|
| 555 | n/a | /* If this was a result of the schema changing, let's try |
|---|
| 556 | n/a | again. */ |
|---|
| 557 | n/a | rc = pysqlite_statement_recompile(self->statement, parameters); |
|---|
| 558 | n/a | if (rc == SQLITE_OK) { |
|---|
| 559 | n/a | continue; |
|---|
| 560 | n/a | } else { |
|---|
| 561 | n/a | /* If the database gave us an error, promote it to Python. */ |
|---|
| 562 | n/a | (void)pysqlite_statement_reset(self->statement); |
|---|
| 563 | n/a | _pysqlite_seterror(self->connection->db, NULL); |
|---|
| 564 | n/a | goto error; |
|---|
| 565 | n/a | } |
|---|
| 566 | n/a | } else { |
|---|
| 567 | n/a | if (PyErr_Occurred()) { |
|---|
| 568 | n/a | /* there was an error that occurred in a user-defined callback */ |
|---|
| 569 | n/a | if (_enable_callback_tracebacks) { |
|---|
| 570 | n/a | PyErr_Print(); |
|---|
| 571 | n/a | } else { |
|---|
| 572 | n/a | PyErr_Clear(); |
|---|
| 573 | n/a | } |
|---|
| 574 | n/a | } |
|---|
| 575 | n/a | (void)pysqlite_statement_reset(self->statement); |
|---|
| 576 | n/a | _pysqlite_seterror(self->connection->db, NULL); |
|---|
| 577 | n/a | goto error; |
|---|
| 578 | n/a | } |
|---|
| 579 | n/a | } |
|---|
| 580 | n/a | |
|---|
| 581 | n/a | if (pysqlite_build_row_cast_map(self) != 0) { |
|---|
| 582 | n/a | PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map"); |
|---|
| 583 | n/a | goto error; |
|---|
| 584 | n/a | } |
|---|
| 585 | n/a | |
|---|
| 586 | n/a | if (rc == SQLITE_ROW || rc == SQLITE_DONE) { |
|---|
| 587 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 588 | n/a | numcols = sqlite3_column_count(self->statement->st); |
|---|
| 589 | n/a | Py_END_ALLOW_THREADS |
|---|
| 590 | n/a | if (self->description == Py_None && numcols > 0) { |
|---|
| 591 | n/a | Py_SETREF(self->description, PyTuple_New(numcols)); |
|---|
| 592 | n/a | if (!self->description) { |
|---|
| 593 | n/a | goto error; |
|---|
| 594 | n/a | } |
|---|
| 595 | n/a | for (i = 0; i < numcols; i++) { |
|---|
| 596 | n/a | descriptor = PyTuple_New(7); |
|---|
| 597 | n/a | if (!descriptor) { |
|---|
| 598 | n/a | goto error; |
|---|
| 599 | n/a | } |
|---|
| 600 | n/a | PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i))); |
|---|
| 601 | n/a | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None); |
|---|
| 602 | n/a | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None); |
|---|
| 603 | n/a | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None); |
|---|
| 604 | n/a | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None); |
|---|
| 605 | n/a | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None); |
|---|
| 606 | n/a | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None); |
|---|
| 607 | n/a | PyTuple_SetItem(self->description, i, descriptor); |
|---|
| 608 | n/a | } |
|---|
| 609 | n/a | } |
|---|
| 610 | n/a | } |
|---|
| 611 | n/a | |
|---|
| 612 | n/a | if (!sqlite3_stmt_readonly(self->statement->st)) { |
|---|
| 613 | n/a | self->rowcount += (long)sqlite3_changes(self->connection->db); |
|---|
| 614 | n/a | } else { |
|---|
| 615 | n/a | self->rowcount= -1L; |
|---|
| 616 | n/a | } |
|---|
| 617 | n/a | |
|---|
| 618 | n/a | if (!multiple) { |
|---|
| 619 | n/a | Py_DECREF(self->lastrowid); |
|---|
| 620 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 621 | n/a | lastrowid = sqlite3_last_insert_rowid(self->connection->db); |
|---|
| 622 | n/a | Py_END_ALLOW_THREADS |
|---|
| 623 | n/a | self->lastrowid = _pysqlite_long_from_int64(lastrowid); |
|---|
| 624 | n/a | } |
|---|
| 625 | n/a | |
|---|
| 626 | n/a | if (rc == SQLITE_ROW) { |
|---|
| 627 | n/a | if (multiple) { |
|---|
| 628 | n/a | PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements."); |
|---|
| 629 | n/a | goto error; |
|---|
| 630 | n/a | } |
|---|
| 631 | n/a | |
|---|
| 632 | n/a | self->next_row = _pysqlite_fetch_one_row(self); |
|---|
| 633 | n/a | if (self->next_row == NULL) |
|---|
| 634 | n/a | goto error; |
|---|
| 635 | n/a | } else if (rc == SQLITE_DONE && !multiple) { |
|---|
| 636 | n/a | pysqlite_statement_reset(self->statement); |
|---|
| 637 | n/a | Py_CLEAR(self->statement); |
|---|
| 638 | n/a | } |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | if (multiple) { |
|---|
| 641 | n/a | pysqlite_statement_reset(self->statement); |
|---|
| 642 | n/a | } |
|---|
| 643 | n/a | Py_XDECREF(parameters); |
|---|
| 644 | n/a | } |
|---|
| 645 | n/a | |
|---|
| 646 | n/a | error: |
|---|
| 647 | n/a | Py_XDECREF(parameters); |
|---|
| 648 | n/a | Py_XDECREF(parameters_iter); |
|---|
| 649 | n/a | Py_XDECREF(parameters_list); |
|---|
| 650 | n/a | |
|---|
| 651 | n/a | self->locked = 0; |
|---|
| 652 | n/a | |
|---|
| 653 | n/a | if (PyErr_Occurred()) { |
|---|
| 654 | n/a | self->rowcount = -1L; |
|---|
| 655 | n/a | return NULL; |
|---|
| 656 | n/a | } else { |
|---|
| 657 | n/a | Py_INCREF(self); |
|---|
| 658 | n/a | return (PyObject*)self; |
|---|
| 659 | n/a | } |
|---|
| 660 | n/a | } |
|---|
| 661 | n/a | |
|---|
| 662 | n/a | PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args) |
|---|
| 663 | n/a | { |
|---|
| 664 | n/a | return _pysqlite_query_execute(self, 0, args); |
|---|
| 665 | n/a | } |
|---|
| 666 | n/a | |
|---|
| 667 | n/a | PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args) |
|---|
| 668 | n/a | { |
|---|
| 669 | n/a | return _pysqlite_query_execute(self, 1, args); |
|---|
| 670 | n/a | } |
|---|
| 671 | n/a | |
|---|
| 672 | n/a | PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args) |
|---|
| 673 | n/a | { |
|---|
| 674 | n/a | PyObject* script_obj; |
|---|
| 675 | n/a | PyObject* script_str = NULL; |
|---|
| 676 | n/a | const char* script_cstr; |
|---|
| 677 | n/a | sqlite3_stmt* statement; |
|---|
| 678 | n/a | int rc; |
|---|
| 679 | n/a | PyObject* result; |
|---|
| 680 | n/a | |
|---|
| 681 | n/a | if (!PyArg_ParseTuple(args, "O", &script_obj)) { |
|---|
| 682 | n/a | return NULL; |
|---|
| 683 | n/a | } |
|---|
| 684 | n/a | |
|---|
| 685 | n/a | if (!check_cursor(self)) { |
|---|
| 686 | n/a | return NULL; |
|---|
| 687 | n/a | } |
|---|
| 688 | n/a | |
|---|
| 689 | n/a | self->reset = 0; |
|---|
| 690 | n/a | |
|---|
| 691 | n/a | if (PyUnicode_Check(script_obj)) { |
|---|
| 692 | n/a | script_cstr = PyUnicode_AsUTF8(script_obj); |
|---|
| 693 | n/a | if (!script_cstr) { |
|---|
| 694 | n/a | return NULL; |
|---|
| 695 | n/a | } |
|---|
| 696 | n/a | } else { |
|---|
| 697 | n/a | PyErr_SetString(PyExc_ValueError, "script argument must be unicode."); |
|---|
| 698 | n/a | return NULL; |
|---|
| 699 | n/a | } |
|---|
| 700 | n/a | |
|---|
| 701 | n/a | /* commit first */ |
|---|
| 702 | n/a | result = pysqlite_connection_commit(self->connection, NULL); |
|---|
| 703 | n/a | if (!result) { |
|---|
| 704 | n/a | goto error; |
|---|
| 705 | n/a | } |
|---|
| 706 | n/a | Py_DECREF(result); |
|---|
| 707 | n/a | |
|---|
| 708 | n/a | while (1) { |
|---|
| 709 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 710 | n/a | rc = sqlite3_prepare(self->connection->db, |
|---|
| 711 | n/a | script_cstr, |
|---|
| 712 | n/a | -1, |
|---|
| 713 | n/a | &statement, |
|---|
| 714 | n/a | &script_cstr); |
|---|
| 715 | n/a | Py_END_ALLOW_THREADS |
|---|
| 716 | n/a | if (rc != SQLITE_OK) { |
|---|
| 717 | n/a | _pysqlite_seterror(self->connection->db, NULL); |
|---|
| 718 | n/a | goto error; |
|---|
| 719 | n/a | } |
|---|
| 720 | n/a | |
|---|
| 721 | n/a | /* execute statement, and ignore results of SELECT statements */ |
|---|
| 722 | n/a | rc = SQLITE_ROW; |
|---|
| 723 | n/a | while (rc == SQLITE_ROW) { |
|---|
| 724 | n/a | rc = pysqlite_step(statement, self->connection); |
|---|
| 725 | n/a | if (PyErr_Occurred()) { |
|---|
| 726 | n/a | (void)sqlite3_finalize(statement); |
|---|
| 727 | n/a | goto error; |
|---|
| 728 | n/a | } |
|---|
| 729 | n/a | } |
|---|
| 730 | n/a | |
|---|
| 731 | n/a | if (rc != SQLITE_DONE) { |
|---|
| 732 | n/a | (void)sqlite3_finalize(statement); |
|---|
| 733 | n/a | _pysqlite_seterror(self->connection->db, NULL); |
|---|
| 734 | n/a | goto error; |
|---|
| 735 | n/a | } |
|---|
| 736 | n/a | |
|---|
| 737 | n/a | rc = sqlite3_finalize(statement); |
|---|
| 738 | n/a | if (rc != SQLITE_OK) { |
|---|
| 739 | n/a | _pysqlite_seterror(self->connection->db, NULL); |
|---|
| 740 | n/a | goto error; |
|---|
| 741 | n/a | } |
|---|
| 742 | n/a | |
|---|
| 743 | n/a | if (*script_cstr == (char)0) { |
|---|
| 744 | n/a | break; |
|---|
| 745 | n/a | } |
|---|
| 746 | n/a | } |
|---|
| 747 | n/a | |
|---|
| 748 | n/a | error: |
|---|
| 749 | n/a | Py_XDECREF(script_str); |
|---|
| 750 | n/a | |
|---|
| 751 | n/a | if (PyErr_Occurred()) { |
|---|
| 752 | n/a | return NULL; |
|---|
| 753 | n/a | } else { |
|---|
| 754 | n/a | Py_INCREF(self); |
|---|
| 755 | n/a | return (PyObject*)self; |
|---|
| 756 | n/a | } |
|---|
| 757 | n/a | } |
|---|
| 758 | n/a | |
|---|
| 759 | n/a | PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self) |
|---|
| 760 | n/a | { |
|---|
| 761 | n/a | Py_INCREF(self); |
|---|
| 762 | n/a | return (PyObject*)self; |
|---|
| 763 | n/a | } |
|---|
| 764 | n/a | |
|---|
| 765 | n/a | PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self) |
|---|
| 766 | n/a | { |
|---|
| 767 | n/a | PyObject* next_row_tuple; |
|---|
| 768 | n/a | PyObject* next_row; |
|---|
| 769 | n/a | int rc; |
|---|
| 770 | n/a | |
|---|
| 771 | n/a | if (!check_cursor(self)) { |
|---|
| 772 | n/a | return NULL; |
|---|
| 773 | n/a | } |
|---|
| 774 | n/a | |
|---|
| 775 | n/a | if (self->reset) { |
|---|
| 776 | n/a | PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback); |
|---|
| 777 | n/a | return NULL; |
|---|
| 778 | n/a | } |
|---|
| 779 | n/a | |
|---|
| 780 | n/a | if (!self->next_row) { |
|---|
| 781 | n/a | if (self->statement) { |
|---|
| 782 | n/a | (void)pysqlite_statement_reset(self->statement); |
|---|
| 783 | n/a | Py_CLEAR(self->statement); |
|---|
| 784 | n/a | } |
|---|
| 785 | n/a | return NULL; |
|---|
| 786 | n/a | } |
|---|
| 787 | n/a | |
|---|
| 788 | n/a | next_row_tuple = self->next_row; |
|---|
| 789 | n/a | assert(next_row_tuple != NULL); |
|---|
| 790 | n/a | self->next_row = NULL; |
|---|
| 791 | n/a | |
|---|
| 792 | n/a | if (self->row_factory != Py_None) { |
|---|
| 793 | n/a | next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple); |
|---|
| 794 | n/a | if (next_row == NULL) { |
|---|
| 795 | n/a | self->next_row = next_row_tuple; |
|---|
| 796 | n/a | return NULL; |
|---|
| 797 | n/a | } |
|---|
| 798 | n/a | Py_DECREF(next_row_tuple); |
|---|
| 799 | n/a | } else { |
|---|
| 800 | n/a | next_row = next_row_tuple; |
|---|
| 801 | n/a | } |
|---|
| 802 | n/a | |
|---|
| 803 | n/a | if (self->statement) { |
|---|
| 804 | n/a | rc = pysqlite_step(self->statement->st, self->connection); |
|---|
| 805 | n/a | if (PyErr_Occurred()) { |
|---|
| 806 | n/a | (void)pysqlite_statement_reset(self->statement); |
|---|
| 807 | n/a | Py_DECREF(next_row); |
|---|
| 808 | n/a | return NULL; |
|---|
| 809 | n/a | } |
|---|
| 810 | n/a | if (rc != SQLITE_DONE && rc != SQLITE_ROW) { |
|---|
| 811 | n/a | (void)pysqlite_statement_reset(self->statement); |
|---|
| 812 | n/a | Py_DECREF(next_row); |
|---|
| 813 | n/a | _pysqlite_seterror(self->connection->db, NULL); |
|---|
| 814 | n/a | return NULL; |
|---|
| 815 | n/a | } |
|---|
| 816 | n/a | |
|---|
| 817 | n/a | if (rc == SQLITE_ROW) { |
|---|
| 818 | n/a | self->next_row = _pysqlite_fetch_one_row(self); |
|---|
| 819 | n/a | if (self->next_row == NULL) { |
|---|
| 820 | n/a | (void)pysqlite_statement_reset(self->statement); |
|---|
| 821 | n/a | return NULL; |
|---|
| 822 | n/a | } |
|---|
| 823 | n/a | } |
|---|
| 824 | n/a | } |
|---|
| 825 | n/a | |
|---|
| 826 | n/a | return next_row; |
|---|
| 827 | n/a | } |
|---|
| 828 | n/a | |
|---|
| 829 | n/a | PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args) |
|---|
| 830 | n/a | { |
|---|
| 831 | n/a | PyObject* row; |
|---|
| 832 | n/a | |
|---|
| 833 | n/a | row = pysqlite_cursor_iternext(self); |
|---|
| 834 | n/a | if (!row && !PyErr_Occurred()) { |
|---|
| 835 | n/a | Py_RETURN_NONE; |
|---|
| 836 | n/a | } |
|---|
| 837 | n/a | |
|---|
| 838 | n/a | return row; |
|---|
| 839 | n/a | } |
|---|
| 840 | n/a | |
|---|
| 841 | n/a | PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) |
|---|
| 842 | n/a | { |
|---|
| 843 | n/a | static char *kwlist[] = {"size", NULL, NULL}; |
|---|
| 844 | n/a | |
|---|
| 845 | n/a | PyObject* row; |
|---|
| 846 | n/a | PyObject* list; |
|---|
| 847 | n/a | int maxrows = self->arraysize; |
|---|
| 848 | n/a | int counter = 0; |
|---|
| 849 | n/a | |
|---|
| 850 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) { |
|---|
| 851 | n/a | return NULL; |
|---|
| 852 | n/a | } |
|---|
| 853 | n/a | |
|---|
| 854 | n/a | list = PyList_New(0); |
|---|
| 855 | n/a | if (!list) { |
|---|
| 856 | n/a | return NULL; |
|---|
| 857 | n/a | } |
|---|
| 858 | n/a | |
|---|
| 859 | n/a | /* just make sure we enter the loop */ |
|---|
| 860 | n/a | row = Py_None; |
|---|
| 861 | n/a | |
|---|
| 862 | n/a | while (row) { |
|---|
| 863 | n/a | row = pysqlite_cursor_iternext(self); |
|---|
| 864 | n/a | if (row) { |
|---|
| 865 | n/a | PyList_Append(list, row); |
|---|
| 866 | n/a | Py_DECREF(row); |
|---|
| 867 | n/a | } else { |
|---|
| 868 | n/a | break; |
|---|
| 869 | n/a | } |
|---|
| 870 | n/a | |
|---|
| 871 | n/a | if (++counter == maxrows) { |
|---|
| 872 | n/a | break; |
|---|
| 873 | n/a | } |
|---|
| 874 | n/a | } |
|---|
| 875 | n/a | |
|---|
| 876 | n/a | if (PyErr_Occurred()) { |
|---|
| 877 | n/a | Py_DECREF(list); |
|---|
| 878 | n/a | return NULL; |
|---|
| 879 | n/a | } else { |
|---|
| 880 | n/a | return list; |
|---|
| 881 | n/a | } |
|---|
| 882 | n/a | } |
|---|
| 883 | n/a | |
|---|
| 884 | n/a | PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args) |
|---|
| 885 | n/a | { |
|---|
| 886 | n/a | PyObject* row; |
|---|
| 887 | n/a | PyObject* list; |
|---|
| 888 | n/a | |
|---|
| 889 | n/a | list = PyList_New(0); |
|---|
| 890 | n/a | if (!list) { |
|---|
| 891 | n/a | return NULL; |
|---|
| 892 | n/a | } |
|---|
| 893 | n/a | |
|---|
| 894 | n/a | /* just make sure we enter the loop */ |
|---|
| 895 | n/a | row = (PyObject*)Py_None; |
|---|
| 896 | n/a | |
|---|
| 897 | n/a | while (row) { |
|---|
| 898 | n/a | row = pysqlite_cursor_iternext(self); |
|---|
| 899 | n/a | if (row) { |
|---|
| 900 | n/a | PyList_Append(list, row); |
|---|
| 901 | n/a | Py_DECREF(row); |
|---|
| 902 | n/a | } |
|---|
| 903 | n/a | } |
|---|
| 904 | n/a | |
|---|
| 905 | n/a | if (PyErr_Occurred()) { |
|---|
| 906 | n/a | Py_DECREF(list); |
|---|
| 907 | n/a | return NULL; |
|---|
| 908 | n/a | } else { |
|---|
| 909 | n/a | return list; |
|---|
| 910 | n/a | } |
|---|
| 911 | n/a | } |
|---|
| 912 | n/a | |
|---|
| 913 | n/a | PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args) |
|---|
| 914 | n/a | { |
|---|
| 915 | n/a | /* don't care, return None */ |
|---|
| 916 | n/a | Py_RETURN_NONE; |
|---|
| 917 | n/a | } |
|---|
| 918 | n/a | |
|---|
| 919 | n/a | PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) |
|---|
| 920 | n/a | { |
|---|
| 921 | n/a | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
|---|
| 922 | n/a | return NULL; |
|---|
| 923 | n/a | } |
|---|
| 924 | n/a | |
|---|
| 925 | n/a | if (self->statement) { |
|---|
| 926 | n/a | (void)pysqlite_statement_reset(self->statement); |
|---|
| 927 | n/a | Py_CLEAR(self->statement); |
|---|
| 928 | n/a | } |
|---|
| 929 | n/a | |
|---|
| 930 | n/a | self->closed = 1; |
|---|
| 931 | n/a | |
|---|
| 932 | n/a | Py_RETURN_NONE; |
|---|
| 933 | n/a | } |
|---|
| 934 | n/a | |
|---|
| 935 | n/a | static PyMethodDef cursor_methods[] = { |
|---|
| 936 | n/a | {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS, |
|---|
| 937 | n/a | PyDoc_STR("Executes a SQL statement.")}, |
|---|
| 938 | n/a | {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS, |
|---|
| 939 | n/a | PyDoc_STR("Repeatedly executes a SQL statement.")}, |
|---|
| 940 | n/a | {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS, |
|---|
| 941 | n/a | PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, |
|---|
| 942 | n/a | {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS, |
|---|
| 943 | n/a | PyDoc_STR("Fetches one row from the resultset.")}, |
|---|
| 944 | n/a | {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS, |
|---|
| 945 | n/a | PyDoc_STR("Fetches several rows from the resultset.")}, |
|---|
| 946 | n/a | {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS, |
|---|
| 947 | n/a | PyDoc_STR("Fetches all rows from the resultset.")}, |
|---|
| 948 | n/a | {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, |
|---|
| 949 | n/a | PyDoc_STR("Closes the cursor.")}, |
|---|
| 950 | n/a | {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS, |
|---|
| 951 | n/a | PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, |
|---|
| 952 | n/a | {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS, |
|---|
| 953 | n/a | PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, |
|---|
| 954 | n/a | {NULL, NULL} |
|---|
| 955 | n/a | }; |
|---|
| 956 | n/a | |
|---|
| 957 | n/a | static struct PyMemberDef cursor_members[] = |
|---|
| 958 | n/a | { |
|---|
| 959 | n/a | {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY}, |
|---|
| 960 | n/a | {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY}, |
|---|
| 961 | n/a | {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0}, |
|---|
| 962 | n/a | {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY}, |
|---|
| 963 | n/a | {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY}, |
|---|
| 964 | n/a | {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0}, |
|---|
| 965 | n/a | {NULL} |
|---|
| 966 | n/a | }; |
|---|
| 967 | n/a | |
|---|
| 968 | n/a | static const char cursor_doc[] = |
|---|
| 969 | n/a | PyDoc_STR("SQLite database cursor class."); |
|---|
| 970 | n/a | |
|---|
| 971 | n/a | PyTypeObject pysqlite_CursorType = { |
|---|
| 972 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 973 | n/a | MODULE_NAME ".Cursor", /* tp_name */ |
|---|
| 974 | n/a | sizeof(pysqlite_Cursor), /* tp_basicsize */ |
|---|
| 975 | n/a | 0, /* tp_itemsize */ |
|---|
| 976 | n/a | (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */ |
|---|
| 977 | n/a | 0, /* tp_print */ |
|---|
| 978 | n/a | 0, /* tp_getattr */ |
|---|
| 979 | n/a | 0, /* tp_setattr */ |
|---|
| 980 | n/a | 0, /* tp_reserved */ |
|---|
| 981 | n/a | 0, /* tp_repr */ |
|---|
| 982 | n/a | 0, /* tp_as_number */ |
|---|
| 983 | n/a | 0, /* tp_as_sequence */ |
|---|
| 984 | n/a | 0, /* tp_as_mapping */ |
|---|
| 985 | n/a | 0, /* tp_hash */ |
|---|
| 986 | n/a | 0, /* tp_call */ |
|---|
| 987 | n/a | 0, /* tp_str */ |
|---|
| 988 | n/a | 0, /* tp_getattro */ |
|---|
| 989 | n/a | 0, /* tp_setattro */ |
|---|
| 990 | n/a | 0, /* tp_as_buffer */ |
|---|
| 991 | n/a | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 992 | n/a | cursor_doc, /* tp_doc */ |
|---|
| 993 | n/a | 0, /* tp_traverse */ |
|---|
| 994 | n/a | 0, /* tp_clear */ |
|---|
| 995 | n/a | 0, /* tp_richcompare */ |
|---|
| 996 | n/a | offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */ |
|---|
| 997 | n/a | (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */ |
|---|
| 998 | n/a | (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */ |
|---|
| 999 | n/a | cursor_methods, /* tp_methods */ |
|---|
| 1000 | n/a | cursor_members, /* tp_members */ |
|---|
| 1001 | n/a | 0, /* tp_getset */ |
|---|
| 1002 | n/a | 0, /* tp_base */ |
|---|
| 1003 | n/a | 0, /* tp_dict */ |
|---|
| 1004 | n/a | 0, /* tp_descr_get */ |
|---|
| 1005 | n/a | 0, /* tp_descr_set */ |
|---|
| 1006 | n/a | 0, /* tp_dictoffset */ |
|---|
| 1007 | n/a | (initproc)pysqlite_cursor_init, /* tp_init */ |
|---|
| 1008 | n/a | 0, /* tp_alloc */ |
|---|
| 1009 | n/a | 0, /* tp_new */ |
|---|
| 1010 | n/a | 0 /* tp_free */ |
|---|
| 1011 | n/a | }; |
|---|
| 1012 | n/a | |
|---|
| 1013 | n/a | extern int pysqlite_cursor_setup_types(void) |
|---|
| 1014 | n/a | { |
|---|
| 1015 | n/a | pysqlite_CursorType.tp_new = PyType_GenericNew; |
|---|
| 1016 | n/a | return PyType_Ready(&pysqlite_CursorType); |
|---|
| 1017 | n/a | } |
|---|