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