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