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