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