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