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