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