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