Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | /* connection.c - the connection 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 "cache.h" |
| 25 | #include "module.h" |
| 26 | #include "connection.h" |
| 27 | #include "statement.h" |
| 28 | #include "cursor.h" |
| 29 | #include "prepare_protocol.h" |
| 30 | #include "util.h" |
| 31 | #include "sqlitecompat.h" |
| 32 | |
| 33 | #include "pythread.h" |
| 34 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 35 | #define ACTION_FINALIZE 1 |
| 36 | #define ACTION_RESET 2 |
| 37 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 38 | #if SQLITE_VERSION_NUMBER >= 3003008 |
| 39 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 40 | #define HAVE_LOAD_EXTENSION |
| 41 | #endif |
| 42 | #endif |
| 43 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 44 | static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level); |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 45 | static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 46 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 47 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 48 | static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | { |
| 50 | /* in older SQLite versions, calling sqlite3_result_error in callbacks |
| 51 | * triggers a bug in SQLite that leads either to irritating results or |
| 52 | * segfaults, depending on the SQLite version */ |
| 53 | #if SQLITE_VERSION_NUMBER >= 3003003 |
| 54 | sqlite3_result_error(ctx, errmsg, len); |
| 55 | #else |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 56 | PyErr_SetString(pysqlite_OperationalError, errmsg); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 57 | #endif |
| 58 | } |
| 59 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 60 | int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 61 | { |
| 62 | static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL}; |
| 63 | |
| 64 | char* database; |
| 65 | int detect_types = 0; |
| 66 | PyObject* isolation_level = NULL; |
| 67 | PyObject* factory = NULL; |
| 68 | int check_same_thread = 1; |
| 69 | int cached_statements = 100; |
| 70 | double timeout = 5.0; |
| 71 | int rc; |
| 72 | |
| 73 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist, |
| 74 | &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements)) |
| 75 | { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 76 | return -1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 79 | self->initialized = 1; |
| 80 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 81 | self->begin_statement = NULL; |
| 82 | |
| 83 | self->statement_cache = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 84 | self->statements = NULL; |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 85 | self->cursors = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | |
| 87 | Py_INCREF(Py_None); |
| 88 | self->row_factory = Py_None; |
| 89 | |
| 90 | Py_INCREF(&PyUnicode_Type); |
| 91 | self->text_factory = (PyObject*)&PyUnicode_Type; |
| 92 | |
| 93 | Py_BEGIN_ALLOW_THREADS |
| 94 | rc = sqlite3_open(database, &self->db); |
| 95 | Py_END_ALLOW_THREADS |
| 96 | |
| 97 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 98 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | if (!isolation_level) { |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 103 | isolation_level = PyUnicode_FromString(""); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 104 | if (!isolation_level) { |
| 105 | return -1; |
| 106 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 107 | } else { |
| 108 | Py_INCREF(isolation_level); |
| 109 | } |
| 110 | self->isolation_level = NULL; |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 111 | pysqlite_connection_set_isolation_level(self, isolation_level); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 112 | Py_DECREF(isolation_level); |
| 113 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 114 | self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 115 | if (PyErr_Occurred()) { |
| 116 | return -1; |
| 117 | } |
| 118 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 119 | self->created_statements = 0; |
| 120 | self->created_cursors = 0; |
| 121 | |
| 122 | /* Create lists of weak references to statements/cursors */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 123 | self->statements = PyList_New(0); |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 124 | self->cursors = PyList_New(0); |
| 125 | if (!self->statements || !self->cursors) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 126 | return -1; |
| 127 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 128 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 129 | /* By default, the Cache class INCREFs the factory in its initializer, and |
| 130 | * decrefs it in its deallocator method. Since this would create a circular |
| 131 | * reference here, we're breaking it by decrementing self, and telling the |
| 132 | * cache class to not decref the factory (self) in its deallocator. |
| 133 | */ |
| 134 | self->statement_cache->decref_factory = 0; |
| 135 | Py_DECREF(self); |
| 136 | |
| 137 | self->inTransaction = 0; |
| 138 | self->detect_types = detect_types; |
| 139 | self->timeout = timeout; |
| 140 | (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000)); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 141 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 142 | self->thread_ident = PyThread_get_thread_ident(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 143 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 144 | self->check_same_thread = check_same_thread; |
| 145 | |
| 146 | self->function_pinboard = PyDict_New(); |
| 147 | if (!self->function_pinboard) { |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | self->collations = PyDict_New(); |
| 152 | if (!self->collations) { |
| 153 | return -1; |
| 154 | } |
| 155 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 156 | self->Warning = pysqlite_Warning; |
| 157 | self->Error = pysqlite_Error; |
| 158 | self->InterfaceError = pysqlite_InterfaceError; |
| 159 | self->DatabaseError = pysqlite_DatabaseError; |
| 160 | self->DataError = pysqlite_DataError; |
| 161 | self->OperationalError = pysqlite_OperationalError; |
| 162 | self->IntegrityError = pysqlite_IntegrityError; |
| 163 | self->InternalError = pysqlite_InternalError; |
| 164 | self->ProgrammingError = pysqlite_ProgrammingError; |
| 165 | self->NotSupportedError = pysqlite_NotSupportedError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 170 | /* Empty the entire statement cache of this connection */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 171 | void pysqlite_flush_statement_cache(pysqlite_Connection* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 172 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 173 | pysqlite_Node* node; |
| 174 | pysqlite_Statement* statement; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 175 | |
| 176 | node = self->statement_cache->first; |
| 177 | |
| 178 | while (node) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 179 | statement = (pysqlite_Statement*)(node->data); |
| 180 | (void)pysqlite_statement_finalize(statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 181 | node = node->next; |
| 182 | } |
| 183 | |
| 184 | Py_DECREF(self->statement_cache); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 185 | self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 186 | Py_DECREF(self); |
| 187 | self->statement_cache->decref_factory = 0; |
| 188 | } |
| 189 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 190 | /* action in (ACTION_RESET, ACTION_FINALIZE) */ |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 191 | void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 192 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 193 | int i; |
| 194 | PyObject* weakref; |
| 195 | PyObject* statement; |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 196 | pysqlite_Cursor* cursor; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 197 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 198 | for (i = 0; i < PyList_Size(self->statements); i++) { |
| 199 | weakref = PyList_GetItem(self->statements, i); |
| 200 | statement = PyWeakref_GetObject(weakref); |
| 201 | if (statement != Py_None) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 202 | if (action == ACTION_RESET) { |
| 203 | (void)pysqlite_statement_reset((pysqlite_Statement*)statement); |
| 204 | } else { |
| 205 | (void)pysqlite_statement_finalize((pysqlite_Statement*)statement); |
| 206 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 207 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 208 | } |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 209 | |
| 210 | if (reset_cursors) { |
| 211 | for (i = 0; i < PyList_Size(self->cursors); i++) { |
| 212 | weakref = PyList_GetItem(self->cursors, i); |
| 213 | cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref); |
| 214 | if ((PyObject*)cursor != Py_None) { |
| 215 | cursor->reset = 1; |
| 216 | } |
| 217 | } |
| 218 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 221 | void pysqlite_connection_dealloc(pysqlite_Connection* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 222 | { |
| 223 | Py_XDECREF(self->statement_cache); |
| 224 | |
| 225 | /* Clean up if user has not called .close() explicitly. */ |
| 226 | if (self->db) { |
| 227 | Py_BEGIN_ALLOW_THREADS |
| 228 | sqlite3_close(self->db); |
| 229 | Py_END_ALLOW_THREADS |
| 230 | } |
| 231 | |
| 232 | if (self->begin_statement) { |
| 233 | PyMem_Free(self->begin_statement); |
| 234 | } |
| 235 | Py_XDECREF(self->isolation_level); |
| 236 | Py_XDECREF(self->function_pinboard); |
| 237 | Py_XDECREF(self->row_factory); |
| 238 | Py_XDECREF(self->text_factory); |
| 239 | Py_XDECREF(self->collations); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 240 | Py_XDECREF(self->statements); |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 241 | Py_XDECREF(self->cursors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 242 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 243 | Py_TYPE(self)->tp_free((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 246 | /* |
| 247 | * Registers a cursor with the connection. |
| 248 | * |
| 249 | * 0 => error; 1 => ok |
| 250 | */ |
| 251 | int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor) |
| 252 | { |
| 253 | PyObject* weakref; |
| 254 | |
| 255 | weakref = PyWeakref_NewRef((PyObject*)cursor, NULL); |
| 256 | if (!weakref) { |
| 257 | goto error; |
| 258 | } |
| 259 | |
| 260 | if (PyList_Append(connection->cursors, weakref) != 0) { |
| 261 | Py_CLEAR(weakref); |
| 262 | goto error; |
| 263 | } |
| 264 | |
| 265 | Py_DECREF(weakref); |
| 266 | |
| 267 | return 1; |
| 268 | error: |
| 269 | return 0; |
| 270 | } |
| 271 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 272 | PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 273 | { |
| 274 | static char *kwlist[] = {"factory", NULL, NULL}; |
| 275 | PyObject* factory = NULL; |
| 276 | PyObject* cursor; |
| 277 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 278 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, |
| 279 | &factory)) { |
| 280 | return NULL; |
| 281 | } |
| 282 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 283 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | if (factory == NULL) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 288 | factory = (PyObject*)&pysqlite_CursorType; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | cursor = PyObject_CallFunction(factory, "O", self); |
| 292 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 293 | _pysqlite_drop_unused_cursor_references(self); |
| 294 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 295 | if (cursor && self->row_factory != Py_None) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 296 | Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 297 | Py_INCREF(self->row_factory); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 298 | ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | return cursor; |
| 302 | } |
| 303 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 304 | PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 305 | { |
| 306 | int rc; |
| 307 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 308 | if (!pysqlite_check_thread(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 309 | return NULL; |
| 310 | } |
| 311 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 312 | pysqlite_do_all_statements(self, ACTION_FINALIZE, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 313 | |
| 314 | if (self->db) { |
| 315 | Py_BEGIN_ALLOW_THREADS |
| 316 | rc = sqlite3_close(self->db); |
| 317 | Py_END_ALLOW_THREADS |
| 318 | |
| 319 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 320 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 321 | return NULL; |
| 322 | } else { |
| 323 | self->db = NULL; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | Py_INCREF(Py_None); |
| 328 | return Py_None; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Checks if a connection object is usable (i. e. not closed). |
| 333 | * |
| 334 | * 0 => error; 1 => ok |
| 335 | */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 336 | int pysqlite_check_connection(pysqlite_Connection* con) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 337 | { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 338 | if (!con->initialized) { |
| 339 | PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called."); |
| 340 | return 0; |
| 341 | } |
| 342 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | if (!con->db) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 344 | PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 345 | return 0; |
| 346 | } else { |
| 347 | return 1; |
| 348 | } |
| 349 | } |
| 350 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 351 | PyObject* _pysqlite_connection_begin(pysqlite_Connection* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 352 | { |
| 353 | int rc; |
| 354 | const char* tail; |
| 355 | sqlite3_stmt* statement; |
| 356 | |
| 357 | Py_BEGIN_ALLOW_THREADS |
| 358 | rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail); |
| 359 | Py_END_ALLOW_THREADS |
| 360 | |
| 361 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 362 | _pysqlite_seterror(self->db, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 363 | goto error; |
| 364 | } |
| 365 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 366 | rc = pysqlite_step(statement, self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 367 | if (rc == SQLITE_DONE) { |
| 368 | self->inTransaction = 1; |
| 369 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 370 | _pysqlite_seterror(self->db, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | Py_BEGIN_ALLOW_THREADS |
| 374 | rc = sqlite3_finalize(statement); |
| 375 | Py_END_ALLOW_THREADS |
| 376 | |
| 377 | if (rc != SQLITE_OK && !PyErr_Occurred()) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 378 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | error: |
| 382 | if (PyErr_Occurred()) { |
| 383 | return NULL; |
| 384 | } else { |
| 385 | Py_INCREF(Py_None); |
| 386 | return Py_None; |
| 387 | } |
| 388 | } |
| 389 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 390 | PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 391 | { |
| 392 | int rc; |
| 393 | const char* tail; |
| 394 | sqlite3_stmt* statement; |
| 395 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 396 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 397 | return NULL; |
| 398 | } |
| 399 | |
| 400 | if (self->inTransaction) { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 401 | pysqlite_do_all_statements(self, ACTION_RESET, 0); |
| 402 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 403 | Py_BEGIN_ALLOW_THREADS |
| 404 | rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail); |
| 405 | Py_END_ALLOW_THREADS |
| 406 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 407 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 408 | goto error; |
| 409 | } |
| 410 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 411 | rc = pysqlite_step(statement, self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 412 | if (rc == SQLITE_DONE) { |
| 413 | self->inTransaction = 0; |
| 414 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 415 | _pysqlite_seterror(self->db, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | Py_BEGIN_ALLOW_THREADS |
| 419 | rc = sqlite3_finalize(statement); |
| 420 | Py_END_ALLOW_THREADS |
| 421 | if (rc != SQLITE_OK && !PyErr_Occurred()) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 422 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | } |
| 426 | |
| 427 | error: |
| 428 | if (PyErr_Occurred()) { |
| 429 | return NULL; |
| 430 | } else { |
| 431 | Py_INCREF(Py_None); |
| 432 | return Py_None; |
| 433 | } |
| 434 | } |
| 435 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 436 | PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 437 | { |
| 438 | int rc; |
| 439 | const char* tail; |
| 440 | sqlite3_stmt* statement; |
| 441 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 442 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | if (self->inTransaction) { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 447 | pysqlite_do_all_statements(self, ACTION_RESET, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 448 | |
| 449 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 0eaa940 | 2007-08-11 15:39:18 +0000 | [diff] [blame] | 450 | rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 451 | Py_END_ALLOW_THREADS |
| 452 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 453 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 454 | goto error; |
| 455 | } |
| 456 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 457 | rc = pysqlite_step(statement, self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 458 | if (rc == SQLITE_DONE) { |
| 459 | self->inTransaction = 0; |
| 460 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 461 | _pysqlite_seterror(self->db, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | Py_BEGIN_ALLOW_THREADS |
| 465 | rc = sqlite3_finalize(statement); |
| 466 | Py_END_ALLOW_THREADS |
| 467 | if (rc != SQLITE_OK && !PyErr_Occurred()) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 468 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | } |
| 472 | |
| 473 | error: |
| 474 | if (PyErr_Occurred()) { |
| 475 | return NULL; |
| 476 | } else { |
| 477 | Py_INCREF(Py_None); |
| 478 | return Py_None; |
| 479 | } |
| 480 | } |
| 481 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 482 | void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 483 | { |
| 484 | long longval; |
| 485 | const char* buffer; |
| 486 | Py_ssize_t buflen; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 487 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 488 | if ((!py_val) || PyErr_Occurred()) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 489 | sqlite3_result_null(context); |
| 490 | } else if (py_val == Py_None) { |
| 491 | sqlite3_result_null(context); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 492 | } else if (PyLong_Check(py_val)) { |
| 493 | longval = PyLong_AsLong(py_val); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 494 | sqlite3_result_int64(context, (PY_LONG_LONG)longval); |
| 495 | } else if (PyFloat_Check(py_val)) { |
| 496 | sqlite3_result_double(context, PyFloat_AsDouble(py_val)); |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 497 | } else if (PyUnicode_Check(py_val)) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 498 | sqlite3_result_text(context, _PyUnicode_AsString(py_val), -1, SQLITE_TRANSIENT); |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 499 | } else if (PyObject_CheckBuffer(py_val)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 500 | if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) { |
| 501 | PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 502 | } else { |
| 503 | sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 504 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 505 | } else { |
| 506 | /* TODO: raise error */ |
| 507 | } |
| 508 | } |
| 509 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 510 | PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 511 | { |
| 512 | PyObject* args; |
| 513 | int i; |
| 514 | sqlite3_value* cur_value; |
| 515 | PyObject* cur_py_value; |
| 516 | const char* val_str; |
| 517 | PY_LONG_LONG val_int; |
| 518 | Py_ssize_t buflen; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 519 | |
| 520 | args = PyTuple_New(argc); |
| 521 | if (!args) { |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | for (i = 0; i < argc; i++) { |
| 526 | cur_value = argv[i]; |
| 527 | switch (sqlite3_value_type(argv[i])) { |
| 528 | case SQLITE_INTEGER: |
| 529 | val_int = sqlite3_value_int64(cur_value); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 530 | cur_py_value = PyLong_FromLong((long)val_int); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 531 | break; |
| 532 | case SQLITE_FLOAT: |
| 533 | cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value)); |
| 534 | break; |
| 535 | case SQLITE_TEXT: |
| 536 | val_str = (const char*)sqlite3_value_text(cur_value); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 537 | cur_py_value = PyUnicode_FromString(val_str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 538 | /* TODO: have a way to show errors here */ |
| 539 | if (!cur_py_value) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 540 | PyErr_Clear(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 541 | Py_INCREF(Py_None); |
| 542 | cur_py_value = Py_None; |
| 543 | } |
| 544 | break; |
| 545 | case SQLITE_BLOB: |
| 546 | buflen = sqlite3_value_bytes(cur_value); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 547 | cur_py_value = PyBytes_FromStringAndSize( |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 548 | sqlite3_value_blob(cur_value), buflen); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 549 | break; |
| 550 | case SQLITE_NULL: |
| 551 | default: |
| 552 | Py_INCREF(Py_None); |
| 553 | cur_py_value = Py_None; |
| 554 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 555 | |
| 556 | if (!cur_py_value) { |
| 557 | Py_DECREF(args); |
| 558 | return NULL; |
| 559 | } |
| 560 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 561 | PyTuple_SetItem(args, i, cur_py_value); |
| 562 | |
| 563 | } |
| 564 | |
| 565 | return args; |
| 566 | } |
| 567 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 568 | void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 569 | { |
| 570 | PyObject* args; |
| 571 | PyObject* py_func; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 572 | PyObject* py_retval = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 573 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 574 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 575 | PyGILState_STATE threadstate; |
| 576 | |
| 577 | threadstate = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 578 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 579 | |
| 580 | py_func = (PyObject*)sqlite3_user_data(context); |
| 581 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 582 | args = _pysqlite_build_py_params(context, argc, argv); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 583 | if (args) { |
| 584 | py_retval = PyObject_CallObject(py_func, args); |
| 585 | Py_DECREF(args); |
| 586 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 587 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 588 | if (py_retval) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 589 | _pysqlite_set_result(context, py_retval); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 590 | Py_DECREF(py_retval); |
| 591 | } else { |
| 592 | if (_enable_callback_tracebacks) { |
| 593 | PyErr_Print(); |
| 594 | } else { |
| 595 | PyErr_Clear(); |
| 596 | } |
| 597 | _sqlite3_result_error(context, "user-defined function raised exception", -1); |
| 598 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 599 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 600 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 601 | PyGILState_Release(threadstate); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 602 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 605 | static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 606 | { |
| 607 | PyObject* args; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 608 | PyObject* function_result = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 609 | PyObject* aggregate_class; |
| 610 | PyObject** aggregate_instance; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 611 | PyObject* stepmethod = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 612 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 613 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 614 | PyGILState_STATE threadstate; |
| 615 | |
| 616 | threadstate = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 617 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 618 | |
| 619 | aggregate_class = (PyObject*)sqlite3_user_data(context); |
| 620 | |
| 621 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); |
| 622 | |
| 623 | if (*aggregate_instance == 0) { |
| 624 | *aggregate_instance = PyObject_CallFunction(aggregate_class, ""); |
| 625 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 626 | if (PyErr_Occurred()) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 627 | *aggregate_instance = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 628 | if (_enable_callback_tracebacks) { |
| 629 | PyErr_Print(); |
| 630 | } else { |
| 631 | PyErr_Clear(); |
| 632 | } |
| 633 | _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 634 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | stepmethod = PyObject_GetAttrString(*aggregate_instance, "step"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 639 | if (!stepmethod) { |
| 640 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 643 | args = _pysqlite_build_py_params(context, argc, params); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 644 | if (!args) { |
| 645 | goto error; |
| 646 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 647 | |
| 648 | function_result = PyObject_CallObject(stepmethod, args); |
| 649 | Py_DECREF(args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 650 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 651 | if (!function_result) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 652 | if (_enable_callback_tracebacks) { |
| 653 | PyErr_Print(); |
| 654 | } else { |
| 655 | PyErr_Clear(); |
| 656 | } |
| 657 | _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 660 | error: |
| 661 | Py_XDECREF(stepmethod); |
| 662 | Py_XDECREF(function_result); |
| 663 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 664 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 665 | PyGILState_Release(threadstate); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 666 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 669 | void _pysqlite_final_callback(sqlite3_context* context) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 670 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 671 | PyObject* function_result = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 672 | PyObject** aggregate_instance; |
| 673 | PyObject* aggregate_class; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 674 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 675 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 676 | PyGILState_STATE threadstate; |
| 677 | |
| 678 | threadstate = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 679 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 680 | |
| 681 | aggregate_class = (PyObject*)sqlite3_user_data(context); |
| 682 | |
| 683 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); |
| 684 | if (!*aggregate_instance) { |
| 685 | /* this branch is executed if there was an exception in the aggregate's |
| 686 | * __init__ */ |
| 687 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 688 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 691 | function_result = PyObject_CallMethod(*aggregate_instance, "finalize", ""); |
| 692 | if (!function_result) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 693 | if (_enable_callback_tracebacks) { |
| 694 | PyErr_Print(); |
| 695 | } else { |
| 696 | PyErr_Clear(); |
| 697 | } |
| 698 | _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1); |
| 699 | } else { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 700 | _pysqlite_set_result(context, function_result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 703 | error: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 704 | Py_XDECREF(*aggregate_instance); |
| 705 | Py_XDECREF(function_result); |
| 706 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 707 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 708 | PyGILState_Release(threadstate); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 709 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 712 | static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 713 | { |
| 714 | PyObject* new_list; |
| 715 | PyObject* weakref; |
| 716 | int i; |
| 717 | |
| 718 | /* we only need to do this once in a while */ |
| 719 | if (self->created_statements++ < 200) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | self->created_statements = 0; |
| 724 | |
| 725 | new_list = PyList_New(0); |
| 726 | if (!new_list) { |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | for (i = 0; i < PyList_Size(self->statements); i++) { |
| 731 | weakref = PyList_GetItem(self->statements, i); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 732 | if (PyWeakref_GetObject(weakref) != Py_None) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 733 | if (PyList_Append(new_list, weakref) != 0) { |
| 734 | Py_DECREF(new_list); |
| 735 | return; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | Py_DECREF(self->statements); |
| 741 | self->statements = new_list; |
| 742 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 743 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 744 | static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self) |
| 745 | { |
| 746 | PyObject* new_list; |
| 747 | PyObject* weakref; |
| 748 | int i; |
| 749 | |
| 750 | /* we only need to do this once in a while */ |
| 751 | if (self->created_cursors++ < 200) { |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | self->created_cursors = 0; |
| 756 | |
| 757 | new_list = PyList_New(0); |
| 758 | if (!new_list) { |
| 759 | return; |
| 760 | } |
| 761 | |
| 762 | for (i = 0; i < PyList_Size(self->cursors); i++) { |
| 763 | weakref = PyList_GetItem(self->cursors, i); |
| 764 | if (PyWeakref_GetObject(weakref) != Py_None) { |
| 765 | if (PyList_Append(new_list, weakref) != 0) { |
| 766 | Py_DECREF(new_list); |
| 767 | return; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | Py_DECREF(self->cursors); |
| 773 | self->cursors = new_list; |
| 774 | } |
| 775 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 776 | PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 777 | { |
| 778 | static char *kwlist[] = {"name", "narg", "func", NULL, NULL}; |
| 779 | |
| 780 | PyObject* func; |
| 781 | char* name; |
| 782 | int narg; |
| 783 | int rc; |
| 784 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 785 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 786 | return NULL; |
| 787 | } |
| 788 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 789 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist, |
| 790 | &name, &narg, &func)) |
| 791 | { |
| 792 | return NULL; |
| 793 | } |
| 794 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 795 | rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 796 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 797 | if (rc != SQLITE_OK) { |
| 798 | /* Workaround for SQLite bug: no error code or string is available here */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 799 | PyErr_SetString(pysqlite_OperationalError, "Error creating function"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 800 | return NULL; |
| 801 | } else { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 802 | if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1) |
| 803 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 804 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 805 | Py_INCREF(Py_None); |
| 806 | return Py_None; |
| 807 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 810 | PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 811 | { |
| 812 | PyObject* aggregate_class; |
| 813 | |
| 814 | int n_arg; |
| 815 | char* name; |
| 816 | static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL }; |
| 817 | int rc; |
| 818 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 819 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 820 | return NULL; |
| 821 | } |
| 822 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 823 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate", |
| 824 | kwlist, &name, &n_arg, &aggregate_class)) { |
| 825 | return NULL; |
| 826 | } |
| 827 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 828 | rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 829 | if (rc != SQLITE_OK) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 830 | /* Workaround for SQLite bug: no error code or string is available here */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 831 | PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 832 | return NULL; |
| 833 | } else { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 834 | if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1) |
| 835 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 836 | |
| 837 | Py_INCREF(Py_None); |
| 838 | return Py_None; |
| 839 | } |
| 840 | } |
| 841 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 842 | static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 843 | { |
| 844 | PyObject *ret; |
| 845 | int rc; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 846 | #ifdef WITH_THREAD |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 847 | PyGILState_STATE gilstate; |
| 848 | |
| 849 | gilstate = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 850 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 851 | ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source); |
| 852 | |
| 853 | if (!ret) { |
| 854 | if (_enable_callback_tracebacks) { |
| 855 | PyErr_Print(); |
| 856 | } else { |
| 857 | PyErr_Clear(); |
| 858 | } |
| 859 | |
| 860 | rc = SQLITE_DENY; |
| 861 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 862 | if (PyLong_Check(ret)) { |
| 863 | rc = (int)PyLong_AsLong(ret); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 864 | } else { |
| 865 | rc = SQLITE_DENY; |
| 866 | } |
| 867 | Py_DECREF(ret); |
| 868 | } |
| 869 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 870 | #ifdef WITH_THREAD |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 871 | PyGILState_Release(gilstate); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 872 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 873 | return rc; |
| 874 | } |
| 875 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 876 | static int _progress_handler(void* user_arg) |
| 877 | { |
| 878 | int rc; |
| 879 | PyObject *ret; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 880 | #ifdef WITH_THREAD |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 881 | PyGILState_STATE gilstate; |
| 882 | |
| 883 | gilstate = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 884 | #endif |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 885 | ret = PyObject_CallFunction((PyObject*)user_arg, ""); |
| 886 | |
| 887 | if (!ret) { |
| 888 | if (_enable_callback_tracebacks) { |
| 889 | PyErr_Print(); |
| 890 | } else { |
| 891 | PyErr_Clear(); |
| 892 | } |
| 893 | |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 894 | /* abort query if error occurred */ |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 895 | rc = 1; |
| 896 | } else { |
| 897 | rc = (int)PyObject_IsTrue(ret); |
| 898 | Py_DECREF(ret); |
| 899 | } |
| 900 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 901 | #ifdef WITH_THREAD |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 902 | PyGILState_Release(gilstate); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 903 | #endif |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 904 | return rc; |
| 905 | } |
| 906 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 907 | static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 908 | { |
| 909 | PyObject* authorizer_cb; |
| 910 | |
| 911 | static char *kwlist[] = { "authorizer_callback", NULL }; |
| 912 | int rc; |
| 913 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 914 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 915 | return NULL; |
| 916 | } |
| 917 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 918 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer", |
| 919 | kwlist, &authorizer_cb)) { |
| 920 | return NULL; |
| 921 | } |
| 922 | |
| 923 | rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb); |
| 924 | |
| 925 | if (rc != SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 926 | PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 927 | return NULL; |
| 928 | } else { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 929 | if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1) |
| 930 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 931 | |
| 932 | Py_INCREF(Py_None); |
| 933 | return Py_None; |
| 934 | } |
| 935 | } |
| 936 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 937 | static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 938 | { |
| 939 | PyObject* progress_handler; |
| 940 | int n; |
| 941 | |
| 942 | static char *kwlist[] = { "progress_handler", "n", NULL }; |
| 943 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 944 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 945 | return NULL; |
| 946 | } |
| 947 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 948 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler", |
| 949 | kwlist, &progress_handler, &n)) { |
| 950 | return NULL; |
| 951 | } |
| 952 | |
| 953 | if (progress_handler == Py_None) { |
| 954 | /* None clears the progress handler previously set */ |
| 955 | sqlite3_progress_handler(self->db, 0, 0, (void*)0); |
| 956 | } else { |
| 957 | sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler); |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 958 | if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1) |
| 959 | return NULL; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | Py_INCREF(Py_None); |
| 963 | return Py_None; |
| 964 | } |
| 965 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 966 | #ifdef HAVE_LOAD_EXTENSION |
| 967 | static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args) |
| 968 | { |
| 969 | int rc; |
| 970 | int onoff; |
| 971 | |
| 972 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 973 | return NULL; |
| 974 | } |
| 975 | |
| 976 | if (!PyArg_ParseTuple(args, "i", &onoff)) { |
| 977 | return NULL; |
| 978 | } |
| 979 | |
| 980 | rc = sqlite3_enable_load_extension(self->db, onoff); |
| 981 | |
| 982 | if (rc != SQLITE_OK) { |
| 983 | PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension"); |
| 984 | return NULL; |
| 985 | } else { |
| 986 | Py_INCREF(Py_None); |
| 987 | return Py_None; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args) |
| 992 | { |
| 993 | int rc; |
| 994 | char* extension_name; |
| 995 | char* errmsg; |
| 996 | |
| 997 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 998 | return NULL; |
| 999 | } |
| 1000 | |
| 1001 | if (!PyArg_ParseTuple(args, "s", &extension_name)) { |
| 1002 | return NULL; |
| 1003 | } |
| 1004 | |
| 1005 | rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg); |
| 1006 | if (rc != 0) { |
| 1007 | PyErr_SetString(pysqlite_OperationalError, errmsg); |
| 1008 | return NULL; |
| 1009 | } else { |
| 1010 | Py_INCREF(Py_None); |
| 1011 | return Py_None; |
| 1012 | } |
| 1013 | } |
| 1014 | #endif |
| 1015 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1016 | int pysqlite_check_thread(pysqlite_Connection* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1017 | { |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1018 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1019 | if (self->check_same_thread) { |
| 1020 | if (PyThread_get_thread_ident() != self->thread_ident) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1021 | PyErr_Format(pysqlite_ProgrammingError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1022 | "SQLite objects created in a thread can only be used in that same thread." |
| 1023 | "The object was created in thread id %ld and this is thread id %ld", |
| 1024 | self->thread_ident, PyThread_get_thread_ident()); |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
| 1028 | } |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1029 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1030 | return 1; |
| 1031 | } |
| 1032 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1033 | static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1034 | { |
| 1035 | Py_INCREF(self->isolation_level); |
| 1036 | return self->isolation_level; |
| 1037 | } |
| 1038 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1039 | static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1040 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1041 | if (!pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1042 | return NULL; |
| 1043 | } else { |
| 1044 | return Py_BuildValue("i", sqlite3_total_changes(self->db)); |
| 1045 | } |
| 1046 | } |
| 1047 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1048 | static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1049 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1050 | PyObject* res; |
| 1051 | PyObject* begin_statement; |
Georg Brandl | ceab610 | 2007-11-25 00:45:05 +0000 | [diff] [blame] | 1052 | static PyObject* begin_word; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1053 | |
| 1054 | Py_XDECREF(self->isolation_level); |
| 1055 | |
| 1056 | if (self->begin_statement) { |
| 1057 | PyMem_Free(self->begin_statement); |
| 1058 | self->begin_statement = NULL; |
| 1059 | } |
| 1060 | |
| 1061 | if (isolation_level == Py_None) { |
| 1062 | Py_INCREF(Py_None); |
| 1063 | self->isolation_level = Py_None; |
| 1064 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1065 | res = pysqlite_connection_commit(self, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1066 | if (!res) { |
| 1067 | return -1; |
| 1068 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1069 | Py_DECREF(res); |
| 1070 | |
| 1071 | self->inTransaction = 0; |
| 1072 | } else { |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 1073 | const char *statement; |
| 1074 | Py_ssize_t size; |
| 1075 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1076 | Py_INCREF(isolation_level); |
| 1077 | self->isolation_level = isolation_level; |
| 1078 | |
Georg Brandl | ceab610 | 2007-11-25 00:45:05 +0000 | [diff] [blame] | 1079 | if (!begin_word) { |
| 1080 | begin_word = PyUnicode_FromString("BEGIN "); |
| 1081 | if (!begin_word) return -1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1082 | } |
Georg Brandl | ceab610 | 2007-11-25 00:45:05 +0000 | [diff] [blame] | 1083 | begin_statement = PyUnicode_Concat(begin_word, isolation_level); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1084 | if (!begin_statement) { |
| 1085 | return -1; |
| 1086 | } |
| 1087 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1088 | statement = _PyUnicode_AsStringAndSize(begin_statement, &size); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1089 | if (!statement) { |
Victor Stinner | ff27d6b | 2010-03-13 00:57:22 +0000 | [diff] [blame] | 1090 | Py_DECREF(begin_statement); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1091 | return -1; |
| 1092 | } |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 1093 | self->begin_statement = PyMem_Malloc(size + 2); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1094 | if (!self->begin_statement) { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1095 | Py_DECREF(begin_statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1096 | return -1; |
| 1097 | } |
| 1098 | |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 1099 | strcpy(self->begin_statement, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1100 | Py_DECREF(begin_statement); |
| 1101 | } |
| 1102 | |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1106 | PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1107 | { |
| 1108 | PyObject* sql; |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1109 | pysqlite_Statement* statement; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1110 | PyObject* weakref; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1111 | int rc; |
| 1112 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1113 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 1114 | return NULL; |
| 1115 | } |
| 1116 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1117 | if (!PyArg_ParseTuple(args, "O", &sql)) { |
| 1118 | return NULL; |
| 1119 | } |
| 1120 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1121 | _pysqlite_drop_unused_statement_references(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1122 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1123 | statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1124 | if (!statement) { |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | |
Victor Stinner | 0201f44 | 2010-03-13 03:28:34 +0000 | [diff] [blame] | 1128 | statement->db = NULL; |
| 1129 | statement->st = NULL; |
| 1130 | statement->sql = NULL; |
| 1131 | statement->in_use = 0; |
| 1132 | statement->in_weakreflist = NULL; |
| 1133 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1134 | rc = pysqlite_statement_create(statement, self, sql); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1135 | |
| 1136 | if (rc != SQLITE_OK) { |
| 1137 | if (rc == PYSQLITE_TOO_MUCH_SQL) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1138 | PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1139 | } else if (rc == PYSQLITE_SQL_WRONG_TYPE) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1140 | PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1141 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1142 | (void)pysqlite_statement_reset(statement); |
| 1143 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1146 | Py_CLEAR(statement); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1147 | } else { |
| 1148 | weakref = PyWeakref_NewRef((PyObject*)statement, NULL); |
| 1149 | if (!weakref) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1150 | Py_CLEAR(statement); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1151 | goto error; |
| 1152 | } |
| 1153 | |
| 1154 | if (PyList_Append(self->statements, weakref) != 0) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1155 | Py_CLEAR(weakref); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1156 | goto error; |
| 1157 | } |
| 1158 | |
| 1159 | Py_DECREF(weakref); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1162 | error: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1163 | return (PyObject*)statement; |
| 1164 | } |
| 1165 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1166 | PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1167 | { |
| 1168 | PyObject* cursor = 0; |
| 1169 | PyObject* result = 0; |
| 1170 | PyObject* method = 0; |
| 1171 | |
| 1172 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 1173 | if (!cursor) { |
| 1174 | goto error; |
| 1175 | } |
| 1176 | |
| 1177 | method = PyObject_GetAttrString(cursor, "execute"); |
| 1178 | if (!method) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1179 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1180 | goto error; |
| 1181 | } |
| 1182 | |
| 1183 | result = PyObject_CallObject(method, args); |
| 1184 | if (!result) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1185 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | error: |
| 1189 | Py_XDECREF(result); |
| 1190 | Py_XDECREF(method); |
| 1191 | |
| 1192 | return cursor; |
| 1193 | } |
| 1194 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1195 | PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1196 | { |
| 1197 | PyObject* cursor = 0; |
| 1198 | PyObject* result = 0; |
| 1199 | PyObject* method = 0; |
| 1200 | |
| 1201 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 1202 | if (!cursor) { |
| 1203 | goto error; |
| 1204 | } |
| 1205 | |
| 1206 | method = PyObject_GetAttrString(cursor, "executemany"); |
| 1207 | if (!method) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1208 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1209 | goto error; |
| 1210 | } |
| 1211 | |
| 1212 | result = PyObject_CallObject(method, args); |
| 1213 | if (!result) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1214 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | error: |
| 1218 | Py_XDECREF(result); |
| 1219 | Py_XDECREF(method); |
| 1220 | |
| 1221 | return cursor; |
| 1222 | } |
| 1223 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1224 | PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1225 | { |
| 1226 | PyObject* cursor = 0; |
| 1227 | PyObject* result = 0; |
| 1228 | PyObject* method = 0; |
| 1229 | |
| 1230 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 1231 | if (!cursor) { |
| 1232 | goto error; |
| 1233 | } |
| 1234 | |
| 1235 | method = PyObject_GetAttrString(cursor, "executescript"); |
| 1236 | if (!method) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1237 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1238 | goto error; |
| 1239 | } |
| 1240 | |
| 1241 | result = PyObject_CallObject(method, args); |
| 1242 | if (!result) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1243 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | error: |
| 1247 | Py_XDECREF(result); |
| 1248 | Py_XDECREF(method); |
| 1249 | |
| 1250 | return cursor; |
| 1251 | } |
| 1252 | |
| 1253 | /* ------------------------- COLLATION CODE ------------------------ */ |
| 1254 | |
| 1255 | static int |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1256 | pysqlite_collation_callback( |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1257 | void* context, |
| 1258 | int text1_length, const void* text1_data, |
| 1259 | int text2_length, const void* text2_data) |
| 1260 | { |
| 1261 | PyObject* callback = (PyObject*)context; |
| 1262 | PyObject* string1 = 0; |
| 1263 | PyObject* string2 = 0; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1264 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1265 | PyGILState_STATE gilstate; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1266 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1267 | PyObject* retval = NULL; |
| 1268 | int result = 0; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1269 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1270 | gilstate = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1271 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (PyErr_Occurred()) { |
| 1274 | goto finally; |
| 1275 | } |
| 1276 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1277 | string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length); |
| 1278 | string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1279 | |
| 1280 | if (!string1 || !string2) { |
| 1281 | goto finally; /* failed to allocate strings */ |
| 1282 | } |
| 1283 | |
| 1284 | retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL); |
| 1285 | |
| 1286 | if (!retval) { |
| 1287 | /* execution failed */ |
| 1288 | goto finally; |
| 1289 | } |
| 1290 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1291 | result = PyLong_AsLong(retval); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1292 | if (PyErr_Occurred()) { |
| 1293 | result = 0; |
| 1294 | } |
| 1295 | |
| 1296 | finally: |
| 1297 | Py_XDECREF(string1); |
| 1298 | Py_XDECREF(string2); |
| 1299 | Py_XDECREF(retval); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1300 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1301 | PyGILState_Release(gilstate); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1302 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1303 | return result; |
| 1304 | } |
| 1305 | |
| 1306 | static PyObject * |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1307 | pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1308 | { |
| 1309 | PyObject* retval = NULL; |
| 1310 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1311 | if (!pysqlite_check_connection(self)) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1312 | goto finally; |
| 1313 | } |
| 1314 | |
| 1315 | sqlite3_interrupt(self->db); |
| 1316 | |
| 1317 | Py_INCREF(Py_None); |
| 1318 | retval = Py_None; |
| 1319 | |
| 1320 | finally: |
| 1321 | return retval; |
| 1322 | } |
| 1323 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1324 | /* Function author: Paul Kippes <kippesp@gmail.com> |
| 1325 | * Class method of Connection to call the Python function _iterdump |
| 1326 | * of the sqlite3 module. |
| 1327 | */ |
| 1328 | static PyObject * |
| 1329 | pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args) |
| 1330 | { |
| 1331 | PyObject* retval = NULL; |
| 1332 | PyObject* module = NULL; |
| 1333 | PyObject* module_dict; |
| 1334 | PyObject* pyfn_iterdump; |
| 1335 | |
| 1336 | if (!pysqlite_check_connection(self)) { |
| 1337 | goto finally; |
| 1338 | } |
| 1339 | |
| 1340 | module = PyImport_ImportModule(MODULE_NAME ".dump"); |
| 1341 | if (!module) { |
| 1342 | goto finally; |
| 1343 | } |
| 1344 | |
| 1345 | module_dict = PyModule_GetDict(module); |
| 1346 | if (!module_dict) { |
| 1347 | goto finally; |
| 1348 | } |
| 1349 | |
| 1350 | pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump"); |
| 1351 | if (!pyfn_iterdump) { |
| 1352 | PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference"); |
| 1353 | goto finally; |
| 1354 | } |
| 1355 | |
| 1356 | args = PyTuple_New(1); |
| 1357 | if (!args) { |
| 1358 | goto finally; |
| 1359 | } |
| 1360 | Py_INCREF(self); |
| 1361 | PyTuple_SetItem(args, 0, (PyObject*)self); |
| 1362 | retval = PyObject_CallObject(pyfn_iterdump, args); |
| 1363 | |
| 1364 | finally: |
| 1365 | Py_XDECREF(args); |
| 1366 | Py_XDECREF(module); |
| 1367 | return retval; |
| 1368 | } |
| 1369 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1370 | static PyObject * |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1371 | pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1372 | { |
| 1373 | PyObject* callable; |
| 1374 | PyObject* uppercase_name = 0; |
| 1375 | PyObject* name; |
| 1376 | PyObject* retval; |
| 1377 | char* chk; |
| 1378 | int rc; |
| 1379 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1380 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1381 | goto finally; |
| 1382 | } |
| 1383 | |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 1384 | if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1385 | goto finally; |
| 1386 | } |
| 1387 | |
| 1388 | uppercase_name = PyObject_CallMethod(name, "upper", ""); |
| 1389 | if (!uppercase_name) { |
| 1390 | goto finally; |
| 1391 | } |
| 1392 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1393 | chk = _PyUnicode_AsString(uppercase_name); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1394 | while (*chk) { |
| 1395 | if ((*chk >= '0' && *chk <= '9') |
| 1396 | || (*chk >= 'A' && *chk <= 'Z') |
| 1397 | || (*chk == '_')) |
| 1398 | { |
| 1399 | chk++; |
| 1400 | } else { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1401 | PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1402 | goto finally; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | if (callable != Py_None && !PyCallable_Check(callable)) { |
| 1407 | PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 1408 | goto finally; |
| 1409 | } |
| 1410 | |
| 1411 | if (callable != Py_None) { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1412 | if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1) |
| 1413 | goto finally; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1414 | } else { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1415 | if (PyDict_DelItem(self->collations, uppercase_name) == -1) |
| 1416 | goto finally; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | rc = sqlite3_create_collation(self->db, |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1420 | _PyUnicode_AsString(uppercase_name), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1421 | SQLITE_UTF8, |
| 1422 | (callable != Py_None) ? callable : NULL, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1423 | (callable != Py_None) ? pysqlite_collation_callback : NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1424 | if (rc != SQLITE_OK) { |
| 1425 | PyDict_DelItem(self->collations, uppercase_name); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1426 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1427 | goto finally; |
| 1428 | } |
| 1429 | |
| 1430 | finally: |
| 1431 | Py_XDECREF(uppercase_name); |
| 1432 | |
| 1433 | if (PyErr_Occurred()) { |
| 1434 | retval = NULL; |
| 1435 | } else { |
| 1436 | Py_INCREF(Py_None); |
| 1437 | retval = Py_None; |
| 1438 | } |
| 1439 | |
| 1440 | return retval; |
| 1441 | } |
| 1442 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1443 | /* Called when the connection is used as a context manager. Returns itself as a |
| 1444 | * convenience to the caller. */ |
| 1445 | static PyObject * |
| 1446 | pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args) |
| 1447 | { |
| 1448 | Py_INCREF(self); |
| 1449 | return (PyObject*)self; |
| 1450 | } |
| 1451 | |
| 1452 | /** Called when the connection is used as a context manager. If there was any |
| 1453 | * exception, a rollback takes place; otherwise we commit. */ |
| 1454 | static PyObject * |
| 1455 | pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args) |
| 1456 | { |
| 1457 | PyObject* exc_type, *exc_value, *exc_tb; |
| 1458 | char* method_name; |
| 1459 | PyObject* result; |
| 1460 | |
| 1461 | if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) { |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | |
| 1465 | if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) { |
| 1466 | method_name = "commit"; |
| 1467 | } else { |
| 1468 | method_name = "rollback"; |
| 1469 | } |
| 1470 | |
| 1471 | result = PyObject_CallMethod((PyObject*)self, method_name, ""); |
| 1472 | if (!result) { |
| 1473 | return NULL; |
| 1474 | } |
| 1475 | Py_DECREF(result); |
| 1476 | |
| 1477 | Py_RETURN_FALSE; |
| 1478 | } |
| 1479 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1480 | static char connection_doc[] = |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1481 | PyDoc_STR("SQLite database connection object."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1482 | |
| 1483 | static PyGetSetDef connection_getset[] = { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1484 | {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level}, |
| 1485 | {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1486 | {NULL} |
| 1487 | }; |
| 1488 | |
| 1489 | static PyMethodDef connection_methods[] = { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1490 | {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1491 | PyDoc_STR("Return a cursor for the connection.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1492 | {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1493 | PyDoc_STR("Closes the connection.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1494 | {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1495 | PyDoc_STR("Commit the current transaction.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1496 | {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1497 | PyDoc_STR("Roll back the current transaction.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1498 | {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1499 | PyDoc_STR("Creates a new function. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1500 | {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1501 | PyDoc_STR("Creates a new aggregate. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1502 | {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1503 | PyDoc_STR("Sets authorizer callback. Non-standard.")}, |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1504 | #ifdef HAVE_LOAD_EXTENSION |
| 1505 | {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS, |
| 1506 | PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")}, |
| 1507 | {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS, |
| 1508 | PyDoc_STR("Load SQLite extension module. Non-standard.")}, |
| 1509 | #endif |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1510 | {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS, |
| 1511 | PyDoc_STR("Sets progress handler callback. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1512 | {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1513 | PyDoc_STR("Executes a SQL statement. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1514 | {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1515 | PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1516 | {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1517 | PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1518 | {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1519 | PyDoc_STR("Creates a collation function. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1520 | {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1521 | PyDoc_STR("Abort any pending database operation. Non-standard.")}, |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1522 | {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS, |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 1523 | PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")}, |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1524 | {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS, |
| 1525 | PyDoc_STR("For context manager. Non-standard.")}, |
| 1526 | {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS, |
| 1527 | PyDoc_STR("For context manager. Non-standard.")}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1528 | {NULL, NULL} |
| 1529 | }; |
| 1530 | |
| 1531 | static struct PyMemberDef connection_members[] = |
| 1532 | { |
Guido van Rossum | 10f07c4 | 2007-08-11 15:32:55 +0000 | [diff] [blame] | 1533 | {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY}, |
| 1534 | {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY}, |
| 1535 | {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY}, |
| 1536 | {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY}, |
| 1537 | {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY}, |
| 1538 | {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY}, |
| 1539 | {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY}, |
| 1540 | {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY}, |
| 1541 | {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY}, |
| 1542 | {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1543 | {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)}, |
| 1544 | {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1545 | {NULL} |
| 1546 | }; |
| 1547 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1548 | PyTypeObject pysqlite_ConnectionType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1549 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1550 | MODULE_NAME ".Connection", /* tp_name */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1551 | sizeof(pysqlite_Connection), /* tp_basicsize */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1552 | 0, /* tp_itemsize */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1553 | (destructor)pysqlite_connection_dealloc, /* tp_dealloc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1554 | 0, /* tp_print */ |
| 1555 | 0, /* tp_getattr */ |
| 1556 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1557 | 0, /* tp_reserved */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1558 | 0, /* tp_repr */ |
| 1559 | 0, /* tp_as_number */ |
| 1560 | 0, /* tp_as_sequence */ |
| 1561 | 0, /* tp_as_mapping */ |
| 1562 | 0, /* tp_hash */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1563 | (ternaryfunc)pysqlite_connection_call, /* tp_call */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1564 | 0, /* tp_str */ |
| 1565 | 0, /* tp_getattro */ |
| 1566 | 0, /* tp_setattro */ |
| 1567 | 0, /* tp_as_buffer */ |
| 1568 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1569 | connection_doc, /* tp_doc */ |
| 1570 | 0, /* tp_traverse */ |
| 1571 | 0, /* tp_clear */ |
| 1572 | 0, /* tp_richcompare */ |
| 1573 | 0, /* tp_weaklistoffset */ |
| 1574 | 0, /* tp_iter */ |
| 1575 | 0, /* tp_iternext */ |
| 1576 | connection_methods, /* tp_methods */ |
| 1577 | connection_members, /* tp_members */ |
| 1578 | connection_getset, /* tp_getset */ |
| 1579 | 0, /* tp_base */ |
| 1580 | 0, /* tp_dict */ |
| 1581 | 0, /* tp_descr_get */ |
| 1582 | 0, /* tp_descr_set */ |
| 1583 | 0, /* tp_dictoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1584 | (initproc)pysqlite_connection_init, /* tp_init */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1585 | 0, /* tp_alloc */ |
| 1586 | 0, /* tp_new */ |
| 1587 | 0 /* tp_free */ |
| 1588 | }; |
| 1589 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1590 | extern int pysqlite_connection_setup_types(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1591 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1592 | pysqlite_ConnectionType.tp_new = PyType_GenericNew; |
| 1593 | return PyType_Ready(&pysqlite_ConnectionType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1594 | } |