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