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