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