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