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