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 | |
Gerhard Häring | 7857650 | 2010-03-05 15:55:55 +0000 | [diff] [blame] | 693 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 694 | return NULL; |
| 695 | } |
| 696 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 697 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist, |
| 698 | &name, &narg, &func)) |
| 699 | { |
| 700 | return NULL; |
| 701 | } |
| 702 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 703 | 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] | 704 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 705 | if (rc != SQLITE_OK) { |
| 706 | /* Workaround for SQLite bug: no error code or string is available here */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 707 | PyErr_SetString(pysqlite_OperationalError, "Error creating function"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 708 | return NULL; |
| 709 | } else { |
| 710 | PyDict_SetItem(self->function_pinboard, func, Py_None); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 711 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 712 | Py_INCREF(Py_None); |
| 713 | return Py_None; |
| 714 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 717 | PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 718 | { |
| 719 | PyObject* aggregate_class; |
| 720 | |
| 721 | int n_arg; |
| 722 | char* name; |
| 723 | static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL }; |
| 724 | int rc; |
| 725 | |
Gerhard Häring | 7857650 | 2010-03-05 15:55:55 +0000 | [diff] [blame] | 726 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 727 | return NULL; |
| 728 | } |
| 729 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 730 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate", |
| 731 | kwlist, &name, &n_arg, &aggregate_class)) { |
| 732 | return NULL; |
| 733 | } |
| 734 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 735 | 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] | 736 | if (rc != SQLITE_OK) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 737 | /* Workaround for SQLite bug: no error code or string is available here */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 738 | PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 739 | return NULL; |
| 740 | } else { |
| 741 | PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None); |
| 742 | |
| 743 | Py_INCREF(Py_None); |
| 744 | return Py_None; |
| 745 | } |
| 746 | } |
| 747 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 748 | 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] | 749 | { |
| 750 | PyObject *ret; |
| 751 | int rc; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 752 | #ifdef WITH_THREAD |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 753 | PyGILState_STATE gilstate; |
| 754 | |
| 755 | gilstate = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 756 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 757 | ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source); |
| 758 | |
| 759 | if (!ret) { |
| 760 | if (_enable_callback_tracebacks) { |
| 761 | PyErr_Print(); |
| 762 | } else { |
| 763 | PyErr_Clear(); |
| 764 | } |
| 765 | |
| 766 | rc = SQLITE_DENY; |
| 767 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 768 | if (PyLong_Check(ret)) { |
| 769 | rc = (int)PyLong_AsLong(ret); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 770 | } else { |
| 771 | rc = SQLITE_DENY; |
| 772 | } |
| 773 | Py_DECREF(ret); |
| 774 | } |
| 775 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 776 | #ifdef WITH_THREAD |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 777 | PyGILState_Release(gilstate); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 778 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 779 | return rc; |
| 780 | } |
| 781 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 782 | static int _progress_handler(void* user_arg) |
| 783 | { |
| 784 | int rc; |
| 785 | PyObject *ret; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 786 | #ifdef WITH_THREAD |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 787 | PyGILState_STATE gilstate; |
| 788 | |
| 789 | gilstate = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 790 | #endif |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 791 | ret = PyObject_CallFunction((PyObject*)user_arg, ""); |
| 792 | |
| 793 | if (!ret) { |
| 794 | if (_enable_callback_tracebacks) { |
| 795 | PyErr_Print(); |
| 796 | } else { |
| 797 | PyErr_Clear(); |
| 798 | } |
| 799 | |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 800 | /* abort query if error occurred */ |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 801 | rc = 1; |
| 802 | } else { |
| 803 | rc = (int)PyObject_IsTrue(ret); |
| 804 | Py_DECREF(ret); |
| 805 | } |
| 806 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 807 | #ifdef WITH_THREAD |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 808 | PyGILState_Release(gilstate); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 809 | #endif |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 810 | return rc; |
| 811 | } |
| 812 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 813 | PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 814 | { |
| 815 | PyObject* authorizer_cb; |
| 816 | |
| 817 | static char *kwlist[] = { "authorizer_callback", NULL }; |
| 818 | int rc; |
| 819 | |
Gerhard Häring | 7857650 | 2010-03-05 15:55:55 +0000 | [diff] [blame] | 820 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 821 | return NULL; |
| 822 | } |
| 823 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 824 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer", |
| 825 | kwlist, &authorizer_cb)) { |
| 826 | return NULL; |
| 827 | } |
| 828 | |
| 829 | rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb); |
| 830 | |
| 831 | if (rc != SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 832 | PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 833 | return NULL; |
| 834 | } else { |
| 835 | PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None); |
| 836 | |
| 837 | Py_INCREF(Py_None); |
| 838 | return Py_None; |
| 839 | } |
| 840 | } |
| 841 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 842 | PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
| 843 | { |
| 844 | PyObject* progress_handler; |
| 845 | int n; |
| 846 | |
| 847 | static char *kwlist[] = { "progress_handler", "n", NULL }; |
| 848 | |
Gerhard Häring | 7857650 | 2010-03-05 15:55:55 +0000 | [diff] [blame] | 849 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 850 | return NULL; |
| 851 | } |
| 852 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 853 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler", |
| 854 | kwlist, &progress_handler, &n)) { |
| 855 | return NULL; |
| 856 | } |
| 857 | |
| 858 | if (progress_handler == Py_None) { |
| 859 | /* None clears the progress handler previously set */ |
| 860 | sqlite3_progress_handler(self->db, 0, 0, (void*)0); |
| 861 | } else { |
| 862 | sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler); |
| 863 | PyDict_SetItem(self->function_pinboard, progress_handler, Py_None); |
| 864 | } |
| 865 | |
| 866 | Py_INCREF(Py_None); |
| 867 | return Py_None; |
| 868 | } |
| 869 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 870 | int pysqlite_check_thread(pysqlite_Connection* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 871 | { |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 872 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 873 | if (self->check_same_thread) { |
| 874 | if (PyThread_get_thread_ident() != self->thread_ident) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 875 | PyErr_Format(pysqlite_ProgrammingError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 876 | "SQLite objects created in a thread can only be used in that same thread." |
| 877 | "The object was created in thread id %ld and this is thread id %ld", |
| 878 | self->thread_ident, PyThread_get_thread_ident()); |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | } |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 883 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 884 | return 1; |
| 885 | } |
| 886 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 887 | static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 888 | { |
| 889 | Py_INCREF(self->isolation_level); |
| 890 | return self->isolation_level; |
| 891 | } |
| 892 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 893 | static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 894 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 895 | if (!pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 896 | return NULL; |
| 897 | } else { |
| 898 | return Py_BuildValue("i", sqlite3_total_changes(self->db)); |
| 899 | } |
| 900 | } |
| 901 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 902 | 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] | 903 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 904 | PyObject* res; |
| 905 | PyObject* begin_statement; |
Georg Brandl | ceab610 | 2007-11-25 00:45:05 +0000 | [diff] [blame] | 906 | static PyObject* begin_word; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 907 | |
| 908 | Py_XDECREF(self->isolation_level); |
| 909 | |
| 910 | if (self->begin_statement) { |
| 911 | PyMem_Free(self->begin_statement); |
| 912 | self->begin_statement = NULL; |
| 913 | } |
| 914 | |
| 915 | if (isolation_level == Py_None) { |
| 916 | Py_INCREF(Py_None); |
| 917 | self->isolation_level = Py_None; |
| 918 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 919 | res = pysqlite_connection_commit(self, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 920 | if (!res) { |
| 921 | return -1; |
| 922 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 923 | Py_DECREF(res); |
| 924 | |
| 925 | self->inTransaction = 0; |
| 926 | } else { |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 927 | const char *statement; |
| 928 | Py_ssize_t size; |
| 929 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 930 | Py_INCREF(isolation_level); |
| 931 | self->isolation_level = isolation_level; |
| 932 | |
Georg Brandl | ceab610 | 2007-11-25 00:45:05 +0000 | [diff] [blame] | 933 | if (!begin_word) { |
| 934 | begin_word = PyUnicode_FromString("BEGIN "); |
| 935 | if (!begin_word) return -1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 936 | } |
Georg Brandl | ceab610 | 2007-11-25 00:45:05 +0000 | [diff] [blame] | 937 | begin_statement = PyUnicode_Concat(begin_word, isolation_level); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 938 | if (!begin_statement) { |
| 939 | return -1; |
| 940 | } |
| 941 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 942 | statement = _PyUnicode_AsStringAndSize(begin_statement, &size); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 943 | if (!statement) { |
Victor Stinner | 8a685f7 | 2010-03-21 20:05:51 +0000 | [diff] [blame] | 944 | Py_DECREF(begin_statement); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 945 | return -1; |
| 946 | } |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 947 | self->begin_statement = PyMem_Malloc(size + 2); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 948 | if (!self->begin_statement) { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 949 | Py_DECREF(begin_statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 950 | return -1; |
| 951 | } |
| 952 | |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 953 | strcpy(self->begin_statement, statement); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 954 | Py_DECREF(begin_statement); |
| 955 | } |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 960 | PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 961 | { |
| 962 | PyObject* sql; |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 963 | pysqlite_Statement* statement; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 964 | PyObject* weakref; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 965 | int rc; |
| 966 | |
Gerhard Häring | 7857650 | 2010-03-05 15:55:55 +0000 | [diff] [blame] | 967 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
| 968 | return NULL; |
| 969 | } |
| 970 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 971 | if (!PyArg_ParseTuple(args, "O", &sql)) { |
| 972 | return NULL; |
| 973 | } |
| 974 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 975 | _pysqlite_drop_unused_statement_references(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 976 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 977 | statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 978 | if (!statement) { |
| 979 | return NULL; |
| 980 | } |
| 981 | |
Victor Stinner | 09e4697 | 2010-03-21 20:29:20 +0000 | [diff] [blame] | 982 | statement->db = NULL; |
| 983 | statement->st = NULL; |
| 984 | statement->sql = NULL; |
| 985 | statement->in_use = 0; |
| 986 | statement->in_weakreflist = NULL; |
| 987 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 988 | rc = pysqlite_statement_create(statement, self, sql); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 989 | |
| 990 | if (rc != SQLITE_OK) { |
| 991 | if (rc == PYSQLITE_TOO_MUCH_SQL) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 992 | 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] | 993 | } else if (rc == PYSQLITE_SQL_WRONG_TYPE) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 994 | 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] | 995 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 996 | (void)pysqlite_statement_reset(statement); |
| 997 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1000 | Py_CLEAR(statement); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1001 | } else { |
| 1002 | weakref = PyWeakref_NewRef((PyObject*)statement, NULL); |
| 1003 | if (!weakref) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1004 | Py_CLEAR(statement); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1005 | goto error; |
| 1006 | } |
| 1007 | |
| 1008 | if (PyList_Append(self->statements, weakref) != 0) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1009 | Py_CLEAR(weakref); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1010 | goto error; |
| 1011 | } |
| 1012 | |
| 1013 | Py_DECREF(weakref); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1016 | error: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1017 | return (PyObject*)statement; |
| 1018 | } |
| 1019 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1020 | PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1021 | { |
| 1022 | PyObject* cursor = 0; |
| 1023 | PyObject* result = 0; |
| 1024 | PyObject* method = 0; |
| 1025 | |
| 1026 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 1027 | if (!cursor) { |
| 1028 | goto error; |
| 1029 | } |
| 1030 | |
| 1031 | method = PyObject_GetAttrString(cursor, "execute"); |
| 1032 | if (!method) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1033 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1034 | goto error; |
| 1035 | } |
| 1036 | |
| 1037 | result = PyObject_CallObject(method, args); |
| 1038 | if (!result) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1039 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | error: |
| 1043 | Py_XDECREF(result); |
| 1044 | Py_XDECREF(method); |
| 1045 | |
| 1046 | return cursor; |
| 1047 | } |
| 1048 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1049 | PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1050 | { |
| 1051 | PyObject* cursor = 0; |
| 1052 | PyObject* result = 0; |
| 1053 | PyObject* method = 0; |
| 1054 | |
| 1055 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 1056 | if (!cursor) { |
| 1057 | goto error; |
| 1058 | } |
| 1059 | |
| 1060 | method = PyObject_GetAttrString(cursor, "executemany"); |
| 1061 | if (!method) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1062 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1063 | goto error; |
| 1064 | } |
| 1065 | |
| 1066 | result = PyObject_CallObject(method, args); |
| 1067 | if (!result) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1068 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | error: |
| 1072 | Py_XDECREF(result); |
| 1073 | Py_XDECREF(method); |
| 1074 | |
| 1075 | return cursor; |
| 1076 | } |
| 1077 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1078 | PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1079 | { |
| 1080 | PyObject* cursor = 0; |
| 1081 | PyObject* result = 0; |
| 1082 | PyObject* method = 0; |
| 1083 | |
| 1084 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 1085 | if (!cursor) { |
| 1086 | goto error; |
| 1087 | } |
| 1088 | |
| 1089 | method = PyObject_GetAttrString(cursor, "executescript"); |
| 1090 | if (!method) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1091 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1092 | goto error; |
| 1093 | } |
| 1094 | |
| 1095 | result = PyObject_CallObject(method, args); |
| 1096 | if (!result) { |
Alexandre Vassalotti | 1839bac | 2008-07-13 21:57:48 +0000 | [diff] [blame] | 1097 | Py_CLEAR(cursor); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | error: |
| 1101 | Py_XDECREF(result); |
| 1102 | Py_XDECREF(method); |
| 1103 | |
| 1104 | return cursor; |
| 1105 | } |
| 1106 | |
| 1107 | /* ------------------------- COLLATION CODE ------------------------ */ |
| 1108 | |
| 1109 | static int |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1110 | pysqlite_collation_callback( |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1111 | void* context, |
| 1112 | int text1_length, const void* text1_data, |
| 1113 | int text2_length, const void* text2_data) |
| 1114 | { |
| 1115 | PyObject* callback = (PyObject*)context; |
| 1116 | PyObject* string1 = 0; |
| 1117 | PyObject* string2 = 0; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1118 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1119 | PyGILState_STATE gilstate; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1120 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1121 | PyObject* retval = NULL; |
| 1122 | int result = 0; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1123 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1124 | gilstate = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1125 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1126 | |
| 1127 | if (PyErr_Occurred()) { |
| 1128 | goto finally; |
| 1129 | } |
| 1130 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1131 | string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length); |
| 1132 | string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1133 | |
| 1134 | if (!string1 || !string2) { |
| 1135 | goto finally; /* failed to allocate strings */ |
| 1136 | } |
| 1137 | |
| 1138 | retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL); |
| 1139 | |
| 1140 | if (!retval) { |
| 1141 | /* execution failed */ |
| 1142 | goto finally; |
| 1143 | } |
| 1144 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1145 | result = PyLong_AsLong(retval); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1146 | if (PyErr_Occurred()) { |
| 1147 | result = 0; |
| 1148 | } |
| 1149 | |
| 1150 | finally: |
| 1151 | Py_XDECREF(string1); |
| 1152 | Py_XDECREF(string2); |
| 1153 | Py_XDECREF(retval); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1154 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1155 | PyGILState_Release(gilstate); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 1156 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1157 | return result; |
| 1158 | } |
| 1159 | |
| 1160 | static PyObject * |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1161 | pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1162 | { |
| 1163 | PyObject* retval = NULL; |
| 1164 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1165 | if (!pysqlite_check_connection(self)) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1166 | goto finally; |
| 1167 | } |
| 1168 | |
| 1169 | sqlite3_interrupt(self->db); |
| 1170 | |
| 1171 | Py_INCREF(Py_None); |
| 1172 | retval = Py_None; |
| 1173 | |
| 1174 | finally: |
| 1175 | return retval; |
| 1176 | } |
| 1177 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1178 | /* Function author: Paul Kippes <kippesp@gmail.com> |
| 1179 | * Class method of Connection to call the Python function _iterdump |
| 1180 | * of the sqlite3 module. |
| 1181 | */ |
| 1182 | static PyObject * |
| 1183 | pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args) |
| 1184 | { |
| 1185 | PyObject* retval = NULL; |
| 1186 | PyObject* module = NULL; |
| 1187 | PyObject* module_dict; |
| 1188 | PyObject* pyfn_iterdump; |
| 1189 | |
| 1190 | if (!pysqlite_check_connection(self)) { |
| 1191 | goto finally; |
| 1192 | } |
| 1193 | |
| 1194 | module = PyImport_ImportModule(MODULE_NAME ".dump"); |
| 1195 | if (!module) { |
| 1196 | goto finally; |
| 1197 | } |
| 1198 | |
| 1199 | module_dict = PyModule_GetDict(module); |
| 1200 | if (!module_dict) { |
| 1201 | goto finally; |
| 1202 | } |
| 1203 | |
| 1204 | pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump"); |
| 1205 | if (!pyfn_iterdump) { |
| 1206 | PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference"); |
| 1207 | goto finally; |
| 1208 | } |
| 1209 | |
| 1210 | args = PyTuple_New(1); |
| 1211 | if (!args) { |
| 1212 | goto finally; |
| 1213 | } |
| 1214 | Py_INCREF(self); |
| 1215 | PyTuple_SetItem(args, 0, (PyObject*)self); |
| 1216 | retval = PyObject_CallObject(pyfn_iterdump, args); |
| 1217 | |
| 1218 | finally: |
| 1219 | Py_XDECREF(args); |
| 1220 | Py_XDECREF(module); |
| 1221 | return retval; |
| 1222 | } |
| 1223 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1224 | static PyObject * |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1225 | pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1226 | { |
| 1227 | PyObject* callable; |
| 1228 | PyObject* uppercase_name = 0; |
| 1229 | PyObject* name; |
| 1230 | PyObject* retval; |
Victor Stinner | 9b05700 | 2010-04-22 11:24:50 +0000 | [diff] [blame] | 1231 | Py_UNICODE* chk; |
| 1232 | Py_ssize_t i, len; |
| 1233 | char *uppercase_name_str; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1234 | int rc; |
| 1235 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1236 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1237 | goto finally; |
| 1238 | } |
| 1239 | |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 1240 | 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] | 1241 | goto finally; |
| 1242 | } |
| 1243 | |
| 1244 | uppercase_name = PyObject_CallMethod(name, "upper", ""); |
| 1245 | if (!uppercase_name) { |
| 1246 | goto finally; |
| 1247 | } |
| 1248 | |
Victor Stinner | 9b05700 | 2010-04-22 11:24:50 +0000 | [diff] [blame] | 1249 | len = PyUnicode_GET_SIZE(uppercase_name); |
| 1250 | chk = PyUnicode_AS_UNICODE(uppercase_name); |
| 1251 | for (i=0; i<len; i++, chk++) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1252 | if ((*chk >= '0' && *chk <= '9') |
| 1253 | || (*chk >= 'A' && *chk <= 'Z') |
| 1254 | || (*chk == '_')) |
| 1255 | { |
Victor Stinner | 9b05700 | 2010-04-22 11:24:50 +0000 | [diff] [blame] | 1256 | continue; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1257 | } else { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1258 | PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1259 | goto finally; |
| 1260 | } |
| 1261 | } |
| 1262 | |
Victor Stinner | 9b05700 | 2010-04-22 11:24:50 +0000 | [diff] [blame] | 1263 | uppercase_name_str = _PyUnicode_AsString(uppercase_name); |
| 1264 | if (!uppercase_name_str) |
| 1265 | goto finally; |
| 1266 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1267 | if (callable != Py_None && !PyCallable_Check(callable)) { |
| 1268 | PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 1269 | goto finally; |
| 1270 | } |
| 1271 | |
| 1272 | if (callable != Py_None) { |
| 1273 | PyDict_SetItem(self->collations, uppercase_name, callable); |
| 1274 | } else { |
| 1275 | PyDict_DelItem(self->collations, uppercase_name); |
| 1276 | } |
| 1277 | |
| 1278 | rc = sqlite3_create_collation(self->db, |
Victor Stinner | 9b05700 | 2010-04-22 11:24:50 +0000 | [diff] [blame] | 1279 | uppercase_name_str, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1280 | SQLITE_UTF8, |
| 1281 | (callable != Py_None) ? callable : NULL, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1282 | (callable != Py_None) ? pysqlite_collation_callback : NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1283 | if (rc != SQLITE_OK) { |
| 1284 | PyDict_DelItem(self->collations, uppercase_name); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1285 | _pysqlite_seterror(self->db, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1286 | goto finally; |
| 1287 | } |
| 1288 | |
| 1289 | finally: |
| 1290 | Py_XDECREF(uppercase_name); |
| 1291 | |
| 1292 | if (PyErr_Occurred()) { |
| 1293 | retval = NULL; |
| 1294 | } else { |
| 1295 | Py_INCREF(Py_None); |
| 1296 | retval = Py_None; |
| 1297 | } |
| 1298 | |
| 1299 | return retval; |
| 1300 | } |
| 1301 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1302 | /* Called when the connection is used as a context manager. Returns itself as a |
| 1303 | * convenience to the caller. */ |
| 1304 | static PyObject * |
| 1305 | pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args) |
| 1306 | { |
| 1307 | Py_INCREF(self); |
| 1308 | return (PyObject*)self; |
| 1309 | } |
| 1310 | |
| 1311 | /** Called when the connection is used as a context manager. If there was any |
| 1312 | * exception, a rollback takes place; otherwise we commit. */ |
| 1313 | static PyObject * |
| 1314 | pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args) |
| 1315 | { |
| 1316 | PyObject* exc_type, *exc_value, *exc_tb; |
| 1317 | char* method_name; |
| 1318 | PyObject* result; |
| 1319 | |
| 1320 | if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) { |
| 1321 | return NULL; |
| 1322 | } |
| 1323 | |
| 1324 | if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) { |
| 1325 | method_name = "commit"; |
| 1326 | } else { |
| 1327 | method_name = "rollback"; |
| 1328 | } |
| 1329 | |
| 1330 | result = PyObject_CallMethod((PyObject*)self, method_name, ""); |
| 1331 | if (!result) { |
| 1332 | return NULL; |
| 1333 | } |
| 1334 | Py_DECREF(result); |
| 1335 | |
| 1336 | Py_RETURN_FALSE; |
| 1337 | } |
| 1338 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1339 | static char connection_doc[] = |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1340 | PyDoc_STR("SQLite database connection object."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1341 | |
| 1342 | static PyGetSetDef connection_getset[] = { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1343 | {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level}, |
| 1344 | {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1345 | {NULL} |
| 1346 | }; |
| 1347 | |
| 1348 | static PyMethodDef connection_methods[] = { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1349 | {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1350 | PyDoc_STR("Return a cursor for the connection.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1351 | {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1352 | PyDoc_STR("Closes the connection.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1353 | {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1354 | PyDoc_STR("Commit the current transaction.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1355 | {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1356 | PyDoc_STR("Roll back the current transaction.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1357 | {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1358 | PyDoc_STR("Creates a new function. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1359 | {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1360 | PyDoc_STR("Creates a new aggregate. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1361 | {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1362 | PyDoc_STR("Sets authorizer callback. Non-standard.")}, |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1363 | {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS, |
| 1364 | PyDoc_STR("Sets progress handler callback. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1365 | {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1366 | PyDoc_STR("Executes a SQL statement. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1367 | {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1368 | PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1369 | {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1370 | PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1371 | {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1372 | PyDoc_STR("Creates a collation function. Non-standard.")}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1373 | {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1374 | PyDoc_STR("Abort any pending database operation. Non-standard.")}, |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1375 | {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS, |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 1376 | 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] | 1377 | {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS, |
| 1378 | PyDoc_STR("For context manager. Non-standard.")}, |
| 1379 | {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS, |
| 1380 | PyDoc_STR("For context manager. Non-standard.")}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1381 | {NULL, NULL} |
| 1382 | }; |
| 1383 | |
| 1384 | static struct PyMemberDef connection_members[] = |
| 1385 | { |
Guido van Rossum | 10f07c4 | 2007-08-11 15:32:55 +0000 | [diff] [blame] | 1386 | {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY}, |
| 1387 | {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY}, |
| 1388 | {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY}, |
| 1389 | {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY}, |
| 1390 | {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY}, |
| 1391 | {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY}, |
| 1392 | {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY}, |
| 1393 | {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY}, |
| 1394 | {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY}, |
| 1395 | {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY}, |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1396 | {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)}, |
| 1397 | {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1398 | {NULL} |
| 1399 | }; |
| 1400 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1401 | PyTypeObject pysqlite_ConnectionType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1402 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1403 | MODULE_NAME ".Connection", /* tp_name */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1404 | sizeof(pysqlite_Connection), /* tp_basicsize */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1405 | 0, /* tp_itemsize */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1406 | (destructor)pysqlite_connection_dealloc, /* tp_dealloc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1407 | 0, /* tp_print */ |
| 1408 | 0, /* tp_getattr */ |
| 1409 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1410 | 0, /* tp_reserved */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1411 | 0, /* tp_repr */ |
| 1412 | 0, /* tp_as_number */ |
| 1413 | 0, /* tp_as_sequence */ |
| 1414 | 0, /* tp_as_mapping */ |
| 1415 | 0, /* tp_hash */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1416 | (ternaryfunc)pysqlite_connection_call, /* tp_call */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1417 | 0, /* tp_str */ |
| 1418 | 0, /* tp_getattro */ |
| 1419 | 0, /* tp_setattro */ |
| 1420 | 0, /* tp_as_buffer */ |
| 1421 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1422 | connection_doc, /* tp_doc */ |
| 1423 | 0, /* tp_traverse */ |
| 1424 | 0, /* tp_clear */ |
| 1425 | 0, /* tp_richcompare */ |
| 1426 | 0, /* tp_weaklistoffset */ |
| 1427 | 0, /* tp_iter */ |
| 1428 | 0, /* tp_iternext */ |
| 1429 | connection_methods, /* tp_methods */ |
| 1430 | connection_members, /* tp_members */ |
| 1431 | connection_getset, /* tp_getset */ |
| 1432 | 0, /* tp_base */ |
| 1433 | 0, /* tp_dict */ |
| 1434 | 0, /* tp_descr_get */ |
| 1435 | 0, /* tp_descr_set */ |
| 1436 | 0, /* tp_dictoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1437 | (initproc)pysqlite_connection_init, /* tp_init */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1438 | 0, /* tp_alloc */ |
| 1439 | 0, /* tp_new */ |
| 1440 | 0 /* tp_free */ |
| 1441 | }; |
| 1442 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1443 | extern int pysqlite_connection_setup_types(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1444 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1445 | pysqlite_ConnectionType.tp_new = PyType_GenericNew; |
| 1446 | return PyType_Ready(&pysqlite_ConnectionType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1447 | } |