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