blob: 93365495f58856160de13b37627cc0aa8b2b2892 [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 Py_BEGIN_ALLOW_THREADS
106 rc = sqlite3_open_v2(database, &self->db,
107 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
108 (uri ? SQLITE_OPEN_URI : 0), NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000109 Py_END_ALLOW_THREADS
110
Anders Lorentsena22a1272017-11-07 01:47:43 +0100111 Py_DECREF(database_obj);
112
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000114 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 return -1;
116 }
117
118 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000119 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000120 if (!isolation_level) {
121 return -1;
122 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 } else {
124 Py_INCREF(isolation_level);
125 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300126 Py_CLEAR(self->isolation_level);
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200127 if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100128 Py_DECREF(isolation_level);
129 return -1;
130 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131 Py_DECREF(isolation_level);
132
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200133 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 if (PyErr_Occurred()) {
135 return -1;
136 }
137
Gerhard Häringf9cee222010-03-05 15:20:03 +0000138 self->created_statements = 0;
139 self->created_cursors = 0;
140
141 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000142 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000143 self->cursors = PyList_New(0);
144 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000145 return -1;
146 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000147
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 /* By default, the Cache class INCREFs the factory in its initializer, and
149 * decrefs it in its deallocator method. Since this would create a circular
150 * reference here, we're breaking it by decrementing self, and telling the
151 * cache class to not decref the factory (self) in its deallocator.
152 */
153 self->statement_cache->decref_factory = 0;
154 Py_DECREF(self);
155
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156 self->detect_types = detect_types;
157 self->timeout = timeout;
158 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159 self->thread_ident = PyThread_get_thread_ident();
160 self->check_same_thread = check_same_thread;
161
gescheitb9a03762019-07-13 06:15:49 +0300162 self->function_pinboard_trace_callback = NULL;
163 self->function_pinboard_progress_handler = NULL;
164 self->function_pinboard_authorizer_cb = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000165
Oren Milman93c5a5d2017-10-10 22:27:46 +0300166 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167 if (!self->collations) {
168 return -1;
169 }
170
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000171 self->Warning = pysqlite_Warning;
172 self->Error = pysqlite_Error;
173 self->InterfaceError = pysqlite_InterfaceError;
174 self->DatabaseError = pysqlite_DatabaseError;
175 self->DataError = pysqlite_DataError;
176 self->OperationalError = pysqlite_OperationalError;
177 self->IntegrityError = pysqlite_IntegrityError;
178 self->InternalError = pysqlite_InternalError;
179 self->ProgrammingError = pysqlite_ProgrammingError;
180 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000181
182 return 0;
183}
184
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000185/* action in (ACTION_RESET, ACTION_FINALIZE) */
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100186static void
187pysqlite_do_all_statements(pysqlite_Connection *self, int action,
188 int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000190 int i;
191 PyObject* weakref;
192 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000193 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194
Thomas Wouters477c8d52006-05-27 19:21:47 +0000195 for (i = 0; i < PyList_Size(self->statements); i++) {
196 weakref = PyList_GetItem(self->statements, i);
197 statement = PyWeakref_GetObject(weakref);
198 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500199 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000200 if (action == ACTION_RESET) {
201 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
202 } else {
203 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
204 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500205 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000206 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000208
209 if (reset_cursors) {
210 for (i = 0; i < PyList_Size(self->cursors); i++) {
211 weakref = PyList_GetItem(self->cursors, i);
212 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
213 if ((PyObject*)cursor != Py_None) {
214 cursor->reset = 1;
215 }
216 }
217 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000218}
219
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100220static void
221pysqlite_connection_dealloc(pysqlite_Connection *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200223 PyTypeObject *tp = Py_TYPE(self);
224
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225 Py_XDECREF(self->statement_cache);
226
227 /* Clean up if user has not called .close() explicitly. */
228 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100229 sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230 }
231
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232 Py_XDECREF(self->isolation_level);
gescheitb9a03762019-07-13 06:15:49 +0300233 Py_XDECREF(self->function_pinboard_trace_callback);
234 Py_XDECREF(self->function_pinboard_progress_handler);
235 Py_XDECREF(self->function_pinboard_authorizer_cb);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000236 Py_XDECREF(self->row_factory);
237 Py_XDECREF(self->text_factory);
238 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000239 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000240 Py_XDECREF(self->cursors);
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200241
242 tp->tp_free(self);
243 Py_DECREF(tp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244}
245
Gerhard Häringf9cee222010-03-05 15:20:03 +0000246/*
247 * Registers a cursor with the connection.
248 *
249 * 0 => error; 1 => ok
250 */
251int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
252{
253 PyObject* weakref;
254
255 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
256 if (!weakref) {
257 goto error;
258 }
259
260 if (PyList_Append(connection->cursors, weakref) != 0) {
261 Py_CLEAR(weakref);
262 goto error;
263 }
264
265 Py_DECREF(weakref);
266
267 return 1;
268error:
269 return 0;
270}
271
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100272/*[clinic input]
273_sqlite3.Connection.cursor as pysqlite_connection_cursor
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100275 factory: object = NULL
276
277Return a cursor for the connection.
278[clinic start generated code]*/
279
280static PyObject *
281pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
282/*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/
283{
284 PyObject* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000286 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 return NULL;
288 }
289
290 if (factory == NULL) {
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200291 factory = (PyObject*)pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292 }
293
Petr Viktorinffd97532020-02-11 17:46:57 +0100294 cursor = PyObject_CallOneArg(factory, (PyObject *)self);
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300295 if (cursor == NULL)
296 return NULL;
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200297 if (!PyObject_TypeCheck(cursor, pysqlite_CursorType)) {
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300298 PyErr_Format(PyExc_TypeError,
299 "factory must return a cursor, not %.100s",
300 Py_TYPE(cursor)->tp_name);
301 Py_DECREF(cursor);
302 return NULL;
303 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304
Gerhard Häringf9cee222010-03-05 15:20:03 +0000305 _pysqlite_drop_unused_cursor_references(self);
306
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300309 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000310 }
311
312 return cursor;
313}
314
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100315/*[clinic input]
316_sqlite3.Connection.close as pysqlite_connection_close
317
318Closes the connection.
319[clinic start generated code]*/
320
321static PyObject *
322pysqlite_connection_close_impl(pysqlite_Connection *self)
323/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324{
325 int rc;
326
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000327 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000328 return NULL;
329 }
330
Gerhard Häringf9cee222010-03-05 15:20:03 +0000331 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000332
333 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100334 rc = sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335
336 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000337 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 return NULL;
339 } else {
340 self->db = NULL;
341 }
342 }
343
Berker Peksagfe21de92016-04-09 07:34:39 +0300344 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000345}
346
347/*
348 * Checks if a connection object is usable (i. e. not closed).
349 *
350 * 0 => error; 1 => ok
351 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000352int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000353{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000354 if (!con->initialized) {
355 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
356 return 0;
357 }
358
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000360 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000361 return 0;
362 } else {
363 return 1;
364 }
365}
366
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000367PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368{
369 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 sqlite3_stmt* statement;
371
372 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100373 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement,
374 NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000375 Py_END_ALLOW_THREADS
376
377 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000378 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379 goto error;
380 }
381
Benjamin Petersond7b03282008-09-13 15:58:53 +0000382 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300383 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000384 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 }
386
387 Py_BEGIN_ALLOW_THREADS
388 rc = sqlite3_finalize(statement);
389 Py_END_ALLOW_THREADS
390
391 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000392 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000393 }
394
395error:
396 if (PyErr_Occurred()) {
397 return NULL;
398 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200399 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 }
401}
402
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +0100403/*[clinic input]
404_sqlite3.Connection.commit as pysqlite_connection_commit
405
406Commit the current transaction.
407[clinic start generated code]*/
408
409static PyObject *
410pysqlite_connection_commit_impl(pysqlite_Connection *self)
411/*[clinic end generated code: output=3da45579e89407f2 input=39c12c04dda276a8]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412{
413 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000414 sqlite3_stmt* statement;
415
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000416 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000417 return NULL;
418 }
419
Berker Peksag59da4b32016-09-12 07:16:43 +0300420 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000421
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000422 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100423 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 Py_END_ALLOW_THREADS
425 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000426 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000427 goto error;
428 }
429
Benjamin Petersond7b03282008-09-13 15:58:53 +0000430 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300431 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000432 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000433 }
434
435 Py_BEGIN_ALLOW_THREADS
436 rc = sqlite3_finalize(statement);
437 Py_END_ALLOW_THREADS
438 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000439 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000440 }
441
442 }
443
444error:
445 if (PyErr_Occurred()) {
446 return NULL;
447 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200448 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000449 }
450}
451
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100452/*[clinic input]
453_sqlite3.Connection.rollback as pysqlite_connection_rollback
454
455Roll back the current transaction.
456[clinic start generated code]*/
457
458static PyObject *
459pysqlite_connection_rollback_impl(pysqlite_Connection *self)
460/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461{
462 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463 sqlite3_stmt* statement;
464
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000465 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000466 return NULL;
467 }
468
Berker Peksag59da4b32016-09-12 07:16:43 +0300469 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000470 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000471
472 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100473 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474 Py_END_ALLOW_THREADS
475 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000476 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000477 goto error;
478 }
479
Benjamin Petersond7b03282008-09-13 15:58:53 +0000480 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300481 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000482 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483 }
484
485 Py_BEGIN_ALLOW_THREADS
486 rc = sqlite3_finalize(statement);
487 Py_END_ALLOW_THREADS
488 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000489 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490 }
491
492 }
493
494error:
495 if (PyErr_Occurred()) {
496 return NULL;
497 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200498 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499 }
500}
501
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200502static int
503_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000504{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200505 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000506 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000507 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200508 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
509 if (value == -1 && PyErr_Occurred())
510 return -1;
511 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512 } else if (PyFloat_Check(py_val)) {
513 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000514 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200515 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200516 if (str == NULL)
517 return -1;
518 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000519 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200520 Py_buffer view;
521 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100522 PyErr_SetString(PyExc_ValueError,
523 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200524 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000525 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200526 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100527 PyErr_SetString(PyExc_OverflowError,
528 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200529 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100530 return -1;
531 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200532 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
533 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000534 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200535 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200537 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538}
539
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100540static PyObject *
541_pysqlite_build_py_params(sqlite3_context *context, int argc,
542 sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543{
544 PyObject* args;
545 int i;
546 sqlite3_value* cur_value;
547 PyObject* cur_py_value;
548 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549
550 args = PyTuple_New(argc);
551 if (!args) {
552 return NULL;
553 }
554
555 for (i = 0; i < argc; i++) {
556 cur_value = argv[i];
557 switch (sqlite3_value_type(argv[i])) {
558 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500559 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000560 break;
561 case SQLITE_FLOAT:
562 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
563 break;
564 case SQLITE_TEXT:
565 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000566 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567 /* TODO: have a way to show errors here */
568 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000569 PyErr_Clear();
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100570 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000571 }
572 break;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200573 case SQLITE_BLOB: {
574 sqlite3 *db = sqlite3_context_db_handle(context);
575 const void *blob = sqlite3_value_blob(cur_value);
576
577 if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
578 PyErr_NoMemory();
579 goto error;
580 }
581
582 Py_ssize_t size = sqlite3_value_bytes(cur_value);
583 cur_py_value = PyBytes_FromStringAndSize(blob, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000584 break;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200585 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000586 case SQLITE_NULL:
587 default:
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100588 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000590
591 if (!cur_py_value) {
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200592 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000593 }
594
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595 PyTuple_SetItem(args, i, cur_py_value);
596
597 }
598
599 return args;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200600
601error:
602 Py_DECREF(args);
603 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604}
605
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100606static void
607_pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000608{
609 PyObject* args;
610 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200612 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000613
614 PyGILState_STATE threadstate;
615
616 threadstate = PyGILState_Ensure();
617
618 py_func = (PyObject*)sqlite3_user_data(context);
619
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000620 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000621 if (args) {
622 py_retval = PyObject_CallObject(py_func, args);
623 Py_DECREF(args);
624 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000625
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200626 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000627 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200628 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000629 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200630 }
631 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700632 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000633 PyErr_Print();
634 } else {
635 PyErr_Clear();
636 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200637 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000638 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000639
640 PyGILState_Release(threadstate);
641}
642
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000643static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644{
645 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647 PyObject* aggregate_class;
648 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650
651 PyGILState_STATE threadstate;
652
653 threadstate = PyGILState_Ensure();
654
655 aggregate_class = (PyObject*)sqlite3_user_data(context);
656
657 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
658
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200659 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100660 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000663 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700664 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000665 PyErr_Print();
666 } else {
667 PyErr_Clear();
668 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200669 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000670 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671 }
672 }
673
674 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675 if (!stepmethod) {
676 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677 }
678
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000679 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000680 if (!args) {
681 goto error;
682 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683
684 function_result = PyObject_CallObject(stepmethod, args);
685 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000686
Thomas Wouters477c8d52006-05-27 19:21:47 +0000687 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700688 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000689 PyErr_Print();
690 } else {
691 PyErr_Clear();
692 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200693 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694 }
695
Thomas Wouters477c8d52006-05-27 19:21:47 +0000696error:
697 Py_XDECREF(stepmethod);
698 Py_XDECREF(function_result);
699
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000700 PyGILState_Release(threadstate);
701}
702
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100703static void
704_pysqlite_final_callback(sqlite3_context *context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200706 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200708 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200709 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200710 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711
712 PyGILState_STATE threadstate;
713
714 threadstate = PyGILState_Ensure();
715
Erlend Egeberg Aasland979b23c2021-02-19 12:20:32 +0100716 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
717 if (aggregate_instance == NULL) {
718 /* No rows matched the query; the step handler was never called. */
719 goto error;
720 }
721 else if (!*aggregate_instance) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722 /* this branch is executed if there was an exception in the aggregate's
723 * __init__ */
724
Thomas Wouters477c8d52006-05-27 19:21:47 +0000725 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000726 }
727
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200728 /* Keep the exception (if any) of the last call to step() */
729 PyErr_Fetch(&exception, &value, &tb);
730
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200731 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200732
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200733 Py_DECREF(*aggregate_instance);
734
735 ok = 0;
736 if (function_result) {
737 ok = _pysqlite_set_result(context, function_result) == 0;
738 Py_DECREF(function_result);
739 }
740 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700741 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000742 PyErr_Print();
743 } else {
744 PyErr_Clear();
745 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200746 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000747 }
748
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200749 /* Restore the exception (if any) of the last call to step(),
750 but clear also the current exception if finalize() failed */
751 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200752
Thomas Wouters477c8d52006-05-27 19:21:47 +0000753error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000754 PyGILState_Release(threadstate);
755}
756
Gerhard Häringf9cee222010-03-05 15:20:03 +0000757static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000758{
759 PyObject* new_list;
760 PyObject* weakref;
761 int i;
762
763 /* we only need to do this once in a while */
764 if (self->created_statements++ < 200) {
765 return;
766 }
767
768 self->created_statements = 0;
769
770 new_list = PyList_New(0);
771 if (!new_list) {
772 return;
773 }
774
775 for (i = 0; i < PyList_Size(self->statements); i++) {
776 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000777 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000778 if (PyList_Append(new_list, weakref) != 0) {
779 Py_DECREF(new_list);
780 return;
781 }
782 }
783 }
784
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300785 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787
Gerhard Häringf9cee222010-03-05 15:20:03 +0000788static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
789{
790 PyObject* new_list;
791 PyObject* weakref;
792 int i;
793
794 /* we only need to do this once in a while */
795 if (self->created_cursors++ < 200) {
796 return;
797 }
798
799 self->created_cursors = 0;
800
801 new_list = PyList_New(0);
802 if (!new_list) {
803 return;
804 }
805
806 for (i = 0; i < PyList_Size(self->cursors); i++) {
807 weakref = PyList_GetItem(self->cursors, i);
808 if (PyWeakref_GetObject(weakref) != Py_None) {
809 if (PyList_Append(new_list, weakref) != 0) {
810 Py_DECREF(new_list);
811 return;
812 }
813 }
814 }
815
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300816 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000817}
818
gescheitb9a03762019-07-13 06:15:49 +0300819static void _destructor(void* args)
820{
821 Py_DECREF((PyObject*)args);
822}
823
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100824/*[clinic input]
825_sqlite3.Connection.create_function as pysqlite_connection_create_function
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100827 name: str
828 narg: int
829 func: object
830 *
831 deterministic: bool = False
832
833Creates a new function. Non-standard.
834[clinic start generated code]*/
835
836static PyObject *
837pysqlite_connection_create_function_impl(pysqlite_Connection *self,
838 const char *name, int narg,
839 PyObject *func, int deterministic)
840/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
841{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000842 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500843 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000844
Gerhard Häringf9cee222010-03-05 15:20:03 +0000845 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
846 return NULL;
847 }
848
Sergey Fedoseev08308582018-07-08 12:09:20 +0500849 if (deterministic) {
850#if SQLITE_VERSION_NUMBER < 3008003
851 PyErr_SetString(pysqlite_NotSupportedError,
852 "deterministic=True requires SQLite 3.8.3 or higher");
853 return NULL;
854#else
855 if (sqlite3_libversion_number() < 3008003) {
856 PyErr_SetString(pysqlite_NotSupportedError,
857 "deterministic=True requires SQLite 3.8.3 or higher");
858 return NULL;
859 }
860 flags |= SQLITE_DETERMINISTIC;
861#endif
862 }
gescheitb9a03762019-07-13 06:15:49 +0300863 rc = sqlite3_create_function_v2(self->db,
864 name,
865 narg,
866 flags,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100867 (void*)Py_NewRef(func),
gescheitb9a03762019-07-13 06:15:49 +0300868 _pysqlite_func_callback,
869 NULL,
870 NULL,
871 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000872
Thomas Wouters477c8d52006-05-27 19:21:47 +0000873 if (rc != SQLITE_OK) {
874 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000875 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000876 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000877 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500878 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000879}
880
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100881/*[clinic input]
882_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100884 name: str
885 n_arg: int
886 aggregate_class: object
887
888Creates a new aggregate. Non-standard.
889[clinic start generated code]*/
890
891static PyObject *
892pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
893 const char *name, int n_arg,
894 PyObject *aggregate_class)
895/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
896{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000897 int rc;
898
Gerhard Häringf9cee222010-03-05 15:20:03 +0000899 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
900 return NULL;
901 }
902
gescheitb9a03762019-07-13 06:15:49 +0300903 rc = sqlite3_create_function_v2(self->db,
904 name,
905 n_arg,
906 SQLITE_UTF8,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100907 (void*)Py_NewRef(aggregate_class),
gescheitb9a03762019-07-13 06:15:49 +0300908 0,
909 &_pysqlite_step_callback,
910 &_pysqlite_final_callback,
911 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000912 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000913 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000914 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000915 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000916 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500917 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000918}
919
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000920static 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 +0000921{
922 PyObject *ret;
923 int rc;
924 PyGILState_STATE gilstate;
925
926 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000927
Victor Stinnerd4095d92013-07-26 22:23:33 +0200928 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000929
Victor Stinnerd4095d92013-07-26 22:23:33 +0200930 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700931 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200932 PyErr_Print();
933 else
934 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200935
Victor Stinnerd4095d92013-07-26 22:23:33 +0200936 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200937 }
938 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200939 if (PyLong_Check(ret)) {
940 rc = _PyLong_AsInt(ret);
941 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700942 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200943 PyErr_Print();
944 else
945 PyErr_Clear();
946 rc = SQLITE_DENY;
947 }
948 }
949 else {
950 rc = SQLITE_DENY;
951 }
952 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000953 }
954
955 PyGILState_Release(gilstate);
956 return rc;
957}
958
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000959static int _progress_handler(void* user_arg)
960{
961 int rc;
962 PyObject *ret;
963 PyGILState_STATE gilstate;
964
965 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100966 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000967
968 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700969 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000970 PyErr_Print();
971 } else {
972 PyErr_Clear();
973 }
974
Mark Dickinson934896d2009-02-21 20:59:32 +0000975 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000976 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000977 } else {
978 rc = (int)PyObject_IsTrue(ret);
979 Py_DECREF(ret);
980 }
981
982 PyGILState_Release(gilstate);
983 return rc;
984}
985
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200986#ifdef HAVE_TRACE_V2
987/*
988 * From https://sqlite.org/c3ref/trace_v2.html:
989 * The integer return value from the callback is currently ignored, though this
990 * may change in future releases. Callback implementations should return zero
991 * to ensure future compatibility.
992 */
993static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
994#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200995static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200996#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200997{
998 PyObject *py_statement = NULL;
999 PyObject *ret = NULL;
1000
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001001 PyGILState_STATE gilstate;
1002
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001003#ifdef HAVE_TRACE_V2
1004 if (type != SQLITE_TRACE_STMT) {
1005 return 0;
1006 }
1007#endif
1008
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001009 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001010 py_statement = PyUnicode_DecodeUTF8(statement_string,
1011 strlen(statement_string), "replace");
1012 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001013 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001014 Py_DECREF(py_statement);
1015 }
1016
1017 if (ret) {
1018 Py_DECREF(ret);
1019 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001020 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001021 PyErr_Print();
1022 } else {
1023 PyErr_Clear();
1024 }
1025 }
1026
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001027 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001028#ifdef HAVE_TRACE_V2
1029 return 0;
1030#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001031}
1032
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001033/*[clinic input]
1034_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001035
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001036 authorizer_callback as authorizer_cb: object
1037
1038Sets authorizer callback. Non-standard.
1039[clinic start generated code]*/
1040
1041static PyObject *
1042pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1043 PyObject *authorizer_cb)
1044/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1045{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001046 int rc;
1047
Gerhard Häringf9cee222010-03-05 15:20:03 +00001048 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1049 return NULL;
1050 }
1051
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001052 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001053 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001054 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001055 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001056 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001057 } else {
1058 Py_INCREF(authorizer_cb);
1059 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001060 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001061 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001062}
1063
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001064/*[clinic input]
1065_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1066
1067 progress_handler: object
1068 n: int
1069
1070Sets progress handler callback. Non-standard.
1071[clinic start generated code]*/
1072
1073static PyObject *
1074pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1075 PyObject *progress_handler,
1076 int n)
1077/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001078{
Gerhard Häringf9cee222010-03-05 15:20:03 +00001079 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1080 return NULL;
1081 }
1082
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001083 if (progress_handler == Py_None) {
1084 /* None clears the progress handler previously set */
1085 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001086 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001087 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001088 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001089 Py_INCREF(progress_handler);
1090 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001091 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001092 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001093}
1094
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001095/*[clinic input]
1096_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1097
1098 trace_callback: object
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001099
1100Sets a trace callback called for each SQL statement (passed as unicode).
1101
1102Non-standard.
1103[clinic start generated code]*/
1104
1105static PyObject *
Dong-hee Na21793492020-12-19 00:41:33 +09001106pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1107 PyObject *trace_callback)
1108/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001109{
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001110 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1111 return NULL;
1112 }
1113
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001114 if (trace_callback == Py_None) {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001115 /*
1116 * None clears the trace callback previously set
1117 *
1118 * Ref.
1119 * - https://sqlite.org/c3ref/c_trace.html
1120 * - https://sqlite.org/c3ref/trace_v2.html
1121 */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001122#ifdef HAVE_TRACE_V2
1123 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1124#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001125 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001126#endif
gescheitb9a03762019-07-13 06:15:49 +03001127 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001128 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001129#ifdef HAVE_TRACE_V2
1130 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1131#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001132 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001133#endif
gescheitb9a03762019-07-13 06:15:49 +03001134 Py_INCREF(trace_callback);
1135 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001136 }
1137
Berker Peksagfe21de92016-04-09 07:34:39 +03001138 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001139}
1140
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001141#ifndef SQLITE_OMIT_LOAD_EXTENSION
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001142/*[clinic input]
1143_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1144
Dong-hee Na21793492020-12-19 00:41:33 +09001145 enable as onoff: bool(accept={int})
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001146 /
1147
1148Enable dynamic loading of SQLite extension modules. Non-standard.
1149[clinic start generated code]*/
1150
1151static PyObject *
1152pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1153 int onoff)
Dong-hee Na21793492020-12-19 00:41:33 +09001154/*[clinic end generated code: output=9cac37190d388baf input=5c0da5b121121cbc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001155{
1156 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001157
1158 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1159 return NULL;
1160 }
1161
Gerhard Häringf9cee222010-03-05 15:20:03 +00001162 rc = sqlite3_enable_load_extension(self->db, onoff);
1163
1164 if (rc != SQLITE_OK) {
1165 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1166 return NULL;
1167 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001168 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001169 }
1170}
1171
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001172/*[clinic input]
1173_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1174
1175 name as extension_name: str
1176 /
1177
1178Load SQLite extension module. Non-standard.
1179[clinic start generated code]*/
1180
1181static PyObject *
1182pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1183 const char *extension_name)
1184/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001185{
1186 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001187 char* errmsg;
1188
1189 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1190 return NULL;
1191 }
1192
Gerhard Häringf9cee222010-03-05 15:20:03 +00001193 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1194 if (rc != 0) {
1195 PyErr_SetString(pysqlite_OperationalError, errmsg);
1196 return NULL;
1197 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001198 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001199 }
1200}
1201#endif
1202
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001203int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001204{
1205 if (self->check_same_thread) {
1206 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001207 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001208 "SQLite objects created in a thread can only be used in that same thread. "
1209 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001210 self->thread_ident, PyThread_get_thread_ident());
1211 return 0;
1212 }
1213
1214 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001215 return 1;
1216}
1217
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001218static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001219{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001220 return Py_NewRef(self->isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001221}
1222
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001223static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001225 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001226 return NULL;
1227 } else {
1228 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1229 }
1230}
1231
Berker Peksag59da4b32016-09-12 07:16:43 +03001232static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1233{
1234 if (!pysqlite_check_connection(self)) {
1235 return NULL;
1236 }
1237 if (!sqlite3_get_autocommit(self->db)) {
1238 Py_RETURN_TRUE;
1239 }
1240 Py_RETURN_FALSE;
1241}
1242
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001243static int
1244pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001245{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001246 if (isolation_level == NULL) {
1247 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1248 return -1;
1249 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001251 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 if (!res) {
1253 return -1;
1254 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001255 Py_DECREF(res);
1256
Serhiy Storchaka28914922016-09-01 22:18:03 +03001257 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001258 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001259 const char * const *candidate;
1260 PyObject *uppercase_level;
1261 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001262
Serhiy Storchaka28914922016-09-01 22:18:03 +03001263 if (!PyUnicode_Check(isolation_level)) {
1264 PyErr_Format(PyExc_TypeError,
1265 "isolation_level must be a string or None, not %.100s",
1266 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001267 return -1;
1268 }
1269
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001270 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001271 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001272 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001273 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001274 return -1;
1275 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001276 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001277 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001278 break;
1279 }
1280 Py_DECREF(uppercase_level);
1281 if (!*candidate) {
1282 PyErr_SetString(PyExc_ValueError,
1283 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 return -1;
1285 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001286 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287 }
1288
Serhiy Storchaka28914922016-09-01 22:18:03 +03001289 Py_INCREF(isolation_level);
1290 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001291 return 0;
1292}
1293
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +01001294static PyObject *
1295pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1296 PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001297{
1298 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001299 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001300 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001301 int rc;
1302
Gerhard Häringf9cee222010-03-05 15:20:03 +00001303 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1304 return NULL;
1305 }
1306
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001307 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001308 return NULL;
1309
Victor Stinnerc6a23202019-06-26 03:16:24 +02001310 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001311 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001312
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001313 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001314
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +02001315 statement = PyObject_New(pysqlite_Statement, pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001316 if (!statement) {
1317 return NULL;
1318 }
1319
Victor Stinner0201f442010-03-13 03:28:34 +00001320 statement->db = NULL;
1321 statement->st = NULL;
1322 statement->sql = NULL;
1323 statement->in_use = 0;
1324 statement->in_weakreflist = NULL;
1325
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001326 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327 if (rc != SQLITE_OK) {
1328 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001329 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001330 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001331 if (PyErr_ExceptionMatches(PyExc_TypeError))
1332 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001334 (void)pysqlite_statement_reset(statement);
1335 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001336 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001337 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001338 }
1339
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001340 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1341 if (weakref == NULL)
1342 goto error;
1343 if (PyList_Append(self->statements, weakref) != 0) {
1344 Py_DECREF(weakref);
1345 goto error;
1346 }
1347 Py_DECREF(weakref);
1348
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001349 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001350
1351error:
1352 Py_DECREF(statement);
1353 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001354}
1355
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001356/*[clinic input]
1357_sqlite3.Connection.execute as pysqlite_connection_execute
1358
1359 sql: unicode
1360 parameters: object = NULL
1361 /
1362
1363Executes a SQL statement. Non-standard.
1364[clinic start generated code]*/
1365
1366static PyObject *
1367pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
1368 PyObject *parameters)
1369/*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001371 _Py_IDENTIFIER(execute);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001372 PyObject* cursor = 0;
1373 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001374
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001375 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001376 if (!cursor) {
1377 goto error;
1378 }
1379
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001380 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001381 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001382 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001383 }
1384
1385error:
1386 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001387
1388 return cursor;
1389}
1390
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001391/*[clinic input]
1392_sqlite3.Connection.executemany as pysqlite_connection_executemany
1393
1394 sql: unicode
1395 parameters: object
1396 /
1397
1398Repeatedly executes a SQL statement. Non-standard.
1399[clinic start generated code]*/
1400
1401static PyObject *
1402pysqlite_connection_executemany_impl(pysqlite_Connection *self,
1403 PyObject *sql, PyObject *parameters)
1404/*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001405{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001406 _Py_IDENTIFIER(executemany);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001407 PyObject* cursor = 0;
1408 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001409
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001410 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411 if (!cursor) {
1412 goto error;
1413 }
1414
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001415 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
1416 parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001417 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001418 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001419 }
1420
1421error:
1422 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001423
1424 return cursor;
1425}
1426
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001427/*[clinic input]
1428_sqlite3.Connection.executescript as pysqlite_connection_executescript
1429
1430 sql_script as script_obj: object
1431 /
1432
1433Executes a multiple SQL statements at once. Non-standard.
1434[clinic start generated code]*/
1435
1436static PyObject *
1437pysqlite_connection_executescript(pysqlite_Connection *self,
1438 PyObject *script_obj)
1439/*[clinic end generated code: output=4c4f9d77aa0ae37d input=c0b14695aa6c81d9]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001440{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001441 _Py_IDENTIFIER(executescript);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001442 PyObject* cursor = 0;
1443 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001444
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001445 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001446 if (!cursor) {
1447 goto error;
1448 }
1449
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001450 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
1451 script_obj, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001452 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001453 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001454 }
1455
1456error:
1457 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001458
1459 return cursor;
1460}
1461
1462/* ------------------------- COLLATION CODE ------------------------ */
1463
1464static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001465pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001466 void* context,
1467 int text1_length, const void* text1_data,
1468 int text2_length, const void* text2_data)
1469{
1470 PyObject* callback = (PyObject*)context;
1471 PyObject* string1 = 0;
1472 PyObject* string2 = 0;
1473 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001474 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001475 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001476 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477 gilstate = PyGILState_Ensure();
1478
1479 if (PyErr_Occurred()) {
1480 goto finally;
1481 }
1482
Guido van Rossum98297ee2007-11-06 21:34:58 +00001483 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1484 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001485
1486 if (!string1 || !string2) {
1487 goto finally; /* failed to allocate strings */
1488 }
1489
1490 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1491
1492 if (!retval) {
1493 /* execution failed */
1494 goto finally;
1495 }
1496
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001497 longval = PyLong_AsLongAndOverflow(retval, &result);
1498 if (longval == -1 && PyErr_Occurred()) {
1499 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001500 result = 0;
1501 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001502 else if (!result) {
1503 if (longval > 0)
1504 result = 1;
1505 else if (longval < 0)
1506 result = -1;
1507 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001508
1509finally:
1510 Py_XDECREF(string1);
1511 Py_XDECREF(string2);
1512 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001513 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001514 return result;
1515}
1516
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001517/*[clinic input]
1518_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1519
1520Abort any pending database operation. Non-standard.
1521[clinic start generated code]*/
1522
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001524pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1525/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001526{
1527 PyObject* retval = NULL;
1528
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001529 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001530 goto finally;
1531 }
1532
1533 sqlite3_interrupt(self->db);
1534
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001535 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001536
1537finally:
1538 return retval;
1539}
1540
Christian Heimesbbe741d2008-03-28 10:53:29 +00001541/* Function author: Paul Kippes <kippesp@gmail.com>
1542 * Class method of Connection to call the Python function _iterdump
1543 * of the sqlite3 module.
1544 */
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001545/*[clinic input]
1546_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1547
1548Returns iterator to the dump of the database in an SQL text format.
1549
1550Non-standard.
1551[clinic start generated code]*/
1552
Christian Heimesbbe741d2008-03-28 10:53:29 +00001553static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001554pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1555/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001556{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001557 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001558 PyObject* retval = NULL;
1559 PyObject* module = NULL;
1560 PyObject* module_dict;
1561 PyObject* pyfn_iterdump;
1562
1563 if (!pysqlite_check_connection(self)) {
1564 goto finally;
1565 }
1566
1567 module = PyImport_ImportModule(MODULE_NAME ".dump");
1568 if (!module) {
1569 goto finally;
1570 }
1571
1572 module_dict = PyModule_GetDict(module);
1573 if (!module_dict) {
1574 goto finally;
1575 }
1576
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001577 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001578 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001579 if (!PyErr_Occurred()) {
1580 PyErr_SetString(pysqlite_OperationalError,
1581 "Failed to obtain _iterdump() reference");
1582 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001583 goto finally;
1584 }
1585
Petr Viktorinffd97532020-02-11 17:46:57 +01001586 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001587
1588finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001589 Py_XDECREF(module);
1590 return retval;
1591}
1592
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001593/*[clinic input]
1594_sqlite3.Connection.backup as pysqlite_connection_backup
1595
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001596 target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType')
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001597 *
1598 pages: int = -1
1599 progress: object = None
1600 name: str = "main"
1601 sleep: double = 0.250
1602
1603Makes a backup of the database. Non-standard.
1604[clinic start generated code]*/
1605
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001606static PyObject *
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001607pysqlite_connection_backup_impl(pysqlite_Connection *self,
1608 pysqlite_Connection *target, int pages,
1609 PyObject *progress, const char *name,
1610 double sleep)
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001611/*[clinic end generated code: output=306a3e6a38c36334 input=30ae45fc420bfd3b]*/
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001612{
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001613 int rc;
Pablo Galindoa6d63a22020-12-29 00:28:09 +00001614 int sleep_ms = (int)(sleep * 1000.0);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001615 sqlite3 *bck_conn;
1616 sqlite3_backup *bck_handle;
Victor Stinnerca405012018-04-30 12:22:17 +02001617
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001618 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1619 return NULL;
1620 }
1621
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001622 if (!pysqlite_check_connection(target)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001623 return NULL;
1624 }
1625
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001626 if (target == self) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001627 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1628 return NULL;
1629 }
1630
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001631#if SQLITE_VERSION_NUMBER < 3008008
1632 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001633 https://www.sqlite.org/src/info/169b5505498c0a7e */
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001634 if (!sqlite3_get_autocommit(target->db)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001635 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1636 return NULL;
1637 }
1638#endif
1639
1640 if (progress != Py_None && !PyCallable_Check(progress)) {
1641 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1642 return NULL;
1643 }
1644
1645 if (pages == 0) {
1646 pages = -1;
1647 }
1648
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001649 bck_conn = target->db;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001650
1651 Py_BEGIN_ALLOW_THREADS
1652 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1653 Py_END_ALLOW_THREADS
1654
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001655 if (bck_handle == NULL) {
1656 _pysqlite_seterror(bck_conn, NULL);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001657 return NULL;
1658 }
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001659
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 int remaining = sqlite3_backup_remaining(bck_handle);
1667 int pagecount = sqlite3_backup_pagecount(bck_handle);
1668 PyObject *res = PyObject_CallFunction(progress, "iii", rc,
1669 remaining, pagecount);
1670 if (res == NULL) {
1671 /* Callback failed: abort backup and bail. */
1672 Py_BEGIN_ALLOW_THREADS
1673 sqlite3_backup_finish(bck_handle);
1674 Py_END_ALLOW_THREADS
1675 return NULL;
1676 }
1677 Py_DECREF(res);
1678 }
1679
1680 /* Sleep for a while if there are still further pages to copy and
1681 the engine could not make any progress */
1682 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1683 Py_BEGIN_ALLOW_THREADS
1684 sqlite3_sleep(sleep_ms);
1685 Py_END_ALLOW_THREADS
1686 }
1687 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1688
1689 Py_BEGIN_ALLOW_THREADS
1690 rc = sqlite3_backup_finish(bck_handle);
1691 Py_END_ALLOW_THREADS
1692
1693 if (rc != SQLITE_OK) {
1694 _pysqlite_seterror(bck_conn, NULL);
1695 return NULL;
1696 }
1697
1698 Py_RETURN_NONE;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001699}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001700
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001701/*[clinic input]
1702_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
1703
1704 name: unicode
1705 callback as callable: object
1706 /
1707
1708Creates a collation function. Non-standard.
1709[clinic start generated code]*/
1710
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001711static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001712pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1713 PyObject *name, PyObject *callable)
1714/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001715{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001716 PyObject* uppercase_name = 0;
Victor Stinner35466c52010-04-22 11:23:23 +00001717 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001718 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001719 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001720 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001721 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001722 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001723
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001724 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001725 goto finally;
1726 }
1727
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001728 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1729 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001730 if (!uppercase_name) {
1731 goto finally;
1732 }
1733
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001734 if (PyUnicode_READY(uppercase_name))
1735 goto finally;
1736 len = PyUnicode_GET_LENGTH(uppercase_name);
1737 kind = PyUnicode_KIND(uppercase_name);
1738 data = PyUnicode_DATA(uppercase_name);
1739 for (i=0; i<len; i++) {
1740 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1741 if ((ch >= '0' && ch <= '9')
1742 || (ch >= 'A' && ch <= 'Z')
1743 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001744 {
Victor Stinner35466c52010-04-22 11:23:23 +00001745 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001746 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001747 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001748 goto finally;
1749 }
1750 }
1751
Serhiy Storchaka06515832016-11-20 09:13:07 +02001752 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001753 if (!uppercase_name_str)
1754 goto finally;
1755
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001756 if (callable != Py_None && !PyCallable_Check(callable)) {
1757 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1758 goto finally;
1759 }
1760
1761 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001762 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1763 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001764 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001765 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1766 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001767 }
1768
1769 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001770 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001771 SQLITE_UTF8,
1772 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001773 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001774 if (rc != SQLITE_OK) {
Serhiy Storchaka90731802021-01-31 17:42:38 +02001775 if (callable != Py_None) {
1776 if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1777 PyErr_Clear();
1778 }
1779 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001780 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001781 goto finally;
1782 }
1783
1784finally:
1785 Py_XDECREF(uppercase_name);
1786
1787 if (PyErr_Occurred()) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001788 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001789 }
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001790 return Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001791}
1792
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001793/*[clinic input]
1794_sqlite3.Connection.__enter__ as pysqlite_connection_enter
1795
1796Called when the connection is used as a context manager.
1797
1798Returns itself as a convenience to the caller.
1799[clinic start generated code]*/
1800
Christian Heimesbbe741d2008-03-28 10:53:29 +00001801static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001802pysqlite_connection_enter_impl(pysqlite_Connection *self)
1803/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001804{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001805 return Py_NewRef((PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001806}
1807
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001808/*[clinic input]
1809_sqlite3.Connection.__exit__ as pysqlite_connection_exit
1810
1811 type as exc_type: object
1812 value as exc_value: object
1813 traceback as exc_tb: object
1814 /
1815
1816Called when the connection is used as a context manager.
1817
1818If there was any exception, a rollback takes place; otherwise we commit.
1819[clinic start generated code]*/
1820
Christian Heimesbbe741d2008-03-28 10:53:29 +00001821static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001822pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
1823 PyObject *exc_value, PyObject *exc_tb)
1824/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001825{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001826 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001827 PyObject* result;
1828
Christian Heimesbbe741d2008-03-28 10:53:29 +00001829 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1830 method_name = "commit";
1831 } else {
1832 method_name = "rollback";
1833 }
1834
Victor Stinner3466bde2016-09-05 18:16:01 -07001835 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001836 if (!result) {
1837 return NULL;
1838 }
1839 Py_DECREF(result);
1840
1841 Py_RETURN_FALSE;
1842}
1843
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001844static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001845PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001846
1847static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001848 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1849 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001850 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001851 {NULL}
1852};
1853
1854static PyMethodDef connection_methods[] = {
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001855 PYSQLITE_CONNECTION_BACKUP_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001856 PYSQLITE_CONNECTION_CLOSE_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001857 PYSQLITE_CONNECTION_COMMIT_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001858 PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
1859 PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
1860 PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
1861 PYSQLITE_CONNECTION_CURSOR_METHODDEF
1862 PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
1863 PYSQLITE_CONNECTION_ENTER_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001864 PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
1865 PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
1866 PYSQLITE_CONNECTION_EXECUTE_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001867 PYSQLITE_CONNECTION_EXIT_METHODDEF
1868 PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
1869 PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
1870 PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
1871 PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
1872 PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
1873 PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
1874 PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001875 {NULL, NULL}
1876};
1877
1878static struct PyMemberDef connection_members[] =
1879{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001880 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1881 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1882 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1883 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1884 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1885 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1886 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1887 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1888 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1889 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001890 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1891 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001892 {NULL}
1893};
1894
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001895static PyType_Slot connection_slots[] = {
1896 {Py_tp_dealloc, pysqlite_connection_dealloc},
1897 {Py_tp_doc, (void *)connection_doc},
1898 {Py_tp_methods, connection_methods},
1899 {Py_tp_members, connection_members},
1900 {Py_tp_getset, connection_getset},
1901 {Py_tp_new, PyType_GenericNew},
1902 {Py_tp_init, pysqlite_connection_init},
1903 {Py_tp_call, pysqlite_connection_call},
1904 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001905};
1906
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001907static PyType_Spec connection_spec = {
1908 .name = MODULE_NAME ".Connection",
1909 .basicsize = sizeof(pysqlite_Connection),
1910 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1911 .slots = connection_slots,
1912};
1913
1914PyTypeObject *pysqlite_ConnectionType = NULL;
1915
Erlend Egeberg Aasland38b6c2a2021-02-21 11:07:49 +01001916int
1917pysqlite_connection_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001918{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001919 pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
1920 if (pysqlite_ConnectionType == NULL) {
1921 return -1;
1922 }
1923 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001924}