blob: 121850ae7e1f32a96155ef9579650ed0b6e98d16 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* connection.c - the connection type
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
5 * This file is part of pysqlite.
Victor Stinner86999502010-05-19 01:27:23 +00006 *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007 * 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"
Victor Stinner4a21e572020-04-15 02:35:41 +020026#include "structmember.h" // PyMemberDef
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027#include "connection.h"
28#include "statement.h"
29#include "cursor.h"
30#include "prepare_protocol.h"
31#include "util.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032
Gerhard Häringe7ea7452008-03-29 00:45:29 +000033#define ACTION_FINALIZE 1
34#define ACTION_RESET 2
35
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +020036#if SQLITE_VERSION_NUMBER >= 3014000
37#define HAVE_TRACE_V2
38#endif
39
Martin v. Löwise75fc142013-11-07 18:46:53 +010040_Py_IDENTIFIER(cursor);
41
Serhiy Storchaka28914922016-09-01 22:18:03 +030042static const char * const begin_statements[] = {
43 "BEGIN ",
44 "BEGIN DEFERRED",
45 "BEGIN IMMEDIATE",
46 "BEGIN EXCLUSIVE",
47 NULL
48};
49
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +020050static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored));
Gerhard Häringf9cee222010-03-05 15:20:03 +000051static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052
Thomas Wouters0e3f5912006-08-11 14:57:12 +000053
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000054int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010056 static char *kwlist[] = {
57 "database", "timeout", "detect_types", "isolation_level",
58 "check_same_thread", "factory", "cached_statements", "uri",
59 NULL
60 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000061
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +030062 const char* database;
Anders Lorentsena22a1272017-11-07 01:47:43 +010063 PyObject* database_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000064 int detect_types = 0;
65 PyObject* isolation_level = NULL;
66 PyObject* factory = NULL;
67 int check_same_thread = 1;
68 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010069 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000070 double timeout = 5.0;
71 int rc;
72
Anders Lorentsena22a1272017-11-07 01:47:43 +010073 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
74 PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010075 &isolation_level, &check_same_thread,
76 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000078 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000079 }
80
Anders Lorentsena22a1272017-11-07 01:47:43 +010081 database = PyBytes_AsString(database_obj);
82
Gerhard Häringf9cee222010-03-05 15:20:03 +000083 self->initialized = 1;
84
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000085 self->begin_statement = NULL;
86
Oren Milman93c5a5d2017-10-10 22:27:46 +030087 Py_CLEAR(self->statement_cache);
88 Py_CLEAR(self->statements);
89 Py_CLEAR(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000090
91 Py_INCREF(Py_None);
Oren Milman93c5a5d2017-10-10 22:27:46 +030092 Py_XSETREF(self->row_factory, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093
94 Py_INCREF(&PyUnicode_Type);
Oren Milman93c5a5d2017-10-10 22:27:46 +030095 Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010097#ifdef SQLITE_OPEN_URI
98 Py_BEGIN_ALLOW_THREADS
99 rc = sqlite3_open_v2(database, &self->db,
100 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
101 (uri ? SQLITE_OPEN_URI : 0), NULL);
102#else
103 if (uri) {
104 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
105 return -1;
106 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200108 /* No need to use sqlite3_open_v2 as sqlite3_open(filename, db) is the
109 same as sqlite3_open_v2(filename, db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL). */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100111#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112 Py_END_ALLOW_THREADS
113
Anders Lorentsena22a1272017-11-07 01:47:43 +0100114 Py_DECREF(database_obj);
115
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000117 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 return -1;
119 }
120
121 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000122 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000123 if (!isolation_level) {
124 return -1;
125 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 } else {
127 Py_INCREF(isolation_level);
128 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300129 Py_CLEAR(self->isolation_level);
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200130 if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100131 Py_DECREF(isolation_level);
132 return -1;
133 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 Py_DECREF(isolation_level);
135
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200136 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000137 if (PyErr_Occurred()) {
138 return -1;
139 }
140
Gerhard Häringf9cee222010-03-05 15:20:03 +0000141 self->created_statements = 0;
142 self->created_cursors = 0;
143
144 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000145 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000146 self->cursors = PyList_New(0);
147 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000148 return -1;
149 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000150
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000151 /* By default, the Cache class INCREFs the factory in its initializer, and
152 * decrefs it in its deallocator method. Since this would create a circular
153 * reference here, we're breaking it by decrementing self, and telling the
154 * cache class to not decref the factory (self) in its deallocator.
155 */
156 self->statement_cache->decref_factory = 0;
157 Py_DECREF(self);
158
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159 self->detect_types = detect_types;
160 self->timeout = timeout;
161 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000162 self->thread_ident = PyThread_get_thread_ident();
163 self->check_same_thread = check_same_thread;
164
gescheitb9a03762019-07-13 06:15:49 +0300165 self->function_pinboard_trace_callback = NULL;
166 self->function_pinboard_progress_handler = NULL;
167 self->function_pinboard_authorizer_cb = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000168
Oren Milman93c5a5d2017-10-10 22:27:46 +0300169 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000170 if (!self->collations) {
171 return -1;
172 }
173
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000174 self->Warning = pysqlite_Warning;
175 self->Error = pysqlite_Error;
176 self->InterfaceError = pysqlite_InterfaceError;
177 self->DatabaseError = pysqlite_DatabaseError;
178 self->DataError = pysqlite_DataError;
179 self->OperationalError = pysqlite_OperationalError;
180 self->IntegrityError = pysqlite_IntegrityError;
181 self->InternalError = pysqlite_InternalError;
182 self->ProgrammingError = pysqlite_ProgrammingError;
183 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000184
185 return 0;
186}
187
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000188/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000189void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000191 int i;
192 PyObject* weakref;
193 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000194 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195
Thomas Wouters477c8d52006-05-27 19:21:47 +0000196 for (i = 0; i < PyList_Size(self->statements); i++) {
197 weakref = PyList_GetItem(self->statements, i);
198 statement = PyWeakref_GetObject(weakref);
199 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500200 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000201 if (action == ACTION_RESET) {
202 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
203 } else {
204 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
205 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500206 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000207 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000209
210 if (reset_cursors) {
211 for (i = 0; i < PyList_Size(self->cursors); i++) {
212 weakref = PyList_GetItem(self->cursors, i);
213 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
214 if ((PyObject*)cursor != Py_None) {
215 cursor->reset = 1;
216 }
217 }
218 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000219}
220
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000221void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222{
223 Py_XDECREF(self->statement_cache);
224
225 /* Clean up if user has not called .close() explicitly. */
226 if (self->db) {
Aviv Palivoda86a67052017-03-03 12:58:17 +0200227 SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000228 }
229
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230 Py_XDECREF(self->isolation_level);
gescheitb9a03762019-07-13 06:15:49 +0300231 Py_XDECREF(self->function_pinboard_trace_callback);
232 Py_XDECREF(self->function_pinboard_progress_handler);
233 Py_XDECREF(self->function_pinboard_authorizer_cb);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000234 Py_XDECREF(self->row_factory);
235 Py_XDECREF(self->text_factory);
236 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000237 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000238 Py_XDECREF(self->cursors);
Christian Heimes90aa7642007-12-19 02:45:37 +0000239 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240}
241
Gerhard Häringf9cee222010-03-05 15:20:03 +0000242/*
243 * Registers a cursor with the connection.
244 *
245 * 0 => error; 1 => ok
246 */
247int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
248{
249 PyObject* weakref;
250
251 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
252 if (!weakref) {
253 goto error;
254 }
255
256 if (PyList_Append(connection->cursors, weakref) != 0) {
257 Py_CLEAR(weakref);
258 goto error;
259 }
260
261 Py_DECREF(weakref);
262
263 return 1;
264error:
265 return 0;
266}
267
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000268PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300270 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000271 PyObject* factory = NULL;
272 PyObject* cursor;
273
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
275 &factory)) {
276 return NULL;
277 }
278
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000279 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000280 return NULL;
281 }
282
283 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000284 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285 }
286
Petr Viktorinffd97532020-02-11 17:46:57 +0100287 cursor = PyObject_CallOneArg(factory, (PyObject *)self);
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300288 if (cursor == NULL)
289 return NULL;
290 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
291 PyErr_Format(PyExc_TypeError,
292 "factory must return a cursor, not %.100s",
293 Py_TYPE(cursor)->tp_name);
294 Py_DECREF(cursor);
295 return NULL;
296 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000297
Gerhard Häringf9cee222010-03-05 15:20:03 +0000298 _pysqlite_drop_unused_cursor_references(self);
299
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000300 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000301 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300302 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303 }
304
305 return cursor;
306}
307
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000308PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000309{
310 int rc;
311
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000312 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313 return NULL;
314 }
315
Gerhard Häringf9cee222010-03-05 15:20:03 +0000316 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317
318 if (self->db) {
Aviv Palivoda86a67052017-03-03 12:58:17 +0200319 rc = SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320
321 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000322 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 return NULL;
324 } else {
325 self->db = NULL;
326 }
327 }
328
Berker Peksagfe21de92016-04-09 07:34:39 +0300329 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330}
331
332/*
333 * Checks if a connection object is usable (i. e. not closed).
334 *
335 * 0 => error; 1 => ok
336 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000337int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000339 if (!con->initialized) {
340 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
341 return 0;
342 }
343
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000345 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346 return 0;
347 } else {
348 return 1;
349 }
350}
351
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000352PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000353{
354 int rc;
355 const char* tail;
356 sqlite3_stmt* statement;
357
358 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700359 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360 Py_END_ALLOW_THREADS
361
362 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000363 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364 goto error;
365 }
366
Benjamin Petersond7b03282008-09-13 15:58:53 +0000367 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300368 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000369 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 }
371
372 Py_BEGIN_ALLOW_THREADS
373 rc = sqlite3_finalize(statement);
374 Py_END_ALLOW_THREADS
375
376 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000377 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000378 }
379
380error:
381 if (PyErr_Occurred()) {
382 return NULL;
383 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200384 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 }
386}
387
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000388PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000389{
390 int rc;
391 const char* tail;
392 sqlite3_stmt* statement;
393
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000394 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000395 return NULL;
396 }
397
Berker Peksag59da4b32016-09-12 07:16:43 +0300398 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000399
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700401 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402 Py_END_ALLOW_THREADS
403 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000404 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405 goto error;
406 }
407
Benjamin Petersond7b03282008-09-13 15:58:53 +0000408 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300409 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000410 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000411 }
412
413 Py_BEGIN_ALLOW_THREADS
414 rc = sqlite3_finalize(statement);
415 Py_END_ALLOW_THREADS
416 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000417 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000418 }
419
420 }
421
422error:
423 if (PyErr_Occurred()) {
424 return NULL;
425 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200426 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000427 }
428}
429
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000430PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431{
432 int rc;
433 const char* tail;
434 sqlite3_stmt* statement;
435
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000436 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 return NULL;
438 }
439
Berker Peksag59da4b32016-09-12 07:16:43 +0300440 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000441 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442
443 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700444 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000445 Py_END_ALLOW_THREADS
446 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000447 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 goto error;
449 }
450
Benjamin Petersond7b03282008-09-13 15:58:53 +0000451 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300452 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000453 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000454 }
455
456 Py_BEGIN_ALLOW_THREADS
457 rc = sqlite3_finalize(statement);
458 Py_END_ALLOW_THREADS
459 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000460 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461 }
462
463 }
464
465error:
466 if (PyErr_Occurred()) {
467 return NULL;
468 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200469 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000470 }
471}
472
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200473static int
474_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200476 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000477 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000478 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200479 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
480 if (value == -1 && PyErr_Occurred())
481 return -1;
482 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483 } else if (PyFloat_Check(py_val)) {
484 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000485 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200486 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200487 if (str == NULL)
488 return -1;
489 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000490 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200491 Py_buffer view;
492 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100493 PyErr_SetString(PyExc_ValueError,
494 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200495 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000496 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200497 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100498 PyErr_SetString(PyExc_OverflowError,
499 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200500 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100501 return -1;
502 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200503 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
504 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200506 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200508 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509}
510
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000511PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512{
513 PyObject* args;
514 int i;
515 sqlite3_value* cur_value;
516 PyObject* cur_py_value;
517 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000518 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000519
520 args = PyTuple_New(argc);
521 if (!args) {
522 return NULL;
523 }
524
525 for (i = 0; i < argc; i++) {
526 cur_value = argv[i];
527 switch (sqlite3_value_type(argv[i])) {
528 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500529 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000530 break;
531 case SQLITE_FLOAT:
532 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
533 break;
534 case SQLITE_TEXT:
535 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000536 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537 /* TODO: have a way to show errors here */
538 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000539 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000540 Py_INCREF(Py_None);
541 cur_py_value = Py_None;
542 }
543 break;
544 case SQLITE_BLOB:
545 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000546 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000547 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548 break;
549 case SQLITE_NULL:
550 default:
551 Py_INCREF(Py_None);
552 cur_py_value = Py_None;
553 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000554
555 if (!cur_py_value) {
556 Py_DECREF(args);
557 return NULL;
558 }
559
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000560 PyTuple_SetItem(args, i, cur_py_value);
561
562 }
563
564 return args;
565}
566
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000567void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000568{
569 PyObject* args;
570 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000571 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200572 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000573
574 PyGILState_STATE threadstate;
575
576 threadstate = PyGILState_Ensure();
577
578 py_func = (PyObject*)sqlite3_user_data(context);
579
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000580 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000581 if (args) {
582 py_retval = PyObject_CallObject(py_func, args);
583 Py_DECREF(args);
584 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000585
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200586 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000587 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200588 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000589 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200590 }
591 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700592 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593 PyErr_Print();
594 } else {
595 PyErr_Clear();
596 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200597 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000598 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000599
600 PyGILState_Release(threadstate);
601}
602
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000603static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604{
605 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000606 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000607 PyObject* aggregate_class;
608 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000609 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610
611 PyGILState_STATE threadstate;
612
613 threadstate = PyGILState_Ensure();
614
615 aggregate_class = (PyObject*)sqlite3_user_data(context);
616
617 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
618
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200619 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100620 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621
Thomas Wouters477c8d52006-05-27 19:21:47 +0000622 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700624 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000625 PyErr_Print();
626 } else {
627 PyErr_Clear();
628 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200629 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000630 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000631 }
632 }
633
634 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000635 if (!stepmethod) {
636 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637 }
638
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000639 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000640 if (!args) {
641 goto error;
642 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643
644 function_result = PyObject_CallObject(stepmethod, args);
645 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646
Thomas Wouters477c8d52006-05-27 19:21:47 +0000647 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700648 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000649 PyErr_Print();
650 } else {
651 PyErr_Clear();
652 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200653 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654 }
655
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656error:
657 Py_XDECREF(stepmethod);
658 Py_XDECREF(function_result);
659
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660 PyGILState_Release(threadstate);
661}
662
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000663void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000664{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200665 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200667 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200668 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200669 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670
671 PyGILState_STATE threadstate;
672
673 threadstate = PyGILState_Ensure();
674
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
676 if (!*aggregate_instance) {
677 /* this branch is executed if there was an exception in the aggregate's
678 * __init__ */
679
Thomas Wouters477c8d52006-05-27 19:21:47 +0000680 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000681 }
682
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200683 /* Keep the exception (if any) of the last call to step() */
684 PyErr_Fetch(&exception, &value, &tb);
685
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200686 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200687
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200688 Py_DECREF(*aggregate_instance);
689
690 ok = 0;
691 if (function_result) {
692 ok = _pysqlite_set_result(context, function_result) == 0;
693 Py_DECREF(function_result);
694 }
695 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700696 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000697 PyErr_Print();
698 } else {
699 PyErr_Clear();
700 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200701 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000702 }
703
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200704 /* Restore the exception (if any) of the last call to step(),
705 but clear also the current exception if finalize() failed */
706 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200707
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 PyGILState_Release(threadstate);
710}
711
Gerhard Häringf9cee222010-03-05 15:20:03 +0000712static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000713{
714 PyObject* new_list;
715 PyObject* weakref;
716 int i;
717
718 /* we only need to do this once in a while */
719 if (self->created_statements++ < 200) {
720 return;
721 }
722
723 self->created_statements = 0;
724
725 new_list = PyList_New(0);
726 if (!new_list) {
727 return;
728 }
729
730 for (i = 0; i < PyList_Size(self->statements); i++) {
731 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000732 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000733 if (PyList_Append(new_list, weakref) != 0) {
734 Py_DECREF(new_list);
735 return;
736 }
737 }
738 }
739
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300740 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000741}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000742
Gerhard Häringf9cee222010-03-05 15:20:03 +0000743static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
744{
745 PyObject* new_list;
746 PyObject* weakref;
747 int i;
748
749 /* we only need to do this once in a while */
750 if (self->created_cursors++ < 200) {
751 return;
752 }
753
754 self->created_cursors = 0;
755
756 new_list = PyList_New(0);
757 if (!new_list) {
758 return;
759 }
760
761 for (i = 0; i < PyList_Size(self->cursors); i++) {
762 weakref = PyList_GetItem(self->cursors, i);
763 if (PyWeakref_GetObject(weakref) != Py_None) {
764 if (PyList_Append(new_list, weakref) != 0) {
765 Py_DECREF(new_list);
766 return;
767 }
768 }
769 }
770
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300771 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000772}
773
gescheitb9a03762019-07-13 06:15:49 +0300774static void _destructor(void* args)
775{
776 Py_DECREF((PyObject*)args);
777}
778
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000779PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780{
Sergey Fedoseev08308582018-07-08 12:09:20 +0500781 static char *kwlist[] = {"name", "narg", "func", "deterministic", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782
783 PyObject* func;
784 char* name;
785 int narg;
786 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500787 int deterministic = 0;
788 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000789
Gerhard Häringf9cee222010-03-05 15:20:03 +0000790 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
791 return NULL;
792 }
793
Sergey Fedoseev08308582018-07-08 12:09:20 +0500794 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|$p", kwlist,
795 &name, &narg, &func, &deterministic))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000796 {
797 return NULL;
798 }
799
Sergey Fedoseev08308582018-07-08 12:09:20 +0500800 if (deterministic) {
801#if SQLITE_VERSION_NUMBER < 3008003
802 PyErr_SetString(pysqlite_NotSupportedError,
803 "deterministic=True requires SQLite 3.8.3 or higher");
804 return NULL;
805#else
806 if (sqlite3_libversion_number() < 3008003) {
807 PyErr_SetString(pysqlite_NotSupportedError,
808 "deterministic=True requires SQLite 3.8.3 or higher");
809 return NULL;
810 }
811 flags |= SQLITE_DETERMINISTIC;
812#endif
813 }
gescheitb9a03762019-07-13 06:15:49 +0300814 Py_INCREF(func);
815 rc = sqlite3_create_function_v2(self->db,
816 name,
817 narg,
818 flags,
819 (void*)func,
820 _pysqlite_func_callback,
821 NULL,
822 NULL,
823 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824
Thomas Wouters477c8d52006-05-27 19:21:47 +0000825 if (rc != SQLITE_OK) {
826 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000827 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000828 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000829 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500830 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831}
832
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000833PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834{
835 PyObject* aggregate_class;
836
837 int n_arg;
838 char* name;
839 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
840 int rc;
841
Gerhard Häringf9cee222010-03-05 15:20:03 +0000842 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
843 return NULL;
844 }
845
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
847 kwlist, &name, &n_arg, &aggregate_class)) {
848 return NULL;
849 }
gescheitb9a03762019-07-13 06:15:49 +0300850 Py_INCREF(aggregate_class);
851 rc = sqlite3_create_function_v2(self->db,
852 name,
853 n_arg,
854 SQLITE_UTF8,
855 (void*)aggregate_class,
856 0,
857 &_pysqlite_step_callback,
858 &_pysqlite_final_callback,
859 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000860 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000861 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000862 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000863 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500865 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866}
867
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000868static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000869{
870 PyObject *ret;
871 int rc;
872 PyGILState_STATE gilstate;
873
874 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000875
Victor Stinnerd4095d92013-07-26 22:23:33 +0200876 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000877
Victor Stinnerd4095d92013-07-26 22:23:33 +0200878 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700879 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200880 PyErr_Print();
881 else
882 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200883
Victor Stinnerd4095d92013-07-26 22:23:33 +0200884 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200885 }
886 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200887 if (PyLong_Check(ret)) {
888 rc = _PyLong_AsInt(ret);
889 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700890 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200891 PyErr_Print();
892 else
893 PyErr_Clear();
894 rc = SQLITE_DENY;
895 }
896 }
897 else {
898 rc = SQLITE_DENY;
899 }
900 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000901 }
902
903 PyGILState_Release(gilstate);
904 return rc;
905}
906
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000907static int _progress_handler(void* user_arg)
908{
909 int rc;
910 PyObject *ret;
911 PyGILState_STATE gilstate;
912
913 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100914 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000915
916 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700917 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000918 PyErr_Print();
919 } else {
920 PyErr_Clear();
921 }
922
Mark Dickinson934896d2009-02-21 20:59:32 +0000923 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000924 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000925 } else {
926 rc = (int)PyObject_IsTrue(ret);
927 Py_DECREF(ret);
928 }
929
930 PyGILState_Release(gilstate);
931 return rc;
932}
933
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200934#ifdef HAVE_TRACE_V2
935/*
936 * From https://sqlite.org/c3ref/trace_v2.html:
937 * The integer return value from the callback is currently ignored, though this
938 * may change in future releases. Callback implementations should return zero
939 * to ensure future compatibility.
940 */
941static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
942#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200943static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200944#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200945{
946 PyObject *py_statement = NULL;
947 PyObject *ret = NULL;
948
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200949 PyGILState_STATE gilstate;
950
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200951#ifdef HAVE_TRACE_V2
952 if (type != SQLITE_TRACE_STMT) {
953 return 0;
954 }
955#endif
956
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200957 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200958 py_statement = PyUnicode_DecodeUTF8(statement_string,
959 strlen(statement_string), "replace");
960 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100961 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200962 Py_DECREF(py_statement);
963 }
964
965 if (ret) {
966 Py_DECREF(ret);
967 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700968 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200969 PyErr_Print();
970 } else {
971 PyErr_Clear();
972 }
973 }
974
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200975 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200976#ifdef HAVE_TRACE_V2
977 return 0;
978#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200979}
980
Gerhard Häringf9cee222010-03-05 15:20:03 +0000981static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000982{
983 PyObject* authorizer_cb;
984
985 static char *kwlist[] = { "authorizer_callback", NULL };
986 int rc;
987
Gerhard Häringf9cee222010-03-05 15:20:03 +0000988 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
989 return NULL;
990 }
991
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000992 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
993 kwlist, &authorizer_cb)) {
994 return NULL;
995 }
996
997 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000998 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000999 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001000 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001001 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001002 } else {
1003 Py_INCREF(authorizer_cb);
1004 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001005 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001006 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001007}
1008
Gerhard Häringf9cee222010-03-05 15:20:03 +00001009static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001010{
1011 PyObject* progress_handler;
1012 int n;
1013
1014 static char *kwlist[] = { "progress_handler", "n", NULL };
1015
Gerhard Häringf9cee222010-03-05 15:20:03 +00001016 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1017 return NULL;
1018 }
1019
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001020 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1021 kwlist, &progress_handler, &n)) {
1022 return NULL;
1023 }
1024
1025 if (progress_handler == Py_None) {
1026 /* None clears the progress handler previously set */
1027 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001028 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001029 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001030 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001031 Py_INCREF(progress_handler);
1032 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001033 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001034 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001035}
1036
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001037/*
1038 * Ref.
1039 * - https://sqlite.org/c3ref/c_trace.html
1040 * - https://sqlite.org/c3ref/trace_v2.html
1041 */
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001042static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1043{
1044 PyObject* trace_callback;
1045
1046 static char *kwlist[] = { "trace_callback", NULL };
1047
1048 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1049 return NULL;
1050 }
1051
1052 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1053 kwlist, &trace_callback)) {
1054 return NULL;
1055 }
1056
1057 if (trace_callback == Py_None) {
1058 /* None clears the trace callback previously set */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001059#ifdef HAVE_TRACE_V2
1060 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1061#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001062 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001063#endif
gescheitb9a03762019-07-13 06:15:49 +03001064 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001065 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001066#ifdef HAVE_TRACE_V2
1067 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1068#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001069 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001070#endif
gescheitb9a03762019-07-13 06:15:49 +03001071 Py_INCREF(trace_callback);
1072 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001073 }
1074
Berker Peksagfe21de92016-04-09 07:34:39 +03001075 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001076}
1077
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001078#ifndef SQLITE_OMIT_LOAD_EXTENSION
Gerhard Häringf9cee222010-03-05 15:20:03 +00001079static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1080{
1081 int rc;
1082 int onoff;
1083
1084 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1085 return NULL;
1086 }
1087
1088 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1089 return NULL;
1090 }
1091
1092 rc = sqlite3_enable_load_extension(self->db, onoff);
1093
1094 if (rc != SQLITE_OK) {
1095 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1096 return NULL;
1097 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001098 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001099 }
1100}
1101
1102static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1103{
1104 int rc;
1105 char* extension_name;
1106 char* errmsg;
1107
1108 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1109 return NULL;
1110 }
1111
1112 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1113 return NULL;
1114 }
1115
1116 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1117 if (rc != 0) {
1118 PyErr_SetString(pysqlite_OperationalError, errmsg);
1119 return NULL;
1120 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001121 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001122 }
1123}
1124#endif
1125
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001126int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127{
1128 if (self->check_same_thread) {
1129 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001130 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001131 "SQLite objects created in a thread can only be used in that same thread. "
1132 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001133 self->thread_ident, PyThread_get_thread_ident());
1134 return 0;
1135 }
1136
1137 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001138 return 1;
1139}
1140
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001141static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001142{
1143 Py_INCREF(self->isolation_level);
1144 return self->isolation_level;
1145}
1146
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001147static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001148{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001149 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001150 return NULL;
1151 } else {
1152 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1153 }
1154}
1155
Berker Peksag59da4b32016-09-12 07:16:43 +03001156static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1157{
1158 if (!pysqlite_check_connection(self)) {
1159 return NULL;
1160 }
1161 if (!sqlite3_get_autocommit(self->db)) {
1162 Py_RETURN_TRUE;
1163 }
1164 Py_RETURN_FALSE;
1165}
1166
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001167static int
1168pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001169{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001170 if (isolation_level == NULL) {
1171 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1172 return -1;
1173 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001174 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001175 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001176 if (!res) {
1177 return -1;
1178 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001179 Py_DECREF(res);
1180
Serhiy Storchaka28914922016-09-01 22:18:03 +03001181 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001182 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001183 const char * const *candidate;
1184 PyObject *uppercase_level;
1185 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001186
Serhiy Storchaka28914922016-09-01 22:18:03 +03001187 if (!PyUnicode_Check(isolation_level)) {
1188 PyErr_Format(PyExc_TypeError,
1189 "isolation_level must be a string or None, not %.100s",
1190 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001191 return -1;
1192 }
1193
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001194 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001195 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001196 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001197 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001198 return -1;
1199 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001200 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001201 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001202 break;
1203 }
1204 Py_DECREF(uppercase_level);
1205 if (!*candidate) {
1206 PyErr_SetString(PyExc_ValueError,
1207 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001208 return -1;
1209 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001210 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001211 }
1212
Serhiy Storchaka28914922016-09-01 22:18:03 +03001213 Py_INCREF(isolation_level);
1214 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001215 return 0;
1216}
1217
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001218PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001219{
1220 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001221 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001222 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001223 int rc;
1224
Gerhard Häringf9cee222010-03-05 15:20:03 +00001225 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1226 return NULL;
1227 }
1228
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001229 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001230 return NULL;
1231
Victor Stinnerc6a23202019-06-26 03:16:24 +02001232 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001233 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001234
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001235 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001236
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001237 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001238 if (!statement) {
1239 return NULL;
1240 }
1241
Victor Stinner0201f442010-03-13 03:28:34 +00001242 statement->db = NULL;
1243 statement->st = NULL;
1244 statement->sql = NULL;
1245 statement->in_use = 0;
1246 statement->in_weakreflist = NULL;
1247
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001248 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001249 if (rc != SQLITE_OK) {
1250 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001251 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001253 if (PyErr_ExceptionMatches(PyExc_TypeError))
1254 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001255 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001256 (void)pysqlite_statement_reset(statement);
1257 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001258 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001259 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001260 }
1261
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001262 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1263 if (weakref == NULL)
1264 goto error;
1265 if (PyList_Append(self->statements, weakref) != 0) {
1266 Py_DECREF(weakref);
1267 goto error;
1268 }
1269 Py_DECREF(weakref);
1270
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001272
1273error:
1274 Py_DECREF(statement);
1275 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001276}
1277
Larry Hastings01b08832015-05-08 07:37:49 -07001278PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001279{
1280 PyObject* cursor = 0;
1281 PyObject* result = 0;
1282 PyObject* method = 0;
1283
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001284 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001285 if (!cursor) {
1286 goto error;
1287 }
1288
1289 method = PyObject_GetAttrString(cursor, "execute");
1290 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001291 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001292 goto error;
1293 }
1294
1295 result = PyObject_CallObject(method, args);
1296 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001297 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001298 }
1299
1300error:
1301 Py_XDECREF(result);
1302 Py_XDECREF(method);
1303
1304 return cursor;
1305}
1306
Larry Hastings01b08832015-05-08 07:37:49 -07001307PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001308{
1309 PyObject* cursor = 0;
1310 PyObject* result = 0;
1311 PyObject* method = 0;
1312
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001313 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001314 if (!cursor) {
1315 goto error;
1316 }
1317
1318 method = PyObject_GetAttrString(cursor, "executemany");
1319 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001320 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001321 goto error;
1322 }
1323
1324 result = PyObject_CallObject(method, args);
1325 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001326 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327 }
1328
1329error:
1330 Py_XDECREF(result);
1331 Py_XDECREF(method);
1332
1333 return cursor;
1334}
1335
Larry Hastings01b08832015-05-08 07:37:49 -07001336PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001337{
1338 PyObject* cursor = 0;
1339 PyObject* result = 0;
1340 PyObject* method = 0;
1341
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001342 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343 if (!cursor) {
1344 goto error;
1345 }
1346
1347 method = PyObject_GetAttrString(cursor, "executescript");
1348 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001349 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001350 goto error;
1351 }
1352
1353 result = PyObject_CallObject(method, args);
1354 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001355 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001356 }
1357
1358error:
1359 Py_XDECREF(result);
1360 Py_XDECREF(method);
1361
1362 return cursor;
1363}
1364
1365/* ------------------------- COLLATION CODE ------------------------ */
1366
1367static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001368pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369 void* context,
1370 int text1_length, const void* text1_data,
1371 int text2_length, const void* text2_data)
1372{
1373 PyObject* callback = (PyObject*)context;
1374 PyObject* string1 = 0;
1375 PyObject* string2 = 0;
1376 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001377 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001378 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001379 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001380 gilstate = PyGILState_Ensure();
1381
1382 if (PyErr_Occurred()) {
1383 goto finally;
1384 }
1385
Guido van Rossum98297ee2007-11-06 21:34:58 +00001386 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1387 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001388
1389 if (!string1 || !string2) {
1390 goto finally; /* failed to allocate strings */
1391 }
1392
1393 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1394
1395 if (!retval) {
1396 /* execution failed */
1397 goto finally;
1398 }
1399
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001400 longval = PyLong_AsLongAndOverflow(retval, &result);
1401 if (longval == -1 && PyErr_Occurred()) {
1402 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001403 result = 0;
1404 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001405 else if (!result) {
1406 if (longval > 0)
1407 result = 1;
1408 else if (longval < 0)
1409 result = -1;
1410 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411
1412finally:
1413 Py_XDECREF(string1);
1414 Py_XDECREF(string2);
1415 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001416 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001417 return result;
1418}
1419
1420static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001421pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001422{
1423 PyObject* retval = NULL;
1424
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001425 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001426 goto finally;
1427 }
1428
1429 sqlite3_interrupt(self->db);
1430
1431 Py_INCREF(Py_None);
1432 retval = Py_None;
1433
1434finally:
1435 return retval;
1436}
1437
Christian Heimesbbe741d2008-03-28 10:53:29 +00001438/* Function author: Paul Kippes <kippesp@gmail.com>
1439 * Class method of Connection to call the Python function _iterdump
1440 * of the sqlite3 module.
1441 */
1442static PyObject *
1443pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1444{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001445 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001446 PyObject* retval = NULL;
1447 PyObject* module = NULL;
1448 PyObject* module_dict;
1449 PyObject* pyfn_iterdump;
1450
1451 if (!pysqlite_check_connection(self)) {
1452 goto finally;
1453 }
1454
1455 module = PyImport_ImportModule(MODULE_NAME ".dump");
1456 if (!module) {
1457 goto finally;
1458 }
1459
1460 module_dict = PyModule_GetDict(module);
1461 if (!module_dict) {
1462 goto finally;
1463 }
1464
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001465 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001466 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001467 if (!PyErr_Occurred()) {
1468 PyErr_SetString(pysqlite_OperationalError,
1469 "Failed to obtain _iterdump() reference");
1470 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001471 goto finally;
1472 }
1473
Petr Viktorinffd97532020-02-11 17:46:57 +01001474 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001475
1476finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001477 Py_XDECREF(module);
1478 return retval;
1479}
1480
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001481static PyObject *
1482pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *kwds)
1483{
1484 PyObject *target = NULL;
1485 int pages = -1;
1486 PyObject *progress = Py_None;
1487 const char *name = "main";
1488 int rc;
1489 int callback_error = 0;
Victor Stinnerca405012018-04-30 12:22:17 +02001490 PyObject *sleep_obj = NULL;
1491 int sleep_ms = 250;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001492 sqlite3 *bck_conn;
1493 sqlite3_backup *bck_handle;
1494 static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL};
1495
Victor Stinnerca405012018-04-30 12:22:17 +02001496 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsO:backup", keywords,
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001497 &pysqlite_ConnectionType, &target,
Victor Stinnerca405012018-04-30 12:22:17 +02001498 &pages, &progress, &name, &sleep_obj)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001499 return NULL;
1500 }
1501
Victor Stinnerca405012018-04-30 12:22:17 +02001502 if (sleep_obj != NULL) {
1503 _PyTime_t sleep_secs;
1504 if (_PyTime_FromSecondsObject(&sleep_secs, sleep_obj,
1505 _PyTime_ROUND_TIMEOUT)) {
1506 return NULL;
1507 }
1508 _PyTime_t ms = _PyTime_AsMilliseconds(sleep_secs,
1509 _PyTime_ROUND_TIMEOUT);
1510 if (ms < INT_MIN || ms > INT_MAX) {
1511 PyErr_SetString(PyExc_OverflowError, "sleep is too large");
1512 return NULL;
1513 }
1514 sleep_ms = (int)ms;
1515 }
1516
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001517 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1518 return NULL;
1519 }
1520
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001521 if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
1522 return NULL;
1523 }
1524
1525 if ((pysqlite_Connection *)target == self) {
1526 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1527 return NULL;
1528 }
1529
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001530#if SQLITE_VERSION_NUMBER < 3008008
1531 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001532 https://www.sqlite.org/src/info/169b5505498c0a7e */
1533 if (!sqlite3_get_autocommit(((pysqlite_Connection *)target)->db)) {
1534 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1535 return NULL;
1536 }
1537#endif
1538
1539 if (progress != Py_None && !PyCallable_Check(progress)) {
1540 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1541 return NULL;
1542 }
1543
1544 if (pages == 0) {
1545 pages = -1;
1546 }
1547
1548 bck_conn = ((pysqlite_Connection *)target)->db;
1549
1550 Py_BEGIN_ALLOW_THREADS
1551 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1552 Py_END_ALLOW_THREADS
1553
1554 if (bck_handle) {
1555 do {
1556 Py_BEGIN_ALLOW_THREADS
1557 rc = sqlite3_backup_step(bck_handle, pages);
1558 Py_END_ALLOW_THREADS
1559
1560 if (progress != Py_None) {
1561 PyObject *res;
1562
1563 res = PyObject_CallFunction(progress, "iii", rc,
1564 sqlite3_backup_remaining(bck_handle),
1565 sqlite3_backup_pagecount(bck_handle));
1566 if (res == NULL) {
1567 /* User's callback raised an error: interrupt the loop and
1568 propagate it. */
1569 callback_error = 1;
1570 rc = -1;
1571 } else {
1572 Py_DECREF(res);
1573 }
1574 }
1575
1576 /* Sleep for a while if there are still further pages to copy and
1577 the engine could not make any progress */
1578 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1579 Py_BEGIN_ALLOW_THREADS
Victor Stinnerca405012018-04-30 12:22:17 +02001580 sqlite3_sleep(sleep_ms);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001581 Py_END_ALLOW_THREADS
1582 }
1583 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1584
1585 Py_BEGIN_ALLOW_THREADS
1586 rc = sqlite3_backup_finish(bck_handle);
1587 Py_END_ALLOW_THREADS
1588 } else {
1589 rc = _pysqlite_seterror(bck_conn, NULL);
1590 }
1591
1592 if (!callback_error && rc != SQLITE_OK) {
1593 /* We cannot use _pysqlite_seterror() here because the backup APIs do
1594 not set the error status on the connection object, but rather on
1595 the backup handle. */
1596 if (rc == SQLITE_NOMEM) {
1597 (void)PyErr_NoMemory();
1598 } else {
1599#if SQLITE_VERSION_NUMBER > 3007015
1600 PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
1601#else
1602 switch (rc) {
Berker Peksagb10a64d2018-09-20 14:14:33 +03001603 case SQLITE_ERROR:
1604 /* Description of SQLITE_ERROR in SQLite 3.7.14 and older
1605 releases. */
1606 PyErr_SetString(pysqlite_OperationalError,
1607 "SQL logic error or missing database");
1608 break;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001609 case SQLITE_READONLY:
1610 PyErr_SetString(pysqlite_OperationalError,
1611 "attempt to write a readonly database");
1612 break;
1613 case SQLITE_BUSY:
1614 PyErr_SetString(pysqlite_OperationalError, "database is locked");
1615 break;
1616 case SQLITE_LOCKED:
1617 PyErr_SetString(pysqlite_OperationalError,
1618 "database table is locked");
1619 break;
1620 default:
1621 PyErr_Format(pysqlite_OperationalError,
1622 "unrecognized error code: %d", rc);
1623 break;
1624 }
1625#endif
1626 }
1627 }
1628
1629 if (!callback_error && rc == SQLITE_OK) {
1630 Py_RETURN_NONE;
1631 } else {
1632 return NULL;
1633 }
1634}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001635
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001636static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001637pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001638{
1639 PyObject* callable;
1640 PyObject* uppercase_name = 0;
1641 PyObject* name;
1642 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001643 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001644 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001645 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001646 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001647 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001648 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001649
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001650 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001651 goto finally;
1652 }
1653
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001654 if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1655 &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001656 goto finally;
1657 }
1658
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001659 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1660 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001661 if (!uppercase_name) {
1662 goto finally;
1663 }
1664
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001665 if (PyUnicode_READY(uppercase_name))
1666 goto finally;
1667 len = PyUnicode_GET_LENGTH(uppercase_name);
1668 kind = PyUnicode_KIND(uppercase_name);
1669 data = PyUnicode_DATA(uppercase_name);
1670 for (i=0; i<len; i++) {
1671 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1672 if ((ch >= '0' && ch <= '9')
1673 || (ch >= 'A' && ch <= 'Z')
1674 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001675 {
Victor Stinner35466c52010-04-22 11:23:23 +00001676 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001677 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001678 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001679 goto finally;
1680 }
1681 }
1682
Serhiy Storchaka06515832016-11-20 09:13:07 +02001683 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001684 if (!uppercase_name_str)
1685 goto finally;
1686
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001687 if (callable != Py_None && !PyCallable_Check(callable)) {
1688 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1689 goto finally;
1690 }
1691
1692 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001693 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1694 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001695 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001696 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1697 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001698 }
1699
1700 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001701 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001702 SQLITE_UTF8,
1703 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001704 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001705 if (rc != SQLITE_OK) {
1706 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001707 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001708 goto finally;
1709 }
1710
1711finally:
1712 Py_XDECREF(uppercase_name);
1713
1714 if (PyErr_Occurred()) {
1715 retval = NULL;
1716 } else {
1717 Py_INCREF(Py_None);
1718 retval = Py_None;
1719 }
1720
1721 return retval;
1722}
1723
Christian Heimesbbe741d2008-03-28 10:53:29 +00001724/* Called when the connection is used as a context manager. Returns itself as a
1725 * convenience to the caller. */
1726static PyObject *
1727pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1728{
1729 Py_INCREF(self);
1730 return (PyObject*)self;
1731}
1732
1733/** Called when the connection is used as a context manager. If there was any
1734 * exception, a rollback takes place; otherwise we commit. */
1735static PyObject *
1736pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1737{
1738 PyObject* exc_type, *exc_value, *exc_tb;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001739 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001740 PyObject* result;
1741
1742 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1743 return NULL;
1744 }
1745
1746 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1747 method_name = "commit";
1748 } else {
1749 method_name = "rollback";
1750 }
1751
Victor Stinner3466bde2016-09-05 18:16:01 -07001752 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001753 if (!result) {
1754 return NULL;
1755 }
1756 Py_DECREF(result);
1757
1758 Py_RETURN_FALSE;
1759}
1760
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001761static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001762PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001763
1764static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001765 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1766 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001767 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001768 {NULL}
1769};
1770
1771static PyMethodDef connection_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001772 {"cursor", (PyCFunction)(void(*)(void))pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001773 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001774 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001775 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001776 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001777 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001778 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001779 PyDoc_STR("Roll back the current transaction.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001780 {"create_function", (PyCFunction)(void(*)(void))pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001781 PyDoc_STR("Creates a new function. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001782 {"create_aggregate", (PyCFunction)(void(*)(void))pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001783 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001784 {"set_authorizer", (PyCFunction)(void(*)(void))pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001785 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001786 #ifndef SQLITE_OMIT_LOAD_EXTENSION
Gerhard Häringf9cee222010-03-05 15:20:03 +00001787 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1788 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1789 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1790 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1791 #endif
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001792 {"set_progress_handler", (PyCFunction)(void(*)(void))pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001793 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001794 {"set_trace_callback", (PyCFunction)(void(*)(void))pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001795 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001796 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001797 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001798 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001799 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001800 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001801 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001802 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001803 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001804 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001805 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001806 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001807 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001808 {"backup", (PyCFunction)(void(*)(void))pysqlite_connection_backup, METH_VARARGS | METH_KEYWORDS,
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001809 PyDoc_STR("Makes a backup of the database. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001810 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1811 PyDoc_STR("For context manager. Non-standard.")},
1812 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1813 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001814 {NULL, NULL}
1815};
1816
1817static struct PyMemberDef connection_members[] =
1818{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001819 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1820 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1821 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1822 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1823 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1824 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1825 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1826 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1827 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1828 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001829 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1830 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001831 {NULL}
1832};
1833
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001834PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001835 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001836 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001837 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001838 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001839 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001840 0, /* tp_vectorcall_offset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001841 0, /* tp_getattr */
1842 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001843 0, /* tp_as_async */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001844 0, /* tp_repr */
1845 0, /* tp_as_number */
1846 0, /* tp_as_sequence */
1847 0, /* tp_as_mapping */
1848 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001849 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001850 0, /* tp_str */
1851 0, /* tp_getattro */
1852 0, /* tp_setattro */
1853 0, /* tp_as_buffer */
1854 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1855 connection_doc, /* tp_doc */
1856 0, /* tp_traverse */
1857 0, /* tp_clear */
1858 0, /* tp_richcompare */
1859 0, /* tp_weaklistoffset */
1860 0, /* tp_iter */
1861 0, /* tp_iternext */
1862 connection_methods, /* tp_methods */
1863 connection_members, /* tp_members */
1864 connection_getset, /* tp_getset */
1865 0, /* tp_base */
1866 0, /* tp_dict */
1867 0, /* tp_descr_get */
1868 0, /* tp_descr_set */
1869 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001870 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001871 0, /* tp_alloc */
1872 0, /* tp_new */
1873 0 /* tp_free */
1874};
1875
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001876extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001877{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001878 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1879 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001880}