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