Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 30 | #ifndef INT32_MIN |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 31 | #define INT32_MIN (-2147483647 - 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 32 | #endif |
| 33 | #ifndef INT32_MAX |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 34 | #define INT32_MAX 2147483647 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 35 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 36 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 37 | PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 6374bb5 | 2007-06-13 16:28:25 +0000 | [diff] [blame] | 39 | static pysqlite_StatementKind detect_statement_type(const char* statement) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 40 | { |
| 41 | char buf[20]; |
Guido van Rossum | 6374bb5 | 2007-06-13 16:28:25 +0000 | [diff] [blame] | 42 | const char* src; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 77 | int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 79 | pysqlite_Connection* connection; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 80 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 81 | if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 112 | if (!pysqlite_check_thread(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 119 | void pysqlite_cursor_dealloc(pysqlite_Cursor* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 120 | { |
| 121 | int rc; |
| 122 | |
| 123 | /* Reset the statement if the user has not closed the cursor */ |
| 124 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 125 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 137 | Py_Type(self)->tp_free((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 140 | PyObject* _pysqlite_get_converter(PyObject* key) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 156 | int pysqlite_build_row_cast_map(pysqlite_Cursor* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 178 | if (self->connection->detect_types & PARSE_COLNAMES) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 179 | colname = sqlite3_column_name(self->statement->st, i); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 192 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 193 | converter = _pysqlite_get_converter(key); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 194 | Py_DECREF(key); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 197 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 201 | if (!converter && self->connection->detect_types & PARSE_DECLTYPES) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 214 | converter = _pysqlite_get_converter(py_decltype); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 237 | PyObject* _pysqlite_build_column_name(const char* colname) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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++) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 247 | if (*pos == 0 || *pos == '[') { |
| 248 | if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) { |
| 249 | pos--; |
| 250 | } |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 251 | return PyUnicode_FromStringAndSize(colname, pos - colname); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 256 | PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 288 | PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 302 | const char* colname; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 324 | nbytes = sqlite3_column_bytes(self->statement->st, i); |
| 325 | val_str = (const char*)sqlite3_column_blob(self->statement->st, i); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 326 | if (!val_str) { |
| 327 | Py_INCREF(Py_None); |
| 328 | converted = Py_None; |
| 329 | } else { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 330 | item = PyString_FromStringAndSize(val_str, nbytes); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 331 | if (!item) { |
| 332 | return NULL; |
| 333 | } |
| 334 | converted = PyObject_CallFunction(converter, "O", item); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 335 | Py_DECREF(item); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 336 | if (!converted) { |
| 337 | break; |
| 338 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 359 | || (self->connection->text_factory == pysqlite_OptimizedUnicode)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 361 | converted = pysqlite_unicode_from_string(val_str, |
| 362 | self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 363 | |
| 364 | if (!converted) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 365 | colname = sqlite3_column_name(self->statement->st, i); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 366 | if (!colname) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 367 | colname = "<unknown column name>"; |
| 368 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 369 | PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 370 | colname , val_str); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 371 | PyErr_SetString(pysqlite_OperationalError, buf); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 372 | } |
| 373 | } else if (self->connection->text_factory == (PyObject*)&PyString_Type) { |
| 374 | converted = PyString_FromString(val_str); |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 375 | } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) { |
| 376 | converted = PyBytes_FromStringAndSize(val_str, strlen(val_str)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 377 | } else { |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 378 | converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 379 | } |
| 380 | } else { |
| 381 | /* coltype == SQLITE_BLOB */ |
| 382 | nbytes = sqlite3_column_bytes(self->statement->st, i); |
| 383 | buffer = PyBuffer_New(nbytes); |
| 384 | if (!buffer) { |
| 385 | break; |
| 386 | } |
| 387 | if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &nbytes)) { |
| 388 | break; |
| 389 | } |
| 390 | memcpy(raw_buffer, sqlite3_column_blob(self->statement->st, i), nbytes); |
| 391 | converted = buffer; |
| 392 | } |
| 393 | } |
| 394 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 395 | if (converted) { |
| 396 | PyTuple_SetItem(row, i, converted); |
| 397 | } else { |
| 398 | Py_INCREF(Py_None); |
| 399 | PyTuple_SetItem(row, i, Py_None); |
| 400 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | if (PyErr_Occurred()) { |
| 404 | Py_DECREF(row); |
| 405 | row = NULL; |
| 406 | } |
| 407 | |
| 408 | return row; |
| 409 | } |
| 410 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 411 | PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 412 | { |
| 413 | PyObject* operation; |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 414 | const char* operation_cstr; |
| 415 | Py_ssize_t operation_len; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 416 | PyObject* parameters_list = NULL; |
| 417 | PyObject* parameters_iter = NULL; |
| 418 | PyObject* parameters = NULL; |
| 419 | int i; |
| 420 | int rc; |
| 421 | PyObject* func_args; |
| 422 | PyObject* result; |
| 423 | int numcols; |
| 424 | PY_LONG_LONG lastrowid; |
| 425 | int statement_type; |
| 426 | PyObject* descriptor; |
| 427 | PyObject* second_argument = NULL; |
| 428 | long rowcount = 0; |
| 429 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 430 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | Py_XDECREF(self->next_row); |
| 435 | self->next_row = NULL; |
| 436 | |
| 437 | if (multiple) { |
| 438 | /* executemany() */ |
| 439 | if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { |
| 444 | PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); |
| 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | if (PyIter_Check(second_argument)) { |
| 449 | /* iterator */ |
| 450 | Py_INCREF(second_argument); |
| 451 | parameters_iter = second_argument; |
| 452 | } else { |
| 453 | /* sequence */ |
| 454 | parameters_iter = PyObject_GetIter(second_argument); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 455 | if (!parameters_iter) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
| 458 | } |
| 459 | } else { |
| 460 | /* execute() */ |
| 461 | if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { |
| 466 | PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | parameters_list = PyList_New(0); |
| 471 | if (!parameters_list) { |
| 472 | return NULL; |
| 473 | } |
| 474 | |
| 475 | if (second_argument == NULL) { |
| 476 | second_argument = PyTuple_New(0); |
| 477 | if (!second_argument) { |
| 478 | goto error; |
| 479 | } |
| 480 | } else { |
| 481 | Py_INCREF(second_argument); |
| 482 | } |
| 483 | if (PyList_Append(parameters_list, second_argument) != 0) { |
| 484 | Py_DECREF(second_argument); |
| 485 | goto error; |
| 486 | } |
| 487 | Py_DECREF(second_argument); |
| 488 | |
| 489 | parameters_iter = PyObject_GetIter(parameters_list); |
| 490 | if (!parameters_iter) { |
| 491 | goto error; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (self->statement != NULL) { |
| 496 | /* There is an active statement */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 497 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Guido van Rossum | ec9a4af | 2007-08-29 14:26:02 +0000 | [diff] [blame^] | 500 | operation_cstr = PyUnicode_AsStringAndSize(operation, &operation_len); |
Guido van Rossum | fa9a121 | 2007-08-29 03:34:29 +0000 | [diff] [blame] | 501 | if (operation == NULL) |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 502 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 503 | |
| 504 | /* reset description and rowcount */ |
| 505 | Py_DECREF(self->description); |
| 506 | Py_INCREF(Py_None); |
| 507 | self->description = Py_None; |
| 508 | |
| 509 | Py_DECREF(self->rowcount); |
| 510 | self->rowcount = PyInt_FromLong(-1L); |
| 511 | if (!self->rowcount) { |
| 512 | goto error; |
| 513 | } |
| 514 | |
| 515 | statement_type = detect_statement_type(operation_cstr); |
| 516 | if (self->connection->begin_statement) { |
| 517 | switch (statement_type) { |
| 518 | case STATEMENT_UPDATE: |
| 519 | case STATEMENT_DELETE: |
| 520 | case STATEMENT_INSERT: |
| 521 | case STATEMENT_REPLACE: |
| 522 | if (!self->connection->inTransaction) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 523 | result = _pysqlite_connection_begin(self->connection); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 524 | if (!result) { |
| 525 | goto error; |
| 526 | } |
| 527 | Py_DECREF(result); |
| 528 | } |
| 529 | break; |
| 530 | case STATEMENT_OTHER: |
| 531 | /* it's a DDL statement or something similar |
| 532 | - we better COMMIT first so it works for all cases */ |
| 533 | if (self->connection->inTransaction) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 534 | result = pysqlite_connection_commit(self->connection, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 535 | if (!result) { |
| 536 | goto error; |
| 537 | } |
| 538 | Py_DECREF(result); |
| 539 | } |
| 540 | break; |
| 541 | case STATEMENT_SELECT: |
| 542 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 543 | PyErr_SetString(pysqlite_ProgrammingError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 544 | "You cannot execute SELECT statements in executemany()."); |
| 545 | goto error; |
| 546 | } |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | func_args = PyTuple_New(1); |
| 552 | if (!func_args) { |
| 553 | goto error; |
| 554 | } |
| 555 | Py_INCREF(operation); |
| 556 | if (PyTuple_SetItem(func_args, 0, operation) != 0) { |
| 557 | goto error; |
| 558 | } |
| 559 | |
| 560 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 561 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 562 | Py_DECREF(self->statement); |
| 563 | } |
| 564 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 565 | self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 566 | Py_DECREF(func_args); |
| 567 | |
| 568 | if (!self->statement) { |
| 569 | goto error; |
| 570 | } |
| 571 | |
| 572 | if (self->statement->in_use) { |
| 573 | Py_DECREF(self->statement); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 574 | self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 575 | if (!self->statement) { |
| 576 | goto error; |
| 577 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 578 | rc = pysqlite_statement_create(self->statement, self->connection, operation); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 579 | if (rc != SQLITE_OK) { |
| 580 | self->statement = 0; |
| 581 | goto error; |
| 582 | } |
| 583 | } |
| 584 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 585 | pysqlite_statement_reset(self->statement); |
| 586 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 587 | |
| 588 | while (1) { |
| 589 | parameters = PyIter_Next(parameters_iter); |
| 590 | if (!parameters) { |
| 591 | break; |
| 592 | } |
| 593 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 594 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 595 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 596 | pysqlite_statement_bind_parameters(self->statement, parameters); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 597 | if (PyErr_Occurred()) { |
| 598 | goto error; |
| 599 | } |
| 600 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 601 | if (pysqlite_build_row_cast_map(self) != 0) { |
| 602 | PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 603 | goto error; |
| 604 | } |
| 605 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 606 | /* Keep trying the SQL statement until the schema stops changing. */ |
| 607 | while (1) { |
| 608 | /* Actually execute the SQL statement. */ |
| 609 | rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection); |
| 610 | if (rc == SQLITE_DONE || rc == SQLITE_ROW) { |
| 611 | /* If it worked, let's get out of the loop */ |
| 612 | break; |
| 613 | } |
| 614 | /* Something went wrong. Re-set the statement and try again. */ |
| 615 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 616 | if (rc == SQLITE_SCHEMA) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 617 | /* If this was a result of the schema changing, let's try |
| 618 | again. */ |
| 619 | rc = pysqlite_statement_recompile(self->statement, parameters); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 620 | if (rc == SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 621 | continue; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 622 | } else { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 623 | /* If the database gave us an error, promote it to Python. */ |
| 624 | _pysqlite_seterror(self->connection->db); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 625 | goto error; |
| 626 | } |
| 627 | } else { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 628 | if (PyErr_Occurred()) { |
| 629 | /* there was an error that occurred in a user-defined callback */ |
| 630 | if (_enable_callback_tracebacks) { |
| 631 | PyErr_Print(); |
| 632 | } else { |
| 633 | PyErr_Clear(); |
| 634 | } |
| 635 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 636 | _pysqlite_seterror(self->connection->db); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 637 | goto error; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) { |
| 642 | Py_BEGIN_ALLOW_THREADS |
| 643 | numcols = sqlite3_column_count(self->statement->st); |
| 644 | Py_END_ALLOW_THREADS |
| 645 | |
| 646 | if (self->description == Py_None) { |
| 647 | Py_DECREF(self->description); |
| 648 | self->description = PyTuple_New(numcols); |
| 649 | if (!self->description) { |
| 650 | goto error; |
| 651 | } |
| 652 | for (i = 0; i < numcols; i++) { |
| 653 | descriptor = PyTuple_New(7); |
| 654 | if (!descriptor) { |
| 655 | goto error; |
| 656 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 657 | PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 658 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None); |
| 659 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None); |
| 660 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None); |
| 661 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None); |
| 662 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None); |
| 663 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None); |
| 664 | PyTuple_SetItem(self->description, i, descriptor); |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if (rc == SQLITE_ROW) { |
| 670 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 671 | PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 672 | goto error; |
| 673 | } |
| 674 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 675 | self->next_row = _pysqlite_fetch_one_row(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 676 | } else if (rc == SQLITE_DONE && !multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 677 | pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 678 | Py_DECREF(self->statement); |
| 679 | self->statement = 0; |
| 680 | } |
| 681 | |
| 682 | switch (statement_type) { |
| 683 | case STATEMENT_UPDATE: |
| 684 | case STATEMENT_DELETE: |
| 685 | case STATEMENT_INSERT: |
| 686 | case STATEMENT_REPLACE: |
| 687 | Py_BEGIN_ALLOW_THREADS |
| 688 | rowcount += (long)sqlite3_changes(self->connection->db); |
| 689 | Py_END_ALLOW_THREADS |
| 690 | Py_DECREF(self->rowcount); |
| 691 | self->rowcount = PyInt_FromLong(rowcount); |
| 692 | } |
| 693 | |
| 694 | Py_DECREF(self->lastrowid); |
| 695 | if (statement_type == STATEMENT_INSERT) { |
| 696 | Py_BEGIN_ALLOW_THREADS |
| 697 | lastrowid = sqlite3_last_insert_rowid(self->connection->db); |
| 698 | Py_END_ALLOW_THREADS |
| 699 | self->lastrowid = PyInt_FromLong((long)lastrowid); |
| 700 | } else { |
| 701 | Py_INCREF(Py_None); |
| 702 | self->lastrowid = Py_None; |
| 703 | } |
| 704 | |
| 705 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 706 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 707 | } |
| 708 | Py_XDECREF(parameters); |
| 709 | } |
| 710 | |
| 711 | error: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 712 | Py_XDECREF(parameters); |
| 713 | Py_XDECREF(parameters_iter); |
| 714 | Py_XDECREF(parameters_list); |
| 715 | |
| 716 | if (PyErr_Occurred()) { |
| 717 | return NULL; |
| 718 | } else { |
| 719 | Py_INCREF(self); |
| 720 | return (PyObject*)self; |
| 721 | } |
| 722 | } |
| 723 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 724 | PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 725 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 726 | return _pysqlite_query_execute(self, 0, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 729 | PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 730 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 731 | return _pysqlite_query_execute(self, 1, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 734 | PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 735 | { |
| 736 | PyObject* script_obj; |
| 737 | PyObject* script_str = NULL; |
| 738 | const char* script_cstr; |
| 739 | sqlite3_stmt* statement; |
| 740 | int rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 741 | PyObject* result; |
| 742 | int statement_completed = 0; |
| 743 | |
| 744 | if (!PyArg_ParseTuple(args, "O", &script_obj)) { |
| 745 | return NULL; |
| 746 | } |
| 747 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 748 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 749 | return NULL; |
| 750 | } |
| 751 | |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 752 | if (PyUnicode_Check(script_obj)) { |
| 753 | script_cstr = PyUnicode_AsString(script_obj); |
| 754 | if (!script_cstr) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 755 | return NULL; |
| 756 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 757 | } else { |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 758 | PyErr_SetString(PyExc_ValueError, "script argument must be unicode."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 759 | return NULL; |
| 760 | } |
| 761 | |
| 762 | /* commit first */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 763 | result = pysqlite_connection_commit(self->connection, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 781 | _pysqlite_seterror(self->connection->db); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 793 | _pysqlite_seterror(self->connection->db); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 794 | goto error; |
| 795 | } |
| 796 | |
| 797 | rc = sqlite3_finalize(statement); |
| 798 | if (rc != SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 799 | _pysqlite_seterror(self->connection->db); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 800 | goto error; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | error: |
| 805 | Py_XDECREF(script_str); |
| 806 | |
| 807 | if (!statement_completed) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 808 | PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | if (PyErr_Occurred()) { |
| 812 | return NULL; |
| 813 | } else { |
| 814 | Py_INCREF(self); |
| 815 | return (PyObject*)self; |
| 816 | } |
| 817 | } |
| 818 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 819 | PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 820 | { |
| 821 | Py_INCREF(self); |
| 822 | return (PyObject*)self; |
| 823 | } |
| 824 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 825 | PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 826 | { |
| 827 | PyObject* next_row_tuple; |
| 828 | PyObject* next_row; |
| 829 | int rc; |
| 830 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 831 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 832 | return NULL; |
| 833 | } |
| 834 | |
| 835 | if (!self->next_row) { |
| 836 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 837 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 854 | if (self->statement) { |
| 855 | rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection); |
| 856 | if (rc != SQLITE_DONE && rc != SQLITE_ROW) { |
| 857 | Py_DECREF(next_row); |
| 858 | _pysqlite_seterror(self->connection->db); |
| 859 | return NULL; |
| 860 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 861 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 862 | if (rc == SQLITE_ROW) { |
| 863 | self->next_row = _pysqlite_fetch_one_row(self); |
| 864 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | return next_row; |
| 868 | } |
| 869 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 870 | PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 871 | { |
| 872 | PyObject* row; |
| 873 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 874 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 875 | if (!row && !PyErr_Occurred()) { |
| 876 | Py_INCREF(Py_None); |
| 877 | return Py_None; |
| 878 | } |
| 879 | |
| 880 | return row; |
| 881 | } |
| 882 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 883 | PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 884 | { |
| 885 | PyObject* row; |
| 886 | PyObject* list; |
| 887 | int maxrows = self->arraysize; |
| 888 | int counter = 0; |
| 889 | |
| 890 | if (!PyArg_ParseTuple(args, "|i", &maxrows)) { |
| 891 | return NULL; |
| 892 | } |
| 893 | |
| 894 | list = PyList_New(0); |
| 895 | if (!list) { |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | /* just make sure we enter the loop */ |
| 900 | row = Py_None; |
| 901 | |
| 902 | while (row) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 903 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 904 | if (row) { |
| 905 | PyList_Append(list, row); |
| 906 | Py_DECREF(row); |
| 907 | } else { |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | if (++counter == maxrows) { |
| 912 | break; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if (PyErr_Occurred()) { |
| 917 | Py_DECREF(list); |
| 918 | return NULL; |
| 919 | } else { |
| 920 | return list; |
| 921 | } |
| 922 | } |
| 923 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 924 | PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 925 | { |
| 926 | PyObject* row; |
| 927 | PyObject* list; |
| 928 | |
| 929 | list = PyList_New(0); |
| 930 | if (!list) { |
| 931 | return NULL; |
| 932 | } |
| 933 | |
| 934 | /* just make sure we enter the loop */ |
| 935 | row = (PyObject*)Py_None; |
| 936 | |
| 937 | while (row) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 938 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 939 | if (row) { |
| 940 | PyList_Append(list, row); |
| 941 | Py_DECREF(row); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (PyErr_Occurred()) { |
| 946 | Py_DECREF(list); |
| 947 | return NULL; |
| 948 | } else { |
| 949 | return list; |
| 950 | } |
| 951 | } |
| 952 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 953 | PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 954 | { |
| 955 | /* don't care, return None */ |
| 956 | Py_INCREF(Py_None); |
| 957 | return Py_None; |
| 958 | } |
| 959 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 960 | PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 961 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 962 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 963 | return NULL; |
| 964 | } |
| 965 | |
| 966 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 967 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 968 | Py_DECREF(self->statement); |
| 969 | self->statement = 0; |
| 970 | } |
| 971 | |
| 972 | Py_INCREF(Py_None); |
| 973 | return Py_None; |
| 974 | } |
| 975 | |
| 976 | static PyMethodDef cursor_methods[] = { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 977 | {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 978 | PyDoc_STR("Executes a SQL statement.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 979 | {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 980 | PyDoc_STR("Repeatedly executes a SQL statement.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 981 | {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 982 | PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 983 | {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 984 | PyDoc_STR("Fetches several rows from the resultset.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 985 | {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 986 | PyDoc_STR("Fetches all rows from the resultset.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 987 | {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 988 | PyDoc_STR("Fetches one row from the resultset.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 989 | {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 990 | PyDoc_STR("Closes the cursor.")}, |
| 991 | {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS, |
| 992 | PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, |
| 993 | {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS, |
| 994 | PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, |
| 995 | {NULL, NULL} |
| 996 | }; |
| 997 | |
| 998 | static struct PyMemberDef cursor_members[] = |
| 999 | { |
Guido van Rossum | 10f07c4 | 2007-08-11 15:32:55 +0000 | [diff] [blame] | 1000 | {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY}, |
| 1001 | {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1002 | {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0}, |
Guido van Rossum | 10f07c4 | 2007-08-11 15:32:55 +0000 | [diff] [blame] | 1003 | {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY}, |
| 1004 | {"rowcount", T_OBJECT, offsetof(pysqlite_Cursor, rowcount), READONLY}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1005 | {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1006 | {NULL} |
| 1007 | }; |
| 1008 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1009 | static char cursor_doc[] = |
| 1010 | PyDoc_STR("SQLite database cursor class."); |
| 1011 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1012 | PyTypeObject pysqlite_CursorType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1013 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1014 | MODULE_NAME ".Cursor", /* tp_name */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1015 | sizeof(pysqlite_Cursor), /* tp_basicsize */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1016 | 0, /* tp_itemsize */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1017 | (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1018 | 0, /* tp_print */ |
| 1019 | 0, /* tp_getattr */ |
| 1020 | 0, /* tp_setattr */ |
| 1021 | 0, /* tp_compare */ |
| 1022 | 0, /* tp_repr */ |
| 1023 | 0, /* tp_as_number */ |
| 1024 | 0, /* tp_as_sequence */ |
| 1025 | 0, /* tp_as_mapping */ |
| 1026 | 0, /* tp_hash */ |
| 1027 | 0, /* tp_call */ |
| 1028 | 0, /* tp_str */ |
| 1029 | 0, /* tp_getattro */ |
| 1030 | 0, /* tp_setattro */ |
| 1031 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 1032 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1033 | cursor_doc, /* tp_doc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1034 | 0, /* tp_traverse */ |
| 1035 | 0, /* tp_clear */ |
| 1036 | 0, /* tp_richcompare */ |
| 1037 | 0, /* tp_weaklistoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1038 | (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */ |
| 1039 | (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1040 | cursor_methods, /* tp_methods */ |
| 1041 | cursor_members, /* tp_members */ |
| 1042 | 0, /* tp_getset */ |
| 1043 | 0, /* tp_base */ |
| 1044 | 0, /* tp_dict */ |
| 1045 | 0, /* tp_descr_get */ |
| 1046 | 0, /* tp_descr_set */ |
| 1047 | 0, /* tp_dictoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1048 | (initproc)pysqlite_cursor_init, /* tp_init */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1049 | 0, /* tp_alloc */ |
| 1050 | 0, /* tp_new */ |
| 1051 | 0 /* tp_free */ |
| 1052 | }; |
| 1053 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1054 | extern int pysqlite_cursor_setup_types(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1055 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1056 | pysqlite_CursorType.tp_new = PyType_GenericNew; |
| 1057 | return PyType_Ready(&pysqlite_CursorType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1058 | } |