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