Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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. |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 6 | * |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 7 | * This software is provided 'as-is', without any express or implied |
| 8 | * warranty. In no event will the authors be held liable for any damages |
| 9 | * arising from the use of this software. |
| 10 | * |
| 11 | * Permission is granted to anyone to use this software for any purpose, |
| 12 | * including commercial applications, and to alter it and redistribute it |
| 13 | * freely, subject to the following restrictions: |
| 14 | * |
| 15 | * 1. The origin of this software must not be misrepresented; you must not |
| 16 | * claim that you wrote the original software. If you use this software |
| 17 | * in a product, an acknowledgment in the product documentation would be |
| 18 | * appreciated but is not required. |
| 19 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 20 | * misrepresented as being the original software. |
| 21 | * 3. This notice may not be removed or altered from any source distribution. |
| 22 | */ |
| 23 | |
| 24 | #include "cache.h" |
| 25 | #include "module.h" |
| 26 | #include "connection.h" |
| 27 | #include "statement.h" |
| 28 | #include "cursor.h" |
| 29 | #include "prepare_protocol.h" |
| 30 | #include "util.h" |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 31 | #include "sqlitecompat.h" |
| 32 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 33 | #include "pythread.h" |
| 34 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 35 | static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 36 | |
Gerhard Häring | b2e8816 | 2006-06-14 22:28:37 +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); |
Neal Norwitz | fe7d0c3 | 2006-06-15 04:54:29 +0000 | [diff] [blame] | 45 | #else |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 46 | PyErr_SetString(pysqlite_OperationalError, errmsg); |
Gerhard Häring | b2e8816 | 2006-06-14 22:28:37 +0000 | [diff] [blame] | 47 | #endif |
| 48 | } |
| 49 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 50 | int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 72 | self->statements = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 85 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | if (!isolation_level) { |
| 90 | isolation_level = PyString_FromString(""); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 91 | if (!isolation_level) { |
| 92 | return -1; |
| 93 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 94 | } else { |
| 95 | Py_INCREF(isolation_level); |
| 96 | } |
| 97 | self->isolation_level = NULL; |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 98 | pysqlite_connection_set_isolation_level(self, isolation_level); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 99 | Py_DECREF(isolation_level); |
| 100 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 101 | self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 102 | if (PyErr_Occurred()) { |
| 103 | return -1; |
| 104 | } |
| 105 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 106 | self->statements = PyList_New(0); |
| 107 | if (!self->statements) { |
| 108 | return -1; |
| 109 | } |
| 110 | self->created_statements = 0; |
| 111 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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(); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 129 | if (!self->function_pinboard) { |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | self->collations = PyDict_New(); |
| 134 | if (!self->collations) { |
| 135 | return -1; |
| 136 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 137 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +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; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 152 | /* Empty the entire statement cache of this connection */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 153 | void pysqlite_flush_statement_cache(pysqlite_Connection* self) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 154 | { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 155 | pysqlite_Node* node; |
| 156 | pysqlite_Statement* statement; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 157 | |
| 158 | node = self->statement_cache->first; |
| 159 | |
| 160 | while (node) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 161 | statement = (pysqlite_Statement*)(node->data); |
| 162 | (void)pysqlite_statement_finalize(statement); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 163 | node = node->next; |
| 164 | } |
| 165 | |
| 166 | Py_DECREF(self->statement_cache); |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 167 | self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 168 | Py_DECREF(self); |
| 169 | self->statement_cache->decref_factory = 0; |
| 170 | } |
| 171 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 172 | void pysqlite_reset_all_statements(pysqlite_Connection* self) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 173 | { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 174 | int i; |
| 175 | PyObject* weakref; |
| 176 | PyObject* statement; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 177 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +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) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 182 | (void)pysqlite_statement_reset((pysqlite_Statement*)statement); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 183 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 187 | void pysqlite_connection_dealloc(pysqlite_Connection* self) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 205 | Py_XDECREF(self->collations); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 206 | Py_XDECREF(self->statements); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 207 | |
| 208 | self->ob_type->tp_free((PyObject*)self); |
| 209 | } |
| 210 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 211 | PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 223 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 224 | return NULL; |
| 225 | } |
| 226 | |
| 227 | if (factory == NULL) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 228 | factory = (PyObject*)&pysqlite_CursorType; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | cursor = PyObject_CallFunction(factory, "O", self); |
| 232 | |
| 233 | if (cursor && self->row_factory != Py_None) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 234 | Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 235 | Py_INCREF(self->row_factory); |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 236 | ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return cursor; |
| 240 | } |
| 241 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 242 | PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 243 | { |
| 244 | int rc; |
| 245 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 246 | if (!pysqlite_check_thread(self)) { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 247 | return NULL; |
| 248 | } |
| 249 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 250 | pysqlite_flush_statement_cache(self); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 258 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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 | */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 274 | int pysqlite_check_connection(pysqlite_Connection* con) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 275 | { |
| 276 | if (!con->db) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 277 | PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database."); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 278 | return 0; |
| 279 | } else { |
| 280 | return 1; |
| 281 | } |
| 282 | } |
| 283 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 284 | PyObject* _pysqlite_connection_begin(pysqlite_Connection* self) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 295 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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 { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 303 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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()) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 311 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 323 | PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 324 | { |
| 325 | int rc; |
| 326 | const char* tail; |
| 327 | sqlite3_stmt* statement; |
| 328 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 329 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 338 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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 { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 346 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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()) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 353 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 367 | PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 368 | { |
| 369 | int rc; |
| 370 | const char* tail; |
| 371 | sqlite3_stmt* statement; |
| 372 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 373 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | if (self->inTransaction) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 378 | pysqlite_reset_all_statements(self); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 379 | |
| 380 | Py_BEGIN_ALLOW_THREADS |
| 381 | rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail); |
| 382 | Py_END_ALLOW_THREADS |
| 383 | if (rc != SQLITE_OK) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 384 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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 { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 392 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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()) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 399 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 413 | void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 414 | { |
| 415 | long longval; |
| 416 | const char* buffer; |
Neal Norwitz | 95f0e4c | 2006-04-01 09:08:06 +0000 | [diff] [blame] | 417 | Py_ssize_t buflen; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 418 | PyObject* stringval; |
| 419 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 420 | if ((!py_val) || PyErr_Occurred()) { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 421 | sqlite3_result_null(context); |
| 422 | } else if (py_val == Py_None) { |
| 423 | sqlite3_result_null(context); |
| 424 | } else if (PyInt_Check(py_val)) { |
| 425 | longval = PyInt_AsLong(py_val); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 426 | sqlite3_result_int64(context, (PY_LONG_LONG)longval); |
| 427 | } else if (PyFloat_Check(py_val)) { |
| 428 | sqlite3_result_double(context, PyFloat_AsDouble(py_val)); |
| 429 | } else if (PyBuffer_Check(py_val)) { |
| 430 | if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) { |
| 431 | PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer"); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 432 | } else { |
| 433 | sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 434 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 435 | } else if (PyString_Check(py_val)) { |
| 436 | sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT); |
| 437 | } else if (PyUnicode_Check(py_val)) { |
| 438 | stringval = PyUnicode_AsUTF8String(py_val); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 439 | if (stringval) { |
| 440 | sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT); |
| 441 | Py_DECREF(stringval); |
| 442 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 443 | } else { |
| 444 | /* TODO: raise error */ |
| 445 | } |
| 446 | } |
| 447 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 448 | PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 449 | { |
| 450 | PyObject* args; |
| 451 | int i; |
| 452 | sqlite3_value* cur_value; |
| 453 | PyObject* cur_py_value; |
| 454 | const char* val_str; |
| 455 | PY_LONG_LONG val_int; |
Neal Norwitz | 95f0e4c | 2006-04-01 09:08:06 +0000 | [diff] [blame] | 456 | Py_ssize_t buflen; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 457 | void* raw_buffer; |
| 458 | |
| 459 | args = PyTuple_New(argc); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 460 | if (!args) { |
| 461 | return NULL; |
| 462 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 463 | |
| 464 | for (i = 0; i < argc; i++) { |
| 465 | cur_value = argv[i]; |
| 466 | switch (sqlite3_value_type(argv[i])) { |
| 467 | case SQLITE_INTEGER: |
| 468 | val_int = sqlite3_value_int64(cur_value); |
| 469 | cur_py_value = PyInt_FromLong((long)val_int); |
| 470 | break; |
| 471 | case SQLITE_FLOAT: |
| 472 | cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value)); |
| 473 | break; |
| 474 | case SQLITE_TEXT: |
| 475 | val_str = (const char*)sqlite3_value_text(cur_value); |
| 476 | cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL); |
| 477 | /* TODO: have a way to show errors here */ |
| 478 | if (!cur_py_value) { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 479 | PyErr_Clear(); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 480 | Py_INCREF(Py_None); |
| 481 | cur_py_value = Py_None; |
| 482 | } |
| 483 | break; |
| 484 | case SQLITE_BLOB: |
| 485 | buflen = sqlite3_value_bytes(cur_value); |
| 486 | cur_py_value = PyBuffer_New(buflen); |
| 487 | if (!cur_py_value) { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 488 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 489 | } |
| 490 | if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 491 | Py_DECREF(cur_py_value); |
| 492 | cur_py_value = NULL; |
| 493 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 494 | } |
| 495 | memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen); |
| 496 | break; |
| 497 | case SQLITE_NULL: |
| 498 | default: |
| 499 | Py_INCREF(Py_None); |
| 500 | cur_py_value = Py_None; |
| 501 | } |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 502 | |
| 503 | if (!cur_py_value) { |
| 504 | Py_DECREF(args); |
| 505 | return NULL; |
| 506 | } |
| 507 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 508 | PyTuple_SetItem(args, i, cur_py_value); |
| 509 | |
| 510 | } |
| 511 | |
| 512 | return args; |
| 513 | } |
| 514 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 515 | void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 516 | { |
| 517 | PyObject* args; |
| 518 | PyObject* py_func; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 519 | PyObject* py_retval = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 520 | |
| 521 | PyGILState_STATE threadstate; |
| 522 | |
| 523 | threadstate = PyGILState_Ensure(); |
| 524 | |
| 525 | py_func = (PyObject*)sqlite3_user_data(context); |
| 526 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 527 | args = _pysqlite_build_py_params(context, argc, argv); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 528 | if (args) { |
| 529 | py_retval = PyObject_CallObject(py_func, args); |
| 530 | Py_DECREF(args); |
| 531 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 532 | |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 533 | if (py_retval) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 534 | _pysqlite_set_result(context, py_retval); |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 535 | Py_DECREF(py_retval); |
| 536 | } else { |
| 537 | if (_enable_callback_tracebacks) { |
| 538 | PyErr_Print(); |
| 539 | } else { |
| 540 | PyErr_Clear(); |
| 541 | } |
Gerhard Häring | b2e8816 | 2006-06-14 22:28:37 +0000 | [diff] [blame] | 542 | _sqlite3_result_error(context, "user-defined function raised exception", -1); |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 543 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 544 | |
| 545 | PyGILState_Release(threadstate); |
| 546 | } |
| 547 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 548 | static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 549 | { |
| 550 | PyObject* args; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 551 | PyObject* function_result = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 552 | PyObject* aggregate_class; |
| 553 | PyObject** aggregate_instance; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 554 | PyObject* stepmethod = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 555 | |
| 556 | PyGILState_STATE threadstate; |
| 557 | |
| 558 | threadstate = PyGILState_Ensure(); |
| 559 | |
| 560 | aggregate_class = (PyObject*)sqlite3_user_data(context); |
| 561 | |
| 562 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); |
| 563 | |
| 564 | if (*aggregate_instance == 0) { |
| 565 | *aggregate_instance = PyObject_CallFunction(aggregate_class, ""); |
| 566 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 567 | if (PyErr_Occurred()) { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 568 | *aggregate_instance = 0; |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 569 | if (_enable_callback_tracebacks) { |
| 570 | PyErr_Print(); |
| 571 | } else { |
| 572 | PyErr_Clear(); |
| 573 | } |
Gerhard Häring | b2e8816 | 2006-06-14 22:28:37 +0000 | [diff] [blame] | 574 | _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 575 | goto error; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | stepmethod = PyObject_GetAttrString(*aggregate_instance, "step"); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 580 | if (!stepmethod) { |
| 581 | goto error; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 584 | args = _pysqlite_build_py_params(context, argc, params); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 585 | if (!args) { |
| 586 | goto error; |
| 587 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 588 | |
| 589 | function_result = PyObject_CallObject(stepmethod, args); |
| 590 | Py_DECREF(args); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 591 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 592 | if (!function_result) { |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 593 | if (_enable_callback_tracebacks) { |
| 594 | PyErr_Print(); |
| 595 | } else { |
| 596 | PyErr_Clear(); |
| 597 | } |
Gerhard Häring | b2e8816 | 2006-06-14 22:28:37 +0000 | [diff] [blame] | 598 | _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 601 | error: |
| 602 | Py_XDECREF(stepmethod); |
| 603 | Py_XDECREF(function_result); |
| 604 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 605 | PyGILState_Release(threadstate); |
| 606 | } |
| 607 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 608 | void _pysqlite_final_callback(sqlite3_context* context) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 609 | { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 610 | PyObject* function_result = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 611 | PyObject** aggregate_instance; |
| 612 | PyObject* aggregate_class; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 613 | |
| 614 | PyGILState_STATE threadstate; |
| 615 | |
| 616 | threadstate = PyGILState_Ensure(); |
| 617 | |
| 618 | aggregate_class = (PyObject*)sqlite3_user_data(context); |
| 619 | |
| 620 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); |
| 621 | if (!*aggregate_instance) { |
| 622 | /* this branch is executed if there was an exception in the aggregate's |
| 623 | * __init__ */ |
| 624 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 625 | goto error; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 628 | function_result = PyObject_CallMethod(*aggregate_instance, "finalize", ""); |
| 629 | if (!function_result) { |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 630 | if (_enable_callback_tracebacks) { |
| 631 | PyErr_Print(); |
| 632 | } else { |
| 633 | PyErr_Clear(); |
| 634 | } |
Gerhard Häring | b2e8816 | 2006-06-14 22:28:37 +0000 | [diff] [blame] | 635 | _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1); |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 636 | } else { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 637 | _pysqlite_set_result(context, function_result); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 640 | error: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 641 | Py_XDECREF(*aggregate_instance); |
| 642 | Py_XDECREF(function_result); |
| 643 | |
| 644 | PyGILState_Release(threadstate); |
| 645 | } |
| 646 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 647 | void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self) |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 648 | { |
| 649 | PyObject* new_list; |
| 650 | PyObject* weakref; |
| 651 | int i; |
| 652 | |
| 653 | /* we only need to do this once in a while */ |
| 654 | if (self->created_statements++ < 200) { |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | self->created_statements = 0; |
| 659 | |
| 660 | new_list = PyList_New(0); |
| 661 | if (!new_list) { |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | for (i = 0; i < PyList_Size(self->statements); i++) { |
| 666 | weakref = PyList_GetItem(self->statements, i); |
Gerhard Häring | ecd2010 | 2006-06-19 21:17:35 +0000 | [diff] [blame] | 667 | if (PyWeakref_GetObject(weakref) != Py_None) { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 668 | if (PyList_Append(new_list, weakref) != 0) { |
| 669 | Py_DECREF(new_list); |
| 670 | return; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | Py_DECREF(self->statements); |
| 676 | self->statements = new_list; |
| 677 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 678 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 679 | PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 680 | { |
| 681 | static char *kwlist[] = {"name", "narg", "func", NULL, NULL}; |
| 682 | |
| 683 | PyObject* func; |
| 684 | char* name; |
| 685 | int narg; |
| 686 | int rc; |
| 687 | |
| 688 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist, |
| 689 | &name, &narg, &func)) |
| 690 | { |
| 691 | return NULL; |
| 692 | } |
| 693 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 694 | rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 695 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 696 | if (rc != SQLITE_OK) { |
| 697 | /* Workaround for SQLite bug: no error code or string is available here */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 698 | PyErr_SetString(pysqlite_OperationalError, "Error creating function"); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 699 | return NULL; |
| 700 | } else { |
| 701 | PyDict_SetItem(self->function_pinboard, func, Py_None); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 702 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 703 | Py_INCREF(Py_None); |
| 704 | return Py_None; |
| 705 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 708 | PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 709 | { |
| 710 | PyObject* aggregate_class; |
| 711 | |
| 712 | int n_arg; |
| 713 | char* name; |
| 714 | static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL }; |
| 715 | int rc; |
| 716 | |
| 717 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate", |
| 718 | kwlist, &name, &n_arg, &aggregate_class)) { |
| 719 | return NULL; |
| 720 | } |
| 721 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 722 | rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 723 | if (rc != SQLITE_OK) { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 724 | /* Workaround for SQLite bug: no error code or string is available here */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 725 | PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate"); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 726 | return NULL; |
| 727 | } else { |
| 728 | PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None); |
| 729 | |
| 730 | Py_INCREF(Py_None); |
| 731 | return Py_None; |
| 732 | } |
| 733 | } |
| 734 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 735 | static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source) |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 736 | { |
| 737 | PyObject *ret; |
| 738 | int rc; |
| 739 | PyGILState_STATE gilstate; |
| 740 | |
| 741 | gilstate = PyGILState_Ensure(); |
| 742 | ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source); |
| 743 | |
| 744 | if (!ret) { |
| 745 | if (_enable_callback_tracebacks) { |
| 746 | PyErr_Print(); |
| 747 | } else { |
| 748 | PyErr_Clear(); |
| 749 | } |
| 750 | |
| 751 | rc = SQLITE_DENY; |
| 752 | } else { |
| 753 | if (PyInt_Check(ret)) { |
| 754 | rc = (int)PyInt_AsLong(ret); |
| 755 | } else { |
| 756 | rc = SQLITE_DENY; |
| 757 | } |
| 758 | Py_DECREF(ret); |
| 759 | } |
| 760 | |
| 761 | PyGILState_Release(gilstate); |
| 762 | return rc; |
| 763 | } |
| 764 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 765 | PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 766 | { |
| 767 | PyObject* authorizer_cb; |
| 768 | |
| 769 | static char *kwlist[] = { "authorizer_callback", NULL }; |
| 770 | int rc; |
| 771 | |
| 772 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer", |
| 773 | kwlist, &authorizer_cb)) { |
| 774 | return NULL; |
| 775 | } |
| 776 | |
| 777 | rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb); |
| 778 | |
| 779 | if (rc != SQLITE_OK) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 780 | PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback"); |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 781 | return NULL; |
| 782 | } else { |
| 783 | PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None); |
| 784 | |
| 785 | Py_INCREF(Py_None); |
| 786 | return Py_None; |
| 787 | } |
| 788 | } |
| 789 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 790 | int pysqlite_check_thread(pysqlite_Connection* self) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 791 | { |
| 792 | if (self->check_same_thread) { |
| 793 | if (PyThread_get_thread_ident() != self->thread_ident) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 794 | PyErr_Format(pysqlite_ProgrammingError, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 795 | "SQLite objects created in a thread can only be used in that same thread." |
| 796 | "The object was created in thread id %ld and this is thread id %ld", |
| 797 | self->thread_ident, PyThread_get_thread_ident()); |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | } |
| 802 | |
| 803 | return 1; |
| 804 | } |
| 805 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 806 | static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 807 | { |
| 808 | Py_INCREF(self->isolation_level); |
| 809 | return self->isolation_level; |
| 810 | } |
| 811 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 812 | static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused) |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 813 | { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 814 | if (!pysqlite_check_connection(self)) { |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 815 | return NULL; |
| 816 | } else { |
| 817 | return Py_BuildValue("i", sqlite3_total_changes(self->db)); |
| 818 | } |
| 819 | } |
| 820 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 821 | static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 822 | { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 823 | PyObject* res; |
| 824 | PyObject* begin_statement; |
| 825 | |
| 826 | Py_XDECREF(self->isolation_level); |
| 827 | |
Neal Norwitz | 5b03065 | 2006-04-16 03:28:17 +0000 | [diff] [blame] | 828 | if (self->begin_statement) { |
| 829 | PyMem_Free(self->begin_statement); |
| 830 | self->begin_statement = NULL; |
| 831 | } |
| 832 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 833 | if (isolation_level == Py_None) { |
| 834 | Py_INCREF(Py_None); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 835 | self->isolation_level = Py_None; |
| 836 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 837 | res = pysqlite_connection_commit(self, NULL); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 838 | if (!res) { |
| 839 | return -1; |
| 840 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 841 | Py_DECREF(res); |
| 842 | |
| 843 | self->inTransaction = 0; |
| 844 | } else { |
| 845 | Py_INCREF(isolation_level); |
| 846 | self->isolation_level = isolation_level; |
| 847 | |
| 848 | begin_statement = PyString_FromString("BEGIN "); |
| 849 | if (!begin_statement) { |
| 850 | return -1; |
| 851 | } |
| 852 | PyString_Concat(&begin_statement, isolation_level); |
| 853 | if (!begin_statement) { |
| 854 | return -1; |
| 855 | } |
| 856 | |
| 857 | self->begin_statement = PyMem_Malloc(PyString_Size(begin_statement) + 2); |
| 858 | if (!self->begin_statement) { |
| 859 | return -1; |
| 860 | } |
| 861 | |
| 862 | strcpy(self->begin_statement, PyString_AsString(begin_statement)); |
| 863 | Py_DECREF(begin_statement); |
| 864 | } |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 869 | PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 870 | { |
| 871 | PyObject* sql; |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 872 | pysqlite_Statement* statement; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 873 | PyObject* weakref; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 874 | int rc; |
| 875 | |
| 876 | if (!PyArg_ParseTuple(args, "O", &sql)) { |
| 877 | return NULL; |
| 878 | } |
| 879 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 880 | _pysqlite_drop_unused_statement_references(self); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 881 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 882 | statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 883 | if (!statement) { |
| 884 | return NULL; |
| 885 | } |
| 886 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 887 | rc = pysqlite_statement_create(statement, self, sql); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 888 | |
| 889 | if (rc != SQLITE_OK) { |
| 890 | if (rc == PYSQLITE_TOO_MUCH_SQL) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 891 | PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time."); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 892 | } else if (rc == PYSQLITE_SQL_WRONG_TYPE) { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 893 | PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode."); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 894 | } else { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 895 | _pysqlite_seterror(self->db); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | Py_DECREF(statement); |
| 899 | statement = 0; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 900 | } else { |
| 901 | weakref = PyWeakref_NewRef((PyObject*)statement, NULL); |
| 902 | if (!weakref) { |
| 903 | Py_DECREF(statement); |
| 904 | statement = 0; |
| 905 | goto error; |
| 906 | } |
| 907 | |
| 908 | if (PyList_Append(self->statements, weakref) != 0) { |
| 909 | Py_DECREF(weakref); |
| 910 | statement = 0; |
| 911 | goto error; |
| 912 | } |
| 913 | |
| 914 | Py_DECREF(weakref); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 917 | error: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 918 | return (PyObject*)statement; |
| 919 | } |
| 920 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 921 | PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 922 | { |
| 923 | PyObject* cursor = 0; |
| 924 | PyObject* result = 0; |
| 925 | PyObject* method = 0; |
| 926 | |
| 927 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 928 | if (!cursor) { |
| 929 | goto error; |
| 930 | } |
| 931 | |
| 932 | method = PyObject_GetAttrString(cursor, "execute"); |
| 933 | if (!method) { |
| 934 | Py_DECREF(cursor); |
| 935 | cursor = 0; |
| 936 | goto error; |
| 937 | } |
| 938 | |
| 939 | result = PyObject_CallObject(method, args); |
| 940 | if (!result) { |
| 941 | Py_DECREF(cursor); |
| 942 | cursor = 0; |
| 943 | } |
| 944 | |
| 945 | error: |
| 946 | Py_XDECREF(result); |
| 947 | Py_XDECREF(method); |
| 948 | |
| 949 | return cursor; |
| 950 | } |
| 951 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 952 | PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 953 | { |
| 954 | PyObject* cursor = 0; |
| 955 | PyObject* result = 0; |
| 956 | PyObject* method = 0; |
| 957 | |
| 958 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 959 | if (!cursor) { |
| 960 | goto error; |
| 961 | } |
| 962 | |
| 963 | method = PyObject_GetAttrString(cursor, "executemany"); |
| 964 | if (!method) { |
| 965 | Py_DECREF(cursor); |
| 966 | cursor = 0; |
| 967 | goto error; |
| 968 | } |
| 969 | |
| 970 | result = PyObject_CallObject(method, args); |
| 971 | if (!result) { |
| 972 | Py_DECREF(cursor); |
| 973 | cursor = 0; |
| 974 | } |
| 975 | |
| 976 | error: |
| 977 | Py_XDECREF(result); |
| 978 | Py_XDECREF(method); |
| 979 | |
| 980 | return cursor; |
| 981 | } |
| 982 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 983 | PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 984 | { |
| 985 | PyObject* cursor = 0; |
| 986 | PyObject* result = 0; |
| 987 | PyObject* method = 0; |
| 988 | |
| 989 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 990 | if (!cursor) { |
| 991 | goto error; |
| 992 | } |
| 993 | |
| 994 | method = PyObject_GetAttrString(cursor, "executescript"); |
| 995 | if (!method) { |
| 996 | Py_DECREF(cursor); |
| 997 | cursor = 0; |
| 998 | goto error; |
| 999 | } |
| 1000 | |
| 1001 | result = PyObject_CallObject(method, args); |
| 1002 | if (!result) { |
| 1003 | Py_DECREF(cursor); |
| 1004 | cursor = 0; |
| 1005 | } |
| 1006 | |
| 1007 | error: |
| 1008 | Py_XDECREF(result); |
| 1009 | Py_XDECREF(method); |
| 1010 | |
| 1011 | return cursor; |
| 1012 | } |
| 1013 | |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1014 | /* ------------------------- COLLATION CODE ------------------------ */ |
| 1015 | |
| 1016 | static int |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1017 | pysqlite_collation_callback( |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1018 | void* context, |
| 1019 | int text1_length, const void* text1_data, |
| 1020 | int text2_length, const void* text2_data) |
| 1021 | { |
| 1022 | PyObject* callback = (PyObject*)context; |
| 1023 | PyObject* string1 = 0; |
| 1024 | PyObject* string2 = 0; |
| 1025 | PyGILState_STATE gilstate; |
| 1026 | |
| 1027 | PyObject* retval = NULL; |
| 1028 | int result = 0; |
| 1029 | |
| 1030 | gilstate = PyGILState_Ensure(); |
| 1031 | |
| 1032 | if (PyErr_Occurred()) { |
| 1033 | goto finally; |
| 1034 | } |
| 1035 | |
| 1036 | string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length); |
| 1037 | string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length); |
| 1038 | |
| 1039 | if (!string1 || !string2) { |
| 1040 | goto finally; /* failed to allocate strings */ |
| 1041 | } |
| 1042 | |
| 1043 | retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL); |
| 1044 | |
| 1045 | if (!retval) { |
| 1046 | /* execution failed */ |
| 1047 | goto finally; |
| 1048 | } |
| 1049 | |
| 1050 | result = PyInt_AsLong(retval); |
| 1051 | if (PyErr_Occurred()) { |
| 1052 | result = 0; |
| 1053 | } |
| 1054 | |
| 1055 | finally: |
| 1056 | Py_XDECREF(string1); |
| 1057 | Py_XDECREF(string2); |
| 1058 | Py_XDECREF(retval); |
| 1059 | |
| 1060 | PyGILState_Release(gilstate); |
| 1061 | |
| 1062 | return result; |
| 1063 | } |
| 1064 | |
| 1065 | static PyObject * |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1066 | pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args) |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 1067 | { |
| 1068 | PyObject* retval = NULL; |
| 1069 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1070 | if (!pysqlite_check_connection(self)) { |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 1071 | goto finally; |
| 1072 | } |
| 1073 | |
| 1074 | sqlite3_interrupt(self->db); |
| 1075 | |
| 1076 | Py_INCREF(Py_None); |
| 1077 | retval = Py_None; |
| 1078 | |
| 1079 | finally: |
| 1080 | return retval; |
| 1081 | } |
| 1082 | |
| 1083 | static PyObject * |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1084 | pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1085 | { |
| 1086 | PyObject* callable; |
| 1087 | PyObject* uppercase_name = 0; |
| 1088 | PyObject* name; |
| 1089 | PyObject* retval; |
| 1090 | char* chk; |
| 1091 | int rc; |
| 1092 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1093 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1094 | goto finally; |
| 1095 | } |
| 1096 | |
| 1097 | if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyString_Type, &name, &callable)) { |
| 1098 | goto finally; |
| 1099 | } |
| 1100 | |
| 1101 | uppercase_name = PyObject_CallMethod(name, "upper", ""); |
| 1102 | if (!uppercase_name) { |
| 1103 | goto finally; |
| 1104 | } |
| 1105 | |
| 1106 | chk = PyString_AsString(uppercase_name); |
| 1107 | while (*chk) { |
| 1108 | if ((*chk >= '0' && *chk <= '9') |
| 1109 | || (*chk >= 'A' && *chk <= 'Z') |
| 1110 | || (*chk == '_')) |
| 1111 | { |
| 1112 | chk++; |
| 1113 | } else { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1114 | PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name"); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1115 | goto finally; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | if (callable != Py_None && !PyCallable_Check(callable)) { |
| 1120 | PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 1121 | goto finally; |
| 1122 | } |
| 1123 | |
| 1124 | if (callable != Py_None) { |
| 1125 | PyDict_SetItem(self->collations, uppercase_name, callable); |
| 1126 | } else { |
| 1127 | PyDict_DelItem(self->collations, uppercase_name); |
| 1128 | } |
| 1129 | |
| 1130 | rc = sqlite3_create_collation(self->db, |
| 1131 | PyString_AsString(uppercase_name), |
| 1132 | SQLITE_UTF8, |
| 1133 | (callable != Py_None) ? callable : NULL, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1134 | (callable != Py_None) ? pysqlite_collation_callback : NULL); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1135 | if (rc != SQLITE_OK) { |
| 1136 | PyDict_DelItem(self->collations, uppercase_name); |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1137 | _pysqlite_seterror(self->db); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1138 | goto finally; |
| 1139 | } |
| 1140 | |
| 1141 | finally: |
| 1142 | Py_XDECREF(uppercase_name); |
| 1143 | |
| 1144 | if (PyErr_Occurred()) { |
| 1145 | retval = NULL; |
| 1146 | } else { |
| 1147 | Py_INCREF(Py_None); |
| 1148 | retval = Py_None; |
| 1149 | } |
| 1150 | |
| 1151 | return retval; |
| 1152 | } |
| 1153 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1154 | static char connection_doc[] = |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 1155 | PyDoc_STR("SQLite database connection object."); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1156 | |
| 1157 | static PyGetSetDef connection_getset[] = { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1158 | {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level}, |
| 1159 | {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0}, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1160 | {NULL} |
| 1161 | }; |
| 1162 | |
| 1163 | static PyMethodDef connection_methods[] = { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1164 | {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1165 | PyDoc_STR("Return a cursor for the connection.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1166 | {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1167 | PyDoc_STR("Closes the connection.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1168 | {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1169 | PyDoc_STR("Commit the current transaction.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1170 | {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1171 | PyDoc_STR("Roll back the current transaction.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1172 | {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1173 | PyDoc_STR("Creates a new function. Non-standard.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1174 | {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1175 | PyDoc_STR("Creates a new aggregate. Non-standard.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1176 | {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS, |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 1177 | PyDoc_STR("Sets authorizer callback. Non-standard.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1178 | {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1179 | PyDoc_STR("Executes a SQL statement. Non-standard.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1180 | {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1181 | PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1182 | {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1183 | PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1184 | {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS, |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 1185 | PyDoc_STR("Creates a collation function. Non-standard.")}, |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1186 | {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS, |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame] | 1187 | PyDoc_STR("Abort any pending database operation. Non-standard.")}, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1188 | {NULL, NULL} |
| 1189 | }; |
| 1190 | |
| 1191 | static struct PyMemberDef connection_members[] = |
| 1192 | { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1193 | {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), RO}, |
| 1194 | {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), RO}, |
| 1195 | {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), RO}, |
| 1196 | {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), RO}, |
| 1197 | {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), RO}, |
| 1198 | {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), RO}, |
| 1199 | {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), RO}, |
| 1200 | {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), RO}, |
| 1201 | {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), RO}, |
| 1202 | {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), RO}, |
| 1203 | {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)}, |
| 1204 | {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)}, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1205 | {NULL} |
| 1206 | }; |
| 1207 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1208 | PyTypeObject pysqlite_ConnectionType = { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1209 | PyObject_HEAD_INIT(NULL) |
| 1210 | 0, /* ob_size */ |
Anthony Baxter | 8e7b490 | 2006-04-05 18:25:33 +0000 | [diff] [blame] | 1211 | MODULE_NAME ".Connection", /* tp_name */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1212 | sizeof(pysqlite_Connection), /* tp_basicsize */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1213 | 0, /* tp_itemsize */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1214 | (destructor)pysqlite_connection_dealloc, /* tp_dealloc */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1215 | 0, /* tp_print */ |
| 1216 | 0, /* tp_getattr */ |
| 1217 | 0, /* tp_setattr */ |
| 1218 | 0, /* tp_compare */ |
| 1219 | 0, /* tp_repr */ |
| 1220 | 0, /* tp_as_number */ |
| 1221 | 0, /* tp_as_sequence */ |
| 1222 | 0, /* tp_as_mapping */ |
| 1223 | 0, /* tp_hash */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1224 | (ternaryfunc)pysqlite_connection_call, /* tp_call */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1225 | 0, /* tp_str */ |
| 1226 | 0, /* tp_getattro */ |
| 1227 | 0, /* tp_setattro */ |
| 1228 | 0, /* tp_as_buffer */ |
| 1229 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1230 | connection_doc, /* tp_doc */ |
| 1231 | 0, /* tp_traverse */ |
| 1232 | 0, /* tp_clear */ |
| 1233 | 0, /* tp_richcompare */ |
| 1234 | 0, /* tp_weaklistoffset */ |
| 1235 | 0, /* tp_iter */ |
| 1236 | 0, /* tp_iternext */ |
| 1237 | connection_methods, /* tp_methods */ |
| 1238 | connection_members, /* tp_members */ |
| 1239 | connection_getset, /* tp_getset */ |
| 1240 | 0, /* tp_base */ |
| 1241 | 0, /* tp_dict */ |
| 1242 | 0, /* tp_descr_get */ |
| 1243 | 0, /* tp_descr_set */ |
| 1244 | 0, /* tp_dictoffset */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1245 | (initproc)pysqlite_connection_init, /* tp_init */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1246 | 0, /* tp_alloc */ |
| 1247 | 0, /* tp_new */ |
| 1248 | 0 /* tp_free */ |
| 1249 | }; |
| 1250 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1251 | extern int pysqlite_connection_setup_types(void) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1252 | { |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame^] | 1253 | pysqlite_ConnectionType.tp_new = PyType_GenericNew; |
| 1254 | return PyType_Ready(&pysqlite_ConnectionType); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1255 | } |