Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | /* cursor.c - the cursor type |
| 2 | * |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004-2007 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 | |
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 | { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 83 | return -1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 104 | self->rowcount = PyLong_FromLong(-1L); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +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) { |
Brett Cannon | 4043001 | 2007-10-22 20:24:51 +0000 | [diff] [blame] | 185 | key = PyUnicode_FromStringAndSize(type_start, pos - type_start); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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) { |
Brett Cannon | 4043001 | 2007-10-22 20:24:51 +0000 | [diff] [blame] | 206 | py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 258 | return PyUnicode_FromString(val_str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Returns a row from the currently active SQLite statement |
| 263 | * |
| 264 | * Precondidition: |
| 265 | * - sqlite3_step() has been called before and it returned SQLITE_ROW. |
| 266 | */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 267 | PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 268 | { |
| 269 | int i, numcols; |
| 270 | PyObject* row; |
| 271 | PyObject* item = NULL; |
| 272 | int coltype; |
| 273 | PY_LONG_LONG intval; |
| 274 | PyObject* converter; |
| 275 | PyObject* converted; |
| 276 | Py_ssize_t nbytes; |
| 277 | PyObject* buffer; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 278 | const char* val_str; |
| 279 | char buf[200]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 280 | const char* colname; |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 281 | PyObject* buf_bytes; |
| 282 | PyObject* error_obj; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 283 | |
| 284 | Py_BEGIN_ALLOW_THREADS |
| 285 | numcols = sqlite3_data_count(self->statement->st); |
| 286 | Py_END_ALLOW_THREADS |
| 287 | |
| 288 | row = PyTuple_New(numcols); |
| 289 | if (!row) { |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | for (i = 0; i < numcols; i++) { |
| 294 | if (self->connection->detect_types) { |
| 295 | converter = PyList_GetItem(self->row_cast_map, i); |
| 296 | if (!converter) { |
| 297 | converter = Py_None; |
| 298 | } |
| 299 | } else { |
| 300 | converter = Py_None; |
| 301 | } |
| 302 | |
| 303 | if (converter != Py_None) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 304 | nbytes = sqlite3_column_bytes(self->statement->st, i); |
| 305 | val_str = (const char*)sqlite3_column_blob(self->statement->st, i); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 306 | if (!val_str) { |
| 307 | Py_INCREF(Py_None); |
| 308 | converted = Py_None; |
| 309 | } else { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 310 | item = PyString_FromStringAndSize(val_str, nbytes); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 311 | if (!item) { |
| 312 | return NULL; |
| 313 | } |
| 314 | converted = PyObject_CallFunction(converter, "O", item); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 315 | Py_DECREF(item); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 316 | if (!converted) { |
| 317 | break; |
| 318 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 319 | } |
| 320 | } else { |
| 321 | Py_BEGIN_ALLOW_THREADS |
| 322 | coltype = sqlite3_column_type(self->statement->st, i); |
| 323 | Py_END_ALLOW_THREADS |
| 324 | if (coltype == SQLITE_NULL) { |
| 325 | Py_INCREF(Py_None); |
| 326 | converted = Py_None; |
| 327 | } else if (coltype == SQLITE_INTEGER) { |
| 328 | intval = sqlite3_column_int64(self->statement->st, i); |
| 329 | if (intval < INT32_MIN || intval > INT32_MAX) { |
| 330 | converted = PyLong_FromLongLong(intval); |
| 331 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 332 | converted = PyLong_FromLong((long)intval); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 333 | } |
| 334 | } else if (coltype == SQLITE_FLOAT) { |
| 335 | converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i)); |
| 336 | } else if (coltype == SQLITE_TEXT) { |
| 337 | val_str = (const char*)sqlite3_column_text(self->statement->st, i); |
| 338 | if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 339 | || (self->connection->text_factory == pysqlite_OptimizedUnicode)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 340 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 341 | converted = pysqlite_unicode_from_string(val_str, |
| 342 | self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | |
| 344 | if (!converted) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 345 | colname = sqlite3_column_name(self->statement->st, i); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 346 | if (!colname) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 347 | colname = "<unknown column name>"; |
| 348 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 349 | 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] | 350 | colname , val_str); |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 351 | buf_bytes = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 352 | if (!buf_bytes) { |
| 353 | PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8"); |
| 354 | } else { |
| 355 | error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace"); |
| 356 | if (!error_obj) { |
| 357 | PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8"); |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 358 | } else { |
| 359 | PyErr_SetObject(pysqlite_OperationalError, error_obj); |
Gerhard Häring | e871ad59 | 2008-03-29 14:11:55 +0000 | [diff] [blame] | 360 | Py_DECREF(error_obj); |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 361 | } |
| 362 | Py_DECREF(buf_bytes); |
| 363 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 364 | } |
| 365 | } else if (self->connection->text_factory == (PyObject*)&PyString_Type) { |
| 366 | converted = PyString_FromString(val_str); |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 367 | } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) { |
| 368 | converted = PyBytes_FromStringAndSize(val_str, strlen(val_str)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 369 | } else { |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 370 | converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 371 | } |
| 372 | } else { |
| 373 | /* coltype == SQLITE_BLOB */ |
| 374 | nbytes = sqlite3_column_bytes(self->statement->st, i); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 375 | buffer = PyString_FromStringAndSize( |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 376 | sqlite3_column_blob(self->statement->st, i), nbytes); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 377 | if (!buffer) { |
| 378 | break; |
| 379 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 380 | converted = buffer; |
| 381 | } |
| 382 | } |
| 383 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 384 | if (converted) { |
| 385 | PyTuple_SetItem(row, i, converted); |
| 386 | } else { |
| 387 | Py_INCREF(Py_None); |
| 388 | PyTuple_SetItem(row, i, Py_None); |
| 389 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | if (PyErr_Occurred()) { |
| 393 | Py_DECREF(row); |
| 394 | row = NULL; |
| 395 | } |
| 396 | |
| 397 | return row; |
| 398 | } |
| 399 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 400 | PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 401 | { |
| 402 | PyObject* operation; |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 403 | const char* operation_cstr; |
| 404 | Py_ssize_t operation_len; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 405 | PyObject* parameters_list = NULL; |
| 406 | PyObject* parameters_iter = NULL; |
| 407 | PyObject* parameters = NULL; |
| 408 | int i; |
| 409 | int rc; |
| 410 | PyObject* func_args; |
| 411 | PyObject* result; |
| 412 | int numcols; |
| 413 | PY_LONG_LONG lastrowid; |
| 414 | int statement_type; |
| 415 | PyObject* descriptor; |
| 416 | PyObject* second_argument = NULL; |
| 417 | long rowcount = 0; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 418 | int allow_8bit_chars; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 419 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 420 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 421 | return NULL; |
| 422 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 423 | /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */ |
| 424 | allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) && |
| 425 | (self->connection->text_factory != (PyObject*)&PyUnicode_Type && pysqlite_OptimizedUnicode)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 426 | |
| 427 | Py_XDECREF(self->next_row); |
| 428 | self->next_row = NULL; |
| 429 | |
| 430 | if (multiple) { |
| 431 | /* executemany() */ |
| 432 | if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 433 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 436 | if (!PyUnicode_Check(operation)) { |
| 437 | PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | if (PyIter_Check(second_argument)) { |
| 442 | /* iterator */ |
| 443 | Py_INCREF(second_argument); |
| 444 | parameters_iter = second_argument; |
| 445 | } else { |
| 446 | /* sequence */ |
| 447 | parameters_iter = PyObject_GetIter(second_argument); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 448 | if (!parameters_iter) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 449 | return NULL; |
| 450 | } |
| 451 | } |
| 452 | } else { |
| 453 | /* execute() */ |
| 454 | if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 455 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 458 | if (!PyUnicode_Check(operation)) { |
| 459 | PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | parameters_list = PyList_New(0); |
| 464 | if (!parameters_list) { |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | if (second_argument == NULL) { |
| 469 | second_argument = PyTuple_New(0); |
| 470 | if (!second_argument) { |
| 471 | goto error; |
| 472 | } |
| 473 | } else { |
| 474 | Py_INCREF(second_argument); |
| 475 | } |
| 476 | if (PyList_Append(parameters_list, second_argument) != 0) { |
| 477 | Py_DECREF(second_argument); |
| 478 | goto error; |
| 479 | } |
| 480 | Py_DECREF(second_argument); |
| 481 | |
| 482 | parameters_iter = PyObject_GetIter(parameters_list); |
| 483 | if (!parameters_iter) { |
| 484 | goto error; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | if (self->statement != NULL) { |
| 489 | /* There is an active statement */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 490 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Guido van Rossum | ec9a4af | 2007-08-29 14:26:02 +0000 | [diff] [blame] | 493 | operation_cstr = PyUnicode_AsStringAndSize(operation, &operation_len); |
Guido van Rossum | fa9a121 | 2007-08-29 03:34:29 +0000 | [diff] [blame] | 494 | if (operation == NULL) |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 495 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 496 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 497 | /* reset description */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 498 | Py_DECREF(self->description); |
| 499 | Py_INCREF(Py_None); |
| 500 | self->description = Py_None; |
| 501 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 502 | func_args = PyTuple_New(1); |
| 503 | if (!func_args) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 504 | goto error; |
| 505 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 506 | Py_INCREF(operation); |
| 507 | if (PyTuple_SetItem(func_args, 0, operation) != 0) { |
| 508 | goto error; |
| 509 | } |
| 510 | |
| 511 | if (self->statement) { |
| 512 | (void)pysqlite_statement_reset(self->statement); |
| 513 | Py_DECREF(self->statement); |
| 514 | } |
| 515 | |
| 516 | self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args); |
| 517 | Py_DECREF(func_args); |
| 518 | |
| 519 | if (!self->statement) { |
| 520 | goto error; |
| 521 | } |
| 522 | |
| 523 | if (self->statement->in_use) { |
| 524 | Py_DECREF(self->statement); |
| 525 | self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
| 526 | if (!self->statement) { |
| 527 | goto error; |
| 528 | } |
| 529 | rc = pysqlite_statement_create(self->statement, self->connection, operation); |
| 530 | if (rc != SQLITE_OK) { |
| 531 | self->statement = 0; |
| 532 | goto error; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | pysqlite_statement_reset(self->statement); |
| 537 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 538 | |
| 539 | statement_type = detect_statement_type(operation_cstr); |
| 540 | if (self->connection->begin_statement) { |
| 541 | switch (statement_type) { |
| 542 | case STATEMENT_UPDATE: |
| 543 | case STATEMENT_DELETE: |
| 544 | case STATEMENT_INSERT: |
| 545 | case STATEMENT_REPLACE: |
| 546 | if (!self->connection->inTransaction) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 547 | result = _pysqlite_connection_begin(self->connection); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 548 | if (!result) { |
| 549 | goto error; |
| 550 | } |
| 551 | Py_DECREF(result); |
| 552 | } |
| 553 | break; |
| 554 | case STATEMENT_OTHER: |
| 555 | /* it's a DDL statement or something similar |
| 556 | - we better COMMIT first so it works for all cases */ |
| 557 | if (self->connection->inTransaction) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 558 | result = pysqlite_connection_commit(self->connection, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 559 | if (!result) { |
| 560 | goto error; |
| 561 | } |
| 562 | Py_DECREF(result); |
| 563 | } |
| 564 | break; |
| 565 | case STATEMENT_SELECT: |
| 566 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 567 | PyErr_SetString(pysqlite_ProgrammingError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 568 | "You cannot execute SELECT statements in executemany()."); |
| 569 | goto error; |
| 570 | } |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | func_args = PyTuple_New(1); |
| 576 | if (!func_args) { |
| 577 | goto error; |
| 578 | } |
| 579 | Py_INCREF(operation); |
| 580 | if (PyTuple_SetItem(func_args, 0, operation) != 0) { |
| 581 | goto error; |
| 582 | } |
| 583 | |
| 584 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 585 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 586 | Py_DECREF(self->statement); |
| 587 | } |
| 588 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 589 | 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] | 590 | Py_DECREF(func_args); |
| 591 | |
| 592 | if (!self->statement) { |
| 593 | goto error; |
| 594 | } |
| 595 | |
| 596 | if (self->statement->in_use) { |
| 597 | Py_DECREF(self->statement); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 598 | self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 599 | if (!self->statement) { |
| 600 | goto error; |
| 601 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 602 | rc = pysqlite_statement_create(self->statement, self->connection, operation); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 603 | if (rc != SQLITE_OK) { |
| 604 | self->statement = 0; |
| 605 | goto error; |
| 606 | } |
| 607 | } |
| 608 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 609 | pysqlite_statement_reset(self->statement); |
| 610 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 611 | |
| 612 | while (1) { |
| 613 | parameters = PyIter_Next(parameters_iter); |
| 614 | if (!parameters) { |
| 615 | break; |
| 616 | } |
| 617 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 618 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 619 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 620 | pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 621 | if (PyErr_Occurred()) { |
| 622 | goto error; |
| 623 | } |
| 624 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 625 | if (pysqlite_build_row_cast_map(self) != 0) { |
| 626 | PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 627 | goto error; |
| 628 | } |
| 629 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 630 | /* Keep trying the SQL statement until the schema stops changing. */ |
| 631 | while (1) { |
| 632 | /* Actually execute the SQL statement. */ |
| 633 | rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection); |
| 634 | if (rc == SQLITE_DONE || rc == SQLITE_ROW) { |
| 635 | /* If it worked, let's get out of the loop */ |
| 636 | break; |
| 637 | } |
| 638 | /* Something went wrong. Re-set the statement and try again. */ |
| 639 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 640 | if (rc == SQLITE_SCHEMA) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 641 | /* If this was a result of the schema changing, let's try |
| 642 | again. */ |
| 643 | rc = pysqlite_statement_recompile(self->statement, parameters); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 644 | if (rc == SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 645 | continue; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 646 | } else { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 647 | /* If the database gave us an error, promote it to Python. */ |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 648 | (void)pysqlite_statement_reset(self->statement); |
| 649 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 650 | goto error; |
| 651 | } |
| 652 | } else { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 653 | if (PyErr_Occurred()) { |
| 654 | /* there was an error that occurred in a user-defined callback */ |
| 655 | if (_enable_callback_tracebacks) { |
| 656 | PyErr_Print(); |
| 657 | } else { |
| 658 | PyErr_Clear(); |
| 659 | } |
| 660 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 661 | (void)pysqlite_statement_reset(self->statement); |
| 662 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 663 | goto error; |
| 664 | } |
| 665 | } |
| 666 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 667 | if (pysqlite_build_row_cast_map(self) != 0) { |
| 668 | PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map"); |
| 669 | goto error; |
| 670 | } |
| 671 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 672 | if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) { |
| 673 | Py_BEGIN_ALLOW_THREADS |
| 674 | numcols = sqlite3_column_count(self->statement->st); |
| 675 | Py_END_ALLOW_THREADS |
| 676 | |
| 677 | if (self->description == Py_None) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 678 | Py_BEGIN_ALLOW_THREADS |
| 679 | numcols = sqlite3_column_count(self->statement->st); |
| 680 | Py_END_ALLOW_THREADS |
| 681 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 682 | Py_DECREF(self->description); |
| 683 | self->description = PyTuple_New(numcols); |
| 684 | if (!self->description) { |
| 685 | goto error; |
| 686 | } |
| 687 | for (i = 0; i < numcols; i++) { |
| 688 | descriptor = PyTuple_New(7); |
| 689 | if (!descriptor) { |
| 690 | goto error; |
| 691 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 692 | 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] | 693 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None); |
| 694 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None); |
| 695 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None); |
| 696 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None); |
| 697 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None); |
| 698 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None); |
| 699 | PyTuple_SetItem(self->description, i, descriptor); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | if (rc == SQLITE_ROW) { |
| 705 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 706 | PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 707 | goto error; |
| 708 | } |
| 709 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 710 | self->next_row = _pysqlite_fetch_one_row(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 711 | } else if (rc == SQLITE_DONE && !multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 712 | pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 713 | Py_DECREF(self->statement); |
| 714 | self->statement = 0; |
| 715 | } |
| 716 | |
| 717 | switch (statement_type) { |
| 718 | case STATEMENT_UPDATE: |
| 719 | case STATEMENT_DELETE: |
| 720 | case STATEMENT_INSERT: |
| 721 | case STATEMENT_REPLACE: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 722 | 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()) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 756 | Py_DECREF(self->rowcount); |
| 757 | self->rowcount = PyLong_FromLong(-1L); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 758 | return NULL; |
| 759 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 760 | Py_DECREF(self->rowcount); |
| 761 | self->rowcount = PyLong_FromLong(rowcount); |
| 762 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 763 | Py_INCREF(self); |
| 764 | return (PyObject*)self; |
| 765 | } |
| 766 | } |
| 767 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 768 | PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 769 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 770 | return _pysqlite_query_execute(self, 0, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 773 | PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 774 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 775 | return _pysqlite_query_execute(self, 1, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 778 | PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 779 | { |
| 780 | PyObject* script_obj; |
| 781 | PyObject* script_str = NULL; |
| 782 | const char* script_cstr; |
| 783 | sqlite3_stmt* statement; |
| 784 | int rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 785 | PyObject* result; |
| 786 | int statement_completed = 0; |
| 787 | |
| 788 | if (!PyArg_ParseTuple(args, "O", &script_obj)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 789 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 792 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 793 | return NULL; |
| 794 | } |
| 795 | |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 796 | if (PyUnicode_Check(script_obj)) { |
| 797 | script_cstr = PyUnicode_AsString(script_obj); |
| 798 | if (!script_cstr) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 799 | return NULL; |
| 800 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 801 | } else { |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 802 | PyErr_SetString(PyExc_ValueError, "script argument must be unicode."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 803 | return NULL; |
| 804 | } |
| 805 | |
| 806 | /* commit first */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 807 | result = pysqlite_connection_commit(self->connection, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 808 | if (!result) { |
| 809 | goto error; |
| 810 | } |
| 811 | Py_DECREF(result); |
| 812 | |
| 813 | while (1) { |
| 814 | if (!sqlite3_complete(script_cstr)) { |
| 815 | break; |
| 816 | } |
| 817 | statement_completed = 1; |
| 818 | |
| 819 | rc = sqlite3_prepare(self->connection->db, |
| 820 | script_cstr, |
| 821 | -1, |
| 822 | &statement, |
| 823 | &script_cstr); |
| 824 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 825 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 826 | goto error; |
| 827 | } |
| 828 | |
| 829 | /* execute statement, and ignore results of SELECT statements */ |
| 830 | rc = SQLITE_ROW; |
| 831 | while (rc == SQLITE_ROW) { |
| 832 | rc = _sqlite_step_with_busyhandler(statement, self->connection); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 833 | /* TODO: we probably need more error handling here */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | if (rc != SQLITE_DONE) { |
| 837 | (void)sqlite3_finalize(statement); |
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 | } |
| 841 | |
| 842 | rc = sqlite3_finalize(statement); |
| 843 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 844 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 845 | goto error; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | error: |
| 850 | Py_XDECREF(script_str); |
| 851 | |
| 852 | if (!statement_completed) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 853 | PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | if (PyErr_Occurred()) { |
| 857 | return NULL; |
| 858 | } else { |
| 859 | Py_INCREF(self); |
| 860 | return (PyObject*)self; |
| 861 | } |
| 862 | } |
| 863 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 864 | PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 865 | { |
| 866 | Py_INCREF(self); |
| 867 | return (PyObject*)self; |
| 868 | } |
| 869 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 870 | PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 871 | { |
| 872 | PyObject* next_row_tuple; |
| 873 | PyObject* next_row; |
| 874 | int rc; |
| 875 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 876 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 877 | return NULL; |
| 878 | } |
| 879 | |
| 880 | if (!self->next_row) { |
| 881 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 882 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 883 | Py_DECREF(self->statement); |
| 884 | self->statement = NULL; |
| 885 | } |
| 886 | return NULL; |
| 887 | } |
| 888 | |
| 889 | next_row_tuple = self->next_row; |
| 890 | self->next_row = NULL; |
| 891 | |
| 892 | if (self->row_factory != Py_None) { |
| 893 | next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple); |
| 894 | Py_DECREF(next_row_tuple); |
| 895 | } else { |
| 896 | next_row = next_row_tuple; |
| 897 | } |
| 898 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 899 | if (self->statement) { |
| 900 | rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection); |
| 901 | if (rc != SQLITE_DONE && rc != SQLITE_ROW) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 902 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 903 | Py_DECREF(next_row); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 904 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 905 | return NULL; |
| 906 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 907 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 908 | if (rc == SQLITE_ROW) { |
| 909 | self->next_row = _pysqlite_fetch_one_row(self); |
| 910 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | return next_row; |
| 914 | } |
| 915 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 916 | PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 917 | { |
| 918 | PyObject* row; |
| 919 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 920 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 921 | if (!row && !PyErr_Occurred()) { |
| 922 | Py_INCREF(Py_None); |
| 923 | return Py_None; |
| 924 | } |
| 925 | |
| 926 | return row; |
| 927 | } |
| 928 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 929 | PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 930 | { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 931 | static char *kwlist[] = {"size", NULL, NULL}; |
| 932 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 933 | PyObject* row; |
| 934 | PyObject* list; |
| 935 | int maxrows = self->arraysize; |
| 936 | int counter = 0; |
| 937 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 938 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) { |
| 939 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | list = PyList_New(0); |
| 943 | if (!list) { |
| 944 | return NULL; |
| 945 | } |
| 946 | |
| 947 | /* just make sure we enter the loop */ |
| 948 | row = Py_None; |
| 949 | |
| 950 | while (row) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 951 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 952 | if (row) { |
| 953 | PyList_Append(list, row); |
| 954 | Py_DECREF(row); |
| 955 | } else { |
| 956 | break; |
| 957 | } |
| 958 | |
| 959 | if (++counter == maxrows) { |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | if (PyErr_Occurred()) { |
| 965 | Py_DECREF(list); |
| 966 | return NULL; |
| 967 | } else { |
| 968 | return list; |
| 969 | } |
| 970 | } |
| 971 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 972 | PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 973 | { |
| 974 | PyObject* row; |
| 975 | PyObject* list; |
| 976 | |
| 977 | list = PyList_New(0); |
| 978 | if (!list) { |
| 979 | return NULL; |
| 980 | } |
| 981 | |
| 982 | /* just make sure we enter the loop */ |
| 983 | row = (PyObject*)Py_None; |
| 984 | |
| 985 | while (row) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 986 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 987 | if (row) { |
| 988 | PyList_Append(list, row); |
| 989 | Py_DECREF(row); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | if (PyErr_Occurred()) { |
| 994 | Py_DECREF(list); |
| 995 | return NULL; |
| 996 | } else { |
| 997 | return list; |
| 998 | } |
| 999 | } |
| 1000 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1001 | PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1002 | { |
| 1003 | /* don't care, return None */ |
| 1004 | Py_INCREF(Py_None); |
| 1005 | return Py_None; |
| 1006 | } |
| 1007 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1008 | PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1009 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1010 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1011 | return NULL; |
| 1012 | } |
| 1013 | |
| 1014 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1015 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1016 | Py_DECREF(self->statement); |
| 1017 | self->statement = 0; |
| 1018 | } |
| 1019 | |
| 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}, |
| 1052 | {"rowcount", T_OBJECT, 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 */ |
| 1069 | 0, /* tp_compare */ |
| 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 */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +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 */ |
| 1085 | 0, /* 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 | } |