blob: fccffabc4b2a0b20015c4c27c8328598c312a992 [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
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +010040#include "clinic/connection.c.h"
41/*[clinic input]
42module _sqlite3
43class _sqlite3.Connection "pysqlite_Connection *" "pysqlite_ConnectionType"
44[clinic start generated code]*/
45/*[clinic end generated code: output=da39a3ee5e6b4b0d input=aa796073bd8f69db]*/
46
Martin v. Löwise75fc142013-11-07 18:46:53 +010047_Py_IDENTIFIER(cursor);
48
Serhiy Storchaka28914922016-09-01 22:18:03 +030049static const char * const begin_statements[] = {
50 "BEGIN ",
51 "BEGIN DEFERRED",
52 "BEGIN IMMEDIATE",
53 "BEGIN EXCLUSIVE",
54 NULL
55};
56
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +020057static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored));
Gerhard Häringf9cee222010-03-05 15:20:03 +000058static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +010060static int
61pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
62 PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010064 static char *kwlist[] = {
65 "database", "timeout", "detect_types", "isolation_level",
66 "check_same_thread", "factory", "cached_statements", "uri",
67 NULL
68 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +030070 const char* database;
Anders Lorentsena22a1272017-11-07 01:47:43 +010071 PyObject* database_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072 int detect_types = 0;
73 PyObject* isolation_level = NULL;
74 PyObject* factory = NULL;
75 int check_same_thread = 1;
76 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010077 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 double timeout = 5.0;
79 int rc;
80
Anders Lorentsena22a1272017-11-07 01:47:43 +010081 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
82 PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010083 &isolation_level, &check_same_thread,
84 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000085 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000086 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087 }
88
Erlend Egeberg Aaslandc96cc082021-05-02 23:25:17 +020089 if (PySys_Audit("sqlite3.connect", "O", database_obj) < 0) {
90 return -1;
91 }
92
Anders Lorentsena22a1272017-11-07 01:47:43 +010093 database = PyBytes_AsString(database_obj);
94
Gerhard Häringf9cee222010-03-05 15:20:03 +000095 self->initialized = 1;
96
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097 self->begin_statement = NULL;
98
Oren Milman93c5a5d2017-10-10 22:27:46 +030099 Py_CLEAR(self->statement_cache);
100 Py_CLEAR(self->statements);
101 Py_CLEAR(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000102
103 Py_INCREF(Py_None);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300104 Py_XSETREF(self->row_factory, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105
106 Py_INCREF(&PyUnicode_Type);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300107 Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100109 Py_BEGIN_ALLOW_THREADS
110 rc = sqlite3_open_v2(database, &self->db,
111 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
112 (uri ? SQLITE_OPEN_URI : 0), NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 Py_END_ALLOW_THREADS
114
Anders Lorentsena22a1272017-11-07 01:47:43 +0100115 Py_DECREF(database_obj);
116
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000118 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 return -1;
120 }
121
122 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000123 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 if (!isolation_level) {
125 return -1;
126 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127 } else {
128 Py_INCREF(isolation_level);
129 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300130 Py_CLEAR(self->isolation_level);
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200131 if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100132 Py_DECREF(isolation_level);
133 return -1;
134 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135 Py_DECREF(isolation_level);
136
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200137 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 if (PyErr_Occurred()) {
139 return -1;
140 }
141
Gerhard Häringf9cee222010-03-05 15:20:03 +0000142 self->created_statements = 0;
143 self->created_cursors = 0;
144
145 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000146 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000147 self->cursors = PyList_New(0);
148 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000149 return -1;
150 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000151
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 /* By default, the Cache class INCREFs the factory in its initializer, and
153 * decrefs it in its deallocator method. Since this would create a circular
154 * reference here, we're breaking it by decrementing self, and telling the
155 * cache class to not decref the factory (self) in its deallocator.
156 */
157 self->statement_cache->decref_factory = 0;
158 Py_DECREF(self);
159
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160 self->detect_types = detect_types;
161 self->timeout = timeout;
162 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000163 self->thread_ident = PyThread_get_thread_ident();
164 self->check_same_thread = check_same_thread;
165
gescheitb9a03762019-07-13 06:15:49 +0300166 self->function_pinboard_trace_callback = NULL;
167 self->function_pinboard_progress_handler = NULL;
168 self->function_pinboard_authorizer_cb = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169
Oren Milman93c5a5d2017-10-10 22:27:46 +0300170 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 if (!self->collations) {
172 return -1;
173 }
174
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000175 self->Warning = pysqlite_Warning;
176 self->Error = pysqlite_Error;
177 self->InterfaceError = pysqlite_InterfaceError;
178 self->DatabaseError = pysqlite_DatabaseError;
179 self->DataError = pysqlite_DataError;
180 self->OperationalError = pysqlite_OperationalError;
181 self->IntegrityError = pysqlite_IntegrityError;
182 self->InternalError = pysqlite_InternalError;
183 self->ProgrammingError = pysqlite_ProgrammingError;
184 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000185
Erlend Egeberg Aaslandc96cc082021-05-02 23:25:17 +0200186 if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
187 return -1;
188 }
189
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 return 0;
191}
192
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000193/* action in (ACTION_RESET, ACTION_FINALIZE) */
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100194static void
195pysqlite_do_all_statements(pysqlite_Connection *self, int action,
196 int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000198 int i;
199 PyObject* weakref;
200 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000201 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202
Thomas Wouters477c8d52006-05-27 19:21:47 +0000203 for (i = 0; i < PyList_Size(self->statements); i++) {
204 weakref = PyList_GetItem(self->statements, i);
205 statement = PyWeakref_GetObject(weakref);
206 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500207 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000208 if (action == ACTION_RESET) {
209 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
210 } else {
211 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
212 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500213 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000214 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000216
217 if (reset_cursors) {
218 for (i = 0; i < PyList_Size(self->cursors); i++) {
219 weakref = PyList_GetItem(self->cursors, i);
220 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
221 if ((PyObject*)cursor != Py_None) {
222 cursor->reset = 1;
223 }
224 }
225 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000226}
227
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700228static int
229connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
230{
Miss Islington (bot)ff359d72021-05-31 02:12:27 -0700231 Py_VISIT(Py_TYPE(self));
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700232 Py_VISIT(self->isolation_level);
Miss Islington (bot)ff359d72021-05-31 02:12:27 -0700233 Py_VISIT(self->statement_cache);
234 Py_VISIT(self->statements);
235 Py_VISIT(self->cursors);
236 Py_VISIT(self->row_factory);
237 Py_VISIT(self->text_factory);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700238 Py_VISIT(self->function_pinboard_trace_callback);
239 Py_VISIT(self->function_pinboard_progress_handler);
240 Py_VISIT(self->function_pinboard_authorizer_cb);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700241 Py_VISIT(self->collations);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700242 return 0;
243}
244
245static int
246connection_clear(pysqlite_Connection *self)
247{
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700248 Py_CLEAR(self->isolation_level);
Miss Islington (bot)ff359d72021-05-31 02:12:27 -0700249 Py_CLEAR(self->statement_cache);
250 Py_CLEAR(self->statements);
251 Py_CLEAR(self->cursors);
252 Py_CLEAR(self->row_factory);
253 Py_CLEAR(self->text_factory);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700254 Py_CLEAR(self->function_pinboard_trace_callback);
255 Py_CLEAR(self->function_pinboard_progress_handler);
256 Py_CLEAR(self->function_pinboard_authorizer_cb);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700257 Py_CLEAR(self->collations);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700258 return 0;
259}
260
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100261static void
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700262connection_dealloc(pysqlite_Connection *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200264 PyTypeObject *tp = Py_TYPE(self);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700265 PyObject_GC_UnTrack(self);
266 tp->tp_clear((PyObject *)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267
268 /* Clean up if user has not called .close() explicitly. */
269 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100270 sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000271 }
272
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200273 tp->tp_free(self);
274 Py_DECREF(tp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000275}
276
Gerhard Häringf9cee222010-03-05 15:20:03 +0000277/*
278 * Registers a cursor with the connection.
279 *
280 * 0 => error; 1 => ok
281 */
282int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
283{
284 PyObject* weakref;
285
286 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
287 if (!weakref) {
288 goto error;
289 }
290
291 if (PyList_Append(connection->cursors, weakref) != 0) {
292 Py_CLEAR(weakref);
293 goto error;
294 }
295
296 Py_DECREF(weakref);
297
298 return 1;
299error:
300 return 0;
301}
302
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100303/*[clinic input]
304_sqlite3.Connection.cursor as pysqlite_connection_cursor
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000305
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100306 factory: object = NULL
307
308Return a cursor for the connection.
309[clinic start generated code]*/
310
311static PyObject *
312pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
313/*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/
314{
315 PyObject* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000317 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 return NULL;
319 }
320
321 if (factory == NULL) {
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200322 factory = (PyObject*)pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 }
324
Petr Viktorinffd97532020-02-11 17:46:57 +0100325 cursor = PyObject_CallOneArg(factory, (PyObject *)self);
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300326 if (cursor == NULL)
327 return NULL;
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200328 if (!PyObject_TypeCheck(cursor, pysqlite_CursorType)) {
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300329 PyErr_Format(PyExc_TypeError,
330 "factory must return a cursor, not %.100s",
331 Py_TYPE(cursor)->tp_name);
332 Py_DECREF(cursor);
333 return NULL;
334 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335
Gerhard Häringf9cee222010-03-05 15:20:03 +0000336 _pysqlite_drop_unused_cursor_references(self);
337
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300340 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341 }
342
343 return cursor;
344}
345
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100346/*[clinic input]
347_sqlite3.Connection.close as pysqlite_connection_close
348
349Closes the connection.
350[clinic start generated code]*/
351
352static PyObject *
353pysqlite_connection_close_impl(pysqlite_Connection *self)
354/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355{
356 int rc;
357
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000358 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359 return NULL;
360 }
361
Gerhard Häringf9cee222010-03-05 15:20:03 +0000362 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363
364 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100365 rc = sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366
367 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000368 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369 return NULL;
370 } else {
371 self->db = NULL;
372 }
373 }
374
Berker Peksagfe21de92016-04-09 07:34:39 +0300375 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000376}
377
378/*
379 * Checks if a connection object is usable (i. e. not closed).
380 *
381 * 0 => error; 1 => ok
382 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000383int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000385 if (!con->initialized) {
386 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
387 return 0;
388 }
389
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000391 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 return 0;
393 } else {
394 return 1;
395 }
396}
397
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000398PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399{
400 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000401 sqlite3_stmt* statement;
402
403 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100404 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement,
405 NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406 Py_END_ALLOW_THREADS
407
408 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000409 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410 goto error;
411 }
412
Benjamin Petersond7b03282008-09-13 15:58:53 +0000413 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300414 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000415 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000416 }
417
418 Py_BEGIN_ALLOW_THREADS
419 rc = sqlite3_finalize(statement);
420 Py_END_ALLOW_THREADS
421
422 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000423 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 }
425
426error:
427 if (PyErr_Occurred()) {
428 return NULL;
429 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200430 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431 }
432}
433
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +0100434/*[clinic input]
435_sqlite3.Connection.commit as pysqlite_connection_commit
436
437Commit the current transaction.
438[clinic start generated code]*/
439
440static PyObject *
441pysqlite_connection_commit_impl(pysqlite_Connection *self)
442/*[clinic end generated code: output=3da45579e89407f2 input=39c12c04dda276a8]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443{
444 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000445 sqlite3_stmt* statement;
446
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000447 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 return NULL;
449 }
450
Berker Peksag59da4b32016-09-12 07:16:43 +0300451 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000452
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000453 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100454 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455 Py_END_ALLOW_THREADS
456 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000457 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000458 goto error;
459 }
460
Benjamin Petersond7b03282008-09-13 15:58:53 +0000461 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300462 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000463 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 }
465
466 Py_BEGIN_ALLOW_THREADS
467 rc = sqlite3_finalize(statement);
468 Py_END_ALLOW_THREADS
469 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000470 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000471 }
472
473 }
474
475error:
476 if (PyErr_Occurred()) {
477 return NULL;
478 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200479 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000480 }
481}
482
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100483/*[clinic input]
484_sqlite3.Connection.rollback as pysqlite_connection_rollback
485
486Roll back the current transaction.
487[clinic start generated code]*/
488
489static PyObject *
490pysqlite_connection_rollback_impl(pysqlite_Connection *self)
491/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492{
493 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000494 sqlite3_stmt* statement;
495
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000496 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000497 return NULL;
498 }
499
Berker Peksag59da4b32016-09-12 07:16:43 +0300500 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000501 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502
503 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100504 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505 Py_END_ALLOW_THREADS
506 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000507 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 goto error;
509 }
510
Benjamin Petersond7b03282008-09-13 15:58:53 +0000511 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300512 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000513 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000514 }
515
516 Py_BEGIN_ALLOW_THREADS
517 rc = sqlite3_finalize(statement);
518 Py_END_ALLOW_THREADS
519 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000520 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000521 }
522
523 }
524
525error:
526 if (PyErr_Occurred()) {
527 return NULL;
528 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200529 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000530 }
531}
532
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200533static int
534_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200536 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000538 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200539 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
540 if (value == -1 && PyErr_Occurred())
541 return -1;
542 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 } else if (PyFloat_Check(py_val)) {
544 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000545 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200546 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200547 if (str == NULL)
548 return -1;
549 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000550 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200551 Py_buffer view;
552 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100553 PyErr_SetString(PyExc_ValueError,
554 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200555 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200557 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100558 PyErr_SetString(PyExc_OverflowError,
559 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200560 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100561 return -1;
562 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200563 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
564 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000565 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200566 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200568 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569}
570
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100571static PyObject *
572_pysqlite_build_py_params(sqlite3_context *context, int argc,
573 sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000574{
575 PyObject* args;
576 int i;
577 sqlite3_value* cur_value;
578 PyObject* cur_py_value;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579
580 args = PyTuple_New(argc);
581 if (!args) {
582 return NULL;
583 }
584
585 for (i = 0; i < argc; i++) {
586 cur_value = argv[i];
587 switch (sqlite3_value_type(argv[i])) {
588 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500589 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590 break;
591 case SQLITE_FLOAT:
592 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
593 break;
Miss Islington (bot)067d6d42021-06-04 11:54:39 -0700594 case SQLITE_TEXT: {
595 sqlite3 *db = sqlite3_context_db_handle(context);
596 const char *text = (const char *)sqlite3_value_text(cur_value);
597
598 if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
599 PyErr_NoMemory();
600 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000601 }
Miss Islington (bot)067d6d42021-06-04 11:54:39 -0700602
603 Py_ssize_t size = sqlite3_value_bytes(cur_value);
604 cur_py_value = PyUnicode_FromStringAndSize(text, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000605 break;
Miss Islington (bot)067d6d42021-06-04 11:54:39 -0700606 }
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200607 case SQLITE_BLOB: {
608 sqlite3 *db = sqlite3_context_db_handle(context);
609 const void *blob = sqlite3_value_blob(cur_value);
610
611 if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
612 PyErr_NoMemory();
613 goto error;
614 }
615
616 Py_ssize_t size = sqlite3_value_bytes(cur_value);
617 cur_py_value = PyBytes_FromStringAndSize(blob, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000618 break;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200619 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620 case SQLITE_NULL:
621 default:
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100622 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624
625 if (!cur_py_value) {
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200626 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627 }
628
Erlend Egeberg Aaslande9194ea2021-04-23 13:21:08 +0200629 PyTuple_SET_ITEM(args, i, cur_py_value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000630 }
631
632 return args;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200633
634error:
635 Py_DECREF(args);
636 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637}
638
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100639static void
640_pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000641{
642 PyObject* args;
643 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200645 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646
647 PyGILState_STATE threadstate;
648
649 threadstate = PyGILState_Ensure();
650
651 py_func = (PyObject*)sqlite3_user_data(context);
652
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000653 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 if (args) {
655 py_retval = PyObject_CallObject(py_func, args);
656 Py_DECREF(args);
657 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200659 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000660 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200661 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000662 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200663 }
664 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700665 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000666 PyErr_Print();
667 } else {
668 PyErr_Clear();
669 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200670 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000671 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672
673 PyGILState_Release(threadstate);
674}
675
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000676static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677{
678 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 PyObject* aggregate_class;
681 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683
684 PyGILState_STATE threadstate;
685
686 threadstate = PyGILState_Ensure();
687
688 aggregate_class = (PyObject*)sqlite3_user_data(context);
689
690 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
691
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200692 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100693 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000696 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700697 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000698 PyErr_Print();
699 } else {
700 PyErr_Clear();
701 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200702 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 }
705 }
706
707 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708 if (!stepmethod) {
709 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 }
711
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000712 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000713 if (!args) {
714 goto error;
715 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000716
717 function_result = PyObject_CallObject(stepmethod, args);
718 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719
Thomas Wouters477c8d52006-05-27 19:21:47 +0000720 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700721 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000722 PyErr_Print();
723 } else {
724 PyErr_Clear();
725 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200726 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000727 }
728
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729error:
730 Py_XDECREF(stepmethod);
731 Py_XDECREF(function_result);
732
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000733 PyGILState_Release(threadstate);
734}
735
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100736static void
737_pysqlite_final_callback(sqlite3_context *context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000738{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200739 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000740 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200741 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200742 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200743 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744
745 PyGILState_STATE threadstate;
746
747 threadstate = PyGILState_Ensure();
748
Erlend Egeberg Aasland979b23c2021-02-19 12:20:32 +0100749 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
750 if (aggregate_instance == NULL) {
751 /* No rows matched the query; the step handler was never called. */
752 goto error;
753 }
754 else if (!*aggregate_instance) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000755 /* this branch is executed if there was an exception in the aggregate's
756 * __init__ */
757
Thomas Wouters477c8d52006-05-27 19:21:47 +0000758 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000759 }
760
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200761 /* Keep the exception (if any) of the last call to step() */
762 PyErr_Fetch(&exception, &value, &tb);
763
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200764 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200765
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200766 Py_DECREF(*aggregate_instance);
767
768 ok = 0;
769 if (function_result) {
770 ok = _pysqlite_set_result(context, function_result) == 0;
771 Py_DECREF(function_result);
772 }
773 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700774 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000775 PyErr_Print();
776 } else {
777 PyErr_Clear();
778 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200779 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780 }
781
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200782 /* Restore the exception (if any) of the last call to step(),
783 but clear also the current exception if finalize() failed */
784 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200785
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787 PyGILState_Release(threadstate);
788}
789
Gerhard Häringf9cee222010-03-05 15:20:03 +0000790static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000791{
792 PyObject* new_list;
793 PyObject* weakref;
794 int i;
795
796 /* we only need to do this once in a while */
797 if (self->created_statements++ < 200) {
798 return;
799 }
800
801 self->created_statements = 0;
802
803 new_list = PyList_New(0);
804 if (!new_list) {
805 return;
806 }
807
808 for (i = 0; i < PyList_Size(self->statements); i++) {
809 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000810 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000811 if (PyList_Append(new_list, weakref) != 0) {
812 Py_DECREF(new_list);
813 return;
814 }
815 }
816 }
817
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300818 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820
Gerhard Häringf9cee222010-03-05 15:20:03 +0000821static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
822{
823 PyObject* new_list;
824 PyObject* weakref;
825 int i;
826
827 /* we only need to do this once in a while */
828 if (self->created_cursors++ < 200) {
829 return;
830 }
831
832 self->created_cursors = 0;
833
834 new_list = PyList_New(0);
835 if (!new_list) {
836 return;
837 }
838
839 for (i = 0; i < PyList_Size(self->cursors); i++) {
840 weakref = PyList_GetItem(self->cursors, i);
841 if (PyWeakref_GetObject(weakref) != Py_None) {
842 if (PyList_Append(new_list, weakref) != 0) {
843 Py_DECREF(new_list);
844 return;
845 }
846 }
847 }
848
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300849 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000850}
851
gescheitb9a03762019-07-13 06:15:49 +0300852static void _destructor(void* args)
853{
854 Py_DECREF((PyObject*)args);
855}
856
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100857/*[clinic input]
858_sqlite3.Connection.create_function as pysqlite_connection_create_function
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000859
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100860 name: str
861 narg: int
862 func: object
863 *
864 deterministic: bool = False
865
866Creates a new function. Non-standard.
867[clinic start generated code]*/
868
869static PyObject *
870pysqlite_connection_create_function_impl(pysqlite_Connection *self,
871 const char *name, int narg,
872 PyObject *func, int deterministic)
873/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
874{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000875 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500876 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000877
Gerhard Häringf9cee222010-03-05 15:20:03 +0000878 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
879 return NULL;
880 }
881
Sergey Fedoseev08308582018-07-08 12:09:20 +0500882 if (deterministic) {
883#if SQLITE_VERSION_NUMBER < 3008003
884 PyErr_SetString(pysqlite_NotSupportedError,
885 "deterministic=True requires SQLite 3.8.3 or higher");
886 return NULL;
887#else
888 if (sqlite3_libversion_number() < 3008003) {
889 PyErr_SetString(pysqlite_NotSupportedError,
890 "deterministic=True requires SQLite 3.8.3 or higher");
891 return NULL;
892 }
893 flags |= SQLITE_DETERMINISTIC;
894#endif
895 }
gescheitb9a03762019-07-13 06:15:49 +0300896 rc = sqlite3_create_function_v2(self->db,
897 name,
898 narg,
899 flags,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100900 (void*)Py_NewRef(func),
gescheitb9a03762019-07-13 06:15:49 +0300901 _pysqlite_func_callback,
902 NULL,
903 NULL,
904 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905
Thomas Wouters477c8d52006-05-27 19:21:47 +0000906 if (rc != SQLITE_OK) {
907 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000908 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000909 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000910 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500911 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000912}
913
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100914/*[clinic input]
915_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000916
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100917 name: str
918 n_arg: int
919 aggregate_class: object
920
921Creates a new aggregate. Non-standard.
922[clinic start generated code]*/
923
924static PyObject *
925pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
926 const char *name, int n_arg,
927 PyObject *aggregate_class)
928/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
929{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000930 int rc;
931
Gerhard Häringf9cee222010-03-05 15:20:03 +0000932 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
933 return NULL;
934 }
935
gescheitb9a03762019-07-13 06:15:49 +0300936 rc = sqlite3_create_function_v2(self->db,
937 name,
938 n_arg,
939 SQLITE_UTF8,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100940 (void*)Py_NewRef(aggregate_class),
gescheitb9a03762019-07-13 06:15:49 +0300941 0,
942 &_pysqlite_step_callback,
943 &_pysqlite_final_callback,
944 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000945 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000946 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000947 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000948 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000949 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500950 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000951}
952
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000953static 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 +0000954{
955 PyObject *ret;
956 int rc;
957 PyGILState_STATE gilstate;
958
959 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000960
Victor Stinnerd4095d92013-07-26 22:23:33 +0200961 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000962
Victor Stinnerd4095d92013-07-26 22:23:33 +0200963 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700964 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200965 PyErr_Print();
966 else
967 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200968
Victor Stinnerd4095d92013-07-26 22:23:33 +0200969 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200970 }
971 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200972 if (PyLong_Check(ret)) {
973 rc = _PyLong_AsInt(ret);
974 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700975 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200976 PyErr_Print();
977 else
978 PyErr_Clear();
979 rc = SQLITE_DENY;
980 }
981 }
982 else {
983 rc = SQLITE_DENY;
984 }
985 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000986 }
987
988 PyGILState_Release(gilstate);
989 return rc;
990}
991
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000992static int _progress_handler(void* user_arg)
993{
994 int rc;
995 PyObject *ret;
996 PyGILState_STATE gilstate;
997
998 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100999 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001000
1001 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001002 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001003 PyErr_Print();
1004 } else {
1005 PyErr_Clear();
1006 }
1007
Mark Dickinson934896d2009-02-21 20:59:32 +00001008 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +00001009 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001010 } else {
1011 rc = (int)PyObject_IsTrue(ret);
1012 Py_DECREF(ret);
1013 }
1014
1015 PyGILState_Release(gilstate);
1016 return rc;
1017}
1018
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001019#ifdef HAVE_TRACE_V2
1020/*
1021 * From https://sqlite.org/c3ref/trace_v2.html:
1022 * The integer return value from the callback is currently ignored, though this
1023 * may change in future releases. Callback implementations should return zero
1024 * to ensure future compatibility.
1025 */
1026static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
1027#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001028static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001029#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001030{
1031 PyObject *py_statement = NULL;
1032 PyObject *ret = NULL;
1033
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001034 PyGILState_STATE gilstate;
1035
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001036#ifdef HAVE_TRACE_V2
1037 if (type != SQLITE_TRACE_STMT) {
1038 return 0;
1039 }
1040#endif
1041
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001042 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001043 py_statement = PyUnicode_DecodeUTF8(statement_string,
1044 strlen(statement_string), "replace");
1045 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001046 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001047 Py_DECREF(py_statement);
1048 }
1049
1050 if (ret) {
1051 Py_DECREF(ret);
1052 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001053 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001054 PyErr_Print();
1055 } else {
1056 PyErr_Clear();
1057 }
1058 }
1059
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001060 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001061#ifdef HAVE_TRACE_V2
1062 return 0;
1063#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001064}
1065
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001066/*[clinic input]
1067_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001068
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001069 authorizer_callback as authorizer_cb: object
1070
1071Sets authorizer callback. Non-standard.
1072[clinic start generated code]*/
1073
1074static PyObject *
1075pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1076 PyObject *authorizer_cb)
1077/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1078{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001079 int rc;
1080
Gerhard Häringf9cee222010-03-05 15:20:03 +00001081 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1082 return NULL;
1083 }
1084
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001085 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001086 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001087 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001088 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001089 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001090 } else {
1091 Py_INCREF(authorizer_cb);
1092 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001093 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001094 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001095}
1096
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001097/*[clinic input]
1098_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1099
1100 progress_handler: object
1101 n: int
1102
1103Sets progress handler callback. Non-standard.
1104[clinic start generated code]*/
1105
1106static PyObject *
1107pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1108 PyObject *progress_handler,
1109 int n)
1110/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001111{
Gerhard Häringf9cee222010-03-05 15:20:03 +00001112 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1113 return NULL;
1114 }
1115
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001116 if (progress_handler == Py_None) {
1117 /* None clears the progress handler previously set */
1118 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001119 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001120 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001121 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001122 Py_INCREF(progress_handler);
1123 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001124 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001125 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001126}
1127
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001128/*[clinic input]
1129_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1130
1131 trace_callback: object
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001132
1133Sets a trace callback called for each SQL statement (passed as unicode).
1134
1135Non-standard.
1136[clinic start generated code]*/
1137
1138static PyObject *
Dong-hee Na21793492020-12-19 00:41:33 +09001139pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1140 PyObject *trace_callback)
1141/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001142{
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001143 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1144 return NULL;
1145 }
1146
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001147 if (trace_callback == Py_None) {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001148 /*
1149 * None clears the trace callback previously set
1150 *
1151 * Ref.
1152 * - https://sqlite.org/c3ref/c_trace.html
1153 * - https://sqlite.org/c3ref/trace_v2.html
1154 */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001155#ifdef HAVE_TRACE_V2
1156 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1157#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001158 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001159#endif
gescheitb9a03762019-07-13 06:15:49 +03001160 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001161 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001162#ifdef HAVE_TRACE_V2
1163 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1164#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001165 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001166#endif
gescheitb9a03762019-07-13 06:15:49 +03001167 Py_INCREF(trace_callback);
1168 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001169 }
1170
Berker Peksagfe21de92016-04-09 07:34:39 +03001171 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001172}
1173
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001174#ifndef SQLITE_OMIT_LOAD_EXTENSION
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001175/*[clinic input]
1176_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1177
Dong-hee Na21793492020-12-19 00:41:33 +09001178 enable as onoff: bool(accept={int})
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001179 /
1180
1181Enable dynamic loading of SQLite extension modules. Non-standard.
1182[clinic start generated code]*/
1183
1184static PyObject *
1185pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1186 int onoff)
Dong-hee Na21793492020-12-19 00:41:33 +09001187/*[clinic end generated code: output=9cac37190d388baf input=5c0da5b121121cbc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001188{
1189 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001190
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001191 if (PySys_Audit("sqlite3.enable_load_extension",
1192 "OO", self, onoff ? Py_True : Py_False) < 0) {
1193 return NULL;
1194 }
1195
Gerhard Häringf9cee222010-03-05 15:20:03 +00001196 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1197 return NULL;
1198 }
1199
Gerhard Häringf9cee222010-03-05 15:20:03 +00001200 rc = sqlite3_enable_load_extension(self->db, onoff);
1201
1202 if (rc != SQLITE_OK) {
1203 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1204 return NULL;
1205 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001206 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001207 }
1208}
1209
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001210/*[clinic input]
1211_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1212
1213 name as extension_name: str
1214 /
1215
1216Load SQLite extension module. Non-standard.
1217[clinic start generated code]*/
1218
1219static PyObject *
1220pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1221 const char *extension_name)
1222/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001223{
1224 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001225 char* errmsg;
1226
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001227 if (PySys_Audit("sqlite3.load_extension", "Os", self, extension_name) < 0) {
1228 return NULL;
1229 }
1230
Gerhard Häringf9cee222010-03-05 15:20:03 +00001231 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1232 return NULL;
1233 }
1234
Gerhard Häringf9cee222010-03-05 15:20:03 +00001235 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1236 if (rc != 0) {
1237 PyErr_SetString(pysqlite_OperationalError, errmsg);
1238 return NULL;
1239 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001240 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001241 }
1242}
1243#endif
1244
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001245int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001246{
1247 if (self->check_same_thread) {
1248 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001249 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001250 "SQLite objects created in a thread can only be used in that same thread. "
1251 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 self->thread_ident, PyThread_get_thread_ident());
1253 return 0;
1254 }
1255
1256 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001257 return 1;
1258}
1259
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001260static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001261{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001262 return Py_NewRef(self->isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001263}
1264
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001265static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001267 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268 return NULL;
1269 } else {
1270 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1271 }
1272}
1273
Berker Peksag59da4b32016-09-12 07:16:43 +03001274static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1275{
1276 if (!pysqlite_check_connection(self)) {
1277 return NULL;
1278 }
1279 if (!sqlite3_get_autocommit(self->db)) {
1280 Py_RETURN_TRUE;
1281 }
1282 Py_RETURN_FALSE;
1283}
1284
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001285static int
1286pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001288 if (isolation_level == NULL) {
1289 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1290 return -1;
1291 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001292 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001293 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001294 if (!res) {
1295 return -1;
1296 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001297 Py_DECREF(res);
1298
Serhiy Storchaka28914922016-09-01 22:18:03 +03001299 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001300 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001301 const char * const *candidate;
1302 PyObject *uppercase_level;
1303 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001304
Serhiy Storchaka28914922016-09-01 22:18:03 +03001305 if (!PyUnicode_Check(isolation_level)) {
1306 PyErr_Format(PyExc_TypeError,
1307 "isolation_level must be a string or None, not %.100s",
1308 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001309 return -1;
1310 }
1311
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001312 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001313 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001314 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001315 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001316 return -1;
1317 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001318 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001319 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001320 break;
1321 }
1322 Py_DECREF(uppercase_level);
1323 if (!*candidate) {
1324 PyErr_SetString(PyExc_ValueError,
1325 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001326 return -1;
1327 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001328 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001329 }
1330
Serhiy Storchaka28914922016-09-01 22:18:03 +03001331 Py_INCREF(isolation_level);
1332 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333 return 0;
1334}
1335
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +01001336static PyObject *
1337pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1338 PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001339{
1340 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001341 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001342 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343
Gerhard Häringf9cee222010-03-05 15:20:03 +00001344 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1345 return NULL;
1346 }
1347
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001348 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001349 return NULL;
1350
Victor Stinnerc6a23202019-06-26 03:16:24 +02001351 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001352 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001353
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001354 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001355
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +02001356 statement = pysqlite_statement_create(self, sql);
1357 if (statement == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001358 return NULL;
1359 }
1360
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001361 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1362 if (weakref == NULL)
1363 goto error;
1364 if (PyList_Append(self->statements, weakref) != 0) {
1365 Py_DECREF(weakref);
1366 goto error;
1367 }
1368 Py_DECREF(weakref);
1369
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001371
1372error:
1373 Py_DECREF(statement);
1374 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001375}
1376
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001377/*[clinic input]
1378_sqlite3.Connection.execute as pysqlite_connection_execute
1379
1380 sql: unicode
1381 parameters: object = NULL
1382 /
1383
1384Executes a SQL statement. Non-standard.
1385[clinic start generated code]*/
1386
1387static PyObject *
1388pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
1389 PyObject *parameters)
1390/*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001391{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001392 _Py_IDENTIFIER(execute);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001393 PyObject* cursor = 0;
1394 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001395
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001396 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001397 if (!cursor) {
1398 goto error;
1399 }
1400
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001401 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001402 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001403 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001404 }
1405
1406error:
1407 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001408
1409 return cursor;
1410}
1411
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001412/*[clinic input]
1413_sqlite3.Connection.executemany as pysqlite_connection_executemany
1414
1415 sql: unicode
1416 parameters: object
1417 /
1418
1419Repeatedly executes a SQL statement. Non-standard.
1420[clinic start generated code]*/
1421
1422static PyObject *
1423pysqlite_connection_executemany_impl(pysqlite_Connection *self,
1424 PyObject *sql, PyObject *parameters)
1425/*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001426{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001427 _Py_IDENTIFIER(executemany);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001428 PyObject* cursor = 0;
1429 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001430
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001431 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001432 if (!cursor) {
1433 goto error;
1434 }
1435
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001436 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
1437 parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001438 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001439 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001440 }
1441
1442error:
1443 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001444
1445 return cursor;
1446}
1447
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001448/*[clinic input]
1449_sqlite3.Connection.executescript as pysqlite_connection_executescript
1450
1451 sql_script as script_obj: object
1452 /
1453
1454Executes a multiple SQL statements at once. Non-standard.
1455[clinic start generated code]*/
1456
1457static PyObject *
1458pysqlite_connection_executescript(pysqlite_Connection *self,
1459 PyObject *script_obj)
1460/*[clinic end generated code: output=4c4f9d77aa0ae37d input=c0b14695aa6c81d9]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001461{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001462 _Py_IDENTIFIER(executescript);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001463 PyObject* cursor = 0;
1464 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001465
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001466 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001467 if (!cursor) {
1468 goto error;
1469 }
1470
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001471 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
1472 script_obj, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001473 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001474 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001475 }
1476
1477error:
1478 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001479
1480 return cursor;
1481}
1482
1483/* ------------------------- COLLATION CODE ------------------------ */
1484
1485static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001486pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001487 void* context,
1488 int text1_length, const void* text1_data,
1489 int text2_length, const void* text2_data)
1490{
1491 PyObject* callback = (PyObject*)context;
1492 PyObject* string1 = 0;
1493 PyObject* string2 = 0;
1494 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001495 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001496 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001497 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001498 gilstate = PyGILState_Ensure();
1499
1500 if (PyErr_Occurred()) {
1501 goto finally;
1502 }
1503
Guido van Rossum98297ee2007-11-06 21:34:58 +00001504 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1505 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001506
1507 if (!string1 || !string2) {
1508 goto finally; /* failed to allocate strings */
1509 }
1510
1511 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1512
1513 if (!retval) {
1514 /* execution failed */
1515 goto finally;
1516 }
1517
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001518 longval = PyLong_AsLongAndOverflow(retval, &result);
1519 if (longval == -1 && PyErr_Occurred()) {
1520 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 result = 0;
1522 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001523 else if (!result) {
1524 if (longval > 0)
1525 result = 1;
1526 else if (longval < 0)
1527 result = -1;
1528 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001529
1530finally:
1531 Py_XDECREF(string1);
1532 Py_XDECREF(string2);
1533 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001534 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001535 return result;
1536}
1537
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001538/*[clinic input]
1539_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1540
1541Abort any pending database operation. Non-standard.
1542[clinic start generated code]*/
1543
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001544static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001545pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1546/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001547{
1548 PyObject* retval = NULL;
1549
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001550 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001551 goto finally;
1552 }
1553
1554 sqlite3_interrupt(self->db);
1555
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001556 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001557
1558finally:
1559 return retval;
1560}
1561
Christian Heimesbbe741d2008-03-28 10:53:29 +00001562/* Function author: Paul Kippes <kippesp@gmail.com>
1563 * Class method of Connection to call the Python function _iterdump
1564 * of the sqlite3 module.
1565 */
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001566/*[clinic input]
1567_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1568
1569Returns iterator to the dump of the database in an SQL text format.
1570
1571Non-standard.
1572[clinic start generated code]*/
1573
Christian Heimesbbe741d2008-03-28 10:53:29 +00001574static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001575pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1576/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001577{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001578 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001579 PyObject* retval = NULL;
1580 PyObject* module = NULL;
1581 PyObject* module_dict;
1582 PyObject* pyfn_iterdump;
1583
1584 if (!pysqlite_check_connection(self)) {
1585 goto finally;
1586 }
1587
1588 module = PyImport_ImportModule(MODULE_NAME ".dump");
1589 if (!module) {
1590 goto finally;
1591 }
1592
1593 module_dict = PyModule_GetDict(module);
1594 if (!module_dict) {
1595 goto finally;
1596 }
1597
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001598 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001599 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001600 if (!PyErr_Occurred()) {
1601 PyErr_SetString(pysqlite_OperationalError,
1602 "Failed to obtain _iterdump() reference");
1603 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001604 goto finally;
1605 }
1606
Petr Viktorinffd97532020-02-11 17:46:57 +01001607 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001608
1609finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001610 Py_XDECREF(module);
1611 return retval;
1612}
1613
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001614/*[clinic input]
1615_sqlite3.Connection.backup as pysqlite_connection_backup
1616
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001617 target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType')
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001618 *
1619 pages: int = -1
1620 progress: object = None
1621 name: str = "main"
1622 sleep: double = 0.250
1623
1624Makes a backup of the database. Non-standard.
1625[clinic start generated code]*/
1626
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001627static PyObject *
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001628pysqlite_connection_backup_impl(pysqlite_Connection *self,
1629 pysqlite_Connection *target, int pages,
1630 PyObject *progress, const char *name,
1631 double sleep)
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001632/*[clinic end generated code: output=306a3e6a38c36334 input=30ae45fc420bfd3b]*/
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001633{
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001634 int rc;
Pablo Galindoa6d63a22020-12-29 00:28:09 +00001635 int sleep_ms = (int)(sleep * 1000.0);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001636 sqlite3 *bck_conn;
1637 sqlite3_backup *bck_handle;
Victor Stinnerca405012018-04-30 12:22:17 +02001638
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001639 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1640 return NULL;
1641 }
1642
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001643 if (!pysqlite_check_connection(target)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001644 return NULL;
1645 }
1646
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001647 if (target == self) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001648 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1649 return NULL;
1650 }
1651
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001652#if SQLITE_VERSION_NUMBER < 3008008
1653 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001654 https://www.sqlite.org/src/info/169b5505498c0a7e */
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001655 if (!sqlite3_get_autocommit(target->db)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001656 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1657 return NULL;
1658 }
1659#endif
1660
1661 if (progress != Py_None && !PyCallable_Check(progress)) {
1662 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1663 return NULL;
1664 }
1665
1666 if (pages == 0) {
1667 pages = -1;
1668 }
1669
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001670 bck_conn = target->db;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001671
1672 Py_BEGIN_ALLOW_THREADS
1673 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1674 Py_END_ALLOW_THREADS
1675
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001676 if (bck_handle == NULL) {
1677 _pysqlite_seterror(bck_conn, NULL);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001678 return NULL;
1679 }
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001680
1681 do {
1682 Py_BEGIN_ALLOW_THREADS
1683 rc = sqlite3_backup_step(bck_handle, pages);
1684 Py_END_ALLOW_THREADS
1685
1686 if (progress != Py_None) {
1687 int remaining = sqlite3_backup_remaining(bck_handle);
1688 int pagecount = sqlite3_backup_pagecount(bck_handle);
1689 PyObject *res = PyObject_CallFunction(progress, "iii", rc,
1690 remaining, pagecount);
1691 if (res == NULL) {
1692 /* Callback failed: abort backup and bail. */
1693 Py_BEGIN_ALLOW_THREADS
1694 sqlite3_backup_finish(bck_handle);
1695 Py_END_ALLOW_THREADS
1696 return NULL;
1697 }
1698 Py_DECREF(res);
1699 }
1700
1701 /* Sleep for a while if there are still further pages to copy and
1702 the engine could not make any progress */
1703 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1704 Py_BEGIN_ALLOW_THREADS
1705 sqlite3_sleep(sleep_ms);
1706 Py_END_ALLOW_THREADS
1707 }
1708 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1709
1710 Py_BEGIN_ALLOW_THREADS
1711 rc = sqlite3_backup_finish(bck_handle);
1712 Py_END_ALLOW_THREADS
1713
1714 if (rc != SQLITE_OK) {
1715 _pysqlite_seterror(bck_conn, NULL);
1716 return NULL;
1717 }
1718
1719 Py_RETURN_NONE;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001720}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001721
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001722/*[clinic input]
1723_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
1724
1725 name: unicode
1726 callback as callable: object
1727 /
1728
1729Creates a collation function. Non-standard.
1730[clinic start generated code]*/
1731
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001732static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001733pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1734 PyObject *name, PyObject *callable)
1735/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001736{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001737 PyObject* uppercase_name = 0;
Victor Stinner35466c52010-04-22 11:23:23 +00001738 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001739 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001740 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001741 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001742 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001743 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001744
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001745 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001746 goto finally;
1747 }
1748
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001749 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1750 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001751 if (!uppercase_name) {
1752 goto finally;
1753 }
1754
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001755 if (PyUnicode_READY(uppercase_name))
1756 goto finally;
1757 len = PyUnicode_GET_LENGTH(uppercase_name);
1758 kind = PyUnicode_KIND(uppercase_name);
1759 data = PyUnicode_DATA(uppercase_name);
1760 for (i=0; i<len; i++) {
1761 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1762 if ((ch >= '0' && ch <= '9')
1763 || (ch >= 'A' && ch <= 'Z')
1764 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001765 {
Victor Stinner35466c52010-04-22 11:23:23 +00001766 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001767 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001768 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001769 goto finally;
1770 }
1771 }
1772
Serhiy Storchaka06515832016-11-20 09:13:07 +02001773 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001774 if (!uppercase_name_str)
1775 goto finally;
1776
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001777 if (callable != Py_None && !PyCallable_Check(callable)) {
1778 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1779 goto finally;
1780 }
1781
1782 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001783 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1784 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001785 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001786 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1787 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001788 }
1789
1790 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001791 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001792 SQLITE_UTF8,
1793 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001794 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001795 if (rc != SQLITE_OK) {
Serhiy Storchaka90731802021-01-31 17:42:38 +02001796 if (callable != Py_None) {
1797 if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1798 PyErr_Clear();
1799 }
1800 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001801 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001802 goto finally;
1803 }
1804
1805finally:
1806 Py_XDECREF(uppercase_name);
1807
1808 if (PyErr_Occurred()) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001809 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001810 }
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001811 return Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001812}
1813
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001814/*[clinic input]
1815_sqlite3.Connection.__enter__ as pysqlite_connection_enter
1816
1817Called when the connection is used as a context manager.
1818
1819Returns itself as a convenience to the caller.
1820[clinic start generated code]*/
1821
Christian Heimesbbe741d2008-03-28 10:53:29 +00001822static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001823pysqlite_connection_enter_impl(pysqlite_Connection *self)
1824/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001825{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001826 return Py_NewRef((PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001827}
1828
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001829/*[clinic input]
1830_sqlite3.Connection.__exit__ as pysqlite_connection_exit
1831
1832 type as exc_type: object
1833 value as exc_value: object
1834 traceback as exc_tb: object
1835 /
1836
1837Called when the connection is used as a context manager.
1838
1839If there was any exception, a rollback takes place; otherwise we commit.
1840[clinic start generated code]*/
1841
Christian Heimesbbe741d2008-03-28 10:53:29 +00001842static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001843pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
1844 PyObject *exc_value, PyObject *exc_tb)
1845/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001846{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001847 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001848 PyObject* result;
1849
Christian Heimesbbe741d2008-03-28 10:53:29 +00001850 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1851 method_name = "commit";
1852 } else {
1853 method_name = "rollback";
1854 }
1855
Victor Stinner3466bde2016-09-05 18:16:01 -07001856 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001857 if (!result) {
1858 return NULL;
1859 }
1860 Py_DECREF(result);
1861
1862 Py_RETURN_FALSE;
1863}
1864
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001865static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001866PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001867
1868static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001869 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1870 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001871 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001872 {NULL}
1873};
1874
1875static PyMethodDef connection_methods[] = {
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001876 PYSQLITE_CONNECTION_BACKUP_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001877 PYSQLITE_CONNECTION_CLOSE_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001878 PYSQLITE_CONNECTION_COMMIT_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001879 PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
1880 PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
1881 PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
1882 PYSQLITE_CONNECTION_CURSOR_METHODDEF
1883 PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
1884 PYSQLITE_CONNECTION_ENTER_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001885 PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
1886 PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
1887 PYSQLITE_CONNECTION_EXECUTE_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001888 PYSQLITE_CONNECTION_EXIT_METHODDEF
1889 PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
1890 PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
1891 PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
1892 PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
1893 PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
1894 PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
1895 PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001896 {NULL, NULL}
1897};
1898
1899static struct PyMemberDef connection_members[] =
1900{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001901 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1902 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1903 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1904 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1905 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1906 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1907 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1908 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1909 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1910 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001911 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1912 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001913 {NULL}
1914};
1915
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001916static PyType_Slot connection_slots[] = {
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001917 {Py_tp_dealloc, connection_dealloc},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001918 {Py_tp_doc, (void *)connection_doc},
1919 {Py_tp_methods, connection_methods},
1920 {Py_tp_members, connection_members},
1921 {Py_tp_getset, connection_getset},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001922 {Py_tp_init, pysqlite_connection_init},
1923 {Py_tp_call, pysqlite_connection_call},
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001924 {Py_tp_traverse, connection_traverse},
1925 {Py_tp_clear, connection_clear},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001926 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001927};
1928
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001929static PyType_Spec connection_spec = {
1930 .name = MODULE_NAME ".Connection",
1931 .basicsize = sizeof(pysqlite_Connection),
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001932 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001933 .slots = connection_slots,
1934};
1935
1936PyTypeObject *pysqlite_ConnectionType = NULL;
1937
Erlend Egeberg Aasland38b6c2a2021-02-21 11:07:49 +01001938int
1939pysqlite_connection_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001940{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001941 pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
1942 if (pysqlite_ConnectionType == NULL) {
1943 return -1;
1944 }
1945 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001946}