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