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