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 | |
Oren Milman | 93c5a5d | 2017-10-10 22:27:46 +0300 | [diff] [blame] | 189 | Py_XSETREF(self->function_pinboard, PyDict_New()); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 190 | if (!self->function_pinboard) { |
| 191 | return -1; |
| 192 | } |
| 193 | |
Oren Milman | 93c5a5d | 2017-10-10 22:27:46 +0300 | [diff] [blame] | 194 | Py_XSETREF(self->collations, PyDict_New()); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 195 | if (!self->collations) { |
| 196 | return -1; |
| 197 | } |
| 198 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 199 | self->Warning = pysqlite_Warning; |
| 200 | self->Error = pysqlite_Error; |
| 201 | self->InterfaceError = pysqlite_InterfaceError; |
| 202 | self->DatabaseError = pysqlite_DatabaseError; |
| 203 | self->DataError = pysqlite_DataError; |
| 204 | self->OperationalError = pysqlite_OperationalError; |
| 205 | self->IntegrityError = pysqlite_IntegrityError; |
| 206 | self->InternalError = pysqlite_InternalError; |
| 207 | self->ProgrammingError = pysqlite_ProgrammingError; |
| 208 | self->NotSupportedError = pysqlite_NotSupportedError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 213 | /* action in (ACTION_RESET, ACTION_FINALIZE) */ |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 214 | 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] | 215 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 216 | int i; |
| 217 | PyObject* weakref; |
| 218 | PyObject* statement; |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 219 | pysqlite_Cursor* cursor; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 220 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 221 | for (i = 0; i < PyList_Size(self->statements); i++) { |
| 222 | weakref = PyList_GetItem(self->statements, i); |
| 223 | statement = PyWeakref_GetObject(weakref); |
| 224 | if (statement != Py_None) { |
Benjamin Peterson | 5c2b09e | 2011-05-31 21:31:37 -0500 | [diff] [blame] | 225 | Py_INCREF(statement); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 226 | if (action == ACTION_RESET) { |
| 227 | (void)pysqlite_statement_reset((pysqlite_Statement*)statement); |
| 228 | } else { |
| 229 | (void)pysqlite_statement_finalize((pysqlite_Statement*)statement); |
| 230 | } |
Benjamin Peterson | 5c2b09e | 2011-05-31 21:31:37 -0500 | [diff] [blame] | 231 | Py_DECREF(statement); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 232 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 233 | } |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 234 | |
| 235 | if (reset_cursors) { |
| 236 | for (i = 0; i < PyList_Size(self->cursors); i++) { |
| 237 | weakref = PyList_GetItem(self->cursors, i); |
| 238 | cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref); |
| 239 | if ((PyObject*)cursor != Py_None) { |
| 240 | cursor->reset = 1; |
| 241 | } |
| 242 | } |
| 243 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 246 | void pysqlite_connection_dealloc(pysqlite_Connection* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 247 | { |
| 248 | Py_XDECREF(self->statement_cache); |
| 249 | |
| 250 | /* Clean up if user has not called .close() explicitly. */ |
| 251 | if (self->db) { |
| 252 | Py_BEGIN_ALLOW_THREADS |
Aviv Palivoda | 86a6705 | 2017-03-03 12:58:17 +0200 | [diff] [blame] | 253 | SQLITE3_CLOSE(self->db); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 254 | Py_END_ALLOW_THREADS |
| 255 | } |
| 256 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 257 | Py_XDECREF(self->isolation_level); |
| 258 | Py_XDECREF(self->function_pinboard); |
| 259 | Py_XDECREF(self->row_factory); |
| 260 | Py_XDECREF(self->text_factory); |
| 261 | Py_XDECREF(self->collations); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 262 | Py_XDECREF(self->statements); |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 263 | Py_XDECREF(self->cursors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 264 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 265 | Py_TYPE(self)->tp_free((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 268 | /* |
| 269 | * Registers a cursor with the connection. |
| 270 | * |
| 271 | * 0 => error; 1 => ok |
| 272 | */ |
| 273 | int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor) |
| 274 | { |
| 275 | PyObject* weakref; |
| 276 | |
| 277 | weakref = PyWeakref_NewRef((PyObject*)cursor, NULL); |
| 278 | if (!weakref) { |
| 279 | goto error; |
| 280 | } |
| 281 | |
| 282 | if (PyList_Append(connection->cursors, weakref) != 0) { |
| 283 | Py_CLEAR(weakref); |
| 284 | goto error; |
| 285 | } |
| 286 | |
| 287 | Py_DECREF(weakref); |
| 288 | |
| 289 | return 1; |
| 290 | error: |
| 291 | return 0; |
| 292 | } |
| 293 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 294 | PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 295 | { |
Serhiy Storchaka | ef113cd | 2016-08-29 14:29:55 +0300 | [diff] [blame] | 296 | static char *kwlist[] = {"factory", NULL}; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 297 | PyObject* factory = NULL; |
| 298 | PyObject* cursor; |
| 299 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 300 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, |
| 301 | &factory)) { |
| 302 | return NULL; |
| 303 | } |
| 304 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 305 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | if (factory == NULL) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 310 | factory = (PyObject*)&pysqlite_CursorType; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Serhiy Storchaka | ef113cd | 2016-08-29 14:29:55 +0300 | [diff] [blame] | 313 | cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL); |
| 314 | if (cursor == NULL) |
| 315 | return NULL; |
| 316 | if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) { |
| 317 | PyErr_Format(PyExc_TypeError, |
| 318 | "factory must return a cursor, not %.100s", |
| 319 | Py_TYPE(cursor)->tp_name); |
| 320 | Py_DECREF(cursor); |
| 321 | return NULL; |
| 322 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 323 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 324 | _pysqlite_drop_unused_cursor_references(self); |
| 325 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 326 | if (cursor && self->row_factory != Py_None) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 327 | Py_INCREF(self->row_factory); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 328 | Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | return cursor; |
| 332 | } |
| 333 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 334 | PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 335 | { |
| 336 | int rc; |
| 337 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 338 | if (!pysqlite_check_thread(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 339 | return NULL; |
| 340 | } |
| 341 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 342 | pysqlite_do_all_statements(self, ACTION_FINALIZE, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | |
| 344 | if (self->db) { |
| 345 | Py_BEGIN_ALLOW_THREADS |
Aviv Palivoda | 86a6705 | 2017-03-03 12:58:17 +0200 | [diff] [blame] | 346 | rc = SQLITE3_CLOSE(self->db); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 347 | Py_END_ALLOW_THREADS |
| 348 | |
| 349 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 350 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 351 | return NULL; |
| 352 | } else { |
| 353 | self->db = NULL; |
| 354 | } |
| 355 | } |
| 356 | |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 357 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Checks if a connection object is usable (i. e. not closed). |
| 362 | * |
| 363 | * 0 => error; 1 => ok |
| 364 | */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 365 | int pysqlite_check_connection(pysqlite_Connection* con) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 366 | { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 367 | if (!con->initialized) { |
| 368 | PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called."); |
| 369 | return 0; |
| 370 | } |
| 371 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 372 | if (!con->db) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 373 | PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 374 | return 0; |
| 375 | } else { |
| 376 | return 1; |
| 377 | } |
| 378 | } |
| 379 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 380 | PyObject* _pysqlite_connection_begin(pysqlite_Connection* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 381 | { |
| 382 | int rc; |
| 383 | const char* tail; |
| 384 | sqlite3_stmt* statement; |
| 385 | |
| 386 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | 5252694 | 2017-09-20 07:36:18 -0700 | [diff] [blame] | 387 | rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement, &tail); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 388 | Py_END_ALLOW_THREADS |
| 389 | |
| 390 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 391 | _pysqlite_seterror(self->db, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 392 | goto error; |
| 393 | } |
| 394 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 395 | rc = pysqlite_step(statement, self); |
Berker Peksag | 59da4b3 | 2016-09-12 07:16:43 +0300 | [diff] [blame] | 396 | if (rc != SQLITE_DONE) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 397 | _pysqlite_seterror(self->db, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | Py_BEGIN_ALLOW_THREADS |
| 401 | rc = sqlite3_finalize(statement); |
| 402 | Py_END_ALLOW_THREADS |
| 403 | |
| 404 | if (rc != SQLITE_OK && !PyErr_Occurred()) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 405 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | error: |
| 409 | if (PyErr_Occurred()) { |
| 410 | return NULL; |
| 411 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 412 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 416 | PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 417 | { |
| 418 | int rc; |
| 419 | const char* tail; |
| 420 | sqlite3_stmt* statement; |
| 421 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 422 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 423 | return NULL; |
| 424 | } |
| 425 | |
Berker Peksag | 59da4b3 | 2016-09-12 07:16:43 +0300 | [diff] [blame] | 426 | if (!sqlite3_get_autocommit(self->db)) { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 427 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 428 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | 5252694 | 2017-09-20 07:36:18 -0700 | [diff] [blame] | 429 | rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, &tail); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 430 | Py_END_ALLOW_THREADS |
| 431 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 432 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 433 | goto error; |
| 434 | } |
| 435 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 436 | rc = pysqlite_step(statement, self); |
Berker Peksag | 59da4b3 | 2016-09-12 07:16:43 +0300 | [diff] [blame] | 437 | if (rc != SQLITE_DONE) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 438 | _pysqlite_seterror(self->db, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | Py_BEGIN_ALLOW_THREADS |
| 442 | rc = sqlite3_finalize(statement); |
| 443 | Py_END_ALLOW_THREADS |
| 444 | if (rc != SQLITE_OK && !PyErr_Occurred()) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 445 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | } |
| 449 | |
| 450 | error: |
| 451 | if (PyErr_Occurred()) { |
| 452 | return NULL; |
| 453 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 454 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 458 | PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 459 | { |
| 460 | int rc; |
| 461 | const char* tail; |
| 462 | sqlite3_stmt* statement; |
| 463 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 464 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
| 467 | |
Berker Peksag | 59da4b3 | 2016-09-12 07:16:43 +0300 | [diff] [blame] | 468 | if (!sqlite3_get_autocommit(self->db)) { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 469 | pysqlite_do_all_statements(self, ACTION_RESET, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 470 | |
| 471 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | 5252694 | 2017-09-20 07:36:18 -0700 | [diff] [blame] | 472 | rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 473 | Py_END_ALLOW_THREADS |
| 474 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 475 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 476 | goto error; |
| 477 | } |
| 478 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 479 | rc = pysqlite_step(statement, self); |
Berker Peksag | 59da4b3 | 2016-09-12 07:16:43 +0300 | [diff] [blame] | 480 | if (rc != SQLITE_DONE) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 481 | _pysqlite_seterror(self->db, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | Py_BEGIN_ALLOW_THREADS |
| 485 | rc = sqlite3_finalize(statement); |
| 486 | Py_END_ALLOW_THREADS |
| 487 | if (rc != SQLITE_OK && !PyErr_Occurred()) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 488 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | } |
| 492 | |
| 493 | error: |
| 494 | if (PyErr_Occurred()) { |
| 495 | return NULL; |
| 496 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 497 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 501 | static int |
| 502 | _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 503 | { |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 504 | if (py_val == Py_None) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 505 | sqlite3_result_null(context); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 506 | } else if (PyLong_Check(py_val)) { |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 507 | sqlite_int64 value = _pysqlite_long_as_int64(py_val); |
| 508 | if (value == -1 && PyErr_Occurred()) |
| 509 | return -1; |
| 510 | sqlite3_result_int64(context, value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 511 | } else if (PyFloat_Check(py_val)) { |
| 512 | sqlite3_result_double(context, PyFloat_AsDouble(py_val)); |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 513 | } else if (PyUnicode_Check(py_val)) { |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 514 | const char *str = PyUnicode_AsUTF8(py_val); |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 515 | if (str == NULL) |
| 516 | return -1; |
| 517 | sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT); |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 518 | } else if (PyObject_CheckBuffer(py_val)) { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 519 | Py_buffer view; |
| 520 | if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) { |
Victor Stinner | 83ed42b | 2013-11-18 01:24:31 +0100 | [diff] [blame] | 521 | PyErr_SetString(PyExc_ValueError, |
| 522 | "could not convert BLOB to buffer"); |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 523 | return -1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 524 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 525 | if (view.len > INT_MAX) { |
Victor Stinner | 83ed42b | 2013-11-18 01:24:31 +0100 | [diff] [blame] | 526 | PyErr_SetString(PyExc_OverflowError, |
| 527 | "BLOB longer than INT_MAX bytes"); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 528 | PyBuffer_Release(&view); |
Victor Stinner | 83ed42b | 2013-11-18 01:24:31 +0100 | [diff] [blame] | 529 | return -1; |
| 530 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 531 | sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT); |
| 532 | PyBuffer_Release(&view); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 533 | } else { |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 534 | return -1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 535 | } |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 536 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 539 | 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] | 540 | { |
| 541 | PyObject* args; |
| 542 | int i; |
| 543 | sqlite3_value* cur_value; |
| 544 | PyObject* cur_py_value; |
| 545 | const char* val_str; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 546 | Py_ssize_t buflen; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 547 | |
| 548 | args = PyTuple_New(argc); |
| 549 | if (!args) { |
| 550 | return NULL; |
| 551 | } |
| 552 | |
| 553 | for (i = 0; i < argc; i++) { |
| 554 | cur_value = argv[i]; |
| 555 | switch (sqlite3_value_type(argv[i])) { |
| 556 | case SQLITE_INTEGER: |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 557 | cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 558 | break; |
| 559 | case SQLITE_FLOAT: |
| 560 | cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value)); |
| 561 | break; |
| 562 | case SQLITE_TEXT: |
| 563 | val_str = (const char*)sqlite3_value_text(cur_value); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 564 | cur_py_value = PyUnicode_FromString(val_str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 565 | /* TODO: have a way to show errors here */ |
| 566 | if (!cur_py_value) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 567 | PyErr_Clear(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 568 | Py_INCREF(Py_None); |
| 569 | cur_py_value = Py_None; |
| 570 | } |
| 571 | break; |
| 572 | case SQLITE_BLOB: |
| 573 | buflen = sqlite3_value_bytes(cur_value); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 574 | cur_py_value = PyBytes_FromStringAndSize( |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 575 | sqlite3_value_blob(cur_value), buflen); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 576 | break; |
| 577 | case SQLITE_NULL: |
| 578 | default: |
| 579 | Py_INCREF(Py_None); |
| 580 | cur_py_value = Py_None; |
| 581 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 582 | |
| 583 | if (!cur_py_value) { |
| 584 | Py_DECREF(args); |
| 585 | return NULL; |
| 586 | } |
| 587 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 588 | PyTuple_SetItem(args, i, cur_py_value); |
| 589 | |
| 590 | } |
| 591 | |
| 592 | return args; |
| 593 | } |
| 594 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 595 | void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 596 | { |
| 597 | PyObject* args; |
| 598 | PyObject* py_func; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 599 | PyObject* py_retval = NULL; |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 600 | int ok; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 601 | |
| 602 | PyGILState_STATE threadstate; |
| 603 | |
| 604 | threadstate = PyGILState_Ensure(); |
| 605 | |
| 606 | py_func = (PyObject*)sqlite3_user_data(context); |
| 607 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 608 | args = _pysqlite_build_py_params(context, argc, argv); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 609 | if (args) { |
| 610 | py_retval = PyObject_CallObject(py_func, args); |
| 611 | Py_DECREF(args); |
| 612 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 613 | |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 614 | ok = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 615 | if (py_retval) { |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 616 | ok = _pysqlite_set_result(context, py_retval) == 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 617 | Py_DECREF(py_retval); |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 618 | } |
| 619 | if (!ok) { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 620 | if (_pysqlite_enable_callback_tracebacks) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 621 | PyErr_Print(); |
| 622 | } else { |
| 623 | PyErr_Clear(); |
| 624 | } |
| 625 | _sqlite3_result_error(context, "user-defined function raised exception", -1); |
| 626 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 627 | |
| 628 | PyGILState_Release(threadstate); |
| 629 | } |
| 630 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 631 | 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] | 632 | { |
| 633 | PyObject* args; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 634 | PyObject* function_result = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 635 | PyObject* aggregate_class; |
| 636 | PyObject** aggregate_instance; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 637 | PyObject* stepmethod = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 638 | |
| 639 | PyGILState_STATE threadstate; |
| 640 | |
| 641 | threadstate = PyGILState_Ensure(); |
| 642 | |
| 643 | aggregate_class = (PyObject*)sqlite3_user_data(context); |
| 644 | |
| 645 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); |
| 646 | |
Serhiy Storchaka | 0b3ec19 | 2017-03-23 17:53:47 +0200 | [diff] [blame] | 647 | if (*aggregate_instance == NULL) { |
Victor Stinner | 070c4d7 | 2016-12-09 12:29:18 +0100 | [diff] [blame] | 648 | *aggregate_instance = _PyObject_CallNoArg(aggregate_class); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 649 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 650 | if (PyErr_Occurred()) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 651 | *aggregate_instance = 0; |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 652 | if (_pysqlite_enable_callback_tracebacks) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 653 | PyErr_Print(); |
| 654 | } else { |
| 655 | PyErr_Clear(); |
| 656 | } |
| 657 | _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] | 658 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | stepmethod = PyObject_GetAttrString(*aggregate_instance, "step"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 663 | if (!stepmethod) { |
| 664 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 667 | args = _pysqlite_build_py_params(context, argc, params); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 668 | if (!args) { |
| 669 | goto error; |
| 670 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 671 | |
| 672 | function_result = PyObject_CallObject(stepmethod, args); |
| 673 | Py_DECREF(args); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 674 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 675 | if (!function_result) { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 676 | if (_pysqlite_enable_callback_tracebacks) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 677 | PyErr_Print(); |
| 678 | } else { |
| 679 | PyErr_Clear(); |
| 680 | } |
| 681 | _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] | 682 | } |
| 683 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 684 | error: |
| 685 | Py_XDECREF(stepmethod); |
| 686 | Py_XDECREF(function_result); |
| 687 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 688 | PyGILState_Release(threadstate); |
| 689 | } |
| 690 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 691 | void _pysqlite_final_callback(sqlite3_context* context) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 692 | { |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 693 | PyObject* function_result; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 694 | PyObject** aggregate_instance; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 695 | _Py_IDENTIFIER(finalize); |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 696 | int ok; |
Victor Stinner | e9af4cf | 2013-07-18 01:42:04 +0200 | [diff] [blame] | 697 | PyObject *exception, *value, *tb; |
Victor Stinner | ffff763 | 2013-08-02 01:48:10 +0200 | [diff] [blame] | 698 | int restore; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 699 | |
| 700 | PyGILState_STATE threadstate; |
| 701 | |
| 702 | threadstate = PyGILState_Ensure(); |
| 703 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 704 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); |
| 705 | if (!*aggregate_instance) { |
| 706 | /* this branch is executed if there was an exception in the aggregate's |
| 707 | * __init__ */ |
| 708 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 709 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Victor Stinner | e9af4cf | 2013-07-18 01:42:04 +0200 | [diff] [blame] | 712 | /* Keep the exception (if any) of the last call to step() */ |
| 713 | PyErr_Fetch(&exception, &value, &tb); |
Victor Stinner | ffff763 | 2013-08-02 01:48:10 +0200 | [diff] [blame] | 714 | restore = 1; |
Victor Stinner | e9af4cf | 2013-07-18 01:42:04 +0200 | [diff] [blame] | 715 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 716 | function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL); |
Victor Stinner | e9af4cf | 2013-07-18 01:42:04 +0200 | [diff] [blame] | 717 | |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 718 | Py_DECREF(*aggregate_instance); |
| 719 | |
| 720 | ok = 0; |
| 721 | if (function_result) { |
| 722 | ok = _pysqlite_set_result(context, function_result) == 0; |
| 723 | Py_DECREF(function_result); |
| 724 | } |
| 725 | if (!ok) { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 726 | if (_pysqlite_enable_callback_tracebacks) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 727 | PyErr_Print(); |
| 728 | } else { |
| 729 | PyErr_Clear(); |
| 730 | } |
| 731 | _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] | 732 | #if SQLITE_VERSION_NUMBER < 3003003 |
| 733 | /* with old SQLite versions, _sqlite3_result_error() sets a new Python |
| 734 | exception, so don't restore the previous exception */ |
| 735 | restore = 0; |
| 736 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Victor Stinner | ffff763 | 2013-08-02 01:48:10 +0200 | [diff] [blame] | 739 | if (restore) { |
| 740 | /* Restore the exception (if any) of the last call to step(), |
| 741 | but clear also the current exception if finalize() failed */ |
| 742 | PyErr_Restore(exception, value, tb); |
| 743 | } |
Victor Stinner | 3a85732 | 2013-07-22 08:34:32 +0200 | [diff] [blame] | 744 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 745 | error: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 746 | PyGILState_Release(threadstate); |
| 747 | } |
| 748 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 749 | static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 750 | { |
| 751 | PyObject* new_list; |
| 752 | PyObject* weakref; |
| 753 | int i; |
| 754 | |
| 755 | /* we only need to do this once in a while */ |
| 756 | if (self->created_statements++ < 200) { |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | self->created_statements = 0; |
| 761 | |
| 762 | new_list = PyList_New(0); |
| 763 | if (!new_list) { |
| 764 | return; |
| 765 | } |
| 766 | |
| 767 | for (i = 0; i < PyList_Size(self->statements); i++) { |
| 768 | weakref = PyList_GetItem(self->statements, i); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 769 | if (PyWeakref_GetObject(weakref) != Py_None) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 770 | if (PyList_Append(new_list, weakref) != 0) { |
| 771 | Py_DECREF(new_list); |
| 772 | return; |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 777 | Py_SETREF(self->statements, new_list); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 778 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 779 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 780 | static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self) |
| 781 | { |
| 782 | PyObject* new_list; |
| 783 | PyObject* weakref; |
| 784 | int i; |
| 785 | |
| 786 | /* we only need to do this once in a while */ |
| 787 | if (self->created_cursors++ < 200) { |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | self->created_cursors = 0; |
| 792 | |
| 793 | new_list = PyList_New(0); |
| 794 | if (!new_list) { |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | for (i = 0; i < PyList_Size(self->cursors); i++) { |
| 799 | weakref = PyList_GetItem(self->cursors, i); |
| 800 | if (PyWeakref_GetObject(weakref) != Py_None) { |
| 801 | if (PyList_Append(new_list, weakref) != 0) { |
| 802 | Py_DECREF(new_list); |
| 803 | return; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 808 | Py_SETREF(self->cursors, new_list); |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 811 | PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 812 | { |
Sergey Fedoseev | 0830858 | 2018-07-08 12:09:20 +0500 | [diff] [blame] | 813 | static char *kwlist[] = {"name", "narg", "func", "deterministic", NULL}; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 814 | |
| 815 | PyObject* func; |
| 816 | char* name; |
| 817 | int narg; |
| 818 | int rc; |
Sergey Fedoseev | 0830858 | 2018-07-08 12:09:20 +0500 | [diff] [blame] | 819 | int deterministic = 0; |
| 820 | int flags = SQLITE_UTF8; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 821 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 822 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 823 | return NULL; |
| 824 | } |
| 825 | |
Sergey Fedoseev | 0830858 | 2018-07-08 12:09:20 +0500 | [diff] [blame] | 826 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|$p", kwlist, |
| 827 | &name, &narg, &func, &deterministic)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 828 | { |
| 829 | return NULL; |
| 830 | } |
| 831 | |
Sergey Fedoseev | 0830858 | 2018-07-08 12:09:20 +0500 | [diff] [blame] | 832 | if (deterministic) { |
| 833 | #if SQLITE_VERSION_NUMBER < 3008003 |
| 834 | PyErr_SetString(pysqlite_NotSupportedError, |
| 835 | "deterministic=True requires SQLite 3.8.3 or higher"); |
| 836 | return NULL; |
| 837 | #else |
| 838 | if (sqlite3_libversion_number() < 3008003) { |
| 839 | PyErr_SetString(pysqlite_NotSupportedError, |
| 840 | "deterministic=True requires SQLite 3.8.3 or higher"); |
| 841 | return NULL; |
| 842 | } |
| 843 | flags |= SQLITE_DETERMINISTIC; |
| 844 | #endif |
| 845 | } |
Sergey Fedoseev | 5b25f1d | 2018-12-05 22:50:26 +0500 | [diff] [blame] | 846 | if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1) { |
| 847 | return NULL; |
| 848 | } |
Sergey Fedoseev | 0830858 | 2018-07-08 12:09:20 +0500 | [diff] [blame] | 849 | rc = sqlite3_create_function(self->db, |
| 850 | name, |
| 851 | narg, |
| 852 | flags, |
| 853 | (void*)func, |
| 854 | _pysqlite_func_callback, |
| 855 | NULL, |
| 856 | NULL); |
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 | } |
| 883 | |
Sergey Fedoseev | 5b25f1d | 2018-12-05 22:50:26 +0500 | [diff] [blame] | 884 | if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1) { |
| 885 | return NULL; |
| 886 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 887 | rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 888 | if (rc != SQLITE_OK) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 889 | /* Workaround for SQLite bug: no error code or string is available here */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 890 | PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 891 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 892 | } |
Sergey Fedoseev | 5b25f1d | 2018-12-05 22:50:26 +0500 | [diff] [blame] | 893 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 896 | 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] | 897 | { |
| 898 | PyObject *ret; |
| 899 | int rc; |
| 900 | PyGILState_STATE gilstate; |
| 901 | |
| 902 | gilstate = PyGILState_Ensure(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 903 | |
Victor Stinner | d4095d9 | 2013-07-26 22:23:33 +0200 | [diff] [blame] | 904 | 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] | 905 | |
Victor Stinner | d4095d9 | 2013-07-26 22:23:33 +0200 | [diff] [blame] | 906 | if (ret == NULL) { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 907 | if (_pysqlite_enable_callback_tracebacks) |
Victor Stinner | d4095d9 | 2013-07-26 22:23:33 +0200 | [diff] [blame] | 908 | PyErr_Print(); |
| 909 | else |
| 910 | PyErr_Clear(); |
Victor Stinner | 41801f5 | 2013-07-21 13:05:38 +0200 | [diff] [blame] | 911 | |
Victor Stinner | d4095d9 | 2013-07-26 22:23:33 +0200 | [diff] [blame] | 912 | rc = SQLITE_DENY; |
Victor Stinner | 41801f5 | 2013-07-21 13:05:38 +0200 | [diff] [blame] | 913 | } |
| 914 | else { |
Victor Stinner | d4095d9 | 2013-07-26 22:23:33 +0200 | [diff] [blame] | 915 | if (PyLong_Check(ret)) { |
| 916 | rc = _PyLong_AsInt(ret); |
| 917 | if (rc == -1 && PyErr_Occurred()) { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 918 | if (_pysqlite_enable_callback_tracebacks) |
Victor Stinner | d4095d9 | 2013-07-26 22:23:33 +0200 | [diff] [blame] | 919 | PyErr_Print(); |
| 920 | else |
| 921 | PyErr_Clear(); |
| 922 | rc = SQLITE_DENY; |
| 923 | } |
| 924 | } |
| 925 | else { |
| 926 | rc = SQLITE_DENY; |
| 927 | } |
| 928 | Py_DECREF(ret); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | PyGILState_Release(gilstate); |
| 932 | return rc; |
| 933 | } |
| 934 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 935 | static int _progress_handler(void* user_arg) |
| 936 | { |
| 937 | int rc; |
| 938 | PyObject *ret; |
| 939 | PyGILState_STATE gilstate; |
| 940 | |
| 941 | gilstate = PyGILState_Ensure(); |
Victor Stinner | 070c4d7 | 2016-12-09 12:29:18 +0100 | [diff] [blame] | 942 | ret = _PyObject_CallNoArg((PyObject*)user_arg); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 943 | |
| 944 | if (!ret) { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 945 | if (_pysqlite_enable_callback_tracebacks) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 946 | PyErr_Print(); |
| 947 | } else { |
| 948 | PyErr_Clear(); |
| 949 | } |
| 950 | |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 951 | /* abort query if error occurred */ |
Victor Stinner | 8699950 | 2010-05-19 01:27:23 +0000 | [diff] [blame] | 952 | rc = 1; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 953 | } else { |
| 954 | rc = (int)PyObject_IsTrue(ret); |
| 955 | Py_DECREF(ret); |
| 956 | } |
| 957 | |
| 958 | PyGILState_Release(gilstate); |
| 959 | return rc; |
| 960 | } |
| 961 | |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 962 | static void _trace_callback(void* user_arg, const char* statement_string) |
| 963 | { |
| 964 | PyObject *py_statement = NULL; |
| 965 | PyObject *ret = NULL; |
| 966 | |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 967 | PyGILState_STATE gilstate; |
| 968 | |
| 969 | gilstate = PyGILState_Ensure(); |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 970 | py_statement = PyUnicode_DecodeUTF8(statement_string, |
| 971 | strlen(statement_string), "replace"); |
| 972 | if (py_statement) { |
| 973 | ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL); |
| 974 | Py_DECREF(py_statement); |
| 975 | } |
| 976 | |
| 977 | if (ret) { |
| 978 | Py_DECREF(ret); |
| 979 | } else { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 980 | if (_pysqlite_enable_callback_tracebacks) { |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 981 | PyErr_Print(); |
| 982 | } else { |
| 983 | PyErr_Clear(); |
| 984 | } |
| 985 | } |
| 986 | |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 987 | PyGILState_Release(gilstate); |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 988 | } |
| 989 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 990 | 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] | 991 | { |
| 992 | PyObject* authorizer_cb; |
| 993 | |
| 994 | static char *kwlist[] = { "authorizer_callback", NULL }; |
| 995 | int rc; |
| 996 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 997 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 998 | return NULL; |
| 999 | } |
| 1000 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1001 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer", |
| 1002 | kwlist, &authorizer_cb)) { |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | |
Sergey Fedoseev | 5b25f1d | 2018-12-05 22:50:26 +0500 | [diff] [blame] | 1006 | if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1) { |
| 1007 | return NULL; |
| 1008 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1009 | rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1010 | if (rc != SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1011 | PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1012 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1013 | } |
Sergey Fedoseev | 5b25f1d | 2018-12-05 22:50:26 +0500 | [diff] [blame] | 1014 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1017 | 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] | 1018 | { |
| 1019 | PyObject* progress_handler; |
| 1020 | int n; |
| 1021 | |
| 1022 | static char *kwlist[] = { "progress_handler", "n", NULL }; |
| 1023 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1024 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1028 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler", |
| 1029 | kwlist, &progress_handler, &n)) { |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
| 1033 | if (progress_handler == Py_None) { |
| 1034 | /* None clears the progress handler previously set */ |
| 1035 | sqlite3_progress_handler(self->db, 0, 0, (void*)0); |
| 1036 | } else { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1037 | if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1) |
| 1038 | return NULL; |
Sergey Fedoseev | 5b25f1d | 2018-12-05 22:50:26 +0500 | [diff] [blame] | 1039 | sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 1042 | Py_RETURN_NONE; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 1045 | static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
| 1046 | { |
| 1047 | PyObject* trace_callback; |
| 1048 | |
| 1049 | static char *kwlist[] = { "trace_callback", NULL }; |
| 1050 | |
| 1051 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | |
| 1055 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback", |
| 1056 | kwlist, &trace_callback)) { |
| 1057 | return NULL; |
| 1058 | } |
| 1059 | |
| 1060 | if (trace_callback == Py_None) { |
| 1061 | /* None clears the trace callback previously set */ |
| 1062 | sqlite3_trace(self->db, 0, (void*)0); |
| 1063 | } else { |
| 1064 | if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1) |
| 1065 | return NULL; |
| 1066 | sqlite3_trace(self->db, _trace_callback, trace_callback); |
| 1067 | } |
| 1068 | |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 1069 | Py_RETURN_NONE; |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 1070 | } |
| 1071 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1072 | #ifdef HAVE_LOAD_EXTENSION |
| 1073 | static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args) |
| 1074 | { |
| 1075 | int rc; |
| 1076 | int onoff; |
| 1077 | |
| 1078 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 1079 | return NULL; |
| 1080 | } |
| 1081 | |
| 1082 | if (!PyArg_ParseTuple(args, "i", &onoff)) { |
| 1083 | return NULL; |
| 1084 | } |
| 1085 | |
| 1086 | rc = sqlite3_enable_load_extension(self->db, onoff); |
| 1087 | |
| 1088 | if (rc != SQLITE_OK) { |
| 1089 | PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension"); |
| 1090 | return NULL; |
| 1091 | } else { |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 1092 | Py_RETURN_NONE; |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args) |
| 1097 | { |
| 1098 | int rc; |
| 1099 | char* extension_name; |
| 1100 | char* errmsg; |
| 1101 | |
| 1102 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 1103 | return NULL; |
| 1104 | } |
| 1105 | |
| 1106 | if (!PyArg_ParseTuple(args, "s", &extension_name)) { |
| 1107 | return NULL; |
| 1108 | } |
| 1109 | |
| 1110 | rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg); |
| 1111 | if (rc != 0) { |
| 1112 | PyErr_SetString(pysqlite_OperationalError, errmsg); |
| 1113 | return NULL; |
| 1114 | } else { |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 1115 | Py_RETURN_NONE; |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | #endif |
| 1119 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1120 | int pysqlite_check_thread(pysqlite_Connection* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1121 | { |
| 1122 | if (self->check_same_thread) { |
| 1123 | if (PyThread_get_thread_ident() != self->thread_ident) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1124 | PyErr_Format(pysqlite_ProgrammingError, |
Takuya Akiba | 030345c | 2018-03-27 00:14:00 +0900 | [diff] [blame] | 1125 | "SQLite objects created in a thread can only be used in that same thread. " |
| 1126 | "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] | 1127 | self->thread_ident, PyThread_get_thread_ident()); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1132 | return 1; |
| 1133 | } |
| 1134 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1135 | static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1136 | { |
| 1137 | Py_INCREF(self->isolation_level); |
| 1138 | return self->isolation_level; |
| 1139 | } |
| 1140 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1141 | static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1142 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1143 | if (!pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1144 | return NULL; |
| 1145 | } else { |
| 1146 | return Py_BuildValue("i", sqlite3_total_changes(self->db)); |
| 1147 | } |
| 1148 | } |
| 1149 | |
Berker Peksag | 59da4b3 | 2016-09-12 07:16:43 +0300 | [diff] [blame] | 1150 | static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused) |
| 1151 | { |
| 1152 | if (!pysqlite_check_connection(self)) { |
| 1153 | return NULL; |
| 1154 | } |
| 1155 | if (!sqlite3_get_autocommit(self->db)) { |
| 1156 | Py_RETURN_TRUE; |
| 1157 | } |
| 1158 | Py_RETURN_FALSE; |
| 1159 | } |
| 1160 | |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 1161 | static int |
| 1162 | 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] | 1163 | { |
Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 1164 | if (isolation_level == NULL) { |
| 1165 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 1166 | return -1; |
| 1167 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1168 | if (isolation_level == Py_None) { |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 1169 | PyObject *res = pysqlite_connection_commit(self, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1170 | if (!res) { |
| 1171 | return -1; |
| 1172 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1173 | Py_DECREF(res); |
| 1174 | |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 1175 | self->begin_statement = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1176 | } else { |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 1177 | const char * const *candidate; |
| 1178 | PyObject *uppercase_level; |
| 1179 | _Py_IDENTIFIER(upper); |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 1180 | |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 1181 | if (!PyUnicode_Check(isolation_level)) { |
| 1182 | PyErr_Format(PyExc_TypeError, |
| 1183 | "isolation_level must be a string or None, not %.100s", |
| 1184 | Py_TYPE(isolation_level)->tp_name); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1185 | return -1; |
| 1186 | } |
| 1187 | |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 1188 | uppercase_level = _PyObject_CallMethodIdObjArgs( |
| 1189 | (PyObject *)&PyUnicode_Type, &PyId_upper, |
| 1190 | isolation_level, NULL); |
| 1191 | if (!uppercase_level) { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1192 | return -1; |
| 1193 | } |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 1194 | for (candidate = begin_statements; *candidate; candidate++) { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1195 | if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6)) |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 1196 | break; |
| 1197 | } |
| 1198 | Py_DECREF(uppercase_level); |
| 1199 | if (!*candidate) { |
| 1200 | PyErr_SetString(PyExc_ValueError, |
| 1201 | "invalid value for isolation_level"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1202 | return -1; |
| 1203 | } |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 1204 | self->begin_statement = *candidate; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Serhiy Storchaka | 2891492 | 2016-09-01 22:18:03 +0300 | [diff] [blame] | 1207 | Py_INCREF(isolation_level); |
| 1208 | Py_XSETREF(self->isolation_level, isolation_level); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1209 | return 0; |
| 1210 | } |
| 1211 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1212 | PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1213 | { |
| 1214 | PyObject* sql; |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1215 | pysqlite_Statement* statement; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1216 | PyObject* weakref; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1217 | int rc; |
| 1218 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 1219 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | |
Serhiy Storchaka | 6cca5c8 | 2017-06-08 14:41:19 +0300 | [diff] [blame] | 1223 | if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs)) |
Larry Hastings | 3b12e95 | 2015-05-08 07:45:10 -0700 | [diff] [blame] | 1224 | return NULL; |
| 1225 | |
Victor Stinner | b3e1ef1 | 2013-11-05 14:46:13 +0100 | [diff] [blame] | 1226 | if (!PyArg_ParseTuple(args, "O", &sql)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1227 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1228 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1229 | _pysqlite_drop_unused_statement_references(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1230 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1231 | statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1232 | if (!statement) { |
| 1233 | return NULL; |
| 1234 | } |
| 1235 | |
Victor Stinner | 0201f44 | 2010-03-13 03:28:34 +0000 | [diff] [blame] | 1236 | statement->db = NULL; |
| 1237 | statement->st = NULL; |
| 1238 | statement->sql = NULL; |
| 1239 | statement->in_use = 0; |
| 1240 | statement->in_weakreflist = NULL; |
| 1241 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1242 | rc = pysqlite_statement_create(statement, self, sql); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1243 | if (rc != SQLITE_OK) { |
| 1244 | if (rc == PYSQLITE_TOO_MUCH_SQL) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1245 | 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] | 1246 | } else if (rc == PYSQLITE_SQL_WRONG_TYPE) { |
Serhiy Storchaka | 42d67af | 2014-09-11 13:29:05 +0300 | [diff] [blame] | 1247 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1248 | PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1249 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1250 | (void)pysqlite_statement_reset(statement); |
| 1251 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1252 | } |
Victor Stinner | b3e1ef1 | 2013-11-05 14:46:13 +0100 | [diff] [blame] | 1253 | goto error; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Victor Stinner | b3e1ef1 | 2013-11-05 14:46:13 +0100 | [diff] [blame] | 1256 | weakref = PyWeakref_NewRef((PyObject*)statement, NULL); |
| 1257 | if (weakref == NULL) |
| 1258 | goto error; |
| 1259 | if (PyList_Append(self->statements, weakref) != 0) { |
| 1260 | Py_DECREF(weakref); |
| 1261 | goto error; |
| 1262 | } |
| 1263 | Py_DECREF(weakref); |
| 1264 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1265 | return (PyObject*)statement; |
Victor Stinner | b3e1ef1 | 2013-11-05 14:46:13 +0100 | [diff] [blame] | 1266 | |
| 1267 | error: |
| 1268 | Py_DECREF(statement); |
| 1269 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Larry Hastings | 01b0883 | 2015-05-08 07:37:49 -0700 | [diff] [blame] | 1272 | PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1273 | { |
| 1274 | PyObject* cursor = 0; |
| 1275 | PyObject* result = 0; |
| 1276 | PyObject* method = 0; |
| 1277 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1278 | cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1279 | if (!cursor) { |
| 1280 | goto error; |
| 1281 | } |
| 1282 | |
| 1283 | method = PyObject_GetAttrString(cursor, "execute"); |
| 1284 | if (!method) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1285 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1286 | goto error; |
| 1287 | } |
| 1288 | |
| 1289 | result = PyObject_CallObject(method, args); |
| 1290 | if (!result) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1291 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | error: |
| 1295 | Py_XDECREF(result); |
| 1296 | Py_XDECREF(method); |
| 1297 | |
| 1298 | return cursor; |
| 1299 | } |
| 1300 | |
Larry Hastings | 01b0883 | 2015-05-08 07:37:49 -0700 | [diff] [blame] | 1301 | PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1302 | { |
| 1303 | PyObject* cursor = 0; |
| 1304 | PyObject* result = 0; |
| 1305 | PyObject* method = 0; |
| 1306 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1307 | cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1308 | if (!cursor) { |
| 1309 | goto error; |
| 1310 | } |
| 1311 | |
| 1312 | method = PyObject_GetAttrString(cursor, "executemany"); |
| 1313 | if (!method) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1314 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1315 | goto error; |
| 1316 | } |
| 1317 | |
| 1318 | result = PyObject_CallObject(method, args); |
| 1319 | if (!result) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1320 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | error: |
| 1324 | Py_XDECREF(result); |
| 1325 | Py_XDECREF(method); |
| 1326 | |
| 1327 | return cursor; |
| 1328 | } |
| 1329 | |
Larry Hastings | 01b0883 | 2015-05-08 07:37:49 -0700 | [diff] [blame] | 1330 | PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1331 | { |
| 1332 | PyObject* cursor = 0; |
| 1333 | PyObject* result = 0; |
| 1334 | PyObject* method = 0; |
| 1335 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1336 | cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1337 | if (!cursor) { |
| 1338 | goto error; |
| 1339 | } |
| 1340 | |
| 1341 | method = PyObject_GetAttrString(cursor, "executescript"); |
| 1342 | if (!method) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1343 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1344 | goto error; |
| 1345 | } |
| 1346 | |
| 1347 | result = PyObject_CallObject(method, args); |
| 1348 | if (!result) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1349 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | error: |
| 1353 | Py_XDECREF(result); |
| 1354 | Py_XDECREF(method); |
| 1355 | |
| 1356 | return cursor; |
| 1357 | } |
| 1358 | |
| 1359 | /* ------------------------- COLLATION CODE ------------------------ */ |
| 1360 | |
| 1361 | static int |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1362 | pysqlite_collation_callback( |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1363 | void* context, |
| 1364 | int text1_length, const void* text1_data, |
| 1365 | int text2_length, const void* text2_data) |
| 1366 | { |
| 1367 | PyObject* callback = (PyObject*)context; |
| 1368 | PyObject* string1 = 0; |
| 1369 | PyObject* string2 = 0; |
| 1370 | PyGILState_STATE gilstate; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1371 | PyObject* retval = NULL; |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 1372 | long longval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1373 | int result = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1374 | gilstate = PyGILState_Ensure(); |
| 1375 | |
| 1376 | if (PyErr_Occurred()) { |
| 1377 | goto finally; |
| 1378 | } |
| 1379 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1380 | string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length); |
| 1381 | string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1382 | |
| 1383 | if (!string1 || !string2) { |
| 1384 | goto finally; /* failed to allocate strings */ |
| 1385 | } |
| 1386 | |
| 1387 | retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL); |
| 1388 | |
| 1389 | if (!retval) { |
| 1390 | /* execution failed */ |
| 1391 | goto finally; |
| 1392 | } |
| 1393 | |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 1394 | longval = PyLong_AsLongAndOverflow(retval, &result); |
| 1395 | if (longval == -1 && PyErr_Occurred()) { |
| 1396 | PyErr_Clear(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1397 | result = 0; |
| 1398 | } |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 1399 | else if (!result) { |
| 1400 | if (longval > 0) |
| 1401 | result = 1; |
| 1402 | else if (longval < 0) |
| 1403 | result = -1; |
| 1404 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1405 | |
| 1406 | finally: |
| 1407 | Py_XDECREF(string1); |
| 1408 | Py_XDECREF(string2); |
| 1409 | Py_XDECREF(retval); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1410 | PyGILState_Release(gilstate); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1411 | return result; |
| 1412 | } |
| 1413 | |
| 1414 | static PyObject * |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1415 | pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1416 | { |
| 1417 | PyObject* retval = NULL; |
| 1418 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1419 | if (!pysqlite_check_connection(self)) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1420 | goto finally; |
| 1421 | } |
| 1422 | |
| 1423 | sqlite3_interrupt(self->db); |
| 1424 | |
| 1425 | Py_INCREF(Py_None); |
| 1426 | retval = Py_None; |
| 1427 | |
| 1428 | finally: |
| 1429 | return retval; |
| 1430 | } |
| 1431 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1432 | /* Function author: Paul Kippes <kippesp@gmail.com> |
| 1433 | * Class method of Connection to call the Python function _iterdump |
| 1434 | * of the sqlite3 module. |
| 1435 | */ |
| 1436 | static PyObject * |
| 1437 | pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args) |
| 1438 | { |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 1439 | _Py_IDENTIFIER(_iterdump); |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1440 | PyObject* retval = NULL; |
| 1441 | PyObject* module = NULL; |
| 1442 | PyObject* module_dict; |
| 1443 | PyObject* pyfn_iterdump; |
| 1444 | |
| 1445 | if (!pysqlite_check_connection(self)) { |
| 1446 | goto finally; |
| 1447 | } |
| 1448 | |
| 1449 | module = PyImport_ImportModule(MODULE_NAME ".dump"); |
| 1450 | if (!module) { |
| 1451 | goto finally; |
| 1452 | } |
| 1453 | |
| 1454 | module_dict = PyModule_GetDict(module); |
| 1455 | if (!module_dict) { |
| 1456 | goto finally; |
| 1457 | } |
| 1458 | |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 1459 | pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump); |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1460 | if (!pyfn_iterdump) { |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 1461 | if (!PyErr_Occurred()) { |
| 1462 | PyErr_SetString(pysqlite_OperationalError, |
| 1463 | "Failed to obtain _iterdump() reference"); |
| 1464 | } |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1465 | goto finally; |
| 1466 | } |
| 1467 | |
| 1468 | args = PyTuple_New(1); |
| 1469 | if (!args) { |
| 1470 | goto finally; |
| 1471 | } |
| 1472 | Py_INCREF(self); |
| 1473 | PyTuple_SetItem(args, 0, (PyObject*)self); |
| 1474 | retval = PyObject_CallObject(pyfn_iterdump, args); |
| 1475 | |
| 1476 | finally: |
| 1477 | Py_XDECREF(args); |
| 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 | |
Serhiy Storchaka | 407ac47 | 2016-09-27 00:10:03 +0300 | [diff] [blame] | 1658 | uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type, |
| 1659 | &PyId_upper, name, NULL); |
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 */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1841 | 0, /* tp_print */ |
| 1842 | 0, /* tp_getattr */ |
| 1843 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1844 | 0, /* tp_reserved */ |
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 | } |