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