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