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