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