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