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