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