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