blob: f6a6ef69e179c12a4b3072d063ff5ee92a0e8b55 [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
Anders Lorentsena22a1272017-11-07 01:47:43 +010089 database = PyBytes_AsString(database_obj);
90
Gerhard Häringf9cee222010-03-05 15:20:03 +000091 self->initialized = 1;
92
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093 self->begin_statement = NULL;
94
Oren Milman93c5a5d2017-10-10 22:27:46 +030095 Py_CLEAR(self->statement_cache);
96 Py_CLEAR(self->statements);
97 Py_CLEAR(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098
99 Py_INCREF(Py_None);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300100 Py_XSETREF(self->row_factory, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101
102 Py_INCREF(&PyUnicode_Type);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300103 Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000104
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100105#ifdef SQLITE_OPEN_URI
106 Py_BEGIN_ALLOW_THREADS
107 rc = sqlite3_open_v2(database, &self->db,
108 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
109 (uri ? SQLITE_OPEN_URI : 0), NULL);
110#else
111 if (uri) {
112 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
113 return -1;
114 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200116 /* No need to use sqlite3_open_v2 as sqlite3_open(filename, db) is the
117 same as sqlite3_open_v2(filename, db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL). */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100119#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120 Py_END_ALLOW_THREADS
121
Anders Lorentsena22a1272017-11-07 01:47:43 +0100122 Py_DECREF(database_obj);
123
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000124 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000125 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 return -1;
127 }
128
129 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000130 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000131 if (!isolation_level) {
132 return -1;
133 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 } else {
135 Py_INCREF(isolation_level);
136 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300137 Py_CLEAR(self->isolation_level);
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200138 if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100139 Py_DECREF(isolation_level);
140 return -1;
141 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142 Py_DECREF(isolation_level);
143
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200144 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000145 if (PyErr_Occurred()) {
146 return -1;
147 }
148
Gerhard Häringf9cee222010-03-05 15:20:03 +0000149 self->created_statements = 0;
150 self->created_cursors = 0;
151
152 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000153 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000154 self->cursors = PyList_New(0);
155 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000156 return -1;
157 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000158
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159 /* By default, the Cache class INCREFs the factory in its initializer, and
160 * decrefs it in its deallocator method. Since this would create a circular
161 * reference here, we're breaking it by decrementing self, and telling the
162 * cache class to not decref the factory (self) in its deallocator.
163 */
164 self->statement_cache->decref_factory = 0;
165 Py_DECREF(self);
166
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167 self->detect_types = detect_types;
168 self->timeout = timeout;
169 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000170 self->thread_ident = PyThread_get_thread_ident();
171 self->check_same_thread = check_same_thread;
172
gescheitb9a03762019-07-13 06:15:49 +0300173 self->function_pinboard_trace_callback = NULL;
174 self->function_pinboard_progress_handler = NULL;
175 self->function_pinboard_authorizer_cb = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000176
Oren Milman93c5a5d2017-10-10 22:27:46 +0300177 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000178 if (!self->collations) {
179 return -1;
180 }
181
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000182 self->Warning = pysqlite_Warning;
183 self->Error = pysqlite_Error;
184 self->InterfaceError = pysqlite_InterfaceError;
185 self->DatabaseError = pysqlite_DatabaseError;
186 self->DataError = pysqlite_DataError;
187 self->OperationalError = pysqlite_OperationalError;
188 self->IntegrityError = pysqlite_IntegrityError;
189 self->InternalError = pysqlite_InternalError;
190 self->ProgrammingError = pysqlite_ProgrammingError;
191 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000192
193 return 0;
194}
195
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000196/* action in (ACTION_RESET, ACTION_FINALIZE) */
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100197static void
198pysqlite_do_all_statements(pysqlite_Connection *self, int action,
199 int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000201 int i;
202 PyObject* weakref;
203 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000204 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205
Thomas Wouters477c8d52006-05-27 19:21:47 +0000206 for (i = 0; i < PyList_Size(self->statements); i++) {
207 weakref = PyList_GetItem(self->statements, i);
208 statement = PyWeakref_GetObject(weakref);
209 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500210 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000211 if (action == ACTION_RESET) {
212 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
213 } else {
214 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
215 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500216 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000217 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000218 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000219
220 if (reset_cursors) {
221 for (i = 0; i < PyList_Size(self->cursors); i++) {
222 weakref = PyList_GetItem(self->cursors, i);
223 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
224 if ((PyObject*)cursor != Py_None) {
225 cursor->reset = 1;
226 }
227 }
228 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229}
230
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100231static void
232pysqlite_connection_dealloc(pysqlite_Connection *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000233{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200234 PyTypeObject *tp = Py_TYPE(self);
235
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000236 Py_XDECREF(self->statement_cache);
237
238 /* Clean up if user has not called .close() explicitly. */
239 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100240 sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241 }
242
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243 Py_XDECREF(self->isolation_level);
gescheitb9a03762019-07-13 06:15:49 +0300244 Py_XDECREF(self->function_pinboard_trace_callback);
245 Py_XDECREF(self->function_pinboard_progress_handler);
246 Py_XDECREF(self->function_pinboard_authorizer_cb);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247 Py_XDECREF(self->row_factory);
248 Py_XDECREF(self->text_factory);
249 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000250 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000251 Py_XDECREF(self->cursors);
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200252
253 tp->tp_free(self);
254 Py_DECREF(tp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255}
256
Gerhard Häringf9cee222010-03-05 15:20:03 +0000257/*
258 * Registers a cursor with the connection.
259 *
260 * 0 => error; 1 => ok
261 */
262int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
263{
264 PyObject* weakref;
265
266 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
267 if (!weakref) {
268 goto error;
269 }
270
271 if (PyList_Append(connection->cursors, weakref) != 0) {
272 Py_CLEAR(weakref);
273 goto error;
274 }
275
276 Py_DECREF(weakref);
277
278 return 1;
279error:
280 return 0;
281}
282
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100283/*[clinic input]
284_sqlite3.Connection.cursor as pysqlite_connection_cursor
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100286 factory: object = NULL
287
288Return a cursor for the connection.
289[clinic start generated code]*/
290
291static PyObject *
292pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
293/*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/
294{
295 PyObject* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000297 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298 return NULL;
299 }
300
301 if (factory == NULL) {
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200302 factory = (PyObject*)pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303 }
304
Petr Viktorinffd97532020-02-11 17:46:57 +0100305 cursor = PyObject_CallOneArg(factory, (PyObject *)self);
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300306 if (cursor == NULL)
307 return NULL;
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200308 if (!PyObject_TypeCheck(cursor, pysqlite_CursorType)) {
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300309 PyErr_Format(PyExc_TypeError,
310 "factory must return a cursor, not %.100s",
311 Py_TYPE(cursor)->tp_name);
312 Py_DECREF(cursor);
313 return NULL;
314 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315
Gerhard Häringf9cee222010-03-05 15:20:03 +0000316 _pysqlite_drop_unused_cursor_references(self);
317
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300320 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000321 }
322
323 return cursor;
324}
325
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100326/*[clinic input]
327_sqlite3.Connection.close as pysqlite_connection_close
328
329Closes the connection.
330[clinic start generated code]*/
331
332static PyObject *
333pysqlite_connection_close_impl(pysqlite_Connection *self)
334/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335{
336 int rc;
337
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000338 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 return NULL;
340 }
341
Gerhard Häringf9cee222010-03-05 15:20:03 +0000342 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343
344 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100345 rc = sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346
347 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000348 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000349 return NULL;
350 } else {
351 self->db = NULL;
352 }
353 }
354
Berker Peksagfe21de92016-04-09 07:34:39 +0300355 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000356}
357
358/*
359 * Checks if a connection object is usable (i. e. not closed).
360 *
361 * 0 => error; 1 => ok
362 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000363int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000365 if (!con->initialized) {
366 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
367 return 0;
368 }
369
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000371 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372 return 0;
373 } else {
374 return 1;
375 }
376}
377
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000378PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379{
380 int rc;
381 const char* tail;
382 sqlite3_stmt* statement;
383
384 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700385 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 Py_END_ALLOW_THREADS
387
388 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000389 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 goto error;
391 }
392
Benjamin Petersond7b03282008-09-13 15:58:53 +0000393 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300394 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000395 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000396 }
397
398 Py_BEGIN_ALLOW_THREADS
399 rc = sqlite3_finalize(statement);
400 Py_END_ALLOW_THREADS
401
402 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000403 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404 }
405
406error:
407 if (PyErr_Occurred()) {
408 return NULL;
409 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200410 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000411 }
412}
413
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +0100414/*[clinic input]
415_sqlite3.Connection.commit as pysqlite_connection_commit
416
417Commit the current transaction.
418[clinic start generated code]*/
419
420static PyObject *
421pysqlite_connection_commit_impl(pysqlite_Connection *self)
422/*[clinic end generated code: output=3da45579e89407f2 input=39c12c04dda276a8]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423{
424 int rc;
425 const char* tail;
426 sqlite3_stmt* statement;
427
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000428 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000429 return NULL;
430 }
431
Berker Peksag59da4b32016-09-12 07:16:43 +0300432 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000433
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000434 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700435 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000436 Py_END_ALLOW_THREADS
437 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000438 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439 goto error;
440 }
441
Benjamin Petersond7b03282008-09-13 15:58:53 +0000442 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300443 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000444 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000445 }
446
447 Py_BEGIN_ALLOW_THREADS
448 rc = sqlite3_finalize(statement);
449 Py_END_ALLOW_THREADS
450 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000451 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000452 }
453
454 }
455
456error:
457 if (PyErr_Occurred()) {
458 return NULL;
459 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200460 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461 }
462}
463
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100464/*[clinic input]
465_sqlite3.Connection.rollback as pysqlite_connection_rollback
466
467Roll back the current transaction.
468[clinic start generated code]*/
469
470static PyObject *
471pysqlite_connection_rollback_impl(pysqlite_Connection *self)
472/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000473{
474 int rc;
475 const char* tail;
476 sqlite3_stmt* statement;
477
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000478 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000479 return NULL;
480 }
481
Berker Peksag59da4b32016-09-12 07:16:43 +0300482 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000483 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000484
485 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700486 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000487 Py_END_ALLOW_THREADS
488 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000489 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490 goto error;
491 }
492
Benjamin Petersond7b03282008-09-13 15:58:53 +0000493 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300494 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000495 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000496 }
497
498 Py_BEGIN_ALLOW_THREADS
499 rc = sqlite3_finalize(statement);
500 Py_END_ALLOW_THREADS
501 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000502 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503 }
504
505 }
506
507error:
508 if (PyErr_Occurred()) {
509 return NULL;
510 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200511 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512 }
513}
514
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200515static int
516_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200518 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000519 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000520 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200521 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
522 if (value == -1 && PyErr_Occurred())
523 return -1;
524 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000525 } else if (PyFloat_Check(py_val)) {
526 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000527 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200528 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200529 if (str == NULL)
530 return -1;
531 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000532 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200533 Py_buffer view;
534 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100535 PyErr_SetString(PyExc_ValueError,
536 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200537 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200539 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100540 PyErr_SetString(PyExc_OverflowError,
541 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200542 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100543 return -1;
544 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200545 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
546 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000547 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200548 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200550 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551}
552
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100553static PyObject *
554_pysqlite_build_py_params(sqlite3_context *context, int argc,
555 sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556{
557 PyObject* args;
558 int i;
559 sqlite3_value* cur_value;
560 PyObject* cur_py_value;
561 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000563
564 args = PyTuple_New(argc);
565 if (!args) {
566 return NULL;
567 }
568
569 for (i = 0; i < argc; i++) {
570 cur_value = argv[i];
571 switch (sqlite3_value_type(argv[i])) {
572 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500573 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000574 break;
575 case SQLITE_FLOAT:
576 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
577 break;
578 case SQLITE_TEXT:
579 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000580 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000581 /* TODO: have a way to show errors here */
582 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000583 PyErr_Clear();
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100584 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000585 }
586 break;
587 case SQLITE_BLOB:
588 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000589 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000590 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000591 break;
592 case SQLITE_NULL:
593 default:
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100594 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000596
597 if (!cur_py_value) {
598 Py_DECREF(args);
599 return NULL;
600 }
601
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602 PyTuple_SetItem(args, i, cur_py_value);
603
604 }
605
606 return args;
607}
608
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100609static void
610_pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611{
612 PyObject* args;
613 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200615 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616
617 PyGILState_STATE threadstate;
618
619 threadstate = PyGILState_Ensure();
620
621 py_func = (PyObject*)sqlite3_user_data(context);
622
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000623 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 if (args) {
625 py_retval = PyObject_CallObject(py_func, args);
626 Py_DECREF(args);
627 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200629 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200631 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000632 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200633 }
634 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700635 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000636 PyErr_Print();
637 } else {
638 PyErr_Clear();
639 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200640 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000642
643 PyGILState_Release(threadstate);
644}
645
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000646static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647{
648 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650 PyObject* aggregate_class;
651 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000653
654 PyGILState_STATE threadstate;
655
656 threadstate = PyGILState_Ensure();
657
658 aggregate_class = (PyObject*)sqlite3_user_data(context);
659
660 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
661
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200662 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100663 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000664
Thomas Wouters477c8d52006-05-27 19:21:47 +0000665 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700667 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000668 PyErr_Print();
669 } else {
670 PyErr_Clear();
671 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200672 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000673 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674 }
675 }
676
677 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678 if (!stepmethod) {
679 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 }
681
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000682 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000683 if (!args) {
684 goto error;
685 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000686
687 function_result = PyObject_CallObject(stepmethod, args);
688 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000689
Thomas Wouters477c8d52006-05-27 19:21:47 +0000690 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700691 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000692 PyErr_Print();
693 } else {
694 PyErr_Clear();
695 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200696 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000697 }
698
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699error:
700 Py_XDECREF(stepmethod);
701 Py_XDECREF(function_result);
702
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000703 PyGILState_Release(threadstate);
704}
705
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100706static void
707_pysqlite_final_callback(sqlite3_context *context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000708{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200709 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200711 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200712 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200713 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000714
715 PyGILState_STATE threadstate;
716
717 threadstate = PyGILState_Ensure();
718
Erlend Egeberg Aasland979b23c2021-02-19 12:20:32 +0100719 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
720 if (aggregate_instance == NULL) {
721 /* No rows matched the query; the step handler was never called. */
722 goto error;
723 }
724 else if (!*aggregate_instance) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000725 /* this branch is executed if there was an exception in the aggregate's
726 * __init__ */
727
Thomas Wouters477c8d52006-05-27 19:21:47 +0000728 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729 }
730
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200731 /* Keep the exception (if any) of the last call to step() */
732 PyErr_Fetch(&exception, &value, &tb);
733
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200734 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200735
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200736 Py_DECREF(*aggregate_instance);
737
738 ok = 0;
739 if (function_result) {
740 ok = _pysqlite_set_result(context, function_result) == 0;
741 Py_DECREF(function_result);
742 }
743 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700744 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000745 PyErr_Print();
746 } else {
747 PyErr_Clear();
748 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200749 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000750 }
751
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200752 /* Restore the exception (if any) of the last call to step(),
753 but clear also the current exception if finalize() failed */
754 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200755
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757 PyGILState_Release(threadstate);
758}
759
Gerhard Häringf9cee222010-03-05 15:20:03 +0000760static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000761{
762 PyObject* new_list;
763 PyObject* weakref;
764 int i;
765
766 /* we only need to do this once in a while */
767 if (self->created_statements++ < 200) {
768 return;
769 }
770
771 self->created_statements = 0;
772
773 new_list = PyList_New(0);
774 if (!new_list) {
775 return;
776 }
777
778 for (i = 0; i < PyList_Size(self->statements); i++) {
779 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000780 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000781 if (PyList_Append(new_list, weakref) != 0) {
782 Py_DECREF(new_list);
783 return;
784 }
785 }
786 }
787
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300788 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000789}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790
Gerhard Häringf9cee222010-03-05 15:20:03 +0000791static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
792{
793 PyObject* new_list;
794 PyObject* weakref;
795 int i;
796
797 /* we only need to do this once in a while */
798 if (self->created_cursors++ < 200) {
799 return;
800 }
801
802 self->created_cursors = 0;
803
804 new_list = PyList_New(0);
805 if (!new_list) {
806 return;
807 }
808
809 for (i = 0; i < PyList_Size(self->cursors); i++) {
810 weakref = PyList_GetItem(self->cursors, i);
811 if (PyWeakref_GetObject(weakref) != Py_None) {
812 if (PyList_Append(new_list, weakref) != 0) {
813 Py_DECREF(new_list);
814 return;
815 }
816 }
817 }
818
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300819 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000820}
821
gescheitb9a03762019-07-13 06:15:49 +0300822static void _destructor(void* args)
823{
824 Py_DECREF((PyObject*)args);
825}
826
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100827/*[clinic input]
828_sqlite3.Connection.create_function as pysqlite_connection_create_function
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000829
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100830 name: str
831 narg: int
832 func: object
833 *
834 deterministic: bool = False
835
836Creates a new function. Non-standard.
837[clinic start generated code]*/
838
839static PyObject *
840pysqlite_connection_create_function_impl(pysqlite_Connection *self,
841 const char *name, int narg,
842 PyObject *func, int deterministic)
843/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
844{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000845 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500846 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000847
Gerhard Häringf9cee222010-03-05 15:20:03 +0000848 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
849 return NULL;
850 }
851
Sergey Fedoseev08308582018-07-08 12:09:20 +0500852 if (deterministic) {
853#if SQLITE_VERSION_NUMBER < 3008003
854 PyErr_SetString(pysqlite_NotSupportedError,
855 "deterministic=True requires SQLite 3.8.3 or higher");
856 return NULL;
857#else
858 if (sqlite3_libversion_number() < 3008003) {
859 PyErr_SetString(pysqlite_NotSupportedError,
860 "deterministic=True requires SQLite 3.8.3 or higher");
861 return NULL;
862 }
863 flags |= SQLITE_DETERMINISTIC;
864#endif
865 }
gescheitb9a03762019-07-13 06:15:49 +0300866 rc = sqlite3_create_function_v2(self->db,
867 name,
868 narg,
869 flags,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100870 (void*)Py_NewRef(func),
gescheitb9a03762019-07-13 06:15:49 +0300871 _pysqlite_func_callback,
872 NULL,
873 NULL,
874 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000875
Thomas Wouters477c8d52006-05-27 19:21:47 +0000876 if (rc != SQLITE_OK) {
877 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000878 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000879 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000880 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500881 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882}
883
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100884/*[clinic input]
885_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000886
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100887 name: str
888 n_arg: int
889 aggregate_class: object
890
891Creates a new aggregate. Non-standard.
892[clinic start generated code]*/
893
894static PyObject *
895pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
896 const char *name, int n_arg,
897 PyObject *aggregate_class)
898/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
899{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000900 int rc;
901
Gerhard Häringf9cee222010-03-05 15:20:03 +0000902 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
903 return NULL;
904 }
905
gescheitb9a03762019-07-13 06:15:49 +0300906 rc = sqlite3_create_function_v2(self->db,
907 name,
908 n_arg,
909 SQLITE_UTF8,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100910 (void*)Py_NewRef(aggregate_class),
gescheitb9a03762019-07-13 06:15:49 +0300911 0,
912 &_pysqlite_step_callback,
913 &_pysqlite_final_callback,
914 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000915 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000916 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000917 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000918 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000919 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500920 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921}
922
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000923static 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 +0000924{
925 PyObject *ret;
926 int rc;
927 PyGILState_STATE gilstate;
928
929 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000930
Victor Stinnerd4095d92013-07-26 22:23:33 +0200931 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000932
Victor Stinnerd4095d92013-07-26 22:23:33 +0200933 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700934 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200935 PyErr_Print();
936 else
937 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200938
Victor Stinnerd4095d92013-07-26 22:23:33 +0200939 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200940 }
941 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200942 if (PyLong_Check(ret)) {
943 rc = _PyLong_AsInt(ret);
944 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700945 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200946 PyErr_Print();
947 else
948 PyErr_Clear();
949 rc = SQLITE_DENY;
950 }
951 }
952 else {
953 rc = SQLITE_DENY;
954 }
955 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000956 }
957
958 PyGILState_Release(gilstate);
959 return rc;
960}
961
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000962static int _progress_handler(void* user_arg)
963{
964 int rc;
965 PyObject *ret;
966 PyGILState_STATE gilstate;
967
968 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100969 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000970
971 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700972 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000973 PyErr_Print();
974 } else {
975 PyErr_Clear();
976 }
977
Mark Dickinson934896d2009-02-21 20:59:32 +0000978 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000979 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000980 } else {
981 rc = (int)PyObject_IsTrue(ret);
982 Py_DECREF(ret);
983 }
984
985 PyGILState_Release(gilstate);
986 return rc;
987}
988
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200989#ifdef HAVE_TRACE_V2
990/*
991 * From https://sqlite.org/c3ref/trace_v2.html:
992 * The integer return value from the callback is currently ignored, though this
993 * may change in future releases. Callback implementations should return zero
994 * to ensure future compatibility.
995 */
996static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
997#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200998static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200999#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001000{
1001 PyObject *py_statement = NULL;
1002 PyObject *ret = NULL;
1003
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001004 PyGILState_STATE gilstate;
1005
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001006#ifdef HAVE_TRACE_V2
1007 if (type != SQLITE_TRACE_STMT) {
1008 return 0;
1009 }
1010#endif
1011
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001012 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001013 py_statement = PyUnicode_DecodeUTF8(statement_string,
1014 strlen(statement_string), "replace");
1015 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001016 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001017 Py_DECREF(py_statement);
1018 }
1019
1020 if (ret) {
1021 Py_DECREF(ret);
1022 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001023 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001024 PyErr_Print();
1025 } else {
1026 PyErr_Clear();
1027 }
1028 }
1029
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001030 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001031#ifdef HAVE_TRACE_V2
1032 return 0;
1033#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001034}
1035
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001036/*[clinic input]
1037_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001038
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001039 authorizer_callback as authorizer_cb: object
1040
1041Sets authorizer callback. Non-standard.
1042[clinic start generated code]*/
1043
1044static PyObject *
1045pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1046 PyObject *authorizer_cb)
1047/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1048{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001049 int rc;
1050
Gerhard Häringf9cee222010-03-05 15:20:03 +00001051 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1052 return NULL;
1053 }
1054
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001055 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001056 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001057 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001058 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001059 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001060 } else {
1061 Py_INCREF(authorizer_cb);
1062 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001063 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001064 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001065}
1066
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001067/*[clinic input]
1068_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1069
1070 progress_handler: object
1071 n: int
1072
1073Sets progress handler callback. Non-standard.
1074[clinic start generated code]*/
1075
1076static PyObject *
1077pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1078 PyObject *progress_handler,
1079 int n)
1080/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001081{
Gerhard Häringf9cee222010-03-05 15:20:03 +00001082 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1083 return NULL;
1084 }
1085
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001086 if (progress_handler == Py_None) {
1087 /* None clears the progress handler previously set */
1088 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001089 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001090 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001091 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001092 Py_INCREF(progress_handler);
1093 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001094 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001095 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001096}
1097
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001098/*[clinic input]
1099_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1100
1101 trace_callback: object
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001102
1103Sets a trace callback called for each SQL statement (passed as unicode).
1104
1105Non-standard.
1106[clinic start generated code]*/
1107
1108static PyObject *
Dong-hee Na21793492020-12-19 00:41:33 +09001109pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1110 PyObject *trace_callback)
1111/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001112{
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001113 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1114 return NULL;
1115 }
1116
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001117 if (trace_callback == Py_None) {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001118 /*
1119 * None clears the trace callback previously set
1120 *
1121 * Ref.
1122 * - https://sqlite.org/c3ref/c_trace.html
1123 * - https://sqlite.org/c3ref/trace_v2.html
1124 */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001125#ifdef HAVE_TRACE_V2
1126 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1127#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001128 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001129#endif
gescheitb9a03762019-07-13 06:15:49 +03001130 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001131 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001132#ifdef HAVE_TRACE_V2
1133 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1134#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001135 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001136#endif
gescheitb9a03762019-07-13 06:15:49 +03001137 Py_INCREF(trace_callback);
1138 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001139 }
1140
Berker Peksagfe21de92016-04-09 07:34:39 +03001141 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001142}
1143
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001144#ifndef SQLITE_OMIT_LOAD_EXTENSION
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001145/*[clinic input]
1146_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1147
Dong-hee Na21793492020-12-19 00:41:33 +09001148 enable as onoff: bool(accept={int})
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001149 /
1150
1151Enable dynamic loading of SQLite extension modules. Non-standard.
1152[clinic start generated code]*/
1153
1154static PyObject *
1155pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1156 int onoff)
Dong-hee Na21793492020-12-19 00:41:33 +09001157/*[clinic end generated code: output=9cac37190d388baf input=5c0da5b121121cbc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001158{
1159 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001160
1161 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1162 return NULL;
1163 }
1164
Gerhard Häringf9cee222010-03-05 15:20:03 +00001165 rc = sqlite3_enable_load_extension(self->db, onoff);
1166
1167 if (rc != SQLITE_OK) {
1168 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1169 return NULL;
1170 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001171 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001172 }
1173}
1174
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001175/*[clinic input]
1176_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1177
1178 name as extension_name: str
1179 /
1180
1181Load SQLite extension module. Non-standard.
1182[clinic start generated code]*/
1183
1184static PyObject *
1185pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1186 const char *extension_name)
1187/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001188{
1189 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001190 char* errmsg;
1191
1192 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1193 return NULL;
1194 }
1195
Gerhard Häringf9cee222010-03-05 15:20:03 +00001196 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1197 if (rc != 0) {
1198 PyErr_SetString(pysqlite_OperationalError, errmsg);
1199 return NULL;
1200 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001201 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001202 }
1203}
1204#endif
1205
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001206int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001207{
1208 if (self->check_same_thread) {
1209 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001210 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001211 "SQLite objects created in a thread can only be used in that same thread. "
1212 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001213 self->thread_ident, PyThread_get_thread_ident());
1214 return 0;
1215 }
1216
1217 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001218 return 1;
1219}
1220
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001221static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001222{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001223 return Py_NewRef(self->isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224}
1225
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001226static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001228 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001229 return NULL;
1230 } else {
1231 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1232 }
1233}
1234
Berker Peksag59da4b32016-09-12 07:16:43 +03001235static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1236{
1237 if (!pysqlite_check_connection(self)) {
1238 return NULL;
1239 }
1240 if (!sqlite3_get_autocommit(self->db)) {
1241 Py_RETURN_TRUE;
1242 }
1243 Py_RETURN_FALSE;
1244}
1245
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001246static int
1247pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001248{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001249 if (isolation_level == NULL) {
1250 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1251 return -1;
1252 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001253 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001254 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001255 if (!res) {
1256 return -1;
1257 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001258 Py_DECREF(res);
1259
Serhiy Storchaka28914922016-09-01 22:18:03 +03001260 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001261 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001262 const char * const *candidate;
1263 PyObject *uppercase_level;
1264 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001265
Serhiy Storchaka28914922016-09-01 22:18:03 +03001266 if (!PyUnicode_Check(isolation_level)) {
1267 PyErr_Format(PyExc_TypeError,
1268 "isolation_level must be a string or None, not %.100s",
1269 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270 return -1;
1271 }
1272
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001273 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001274 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001275 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001276 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001277 return -1;
1278 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001279 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001280 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001281 break;
1282 }
1283 Py_DECREF(uppercase_level);
1284 if (!*candidate) {
1285 PyErr_SetString(PyExc_ValueError,
1286 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287 return -1;
1288 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001289 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001290 }
1291
Serhiy Storchaka28914922016-09-01 22:18:03 +03001292 Py_INCREF(isolation_level);
1293 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001294 return 0;
1295}
1296
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +01001297static PyObject *
1298pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1299 PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001300{
1301 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001302 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001303 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001304 int rc;
1305
Gerhard Häringf9cee222010-03-05 15:20:03 +00001306 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1307 return NULL;
1308 }
1309
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001310 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001311 return NULL;
1312
Victor Stinnerc6a23202019-06-26 03:16:24 +02001313 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001314 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001315
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001316 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001317
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +02001318 statement = PyObject_New(pysqlite_Statement, pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001319 if (!statement) {
1320 return NULL;
1321 }
1322
Victor Stinner0201f442010-03-13 03:28:34 +00001323 statement->db = NULL;
1324 statement->st = NULL;
1325 statement->sql = NULL;
1326 statement->in_use = 0;
1327 statement->in_weakreflist = NULL;
1328
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001329 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001330 if (rc != SQLITE_OK) {
1331 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001332 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001334 if (PyErr_ExceptionMatches(PyExc_TypeError))
1335 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001336 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001337 (void)pysqlite_statement_reset(statement);
1338 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001339 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001340 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001341 }
1342
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001343 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1344 if (weakref == NULL)
1345 goto error;
1346 if (PyList_Append(self->statements, weakref) != 0) {
1347 Py_DECREF(weakref);
1348 goto error;
1349 }
1350 Py_DECREF(weakref);
1351
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001352 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001353
1354error:
1355 Py_DECREF(statement);
1356 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001357}
1358
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001359/*[clinic input]
1360_sqlite3.Connection.execute as pysqlite_connection_execute
1361
1362 sql: unicode
1363 parameters: object = NULL
1364 /
1365
1366Executes a SQL statement. Non-standard.
1367[clinic start generated code]*/
1368
1369static PyObject *
1370pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
1371 PyObject *parameters)
1372/*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001374 _Py_IDENTIFIER(execute);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001375 PyObject* cursor = 0;
1376 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001377
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001378 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001379 if (!cursor) {
1380 goto error;
1381 }
1382
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001383 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001384 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001385 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001386 }
1387
1388error:
1389 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001390
1391 return cursor;
1392}
1393
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001394/*[clinic input]
1395_sqlite3.Connection.executemany as pysqlite_connection_executemany
1396
1397 sql: unicode
1398 parameters: object
1399 /
1400
1401Repeatedly executes a SQL statement. Non-standard.
1402[clinic start generated code]*/
1403
1404static PyObject *
1405pysqlite_connection_executemany_impl(pysqlite_Connection *self,
1406 PyObject *sql, PyObject *parameters)
1407/*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001408{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001409 _Py_IDENTIFIER(executemany);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001410 PyObject* cursor = 0;
1411 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001412
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001413 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001414 if (!cursor) {
1415 goto error;
1416 }
1417
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001418 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
1419 parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001420 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001421 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001422 }
1423
1424error:
1425 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001426
1427 return cursor;
1428}
1429
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001430/*[clinic input]
1431_sqlite3.Connection.executescript as pysqlite_connection_executescript
1432
1433 sql_script as script_obj: object
1434 /
1435
1436Executes a multiple SQL statements at once. Non-standard.
1437[clinic start generated code]*/
1438
1439static PyObject *
1440pysqlite_connection_executescript(pysqlite_Connection *self,
1441 PyObject *script_obj)
1442/*[clinic end generated code: output=4c4f9d77aa0ae37d input=c0b14695aa6c81d9]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001443{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001444 _Py_IDENTIFIER(executescript);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001445 PyObject* cursor = 0;
1446 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001447
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001448 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001449 if (!cursor) {
1450 goto error;
1451 }
1452
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001453 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
1454 script_obj, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001455 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001456 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001457 }
1458
1459error:
1460 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001461
1462 return cursor;
1463}
1464
1465/* ------------------------- COLLATION CODE ------------------------ */
1466
1467static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001468pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001469 void* context,
1470 int text1_length, const void* text1_data,
1471 int text2_length, const void* text2_data)
1472{
1473 PyObject* callback = (PyObject*)context;
1474 PyObject* string1 = 0;
1475 PyObject* string2 = 0;
1476 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001478 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001479 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001480 gilstate = PyGILState_Ensure();
1481
1482 if (PyErr_Occurred()) {
1483 goto finally;
1484 }
1485
Guido van Rossum98297ee2007-11-06 21:34:58 +00001486 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1487 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001488
1489 if (!string1 || !string2) {
1490 goto finally; /* failed to allocate strings */
1491 }
1492
1493 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1494
1495 if (!retval) {
1496 /* execution failed */
1497 goto finally;
1498 }
1499
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001500 longval = PyLong_AsLongAndOverflow(retval, &result);
1501 if (longval == -1 && PyErr_Occurred()) {
1502 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001503 result = 0;
1504 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001505 else if (!result) {
1506 if (longval > 0)
1507 result = 1;
1508 else if (longval < 0)
1509 result = -1;
1510 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001511
1512finally:
1513 Py_XDECREF(string1);
1514 Py_XDECREF(string2);
1515 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001516 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001517 return result;
1518}
1519
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001520/*[clinic input]
1521_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1522
1523Abort any pending database operation. Non-standard.
1524[clinic start generated code]*/
1525
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001526static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001527pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1528/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001529{
1530 PyObject* retval = NULL;
1531
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001532 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001533 goto finally;
1534 }
1535
1536 sqlite3_interrupt(self->db);
1537
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001538 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001539
1540finally:
1541 return retval;
1542}
1543
Christian Heimesbbe741d2008-03-28 10:53:29 +00001544/* Function author: Paul Kippes <kippesp@gmail.com>
1545 * Class method of Connection to call the Python function _iterdump
1546 * of the sqlite3 module.
1547 */
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001548/*[clinic input]
1549_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1550
1551Returns iterator to the dump of the database in an SQL text format.
1552
1553Non-standard.
1554[clinic start generated code]*/
1555
Christian Heimesbbe741d2008-03-28 10:53:29 +00001556static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001557pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1558/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001559{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001560 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001561 PyObject* retval = NULL;
1562 PyObject* module = NULL;
1563 PyObject* module_dict;
1564 PyObject* pyfn_iterdump;
1565
1566 if (!pysqlite_check_connection(self)) {
1567 goto finally;
1568 }
1569
1570 module = PyImport_ImportModule(MODULE_NAME ".dump");
1571 if (!module) {
1572 goto finally;
1573 }
1574
1575 module_dict = PyModule_GetDict(module);
1576 if (!module_dict) {
1577 goto finally;
1578 }
1579
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001580 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001581 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001582 if (!PyErr_Occurred()) {
1583 PyErr_SetString(pysqlite_OperationalError,
1584 "Failed to obtain _iterdump() reference");
1585 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001586 goto finally;
1587 }
1588
Petr Viktorinffd97532020-02-11 17:46:57 +01001589 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001590
1591finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001592 Py_XDECREF(module);
1593 return retval;
1594}
1595
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001596/*[clinic input]
1597_sqlite3.Connection.backup as pysqlite_connection_backup
1598
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001599 target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType')
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001600 *
1601 pages: int = -1
1602 progress: object = None
1603 name: str = "main"
1604 sleep: double = 0.250
1605
1606Makes a backup of the database. Non-standard.
1607[clinic start generated code]*/
1608
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001609static PyObject *
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001610pysqlite_connection_backup_impl(pysqlite_Connection *self,
1611 pysqlite_Connection *target, int pages,
1612 PyObject *progress, const char *name,
1613 double sleep)
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001614/*[clinic end generated code: output=306a3e6a38c36334 input=30ae45fc420bfd3b]*/
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001615{
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001616 int rc;
1617 int callback_error = 0;
Pablo Galindoa6d63a22020-12-29 00:28:09 +00001618 int sleep_ms = (int)(sleep * 1000.0);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001619 sqlite3 *bck_conn;
1620 sqlite3_backup *bck_handle;
Victor Stinnerca405012018-04-30 12:22:17 +02001621
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001622 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1623 return NULL;
1624 }
1625
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001626 if (!pysqlite_check_connection(target)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001627 return NULL;
1628 }
1629
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001630 if (target == self) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001631 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1632 return NULL;
1633 }
1634
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001635#if SQLITE_VERSION_NUMBER < 3008008
1636 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001637 https://www.sqlite.org/src/info/169b5505498c0a7e */
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001638 if (!sqlite3_get_autocommit(target->db)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001639 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1640 return NULL;
1641 }
1642#endif
1643
1644 if (progress != Py_None && !PyCallable_Check(progress)) {
1645 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1646 return NULL;
1647 }
1648
1649 if (pages == 0) {
1650 pages = -1;
1651 }
1652
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001653 bck_conn = target->db;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001654
1655 Py_BEGIN_ALLOW_THREADS
1656 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1657 Py_END_ALLOW_THREADS
1658
1659 if (bck_handle) {
1660 do {
1661 Py_BEGIN_ALLOW_THREADS
1662 rc = sqlite3_backup_step(bck_handle, pages);
1663 Py_END_ALLOW_THREADS
1664
1665 if (progress != Py_None) {
1666 PyObject *res;
1667
1668 res = PyObject_CallFunction(progress, "iii", rc,
1669 sqlite3_backup_remaining(bck_handle),
1670 sqlite3_backup_pagecount(bck_handle));
1671 if (res == NULL) {
1672 /* User's callback raised an error: interrupt the loop and
1673 propagate it. */
1674 callback_error = 1;
1675 rc = -1;
1676 } else {
1677 Py_DECREF(res);
1678 }
1679 }
1680
1681 /* Sleep for a while if there are still further pages to copy and
1682 the engine could not make any progress */
1683 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1684 Py_BEGIN_ALLOW_THREADS
Victor Stinnerca405012018-04-30 12:22:17 +02001685 sqlite3_sleep(sleep_ms);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001686 Py_END_ALLOW_THREADS
1687 }
1688 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1689
1690 Py_BEGIN_ALLOW_THREADS
1691 rc = sqlite3_backup_finish(bck_handle);
1692 Py_END_ALLOW_THREADS
1693 } else {
1694 rc = _pysqlite_seterror(bck_conn, NULL);
1695 }
1696
1697 if (!callback_error && rc != SQLITE_OK) {
1698 /* We cannot use _pysqlite_seterror() here because the backup APIs do
1699 not set the error status on the connection object, but rather on
1700 the backup handle. */
1701 if (rc == SQLITE_NOMEM) {
1702 (void)PyErr_NoMemory();
1703 } else {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001704 PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001705 }
1706 }
1707
1708 if (!callback_error && rc == SQLITE_OK) {
1709 Py_RETURN_NONE;
1710 } else {
1711 return NULL;
1712 }
1713}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001714
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001715/*[clinic input]
1716_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
1717
1718 name: unicode
1719 callback as callable: object
1720 /
1721
1722Creates a collation function. Non-standard.
1723[clinic start generated code]*/
1724
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001725static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001726pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1727 PyObject *name, PyObject *callable)
1728/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001729{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001730 PyObject* uppercase_name = 0;
Victor Stinner35466c52010-04-22 11:23:23 +00001731 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001732 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001733 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001734 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001735 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001736 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001737
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001738 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001739 goto finally;
1740 }
1741
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001742 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1743 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001744 if (!uppercase_name) {
1745 goto finally;
1746 }
1747
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001748 if (PyUnicode_READY(uppercase_name))
1749 goto finally;
1750 len = PyUnicode_GET_LENGTH(uppercase_name);
1751 kind = PyUnicode_KIND(uppercase_name);
1752 data = PyUnicode_DATA(uppercase_name);
1753 for (i=0; i<len; i++) {
1754 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1755 if ((ch >= '0' && ch <= '9')
1756 || (ch >= 'A' && ch <= 'Z')
1757 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001758 {
Victor Stinner35466c52010-04-22 11:23:23 +00001759 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001760 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001761 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762 goto finally;
1763 }
1764 }
1765
Serhiy Storchaka06515832016-11-20 09:13:07 +02001766 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001767 if (!uppercase_name_str)
1768 goto finally;
1769
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001770 if (callable != Py_None && !PyCallable_Check(callable)) {
1771 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1772 goto finally;
1773 }
1774
1775 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001776 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1777 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001778 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001779 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1780 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001781 }
1782
1783 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001784 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001785 SQLITE_UTF8,
1786 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001787 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001788 if (rc != SQLITE_OK) {
Serhiy Storchaka90731802021-01-31 17:42:38 +02001789 if (callable != Py_None) {
1790 if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1791 PyErr_Clear();
1792 }
1793 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001794 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001795 goto finally;
1796 }
1797
1798finally:
1799 Py_XDECREF(uppercase_name);
1800
1801 if (PyErr_Occurred()) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001802 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001803 }
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001804 return Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001805}
1806
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001807/*[clinic input]
1808_sqlite3.Connection.__enter__ as pysqlite_connection_enter
1809
1810Called when the connection is used as a context manager.
1811
1812Returns itself as a convenience to the caller.
1813[clinic start generated code]*/
1814
Christian Heimesbbe741d2008-03-28 10:53:29 +00001815static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001816pysqlite_connection_enter_impl(pysqlite_Connection *self)
1817/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001818{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001819 return Py_NewRef((PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001820}
1821
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001822/*[clinic input]
1823_sqlite3.Connection.__exit__ as pysqlite_connection_exit
1824
1825 type as exc_type: object
1826 value as exc_value: object
1827 traceback as exc_tb: object
1828 /
1829
1830Called when the connection is used as a context manager.
1831
1832If there was any exception, a rollback takes place; otherwise we commit.
1833[clinic start generated code]*/
1834
Christian Heimesbbe741d2008-03-28 10:53:29 +00001835static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001836pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
1837 PyObject *exc_value, PyObject *exc_tb)
1838/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001839{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001840 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001841 PyObject* result;
1842
Christian Heimesbbe741d2008-03-28 10:53:29 +00001843 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1844 method_name = "commit";
1845 } else {
1846 method_name = "rollback";
1847 }
1848
Victor Stinner3466bde2016-09-05 18:16:01 -07001849 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001850 if (!result) {
1851 return NULL;
1852 }
1853 Py_DECREF(result);
1854
1855 Py_RETURN_FALSE;
1856}
1857
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001858static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001859PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001860
1861static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001862 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1863 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001864 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001865 {NULL}
1866};
1867
1868static PyMethodDef connection_methods[] = {
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001869 PYSQLITE_CONNECTION_BACKUP_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001870 PYSQLITE_CONNECTION_CLOSE_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001871 PYSQLITE_CONNECTION_COMMIT_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001872 PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
1873 PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
1874 PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
1875 PYSQLITE_CONNECTION_CURSOR_METHODDEF
1876 PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
1877 PYSQLITE_CONNECTION_ENTER_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001878 PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
1879 PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
1880 PYSQLITE_CONNECTION_EXECUTE_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001881 PYSQLITE_CONNECTION_EXIT_METHODDEF
1882 PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
1883 PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
1884 PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
1885 PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
1886 PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
1887 PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
1888 PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001889 {NULL, NULL}
1890};
1891
1892static struct PyMemberDef connection_members[] =
1893{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001894 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1895 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1896 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1897 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1898 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1899 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1900 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1901 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1902 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1903 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001904 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1905 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001906 {NULL}
1907};
1908
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001909static PyType_Slot connection_slots[] = {
1910 {Py_tp_dealloc, pysqlite_connection_dealloc},
1911 {Py_tp_doc, (void *)connection_doc},
1912 {Py_tp_methods, connection_methods},
1913 {Py_tp_members, connection_members},
1914 {Py_tp_getset, connection_getset},
1915 {Py_tp_new, PyType_GenericNew},
1916 {Py_tp_init, pysqlite_connection_init},
1917 {Py_tp_call, pysqlite_connection_call},
1918 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001919};
1920
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001921static PyType_Spec connection_spec = {
1922 .name = MODULE_NAME ".Connection",
1923 .basicsize = sizeof(pysqlite_Connection),
1924 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1925 .slots = connection_slots,
1926};
1927
1928PyTypeObject *pysqlite_ConnectionType = NULL;
1929
Erlend Egeberg Aasland38b6c2a2021-02-21 11:07:49 +01001930int
1931pysqlite_connection_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001932{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001933 pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
1934 if (pysqlite_ConnectionType == NULL) {
1935 return -1;
1936 }
1937 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001938}