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++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame^] | 205 | /* Converter names are split at '(' and blanks. |
| 206 | * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and |
| 207 | * 'NUMBER(10)' to be treated as 'NUMBER', for example. |
| 208 | * In other words, it will work as people expect it to work.*/ |
| 209 | if (*pos == ' ' || *pos == '(' || *pos == 0) { |
Brett Cannon | 4043001 | 2007-10-22 20:24:51 +0000 | [diff] [blame] | 210 | py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 211 | if (!py_decltype) { |
| 212 | return -1; |
| 213 | } |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 218 | converter = _pysqlite_get_converter(py_decltype); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 219 | Py_DECREF(py_decltype); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (!converter) { |
| 224 | converter = Py_None; |
| 225 | } |
| 226 | |
| 227 | if (PyList_Append(self->row_cast_map, converter) != 0) { |
| 228 | if (converter != Py_None) { |
| 229 | Py_DECREF(converter); |
| 230 | } |
| 231 | Py_XDECREF(self->row_cast_map); |
| 232 | self->row_cast_map = NULL; |
| 233 | |
| 234 | return -1; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 241 | PyObject* _pysqlite_build_column_name(const char* colname) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 242 | { |
| 243 | const char* pos; |
| 244 | |
| 245 | if (!colname) { |
| 246 | Py_INCREF(Py_None); |
| 247 | return Py_None; |
| 248 | } |
| 249 | |
| 250 | for (pos = colname;; pos++) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 251 | if (*pos == 0 || *pos == '[') { |
| 252 | if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) { |
| 253 | pos--; |
| 254 | } |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 255 | return PyUnicode_FromStringAndSize(colname, pos - colname); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 260 | PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 261 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 262 | return PyUnicode_FromString(val_str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Returns a row from the currently active SQLite statement |
| 267 | * |
| 268 | * Precondidition: |
| 269 | * - sqlite3_step() has been called before and it returned SQLITE_ROW. |
| 270 | */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 271 | PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 272 | { |
| 273 | int i, numcols; |
| 274 | PyObject* row; |
| 275 | PyObject* item = NULL; |
| 276 | int coltype; |
| 277 | PY_LONG_LONG intval; |
| 278 | PyObject* converter; |
| 279 | PyObject* converted; |
| 280 | Py_ssize_t nbytes; |
| 281 | PyObject* buffer; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 282 | const char* val_str; |
| 283 | char buf[200]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 284 | const char* colname; |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 285 | PyObject* buf_bytes; |
| 286 | PyObject* error_obj; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 287 | |
| 288 | Py_BEGIN_ALLOW_THREADS |
| 289 | numcols = sqlite3_data_count(self->statement->st); |
| 290 | Py_END_ALLOW_THREADS |
| 291 | |
| 292 | row = PyTuple_New(numcols); |
| 293 | if (!row) { |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | for (i = 0; i < numcols; i++) { |
| 298 | if (self->connection->detect_types) { |
| 299 | converter = PyList_GetItem(self->row_cast_map, i); |
| 300 | if (!converter) { |
| 301 | converter = Py_None; |
| 302 | } |
| 303 | } else { |
| 304 | converter = Py_None; |
| 305 | } |
| 306 | |
| 307 | if (converter != Py_None) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 308 | nbytes = sqlite3_column_bytes(self->statement->st, i); |
| 309 | val_str = (const char*)sqlite3_column_blob(self->statement->st, i); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 310 | if (!val_str) { |
| 311 | Py_INCREF(Py_None); |
| 312 | converted = Py_None; |
| 313 | } else { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 314 | item = PyString_FromStringAndSize(val_str, nbytes); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 315 | if (!item) { |
| 316 | return NULL; |
| 317 | } |
| 318 | converted = PyObject_CallFunction(converter, "O", item); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 319 | Py_DECREF(item); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 320 | if (!converted) { |
| 321 | break; |
| 322 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 323 | } |
| 324 | } else { |
| 325 | Py_BEGIN_ALLOW_THREADS |
| 326 | coltype = sqlite3_column_type(self->statement->st, i); |
| 327 | Py_END_ALLOW_THREADS |
| 328 | if (coltype == SQLITE_NULL) { |
| 329 | Py_INCREF(Py_None); |
| 330 | converted = Py_None; |
| 331 | } else if (coltype == SQLITE_INTEGER) { |
| 332 | intval = sqlite3_column_int64(self->statement->st, i); |
| 333 | if (intval < INT32_MIN || intval > INT32_MAX) { |
| 334 | converted = PyLong_FromLongLong(intval); |
| 335 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 336 | converted = PyLong_FromLong((long)intval); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 337 | } |
| 338 | } else if (coltype == SQLITE_FLOAT) { |
| 339 | converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i)); |
| 340 | } else if (coltype == SQLITE_TEXT) { |
| 341 | val_str = (const char*)sqlite3_column_text(self->statement->st, i); |
| 342 | if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 343 | || (self->connection->text_factory == pysqlite_OptimizedUnicode)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 344 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 345 | converted = pysqlite_unicode_from_string(val_str, |
| 346 | self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 347 | |
| 348 | if (!converted) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 349 | colname = sqlite3_column_name(self->statement->st, i); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 350 | if (!colname) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 351 | colname = "<unknown column name>"; |
| 352 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 353 | 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] | 354 | colname , val_str); |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 355 | buf_bytes = PyBytes_FromStringAndSize(buf, strlen(buf)); |
| 356 | if (!buf_bytes) { |
| 357 | PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8"); |
| 358 | } else { |
| 359 | error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace"); |
| 360 | if (!error_obj) { |
| 361 | PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8"); |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 362 | } else { |
| 363 | PyErr_SetObject(pysqlite_OperationalError, error_obj); |
Gerhard Häring | e871ad59 | 2008-03-29 14:11:55 +0000 | [diff] [blame] | 364 | Py_DECREF(error_obj); |
Gerhard Häring | 873d9ff | 2008-02-29 22:22:09 +0000 | [diff] [blame] | 365 | } |
| 366 | Py_DECREF(buf_bytes); |
| 367 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 368 | } |
| 369 | } else if (self->connection->text_factory == (PyObject*)&PyString_Type) { |
| 370 | converted = PyString_FromString(val_str); |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 371 | } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) { |
| 372 | converted = PyBytes_FromStringAndSize(val_str, strlen(val_str)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 373 | } else { |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 374 | converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 375 | } |
| 376 | } else { |
| 377 | /* coltype == SQLITE_BLOB */ |
| 378 | nbytes = sqlite3_column_bytes(self->statement->st, i); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 379 | buffer = PyString_FromStringAndSize( |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 380 | sqlite3_column_blob(self->statement->st, i), nbytes); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 381 | if (!buffer) { |
| 382 | break; |
| 383 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 384 | converted = buffer; |
| 385 | } |
| 386 | } |
| 387 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 388 | if (converted) { |
| 389 | PyTuple_SetItem(row, i, converted); |
| 390 | } else { |
| 391 | Py_INCREF(Py_None); |
| 392 | PyTuple_SetItem(row, i, Py_None); |
| 393 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | if (PyErr_Occurred()) { |
| 397 | Py_DECREF(row); |
| 398 | row = NULL; |
| 399 | } |
| 400 | |
| 401 | return row; |
| 402 | } |
| 403 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 404 | PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 405 | { |
| 406 | PyObject* operation; |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 407 | const char* operation_cstr; |
| 408 | Py_ssize_t operation_len; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 409 | PyObject* parameters_list = NULL; |
| 410 | PyObject* parameters_iter = NULL; |
| 411 | PyObject* parameters = NULL; |
| 412 | int i; |
| 413 | int rc; |
| 414 | PyObject* func_args; |
| 415 | PyObject* result; |
| 416 | int numcols; |
| 417 | PY_LONG_LONG lastrowid; |
| 418 | int statement_type; |
| 419 | PyObject* descriptor; |
| 420 | PyObject* second_argument = NULL; |
| 421 | long rowcount = 0; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 422 | int allow_8bit_chars; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 423 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 424 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 425 | return NULL; |
| 426 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 427 | /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */ |
| 428 | allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) && |
| 429 | (self->connection->text_factory != (PyObject*)&PyUnicode_Type && pysqlite_OptimizedUnicode)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 430 | |
| 431 | Py_XDECREF(self->next_row); |
| 432 | self->next_row = NULL; |
| 433 | |
| 434 | if (multiple) { |
| 435 | /* executemany() */ |
| 436 | if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 437 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 440 | if (!PyUnicode_Check(operation)) { |
| 441 | PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 442 | return NULL; |
| 443 | } |
| 444 | |
| 445 | if (PyIter_Check(second_argument)) { |
| 446 | /* iterator */ |
| 447 | Py_INCREF(second_argument); |
| 448 | parameters_iter = second_argument; |
| 449 | } else { |
| 450 | /* sequence */ |
| 451 | parameters_iter = PyObject_GetIter(second_argument); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 452 | if (!parameters_iter) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 453 | return NULL; |
| 454 | } |
| 455 | } |
| 456 | } else { |
| 457 | /* execute() */ |
| 458 | if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 459 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 462 | if (!PyUnicode_Check(operation)) { |
| 463 | PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | parameters_list = PyList_New(0); |
| 468 | if (!parameters_list) { |
| 469 | return NULL; |
| 470 | } |
| 471 | |
| 472 | if (second_argument == NULL) { |
| 473 | second_argument = PyTuple_New(0); |
| 474 | if (!second_argument) { |
| 475 | goto error; |
| 476 | } |
| 477 | } else { |
| 478 | Py_INCREF(second_argument); |
| 479 | } |
| 480 | if (PyList_Append(parameters_list, second_argument) != 0) { |
| 481 | Py_DECREF(second_argument); |
| 482 | goto error; |
| 483 | } |
| 484 | Py_DECREF(second_argument); |
| 485 | |
| 486 | parameters_iter = PyObject_GetIter(parameters_list); |
| 487 | if (!parameters_iter) { |
| 488 | goto error; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (self->statement != NULL) { |
| 493 | /* There is an active statement */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 494 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Guido van Rossum | ec9a4af | 2007-08-29 14:26:02 +0000 | [diff] [blame] | 497 | operation_cstr = PyUnicode_AsStringAndSize(operation, &operation_len); |
Guido van Rossum | fa9a121 | 2007-08-29 03:34:29 +0000 | [diff] [blame] | 498 | if (operation == NULL) |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 499 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 500 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 501 | /* reset description */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 502 | Py_DECREF(self->description); |
| 503 | Py_INCREF(Py_None); |
| 504 | self->description = Py_None; |
| 505 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 506 | func_args = PyTuple_New(1); |
| 507 | if (!func_args) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 508 | goto error; |
| 509 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 510 | Py_INCREF(operation); |
| 511 | if (PyTuple_SetItem(func_args, 0, operation) != 0) { |
| 512 | goto error; |
| 513 | } |
| 514 | |
| 515 | if (self->statement) { |
| 516 | (void)pysqlite_statement_reset(self->statement); |
| 517 | Py_DECREF(self->statement); |
| 518 | } |
| 519 | |
| 520 | self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args); |
| 521 | Py_DECREF(func_args); |
| 522 | |
| 523 | if (!self->statement) { |
| 524 | goto error; |
| 525 | } |
| 526 | |
| 527 | if (self->statement->in_use) { |
| 528 | Py_DECREF(self->statement); |
| 529 | self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
| 530 | if (!self->statement) { |
| 531 | goto error; |
| 532 | } |
| 533 | rc = pysqlite_statement_create(self->statement, self->connection, operation); |
| 534 | if (rc != SQLITE_OK) { |
| 535 | self->statement = 0; |
| 536 | goto error; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | pysqlite_statement_reset(self->statement); |
| 541 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 542 | |
| 543 | statement_type = detect_statement_type(operation_cstr); |
| 544 | if (self->connection->begin_statement) { |
| 545 | switch (statement_type) { |
| 546 | case STATEMENT_UPDATE: |
| 547 | case STATEMENT_DELETE: |
| 548 | case STATEMENT_INSERT: |
| 549 | case STATEMENT_REPLACE: |
| 550 | if (!self->connection->inTransaction) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 551 | result = _pysqlite_connection_begin(self->connection); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 552 | if (!result) { |
| 553 | goto error; |
| 554 | } |
| 555 | Py_DECREF(result); |
| 556 | } |
| 557 | break; |
| 558 | case STATEMENT_OTHER: |
| 559 | /* it's a DDL statement or something similar |
| 560 | - we better COMMIT first so it works for all cases */ |
| 561 | if (self->connection->inTransaction) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 562 | result = pysqlite_connection_commit(self->connection, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 563 | if (!result) { |
| 564 | goto error; |
| 565 | } |
| 566 | Py_DECREF(result); |
| 567 | } |
| 568 | break; |
| 569 | case STATEMENT_SELECT: |
| 570 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 571 | PyErr_SetString(pysqlite_ProgrammingError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 572 | "You cannot execute SELECT statements in executemany()."); |
| 573 | goto error; |
| 574 | } |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | func_args = PyTuple_New(1); |
| 580 | if (!func_args) { |
| 581 | goto error; |
| 582 | } |
| 583 | Py_INCREF(operation); |
| 584 | if (PyTuple_SetItem(func_args, 0, operation) != 0) { |
| 585 | goto error; |
| 586 | } |
| 587 | |
| 588 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 589 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 590 | Py_DECREF(self->statement); |
| 591 | } |
| 592 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 593 | 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] | 594 | Py_DECREF(func_args); |
| 595 | |
| 596 | if (!self->statement) { |
| 597 | goto error; |
| 598 | } |
| 599 | |
| 600 | if (self->statement->in_use) { |
| 601 | Py_DECREF(self->statement); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 602 | self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 603 | if (!self->statement) { |
| 604 | goto error; |
| 605 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 606 | rc = pysqlite_statement_create(self->statement, self->connection, operation); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 607 | if (rc != SQLITE_OK) { |
| 608 | self->statement = 0; |
| 609 | goto error; |
| 610 | } |
| 611 | } |
| 612 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 613 | pysqlite_statement_reset(self->statement); |
| 614 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 615 | |
| 616 | while (1) { |
| 617 | parameters = PyIter_Next(parameters_iter); |
| 618 | if (!parameters) { |
| 619 | break; |
| 620 | } |
| 621 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 622 | pysqlite_statement_mark_dirty(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 623 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 624 | pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 625 | if (PyErr_Occurred()) { |
| 626 | goto error; |
| 627 | } |
| 628 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 629 | if (pysqlite_build_row_cast_map(self) != 0) { |
| 630 | PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 631 | goto error; |
| 632 | } |
| 633 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 634 | /* Keep trying the SQL statement until the schema stops changing. */ |
| 635 | while (1) { |
| 636 | /* Actually execute the SQL statement. */ |
| 637 | rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection); |
| 638 | if (rc == SQLITE_DONE || rc == SQLITE_ROW) { |
| 639 | /* If it worked, let's get out of the loop */ |
| 640 | break; |
| 641 | } |
| 642 | /* Something went wrong. Re-set the statement and try again. */ |
| 643 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 644 | if (rc == SQLITE_SCHEMA) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 645 | /* If this was a result of the schema changing, let's try |
| 646 | again. */ |
| 647 | rc = pysqlite_statement_recompile(self->statement, parameters); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 648 | if (rc == SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 649 | continue; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 650 | } else { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 651 | /* If the database gave us an error, promote it to Python. */ |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 652 | (void)pysqlite_statement_reset(self->statement); |
| 653 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 654 | goto error; |
| 655 | } |
| 656 | } else { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 657 | if (PyErr_Occurred()) { |
| 658 | /* there was an error that occurred in a user-defined callback */ |
| 659 | if (_enable_callback_tracebacks) { |
| 660 | PyErr_Print(); |
| 661 | } else { |
| 662 | PyErr_Clear(); |
| 663 | } |
| 664 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 665 | (void)pysqlite_statement_reset(self->statement); |
| 666 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 667 | goto error; |
| 668 | } |
| 669 | } |
| 670 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 671 | if (pysqlite_build_row_cast_map(self) != 0) { |
| 672 | PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map"); |
| 673 | goto error; |
| 674 | } |
| 675 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 676 | if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) { |
| 677 | Py_BEGIN_ALLOW_THREADS |
| 678 | numcols = sqlite3_column_count(self->statement->st); |
| 679 | Py_END_ALLOW_THREADS |
| 680 | |
| 681 | if (self->description == Py_None) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 682 | Py_BEGIN_ALLOW_THREADS |
| 683 | numcols = sqlite3_column_count(self->statement->st); |
| 684 | Py_END_ALLOW_THREADS |
| 685 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 686 | Py_DECREF(self->description); |
| 687 | self->description = PyTuple_New(numcols); |
| 688 | if (!self->description) { |
| 689 | goto error; |
| 690 | } |
| 691 | for (i = 0; i < numcols; i++) { |
| 692 | descriptor = PyTuple_New(7); |
| 693 | if (!descriptor) { |
| 694 | goto error; |
| 695 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 696 | 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] | 697 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None); |
| 698 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None); |
| 699 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None); |
| 700 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None); |
| 701 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None); |
| 702 | Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None); |
| 703 | PyTuple_SetItem(self->description, i, descriptor); |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | if (rc == SQLITE_ROW) { |
| 709 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 710 | PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 711 | goto error; |
| 712 | } |
| 713 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 714 | self->next_row = _pysqlite_fetch_one_row(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 715 | } else if (rc == SQLITE_DONE && !multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 716 | pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 717 | Py_DECREF(self->statement); |
| 718 | self->statement = 0; |
| 719 | } |
| 720 | |
| 721 | switch (statement_type) { |
| 722 | case STATEMENT_UPDATE: |
| 723 | case STATEMENT_DELETE: |
| 724 | case STATEMENT_INSERT: |
| 725 | case STATEMENT_REPLACE: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 726 | rowcount += (long)sqlite3_changes(self->connection->db); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | Py_DECREF(self->lastrowid); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 730 | if (!multiple && statement_type == STATEMENT_INSERT) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 731 | Py_BEGIN_ALLOW_THREADS |
| 732 | lastrowid = sqlite3_last_insert_rowid(self->connection->db); |
| 733 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 734 | self->lastrowid = PyLong_FromLong((long)lastrowid); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 735 | } else { |
| 736 | Py_INCREF(Py_None); |
| 737 | self->lastrowid = Py_None; |
| 738 | } |
| 739 | |
| 740 | if (multiple) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 741 | rc = pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 742 | } |
| 743 | Py_XDECREF(parameters); |
| 744 | } |
| 745 | |
| 746 | error: |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 747 | /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR |
| 748 | * ROLLBACK could have happened */ |
| 749 | #ifdef SQLITE_VERSION_NUMBER |
| 750 | #if SQLITE_VERSION_NUMBER >= 3002002 |
| 751 | self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db); |
| 752 | #endif |
| 753 | #endif |
| 754 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 755 | Py_XDECREF(parameters); |
| 756 | Py_XDECREF(parameters_iter); |
| 757 | Py_XDECREF(parameters_list); |
| 758 | |
| 759 | if (PyErr_Occurred()) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 760 | Py_DECREF(self->rowcount); |
| 761 | self->rowcount = PyLong_FromLong(-1L); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 762 | return NULL; |
| 763 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 764 | Py_DECREF(self->rowcount); |
| 765 | self->rowcount = PyLong_FromLong(rowcount); |
| 766 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 767 | Py_INCREF(self); |
| 768 | return (PyObject*)self; |
| 769 | } |
| 770 | } |
| 771 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 772 | PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 773 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 774 | return _pysqlite_query_execute(self, 0, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 777 | PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 778 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 779 | return _pysqlite_query_execute(self, 1, args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 782 | PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 783 | { |
| 784 | PyObject* script_obj; |
| 785 | PyObject* script_str = NULL; |
| 786 | const char* script_cstr; |
| 787 | sqlite3_stmt* statement; |
| 788 | int rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 789 | PyObject* result; |
| 790 | int statement_completed = 0; |
| 791 | |
| 792 | if (!PyArg_ParseTuple(args, "O", &script_obj)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 793 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 796 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 797 | return NULL; |
| 798 | } |
| 799 | |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 800 | if (PyUnicode_Check(script_obj)) { |
| 801 | script_cstr = PyUnicode_AsString(script_obj); |
| 802 | if (!script_cstr) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 803 | return NULL; |
| 804 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 805 | } else { |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 806 | PyErr_SetString(PyExc_ValueError, "script argument must be unicode."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 807 | return NULL; |
| 808 | } |
| 809 | |
| 810 | /* commit first */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 811 | result = pysqlite_connection_commit(self->connection, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 812 | if (!result) { |
| 813 | goto error; |
| 814 | } |
| 815 | Py_DECREF(result); |
| 816 | |
| 817 | while (1) { |
| 818 | if (!sqlite3_complete(script_cstr)) { |
| 819 | break; |
| 820 | } |
| 821 | statement_completed = 1; |
| 822 | |
| 823 | rc = sqlite3_prepare(self->connection->db, |
| 824 | script_cstr, |
| 825 | -1, |
| 826 | &statement, |
| 827 | &script_cstr); |
| 828 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 829 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 830 | goto error; |
| 831 | } |
| 832 | |
| 833 | /* execute statement, and ignore results of SELECT statements */ |
| 834 | rc = SQLITE_ROW; |
| 835 | while (rc == SQLITE_ROW) { |
| 836 | rc = _sqlite_step_with_busyhandler(statement, self->connection); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 837 | /* TODO: we probably need more error handling here */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | if (rc != SQLITE_DONE) { |
| 841 | (void)sqlite3_finalize(statement); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 842 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 843 | goto error; |
| 844 | } |
| 845 | |
| 846 | rc = sqlite3_finalize(statement); |
| 847 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 848 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 849 | goto error; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | error: |
| 854 | Py_XDECREF(script_str); |
| 855 | |
| 856 | if (!statement_completed) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 857 | PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | if (PyErr_Occurred()) { |
| 861 | return NULL; |
| 862 | } else { |
| 863 | Py_INCREF(self); |
| 864 | return (PyObject*)self; |
| 865 | } |
| 866 | } |
| 867 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 868 | PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 869 | { |
| 870 | Py_INCREF(self); |
| 871 | return (PyObject*)self; |
| 872 | } |
| 873 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 874 | PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 875 | { |
| 876 | PyObject* next_row_tuple; |
| 877 | PyObject* next_row; |
| 878 | int rc; |
| 879 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 880 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 881 | return NULL; |
| 882 | } |
| 883 | |
| 884 | if (!self->next_row) { |
| 885 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 886 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 887 | Py_DECREF(self->statement); |
| 888 | self->statement = NULL; |
| 889 | } |
| 890 | return NULL; |
| 891 | } |
| 892 | |
| 893 | next_row_tuple = self->next_row; |
| 894 | self->next_row = NULL; |
| 895 | |
| 896 | if (self->row_factory != Py_None) { |
| 897 | next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple); |
| 898 | Py_DECREF(next_row_tuple); |
| 899 | } else { |
| 900 | next_row = next_row_tuple; |
| 901 | } |
| 902 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 903 | if (self->statement) { |
| 904 | rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection); |
| 905 | if (rc != SQLITE_DONE && rc != SQLITE_ROW) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 906 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 907 | Py_DECREF(next_row); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 908 | _pysqlite_seterror(self->connection->db, NULL); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 909 | return NULL; |
| 910 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 911 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 912 | if (rc == SQLITE_ROW) { |
| 913 | self->next_row = _pysqlite_fetch_one_row(self); |
| 914 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | return next_row; |
| 918 | } |
| 919 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 920 | PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 921 | { |
| 922 | PyObject* row; |
| 923 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 924 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 925 | if (!row && !PyErr_Occurred()) { |
| 926 | Py_INCREF(Py_None); |
| 927 | return Py_None; |
| 928 | } |
| 929 | |
| 930 | return row; |
| 931 | } |
| 932 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 933 | PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 934 | { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 935 | static char *kwlist[] = {"size", NULL, NULL}; |
| 936 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 937 | PyObject* row; |
| 938 | PyObject* list; |
| 939 | int maxrows = self->arraysize; |
| 940 | int counter = 0; |
| 941 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 942 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) { |
| 943 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | list = PyList_New(0); |
| 947 | if (!list) { |
| 948 | return NULL; |
| 949 | } |
| 950 | |
| 951 | /* just make sure we enter the loop */ |
| 952 | row = Py_None; |
| 953 | |
| 954 | while (row) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 955 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 956 | if (row) { |
| 957 | PyList_Append(list, row); |
| 958 | Py_DECREF(row); |
| 959 | } else { |
| 960 | break; |
| 961 | } |
| 962 | |
| 963 | if (++counter == maxrows) { |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | if (PyErr_Occurred()) { |
| 969 | Py_DECREF(list); |
| 970 | return NULL; |
| 971 | } else { |
| 972 | return list; |
| 973 | } |
| 974 | } |
| 975 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 976 | PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 977 | { |
| 978 | PyObject* row; |
| 979 | PyObject* list; |
| 980 | |
| 981 | list = PyList_New(0); |
| 982 | if (!list) { |
| 983 | return NULL; |
| 984 | } |
| 985 | |
| 986 | /* just make sure we enter the loop */ |
| 987 | row = (PyObject*)Py_None; |
| 988 | |
| 989 | while (row) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 990 | row = pysqlite_cursor_iternext(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 991 | if (row) { |
| 992 | PyList_Append(list, row); |
| 993 | Py_DECREF(row); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | if (PyErr_Occurred()) { |
| 998 | Py_DECREF(list); |
| 999 | return NULL; |
| 1000 | } else { |
| 1001 | return list; |
| 1002 | } |
| 1003 | } |
| 1004 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1005 | PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1006 | { |
| 1007 | /* don't care, return None */ |
| 1008 | Py_INCREF(Py_None); |
| 1009 | return Py_None; |
| 1010 | } |
| 1011 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1012 | PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1013 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1014 | if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1015 | return NULL; |
| 1016 | } |
| 1017 | |
| 1018 | if (self->statement) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1019 | (void)pysqlite_statement_reset(self->statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1020 | Py_DECREF(self->statement); |
| 1021 | self->statement = 0; |
| 1022 | } |
| 1023 | |
| 1024 | Py_INCREF(Py_None); |
| 1025 | return Py_None; |
| 1026 | } |
| 1027 | |
| 1028 | static PyMethodDef cursor_methods[] = { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1029 | {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1030 | PyDoc_STR("Executes a SQL statement.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1031 | {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1032 | PyDoc_STR("Repeatedly executes a SQL statement.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1033 | {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1034 | PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1035 | {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1036 | PyDoc_STR("Fetches one row from the resultset.")}, |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1037 | {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1038 | PyDoc_STR("Fetches several rows from the resultset.")}, |
| 1039 | {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS, |
| 1040 | PyDoc_STR("Fetches all rows from the resultset.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1041 | {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1042 | PyDoc_STR("Closes the cursor.")}, |
| 1043 | {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS, |
| 1044 | PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, |
| 1045 | {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS, |
| 1046 | PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, |
| 1047 | {NULL, NULL} |
| 1048 | }; |
| 1049 | |
| 1050 | static struct PyMemberDef cursor_members[] = |
| 1051 | { |
Guido van Rossum | 10f07c4 | 2007-08-11 15:32:55 +0000 | [diff] [blame] | 1052 | {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY}, |
| 1053 | {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1054 | {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0}, |
Guido van Rossum | 10f07c4 | 2007-08-11 15:32:55 +0000 | [diff] [blame] | 1055 | {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY}, |
| 1056 | {"rowcount", T_OBJECT, offsetof(pysqlite_Cursor, rowcount), READONLY}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1057 | {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1058 | {NULL} |
| 1059 | }; |
| 1060 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1061 | static char cursor_doc[] = |
| 1062 | PyDoc_STR("SQLite database cursor class."); |
| 1063 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1064 | PyTypeObject pysqlite_CursorType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1065 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1066 | MODULE_NAME ".Cursor", /* tp_name */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1067 | sizeof(pysqlite_Cursor), /* tp_basicsize */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1068 | 0, /* tp_itemsize */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1069 | (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1070 | 0, /* tp_print */ |
| 1071 | 0, /* tp_getattr */ |
| 1072 | 0, /* tp_setattr */ |
| 1073 | 0, /* tp_compare */ |
| 1074 | 0, /* tp_repr */ |
| 1075 | 0, /* tp_as_number */ |
| 1076 | 0, /* tp_as_sequence */ |
| 1077 | 0, /* tp_as_mapping */ |
| 1078 | 0, /* tp_hash */ |
| 1079 | 0, /* tp_call */ |
| 1080 | 0, /* tp_str */ |
| 1081 | 0, /* tp_getattro */ |
| 1082 | 0, /* tp_setattro */ |
| 1083 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 1084 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1085 | cursor_doc, /* tp_doc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1086 | 0, /* tp_traverse */ |
| 1087 | 0, /* tp_clear */ |
| 1088 | 0, /* tp_richcompare */ |
| 1089 | 0, /* tp_weaklistoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1090 | (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */ |
| 1091 | (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1092 | cursor_methods, /* tp_methods */ |
| 1093 | cursor_members, /* tp_members */ |
| 1094 | 0, /* tp_getset */ |
| 1095 | 0, /* tp_base */ |
| 1096 | 0, /* tp_dict */ |
| 1097 | 0, /* tp_descr_get */ |
| 1098 | 0, /* tp_descr_set */ |
| 1099 | 0, /* tp_dictoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1100 | (initproc)pysqlite_cursor_init, /* tp_init */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1101 | 0, /* tp_alloc */ |
| 1102 | 0, /* tp_new */ |
| 1103 | 0 /* tp_free */ |
| 1104 | }; |
| 1105 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1106 | extern int pysqlite_cursor_setup_types(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1107 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1108 | pysqlite_CursorType.tp_new = PyType_GenericNew; |
| 1109 | return PyType_Ready(&pysqlite_CursorType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1110 | } |