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