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