Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | /* cursor.c - the cursor type |
| 2 | * |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 29 | /* used to decide wether to call PyLong_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 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 39 | static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from."; |
| 40 | |
Guido van Rossum | 6374bb5 | 2007-06-13 16:28:25 +0000 | [diff] [blame] | 41 | static pysqlite_StatementKind detect_statement_type(const char* statement) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | { |
| 43 | char buf[20]; |
Guido van Rossum | 6374bb5 | 2007-06-13 16:28:25 +0000 | [diff] [blame] | 44 | const char* src; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 45 | char* dst; |
| 46 | |
| 47 | src = statement; |
| 48 | /* skip over whitepace */ |
| 49 | while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') { |
| 50 | src++; |
| 51 | } |
| 52 | |
| 53 | if (*src == 0) |
| 54 | return STATEMENT_INVALID; |
| 55 | |
| 56 | dst = buf; |
| 57 | *dst = 0; |
| 58 | while (isalpha(*src) && dst - buf < sizeof(buf) - 2) { |
| 59 | *dst++ = tolower(*src++); |
| 60 | } |
| 61 | |
| 62 | *dst = 0; |
| 63 | |
| 64 | if (!strcmp(buf, "select")) { |
| 65 | return STATEMENT_SELECT; |
| 66 | } else if (!strcmp(buf, "insert")) { |
| 67 | return STATEMENT_INSERT; |
| 68 | } else if (!strcmp(buf, "update")) { |
| 69 | return STATEMENT_UPDATE; |
| 70 | } else if (!strcmp(buf, "delete")) { |
| 71 | return STATEMENT_DELETE; |
| 72 | } else if (!strcmp(buf, "replace")) { |
| 73 | return STATEMENT_REPLACE; |
| 74 | } else { |
| 75 | return STATEMENT_OTHER; |
| 76 | } |
| 77 | } |
| 78 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 79 | static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) |
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 | pysqlite_Connection* connection; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 82 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 83 | if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 84 | { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 85 | return -1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | Py_INCREF(connection); |
| 89 | self->connection = connection; |
| 90 | self->statement = NULL; |
| 91 | self->next_row = NULL; |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 92 | self->in_weakreflist = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 93 | |
| 94 | self->row_cast_map = PyList_New(0); |
| 95 | if (!self->row_cast_map) { |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | Py_INCREF(Py_None); |
| 100 | self->description = Py_None; |
| 101 | |
| 102 | Py_INCREF(Py_None); |
| 103 | self->lastrowid= Py_None; |
| 104 | |
| 105 | self->arraysize = 1; |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 106 | self->closed = 0; |
| 107 | self->reset = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 108 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 109 | self->rowcount = -1L; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 110 | |
| 111 | Py_INCREF(Py_None); |
| 112 | self->row_factory = Py_None; |
| 113 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 114 | if (!pysqlite_check_thread(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 115 | return -1; |
| 116 | } |
| 117 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 118 | if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) { |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | self->initialized = 1; |
| 123 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 127 | static void pysqlite_cursor_dealloc(pysqlite_Cursor* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 128 | { |
| 129 | int rc; |
| 130 | |
| 131 | /* Reset the statement if the user has not closed the cursor */ |
| 132 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 133 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 134 | Py_DECREF(self->statement); |
| 135 | } |
| 136 | |
| 137 | Py_XDECREF(self->connection); |
| 138 | Py_XDECREF(self->row_cast_map); |
| 139 | Py_XDECREF(self->description); |
| 140 | Py_XDECREF(self->lastrowid); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 141 | Py_XDECREF(self->row_factory); |
| 142 | Py_XDECREF(self->next_row); |
| 143 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 144 | if (self->in_weakreflist != NULL) { |
| 145 | PyObject_ClearWeakRefs((PyObject*)self); |
| 146 | } |
| 147 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 148 | Py_TYPE(self)->tp_free((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 151 | PyObject* _pysqlite_get_converter(PyObject* key) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 152 | { |
| 153 | PyObject* upcase_key; |
| 154 | PyObject* retval; |
| 155 | |
| 156 | upcase_key = PyObject_CallMethod(key, "upper", ""); |
| 157 | if (!upcase_key) { |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | retval = PyDict_GetItem(converters, upcase_key); |
| 162 | Py_DECREF(upcase_key); |
| 163 | |
| 164 | return retval; |
| 165 | } |
| 166 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 167 | int pysqlite_build_row_cast_map(pysqlite_Cursor* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 168 | { |
| 169 | int i; |
| 170 | const char* type_start = (const char*)-1; |
| 171 | const char* pos; |
| 172 | |
| 173 | const char* colname; |
| 174 | const char* decltype; |
| 175 | PyObject* py_decltype; |
| 176 | PyObject* converter; |
| 177 | PyObject* key; |
| 178 | |
| 179 | if (!self->connection->detect_types) { |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | Py_XDECREF(self->row_cast_map); |
| 184 | self->row_cast_map = PyList_New(0); |
| 185 | |
| 186 | for (i = 0; i < sqlite3_column_count(self->statement->st); i++) { |
| 187 | converter = NULL; |
| 188 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 189 | if (self->connection->detect_types & PARSE_COLNAMES) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 190 | colname = sqlite3_column_name(self->statement->st, i); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 191 | if (colname) { |
| 192 | for (pos = colname; *pos != 0; pos++) { |
| 193 | if (*pos == '[') { |
| 194 | type_start = pos + 1; |
| 195 | } else if (*pos == ']' && type_start != (const char*)-1) { |
Brett Cannon | 4043001 | 2007-10-22 20:24:51 +0000 | [diff] [blame] | 196 | key = PyUnicode_FromStringAndSize(type_start, pos - type_start); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 197 | if (!key) { |
| 198 | /* creating a string failed, but it is too complicated |
| 199 | * to propagate the error here, we just assume there is |
| 200 | * no converter and proceed */ |
| 201 | break; |
| 202 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 203 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 204 | converter = _pysqlite_get_converter(key); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 205 | Py_DECREF(key); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 206 | break; |
| 207 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 208 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 212 | if (!converter && self->connection->detect_types & PARSE_DECLTYPES) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 213 | decltype = sqlite3_column_decltype(self->statement->st, i); |
| 214 | if (decltype) { |
| 215 | for (pos = decltype;;pos++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 216 | /* Converter names are split at '(' and blanks. |
| 217 | * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and |
| 218 | * 'NUMBER(10)' to be treated as 'NUMBER', for example. |
| 219 | * In other words, it will work as people expect it to work.*/ |
| 220 | if (*pos == ' ' || *pos == '(' || *pos == 0) { |
Brett Cannon | 4043001 | 2007-10-22 20:24:51 +0000 | [diff] [blame] | 221 | py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 222 | if (!py_decltype) { |
| 223 | return -1; |
| 224 | } |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 229 | converter = _pysqlite_get_converter(py_decltype); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 230 | Py_DECREF(py_decltype); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if (!converter) { |
| 235 | converter = Py_None; |
| 236 | } |
| 237 | |
| 238 | if (PyList_Append(self->row_cast_map, converter) != 0) { |
| 239 | if (converter != Py_None) { |
| 240 | Py_DECREF(converter); |
| 241 | } |
| 242 | Py_XDECREF(self->row_cast_map); |
| 243 | self->row_cast_map = NULL; |
| 244 | |
| 245 | return -1; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 252 | PyObject* _pysqlite_build_column_name(const char* colname) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 253 | { |
| 254 | const char* pos; |
| 255 | |
| 256 | if (!colname) { |
| 257 | Py_INCREF(Py_None); |
| 258 | return Py_None; |
| 259 | } |
| 260 | |
| 261 | for (pos = colname;; pos++) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 262 | if (*pos == 0 || *pos == '[') { |
| 263 | if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) { |
| 264 | pos--; |
| 265 | } |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 266 | return PyUnicode_FromStringAndSize(colname, pos - colname); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 271 | PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 272 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 273 | return PyUnicode_FromString(val_str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Returns a row from the currently active SQLite statement |
| 278 | * |
| 279 | * Precondidition: |
| 280 | * - sqlite3_step() has been called before and it returned SQLITE_ROW. |
| 281 | */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 282 | PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 283 | { |
| 284 | int i, numcols; |
| 285 | PyObject* row; |
| 286 | PyObject* item = NULL; |
| 287 | int coltype; |
| 288 | PY_LONG_LONG intval; |
| 289 | PyObject* converter; |
| 290 | PyObject* converted; |
| 291 | Py_ssize_t nbytes; |
| 292 | PyObject* buffer; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 293 | const char* val_str; |
| 294 | char buf[200]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 295 | const char* colname; |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 296 | PyObject* buf_bytes; |
| 297 | PyObject* error_obj; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 298 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 299 | if (self->reset) { |
| 300 | PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback); |
| 301 | return NULL; |
| 302 | } |
| 303 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 330 | item = PyBytes_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 { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 352 | converted = PyLong_FromLong((long)intval); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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); |
Victor Stinner | 8699950 | 2010-05-19 01:27:23 +0000 | [diff] [blame] | 371 | buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf)); |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 372 | if (!buf_bytes) { |
| 373 | PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8"); |
| 374 | } else { |
| 375 | error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace"); |
| 376 | if (!error_obj) { |
| 377 | PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8"); |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 378 | } else { |
| 379 | PyErr_SetObject(pysqlite_OperationalError, error_obj); |
Gerhard Häring | e871ad59 | 2008-03-29 14:11:55 +0000 | [diff] [blame] | 380 | Py_DECREF(error_obj); |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 381 | } |
| 382 | Py_DECREF(buf_bytes); |
| 383 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 384 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 385 | } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) { |
| 386 | converted = PyBytes_FromString(val_str); |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 387 | } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) { |
| 388 | converted = PyByteArray_FromStringAndSize(val_str, strlen(val_str)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 389 | } else { |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 390 | converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 391 | } |
| 392 | } else { |
| 393 | /* coltype == SQLITE_BLOB */ |
| 394 | nbytes = sqlite3_column_bytes(self->statement->st, i); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 395 | buffer = PyBytes_FromStringAndSize( |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 396 | sqlite3_column_blob(self->statement->st, i), nbytes); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 397 | if (!buffer) { |
| 398 | break; |
| 399 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 400 | converted = buffer; |
| 401 | } |
| 402 | } |
| 403 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 404 | if (converted) { |
| 405 | PyTuple_SetItem(row, i, converted); |
| 406 | } else { |
| 407 | Py_INCREF(Py_None); |
| 408 | PyTuple_SetItem(row, i, Py_None); |
| 409 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | if (PyErr_Occurred()) { |
| 413 | Py_DECREF(row); |
| 414 | row = NULL; |
| 415 | } |
| 416 | |
| 417 | return row; |
| 418 | } |
| 419 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 420 | /* |
| 421 | * Checks if a cursor object is usable. |
| 422 | * |
| 423 | * 0 => error; 1 => ok |
| 424 | */ |
| 425 | static int check_cursor(pysqlite_Cursor* cur) |
| 426 | { |
| 427 | if (!cur->initialized) { |
| 428 | PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called."); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | if (cur->closed) { |
| 433 | PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor."); |
| 434 | return 0; |
| 435 | } else { |
| 436 | return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection); |
| 437 | } |
| 438 | } |
| 439 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 440 | PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 441 | { |
| 442 | PyObject* operation; |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 443 | const char* operation_cstr; |
| 444 | Py_ssize_t operation_len; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 445 | PyObject* parameters_list = NULL; |
| 446 | PyObject* parameters_iter = NULL; |
| 447 | PyObject* parameters = NULL; |
| 448 | int i; |
| 449 | int rc; |
| 450 | PyObject* func_args; |
| 451 | PyObject* result; |
| 452 | int numcols; |
| 453 | PY_LONG_LONG lastrowid; |
| 454 | int statement_type; |
| 455 | PyObject* descriptor; |
| 456 | PyObject* second_argument = NULL; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 457 | int allow_8bit_chars; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 458 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 459 | if (!check_cursor(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 460 | return NULL; |
| 461 | } |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 462 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 463 | self->reset = 0; |
| 464 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 465 | /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */ |
| 466 | allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) && |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 467 | (self->connection->text_factory != pysqlite_OptimizedUnicode)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 468 | |
| 469 | Py_XDECREF(self->next_row); |
| 470 | self->next_row = NULL; |
| 471 | |
| 472 | if (multiple) { |
| 473 | /* executemany() */ |
| 474 | if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 475 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 478 | if (!PyUnicode_Check(operation)) { |
| 479 | PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 480 | return NULL; |
| 481 | } |
| 482 | |
| 483 | if (PyIter_Check(second_argument)) { |
| 484 | /* iterator */ |
| 485 | Py_INCREF(second_argument); |
| 486 | parameters_iter = second_argument; |
| 487 | } else { |
| 488 | /* sequence */ |
| 489 | parameters_iter = PyObject_GetIter(second_argument); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 490 | if (!parameters_iter) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 491 | return NULL; |
| 492 | } |
| 493 | } |
| 494 | } else { |
| 495 | /* execute() */ |
| 496 | if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 497 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 500 | if (!PyUnicode_Check(operation)) { |
| 501 | PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | parameters_list = PyList_New(0); |
| 506 | if (!parameters_list) { |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | if (second_argument == NULL) { |
| 511 | second_argument = PyTuple_New(0); |
| 512 | if (!second_argument) { |
| 513 | goto error; |
| 514 | } |
| 515 | } else { |
| 516 | Py_INCREF(second_argument); |
| 517 | } |
| 518 | if (PyList_Append(parameters_list, second_argument) != 0) { |
| 519 | Py_DECREF(second_argument); |
| 520 | goto error; |
| 521 | } |
| 522 | Py_DECREF(second_argument); |
| 523 | |
| 524 | parameters_iter = PyObject_GetIter(parameters_list); |
| 525 | if (!parameters_iter) { |
| 526 | goto error; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | if (self->statement != NULL) { |
| 531 | /* There is an active statement */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 532 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 535 | operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len); |
Victor Stinner | 8699950 | 2010-05-19 01:27:23 +0000 | [diff] [blame] | 536 | if (operation_cstr == NULL) |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 537 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 538 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 539 | /* reset description and rowcount */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 540 | Py_DECREF(self->description); |
| 541 | Py_INCREF(Py_None); |
| 542 | self->description = Py_None; |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 543 | self->rowcount = -1L; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 544 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 545 | func_args = PyTuple_New(1); |
| 546 | if (!func_args) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 547 | goto error; |
| 548 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 549 | Py_INCREF(operation); |
| 550 | if (PyTuple_SetItem(func_args, 0, operation) != 0) { |
| 551 | goto error; |
| 552 | } |
| 553 | |
| 554 | if (self->statement) { |
| 555 | (void)pysqlite_statement_reset(self->statement); |
| 556 | Py_DECREF(self->statement); |
| 557 | } |
| 558 | |
| 559 | self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args); |
| 560 | Py_DECREF(func_args); |
| 561 | |
| 562 | if (!self->statement) { |
| 563 | goto error; |
| 564 | } |
| 565 | |
| 566 | if (self->statement->in_use) { |
| 567 | Py_DECREF(self->statement); |
| 568 | self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
| 569 | if (!self->statement) { |
| 570 | goto error; |
| 571 | } |
| 572 | rc = pysqlite_statement_create(self->statement, self->connection, operation); |
| 573 | if (rc != SQLITE_OK) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 574 | Py_CLEAR(self->statement); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 575 | goto error; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | pysqlite_statement_reset(self->statement); |
| 580 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 581 | |
| 582 | statement_type = detect_statement_type(operation_cstr); |
| 583 | if (self->connection->begin_statement) { |
| 584 | switch (statement_type) { |
| 585 | case STATEMENT_UPDATE: |
| 586 | case STATEMENT_DELETE: |
| 587 | case STATEMENT_INSERT: |
| 588 | case STATEMENT_REPLACE: |
| 589 | if (!self->connection->inTransaction) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 590 | result = _pysqlite_connection_begin(self->connection); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 591 | if (!result) { |
| 592 | goto error; |
| 593 | } |
| 594 | Py_DECREF(result); |
| 595 | } |
| 596 | break; |
| 597 | case STATEMENT_OTHER: |
| 598 | /* it's a DDL statement or something similar |
| 599 | - we better COMMIT first so it works for all cases */ |
| 600 | if (self->connection->inTransaction) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 601 | result = pysqlite_connection_commit(self->connection, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 602 | if (!result) { |
| 603 | goto error; |
| 604 | } |
| 605 | Py_DECREF(result); |
| 606 | } |
| 607 | break; |
| 608 | case STATEMENT_SELECT: |
| 609 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 610 | PyErr_SetString(pysqlite_ProgrammingError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 611 | "You cannot execute SELECT statements in executemany()."); |
| 612 | goto error; |
| 613 | } |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 618 | |
| 619 | while (1) { |
| 620 | parameters = PyIter_Next(parameters_iter); |
| 621 | if (!parameters) { |
| 622 | break; |
| 623 | } |
| 624 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 625 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 626 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 627 | pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 628 | if (PyErr_Occurred()) { |
| 629 | goto error; |
| 630 | } |
| 631 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 632 | /* Keep trying the SQL statement until the schema stops changing. */ |
| 633 | while (1) { |
| 634 | /* Actually execute the SQL statement. */ |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 635 | rc = pysqlite_step(self->statement->st, self->connection); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 636 | if (rc == SQLITE_DONE || rc == SQLITE_ROW) { |
| 637 | /* If it worked, let's get out of the loop */ |
| 638 | break; |
| 639 | } |
| 640 | /* Something went wrong. Re-set the statement and try again. */ |
| 641 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 642 | if (rc == SQLITE_SCHEMA) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 643 | /* If this was a result of the schema changing, let's try |
| 644 | again. */ |
| 645 | rc = pysqlite_statement_recompile(self->statement, parameters); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 646 | if (rc == SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 647 | continue; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 648 | } else { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 649 | /* If the database gave us an error, promote it to Python. */ |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 650 | (void)pysqlite_statement_reset(self->statement); |
| 651 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 652 | goto error; |
| 653 | } |
| 654 | } else { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 655 | if (PyErr_Occurred()) { |
| 656 | /* there was an error that occurred in a user-defined callback */ |
| 657 | if (_enable_callback_tracebacks) { |
| 658 | PyErr_Print(); |
| 659 | } else { |
| 660 | PyErr_Clear(); |
| 661 | } |
| 662 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 663 | (void)pysqlite_statement_reset(self->statement); |
| 664 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 665 | goto error; |
| 666 | } |
| 667 | } |
| 668 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 669 | if (pysqlite_build_row_cast_map(self) != 0) { |
| 670 | PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map"); |
| 671 | goto error; |
| 672 | } |
| 673 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 674 | if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 675 | if (self->description == Py_None) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 676 | Py_BEGIN_ALLOW_THREADS |
| 677 | numcols = sqlite3_column_count(self->statement->st); |
| 678 | Py_END_ALLOW_THREADS |
| 679 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 680 | Py_DECREF(self->description); |
| 681 | self->description = PyTuple_New(numcols); |
| 682 | if (!self->description) { |
| 683 | goto error; |
| 684 | } |
| 685 | for (i = 0; i < numcols; i++) { |
| 686 | descriptor = PyTuple_New(7); |
| 687 | if (!descriptor) { |
| 688 | goto error; |
| 689 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 690 | 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] | 691 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None); |
| 692 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None); |
| 693 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None); |
| 694 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None); |
| 695 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None); |
| 696 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None); |
| 697 | PyTuple_SetItem(self->description, i, descriptor); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if (rc == SQLITE_ROW) { |
| 703 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 704 | PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 705 | goto error; |
| 706 | } |
| 707 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 708 | self->next_row = _pysqlite_fetch_one_row(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 709 | } else if (rc == SQLITE_DONE && !multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 710 | pysqlite_statement_reset(self->statement); |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 711 | Py_CLEAR(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | switch (statement_type) { |
| 715 | case STATEMENT_UPDATE: |
| 716 | case STATEMENT_DELETE: |
| 717 | case STATEMENT_INSERT: |
| 718 | case STATEMENT_REPLACE: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 719 | if (self->rowcount == -1L) { |
| 720 | self->rowcount = 0L; |
| 721 | } |
| 722 | self->rowcount += (long)sqlite3_changes(self->connection->db); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | Py_DECREF(self->lastrowid); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 726 | if (!multiple && statement_type == STATEMENT_INSERT) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 727 | Py_BEGIN_ALLOW_THREADS |
| 728 | lastrowid = sqlite3_last_insert_rowid(self->connection->db); |
| 729 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 730 | self->lastrowid = PyLong_FromLong((long)lastrowid); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 731 | } else { |
| 732 | Py_INCREF(Py_None); |
| 733 | self->lastrowid = Py_None; |
| 734 | } |
| 735 | |
| 736 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 737 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 738 | } |
| 739 | Py_XDECREF(parameters); |
| 740 | } |
| 741 | |
| 742 | error: |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 743 | /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR |
| 744 | * ROLLBACK could have happened */ |
| 745 | #ifdef SQLITE_VERSION_NUMBER |
| 746 | #if SQLITE_VERSION_NUMBER >= 3002002 |
| 747 | self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db); |
| 748 | #endif |
| 749 | #endif |
| 750 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 751 | Py_XDECREF(parameters); |
| 752 | Py_XDECREF(parameters_iter); |
| 753 | Py_XDECREF(parameters_list); |
| 754 | |
| 755 | if (PyErr_Occurred()) { |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 756 | self->rowcount = -1L; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 757 | return NULL; |
| 758 | } else { |
| 759 | Py_INCREF(self); |
| 760 | return (PyObject*)self; |
| 761 | } |
| 762 | } |
| 763 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 764 | PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 765 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 766 | return _pysqlite_query_execute(self, 0, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 769 | PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 770 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 771 | return _pysqlite_query_execute(self, 1, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 774 | PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 775 | { |
| 776 | PyObject* script_obj; |
| 777 | PyObject* script_str = NULL; |
| 778 | const char* script_cstr; |
| 779 | sqlite3_stmt* statement; |
| 780 | int rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 781 | PyObject* result; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 782 | |
| 783 | if (!PyArg_ParseTuple(args, "O", &script_obj)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 784 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 787 | if (!check_cursor(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 788 | return NULL; |
| 789 | } |
| 790 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 791 | self->reset = 0; |
| 792 | |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 793 | if (PyUnicode_Check(script_obj)) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 794 | script_cstr = _PyUnicode_AsString(script_obj); |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 795 | if (!script_cstr) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 796 | return NULL; |
| 797 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 798 | } else { |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 799 | PyErr_SetString(PyExc_ValueError, "script argument must be unicode."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 800 | return NULL; |
| 801 | } |
| 802 | |
| 803 | /* commit first */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 804 | result = pysqlite_connection_commit(self->connection, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 805 | if (!result) { |
| 806 | goto error; |
| 807 | } |
| 808 | Py_DECREF(result); |
| 809 | |
| 810 | while (1) { |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 811 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 812 | rc = sqlite3_prepare(self->connection->db, |
| 813 | script_cstr, |
| 814 | -1, |
| 815 | &statement, |
| 816 | &script_cstr); |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 817 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 818 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 819 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 820 | goto error; |
| 821 | } |
| 822 | |
| 823 | /* execute statement, and ignore results of SELECT statements */ |
| 824 | rc = SQLITE_ROW; |
| 825 | while (rc == SQLITE_ROW) { |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 826 | rc = pysqlite_step(statement, self->connection); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 827 | /* TODO: we probably need more error handling here */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (rc != SQLITE_DONE) { |
| 831 | (void)sqlite3_finalize(statement); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 832 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 833 | goto error; |
| 834 | } |
| 835 | |
| 836 | rc = sqlite3_finalize(statement); |
| 837 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 838 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 839 | goto error; |
| 840 | } |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 841 | |
| 842 | if (*script_cstr == (char)0) { |
| 843 | break; |
| 844 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | error: |
| 848 | Py_XDECREF(script_str); |
| 849 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 850 | if (PyErr_Occurred()) { |
| 851 | return NULL; |
| 852 | } else { |
| 853 | Py_INCREF(self); |
| 854 | return (PyObject*)self; |
| 855 | } |
| 856 | } |
| 857 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 858 | PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 859 | { |
| 860 | Py_INCREF(self); |
| 861 | return (PyObject*)self; |
| 862 | } |
| 863 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 864 | PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 865 | { |
| 866 | PyObject* next_row_tuple; |
| 867 | PyObject* next_row; |
| 868 | int rc; |
| 869 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 870 | if (!check_cursor(self)) { |
| 871 | return NULL; |
| 872 | } |
| 873 | |
| 874 | if (self->reset) { |
| 875 | PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 876 | return NULL; |
| 877 | } |
| 878 | |
| 879 | if (!self->next_row) { |
| 880 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 881 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 882 | Py_DECREF(self->statement); |
| 883 | self->statement = NULL; |
| 884 | } |
| 885 | return NULL; |
| 886 | } |
| 887 | |
| 888 | next_row_tuple = self->next_row; |
| 889 | self->next_row = NULL; |
| 890 | |
| 891 | if (self->row_factory != Py_None) { |
| 892 | next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple); |
| 893 | Py_DECREF(next_row_tuple); |
| 894 | } else { |
| 895 | next_row = next_row_tuple; |
| 896 | } |
| 897 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 898 | if (self->statement) { |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 899 | rc = pysqlite_step(self->statement->st, self->connection); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 900 | if (rc != SQLITE_DONE && rc != SQLITE_ROW) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 901 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 902 | Py_DECREF(next_row); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 903 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 904 | return NULL; |
| 905 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 906 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 907 | if (rc == SQLITE_ROW) { |
| 908 | self->next_row = _pysqlite_fetch_one_row(self); |
| 909 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | return next_row; |
| 913 | } |
| 914 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 915 | PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 916 | { |
| 917 | PyObject* row; |
| 918 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 919 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 920 | if (!row && !PyErr_Occurred()) { |
| 921 | Py_INCREF(Py_None); |
| 922 | return Py_None; |
| 923 | } |
| 924 | |
| 925 | return row; |
| 926 | } |
| 927 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 928 | PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 929 | { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 930 | static char *kwlist[] = {"size", NULL, NULL}; |
| 931 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 932 | PyObject* row; |
| 933 | PyObject* list; |
| 934 | int maxrows = self->arraysize; |
| 935 | int counter = 0; |
| 936 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 937 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) { |
| 938 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | list = PyList_New(0); |
| 942 | if (!list) { |
| 943 | return NULL; |
| 944 | } |
| 945 | |
| 946 | /* just make sure we enter the loop */ |
| 947 | row = Py_None; |
| 948 | |
| 949 | while (row) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 950 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 951 | if (row) { |
| 952 | PyList_Append(list, row); |
| 953 | Py_DECREF(row); |
| 954 | } else { |
| 955 | break; |
| 956 | } |
| 957 | |
| 958 | if (++counter == maxrows) { |
| 959 | break; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | if (PyErr_Occurred()) { |
| 964 | Py_DECREF(list); |
| 965 | return NULL; |
| 966 | } else { |
| 967 | return list; |
| 968 | } |
| 969 | } |
| 970 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 971 | PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 972 | { |
| 973 | PyObject* row; |
| 974 | PyObject* list; |
| 975 | |
| 976 | list = PyList_New(0); |
| 977 | if (!list) { |
| 978 | return NULL; |
| 979 | } |
| 980 | |
| 981 | /* just make sure we enter the loop */ |
| 982 | row = (PyObject*)Py_None; |
| 983 | |
| 984 | while (row) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 985 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 986 | if (row) { |
| 987 | PyList_Append(list, row); |
| 988 | Py_DECREF(row); |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | if (PyErr_Occurred()) { |
| 993 | Py_DECREF(list); |
| 994 | return NULL; |
| 995 | } else { |
| 996 | return list; |
| 997 | } |
| 998 | } |
| 999 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1000 | PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1001 | { |
| 1002 | /* don't care, return None */ |
| 1003 | Py_INCREF(Py_None); |
| 1004 | return Py_None; |
| 1005 | } |
| 1006 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1007 | PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1008 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1009 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1010 | return NULL; |
| 1011 | } |
| 1012 | |
| 1013 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1014 | (void)pysqlite_statement_reset(self->statement); |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1015 | Py_CLEAR(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1018 | self->closed = 1; |
| 1019 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1020 | Py_INCREF(Py_None); |
| 1021 | return Py_None; |
| 1022 | } |
| 1023 | |
| 1024 | static PyMethodDef cursor_methods[] = { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1025 | {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1026 | PyDoc_STR("Executes a SQL statement.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1027 | {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1028 | PyDoc_STR("Repeatedly executes a SQL statement.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1029 | {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1030 | PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1031 | {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1032 | PyDoc_STR("Fetches one row from the resultset.")}, |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1033 | {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1034 | PyDoc_STR("Fetches several rows from the resultset.")}, |
| 1035 | {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS, |
| 1036 | PyDoc_STR("Fetches all rows from the resultset.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1037 | {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1038 | PyDoc_STR("Closes the cursor.")}, |
| 1039 | {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS, |
| 1040 | PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, |
| 1041 | {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS, |
| 1042 | PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, |
| 1043 | {NULL, NULL} |
| 1044 | }; |
| 1045 | |
| 1046 | static struct PyMemberDef cursor_members[] = |
| 1047 | { |
Guido van Rossum | 10f07c4 | 2007-08-11 15:32:55 +0000 | [diff] [blame] | 1048 | {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY}, |
| 1049 | {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1050 | {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0}, |
Guido van Rossum | 10f07c4 | 2007-08-11 15:32:55 +0000 | [diff] [blame] | 1051 | {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY}, |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1052 | {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1053 | {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1054 | {NULL} |
| 1055 | }; |
| 1056 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1057 | static char cursor_doc[] = |
| 1058 | PyDoc_STR("SQLite database cursor class."); |
| 1059 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1060 | PyTypeObject pysqlite_CursorType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1061 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1062 | MODULE_NAME ".Cursor", /* tp_name */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1063 | sizeof(pysqlite_Cursor), /* tp_basicsize */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1064 | 0, /* tp_itemsize */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1065 | (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1066 | 0, /* tp_print */ |
| 1067 | 0, /* tp_getattr */ |
| 1068 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1069 | 0, /* tp_reserved */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1070 | 0, /* tp_repr */ |
| 1071 | 0, /* tp_as_number */ |
| 1072 | 0, /* tp_as_sequence */ |
| 1073 | 0, /* tp_as_mapping */ |
| 1074 | 0, /* tp_hash */ |
| 1075 | 0, /* tp_call */ |
| 1076 | 0, /* tp_str */ |
| 1077 | 0, /* tp_getattro */ |
| 1078 | 0, /* tp_setattro */ |
| 1079 | 0, /* tp_as_buffer */ |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1080 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1081 | cursor_doc, /* tp_doc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1082 | 0, /* tp_traverse */ |
| 1083 | 0, /* tp_clear */ |
| 1084 | 0, /* tp_richcompare */ |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1085 | offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1086 | (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */ |
| 1087 | (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1088 | cursor_methods, /* tp_methods */ |
| 1089 | cursor_members, /* tp_members */ |
| 1090 | 0, /* tp_getset */ |
| 1091 | 0, /* tp_base */ |
| 1092 | 0, /* tp_dict */ |
| 1093 | 0, /* tp_descr_get */ |
| 1094 | 0, /* tp_descr_set */ |
| 1095 | 0, /* tp_dictoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1096 | (initproc)pysqlite_cursor_init, /* tp_init */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1097 | 0, /* tp_alloc */ |
| 1098 | 0, /* tp_new */ |
| 1099 | 0 /* tp_free */ |
| 1100 | }; |
| 1101 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1102 | extern int pysqlite_cursor_setup_types(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1103 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1104 | pysqlite_CursorType.tp_new = PyType_GenericNew; |
| 1105 | return PyType_Ready(&pysqlite_CursorType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1106 | } |