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