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