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