blob: 81f12e83c2fe11c874d5f7cb47dd2144407862eb [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000060
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000061int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010063 static char *kwlist[] = {
64 "database", "timeout", "detect_types", "isolation_level",
65 "check_same_thread", "factory", "cached_statements", "uri",
66 NULL
67 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +030069 const char* database;
Anders Lorentsena22a1272017-11-07 01:47:43 +010070 PyObject* database_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071 int detect_types = 0;
72 PyObject* isolation_level = NULL;
73 PyObject* factory = NULL;
74 int check_same_thread = 1;
75 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010076 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077 double timeout = 5.0;
78 int rc;
79
Anders Lorentsena22a1272017-11-07 01:47:43 +010080 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
81 PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010082 &isolation_level, &check_same_thread,
83 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000085 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 }
87
Anders Lorentsena22a1272017-11-07 01:47:43 +010088 database = PyBytes_AsString(database_obj);
89
Gerhard Häringf9cee222010-03-05 15:20:03 +000090 self->initialized = 1;
91
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092 self->begin_statement = NULL;
93
Oren Milman93c5a5d2017-10-10 22:27:46 +030094 Py_CLEAR(self->statement_cache);
95 Py_CLEAR(self->statements);
96 Py_CLEAR(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097
98 Py_INCREF(Py_None);
Oren Milman93c5a5d2017-10-10 22:27:46 +030099 Py_XSETREF(self->row_factory, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000100
101 Py_INCREF(&PyUnicode_Type);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300102 Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000103
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100104#ifdef SQLITE_OPEN_URI
105 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);
109#else
110 if (uri) {
111 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
112 return -1;
113 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200115 /* No need to use sqlite3_open_v2 as sqlite3_open(filename, db) is the
116 same as sqlite3_open_v2(filename, db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL). */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100118#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 Py_END_ALLOW_THREADS
120
Anders Lorentsena22a1272017-11-07 01:47:43 +0100121 Py_DECREF(database_obj);
122
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000124 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125 return -1;
126 }
127
128 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000129 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000130 if (!isolation_level) {
131 return -1;
132 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000133 } else {
134 Py_INCREF(isolation_level);
135 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300136 Py_CLEAR(self->isolation_level);
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200137 if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100138 Py_DECREF(isolation_level);
139 return -1;
140 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141 Py_DECREF(isolation_level);
142
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200143 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000144 if (PyErr_Occurred()) {
145 return -1;
146 }
147
Gerhard Häringf9cee222010-03-05 15:20:03 +0000148 self->created_statements = 0;
149 self->created_cursors = 0;
150
151 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000152 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000153 self->cursors = PyList_New(0);
154 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000155 return -1;
156 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158 /* By default, the Cache class INCREFs the factory in its initializer, and
159 * decrefs it in its deallocator method. Since this would create a circular
160 * reference here, we're breaking it by decrementing self, and telling the
161 * cache class to not decref the factory (self) in its deallocator.
162 */
163 self->statement_cache->decref_factory = 0;
164 Py_DECREF(self);
165
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000166 self->detect_types = detect_types;
167 self->timeout = timeout;
168 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169 self->thread_ident = PyThread_get_thread_ident();
170 self->check_same_thread = check_same_thread;
171
gescheitb9a03762019-07-13 06:15:49 +0300172 self->function_pinboard_trace_callback = NULL;
173 self->function_pinboard_progress_handler = NULL;
174 self->function_pinboard_authorizer_cb = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175
Oren Milman93c5a5d2017-10-10 22:27:46 +0300176 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000177 if (!self->collations) {
178 return -1;
179 }
180
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000181 self->Warning = pysqlite_Warning;
182 self->Error = pysqlite_Error;
183 self->InterfaceError = pysqlite_InterfaceError;
184 self->DatabaseError = pysqlite_DatabaseError;
185 self->DataError = pysqlite_DataError;
186 self->OperationalError = pysqlite_OperationalError;
187 self->IntegrityError = pysqlite_IntegrityError;
188 self->InternalError = pysqlite_InternalError;
189 self->ProgrammingError = pysqlite_ProgrammingError;
190 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191
192 return 0;
193}
194
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000195/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000196void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000198 int i;
199 PyObject* weakref;
200 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000201 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202
Thomas Wouters477c8d52006-05-27 19:21:47 +0000203 for (i = 0; i < PyList_Size(self->statements); i++) {
204 weakref = PyList_GetItem(self->statements, i);
205 statement = PyWeakref_GetObject(weakref);
206 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500207 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000208 if (action == ACTION_RESET) {
209 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
210 } else {
211 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
212 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500213 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000214 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000216
217 if (reset_cursors) {
218 for (i = 0; i < PyList_Size(self->cursors); i++) {
219 weakref = PyList_GetItem(self->cursors, i);
220 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
221 if ((PyObject*)cursor != Py_None) {
222 cursor->reset = 1;
223 }
224 }
225 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000226}
227
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000228void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200230 PyTypeObject *tp = Py_TYPE(self);
231
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232 Py_XDECREF(self->statement_cache);
233
234 /* Clean up if user has not called .close() explicitly. */
235 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100236 sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237 }
238
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000239 Py_XDECREF(self->isolation_level);
gescheitb9a03762019-07-13 06:15:49 +0300240 Py_XDECREF(self->function_pinboard_trace_callback);
241 Py_XDECREF(self->function_pinboard_progress_handler);
242 Py_XDECREF(self->function_pinboard_authorizer_cb);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243 Py_XDECREF(self->row_factory);
244 Py_XDECREF(self->text_factory);
245 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000246 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000247 Py_XDECREF(self->cursors);
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200248
249 tp->tp_free(self);
250 Py_DECREF(tp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000251}
252
Gerhard Häringf9cee222010-03-05 15:20:03 +0000253/*
254 * Registers a cursor with the connection.
255 *
256 * 0 => error; 1 => ok
257 */
258int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
259{
260 PyObject* weakref;
261
262 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
263 if (!weakref) {
264 goto error;
265 }
266
267 if (PyList_Append(connection->cursors, weakref) != 0) {
268 Py_CLEAR(weakref);
269 goto error;
270 }
271
272 Py_DECREF(weakref);
273
274 return 1;
275error:
276 return 0;
277}
278
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100279/*[clinic input]
280_sqlite3.Connection.cursor as pysqlite_connection_cursor
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000281
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100282 factory: object = NULL
283
284Return a cursor for the connection.
285[clinic start generated code]*/
286
287static PyObject *
288pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
289/*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/
290{
291 PyObject* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000293 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294 return NULL;
295 }
296
297 if (factory == NULL) {
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200298 factory = (PyObject*)pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000299 }
300
Petr Viktorinffd97532020-02-11 17:46:57 +0100301 cursor = PyObject_CallOneArg(factory, (PyObject *)self);
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300302 if (cursor == NULL)
303 return NULL;
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200304 if (!PyObject_TypeCheck(cursor, pysqlite_CursorType)) {
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300305 PyErr_Format(PyExc_TypeError,
306 "factory must return a cursor, not %.100s",
307 Py_TYPE(cursor)->tp_name);
308 Py_DECREF(cursor);
309 return NULL;
310 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000311
Gerhard Häringf9cee222010-03-05 15:20:03 +0000312 _pysqlite_drop_unused_cursor_references(self);
313
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000314 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300316 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317 }
318
319 return cursor;
320}
321
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100322/*[clinic input]
323_sqlite3.Connection.close as pysqlite_connection_close
324
325Closes the connection.
326[clinic start generated code]*/
327
328static PyObject *
329pysqlite_connection_close_impl(pysqlite_Connection *self)
330/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331{
332 int rc;
333
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000334 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335 return NULL;
336 }
337
Gerhard Häringf9cee222010-03-05 15:20:03 +0000338 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339
340 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100341 rc = sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000342
343 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000344 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000345 return NULL;
346 } else {
347 self->db = NULL;
348 }
349 }
350
Berker Peksagfe21de92016-04-09 07:34:39 +0300351 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352}
353
354/*
355 * Checks if a connection object is usable (i. e. not closed).
356 *
357 * 0 => error; 1 => ok
358 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000359int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000361 if (!con->initialized) {
362 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
363 return 0;
364 }
365
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000367 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 return 0;
369 } else {
370 return 1;
371 }
372}
373
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000374PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000375{
376 int rc;
377 const char* tail;
378 sqlite3_stmt* statement;
379
380 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700381 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 Py_END_ALLOW_THREADS
383
384 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000385 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 goto error;
387 }
388
Benjamin Petersond7b03282008-09-13 15:58:53 +0000389 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300390 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000391 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 }
393
394 Py_BEGIN_ALLOW_THREADS
395 rc = sqlite3_finalize(statement);
396 Py_END_ALLOW_THREADS
397
398 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000399 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 }
401
402error:
403 if (PyErr_Occurred()) {
404 return NULL;
405 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200406 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000407 }
408}
409
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +0100410/*[clinic input]
411_sqlite3.Connection.commit as pysqlite_connection_commit
412
413Commit the current transaction.
414[clinic start generated code]*/
415
416static PyObject *
417pysqlite_connection_commit_impl(pysqlite_Connection *self)
418/*[clinic end generated code: output=3da45579e89407f2 input=39c12c04dda276a8]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419{
420 int rc;
421 const char* tail;
422 sqlite3_stmt* statement;
423
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000424 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425 return NULL;
426 }
427
Berker Peksag59da4b32016-09-12 07:16:43 +0300428 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000429
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700431 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432 Py_END_ALLOW_THREADS
433 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000434 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000435 goto error;
436 }
437
Benjamin Petersond7b03282008-09-13 15:58:53 +0000438 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300439 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000440 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000441 }
442
443 Py_BEGIN_ALLOW_THREADS
444 rc = sqlite3_finalize(statement);
445 Py_END_ALLOW_THREADS
446 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000447 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 }
449
450 }
451
452error:
453 if (PyErr_Occurred()) {
454 return NULL;
455 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200456 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457 }
458}
459
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100460/*[clinic input]
461_sqlite3.Connection.rollback as pysqlite_connection_rollback
462
463Roll back the current transaction.
464[clinic start generated code]*/
465
466static PyObject *
467pysqlite_connection_rollback_impl(pysqlite_Connection *self)
468/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469{
470 int rc;
471 const char* tail;
472 sqlite3_stmt* statement;
473
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000474 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 return NULL;
476 }
477
Berker Peksag59da4b32016-09-12 07:16:43 +0300478 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000479 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000480
481 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700482 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483 Py_END_ALLOW_THREADS
484 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000485 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000486 goto error;
487 }
488
Benjamin Petersond7b03282008-09-13 15:58:53 +0000489 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300490 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000491 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492 }
493
494 Py_BEGIN_ALLOW_THREADS
495 rc = sqlite3_finalize(statement);
496 Py_END_ALLOW_THREADS
497 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000498 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499 }
500
501 }
502
503error:
504 if (PyErr_Occurred()) {
505 return NULL;
506 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200507 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 }
509}
510
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200511static int
512_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200514 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000516 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200517 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
518 if (value == -1 && PyErr_Occurred())
519 return -1;
520 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000521 } else if (PyFloat_Check(py_val)) {
522 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000523 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200524 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200525 if (str == NULL)
526 return -1;
527 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000528 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200529 Py_buffer view;
530 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100531 PyErr_SetString(PyExc_ValueError,
532 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200533 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000534 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200535 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100536 PyErr_SetString(PyExc_OverflowError,
537 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200538 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100539 return -1;
540 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200541 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
542 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200544 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200546 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000547}
548
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000549PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550{
551 PyObject* args;
552 int i;
553 sqlite3_value* cur_value;
554 PyObject* cur_py_value;
555 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000557
558 args = PyTuple_New(argc);
559 if (!args) {
560 return NULL;
561 }
562
563 for (i = 0; i < argc; i++) {
564 cur_value = argv[i];
565 switch (sqlite3_value_type(argv[i])) {
566 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500567 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000568 break;
569 case SQLITE_FLOAT:
570 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
571 break;
572 case SQLITE_TEXT:
573 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000574 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000575 /* TODO: have a way to show errors here */
576 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000577 PyErr_Clear();
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100578 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 }
580 break;
581 case SQLITE_BLOB:
582 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000583 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000584 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000585 break;
586 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) {
592 Py_DECREF(args);
593 return NULL;
594 }
595
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596 PyTuple_SetItem(args, i, cur_py_value);
597
598 }
599
600 return args;
601}
602
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100603static void
604_pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000605{
606 PyObject* args;
607 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000608 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200609 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610
611 PyGILState_STATE threadstate;
612
613 threadstate = PyGILState_Ensure();
614
615 py_func = (PyObject*)sqlite3_user_data(context);
616
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000617 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000618 if (args) {
619 py_retval = PyObject_CallObject(py_func, args);
620 Py_DECREF(args);
621 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200623 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000624 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200625 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200627 }
628 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700629 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630 PyErr_Print();
631 } else {
632 PyErr_Clear();
633 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200634 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000635 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000636
637 PyGILState_Release(threadstate);
638}
639
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000640static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000641{
642 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000643 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644 PyObject* aggregate_class;
645 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647
648 PyGILState_STATE threadstate;
649
650 threadstate = PyGILState_Ensure();
651
652 aggregate_class = (PyObject*)sqlite3_user_data(context);
653
654 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
655
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200656 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100657 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700661 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000662 PyErr_Print();
663 } else {
664 PyErr_Clear();
665 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200666 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000667 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668 }
669 }
670
671 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 if (!stepmethod) {
673 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674 }
675
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000676 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 if (!args) {
678 goto error;
679 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680
681 function_result = PyObject_CallObject(stepmethod, args);
682 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683
Thomas Wouters477c8d52006-05-27 19:21:47 +0000684 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700685 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000686 PyErr_Print();
687 } else {
688 PyErr_Clear();
689 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200690 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000691 }
692
Thomas Wouters477c8d52006-05-27 19:21:47 +0000693error:
694 Py_XDECREF(stepmethod);
695 Py_XDECREF(function_result);
696
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000697 PyGILState_Release(threadstate);
698}
699
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100700static void
701_pysqlite_final_callback(sqlite3_context *context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000702{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200703 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200705 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200706 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200707 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000708
709 PyGILState_STATE threadstate;
710
711 threadstate = PyGILState_Ensure();
712
Erlend Egeberg Aasland979b23c2021-02-19 12:20:32 +0100713 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
714 if (aggregate_instance == NULL) {
715 /* No rows matched the query; the step handler was never called. */
716 goto error;
717 }
718 else if (!*aggregate_instance) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719 /* this branch is executed if there was an exception in the aggregate's
720 * __init__ */
721
Thomas Wouters477c8d52006-05-27 19:21:47 +0000722 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000723 }
724
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200725 /* Keep the exception (if any) of the last call to step() */
726 PyErr_Fetch(&exception, &value, &tb);
727
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200728 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200729
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200730 Py_DECREF(*aggregate_instance);
731
732 ok = 0;
733 if (function_result) {
734 ok = _pysqlite_set_result(context, function_result) == 0;
735 Py_DECREF(function_result);
736 }
737 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700738 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000739 PyErr_Print();
740 } else {
741 PyErr_Clear();
742 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200743 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744 }
745
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200746 /* Restore the exception (if any) of the last call to step(),
747 but clear also the current exception if finalize() failed */
748 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200749
Thomas Wouters477c8d52006-05-27 19:21:47 +0000750error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751 PyGILState_Release(threadstate);
752}
753
Gerhard Häringf9cee222010-03-05 15:20:03 +0000754static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000755{
756 PyObject* new_list;
757 PyObject* weakref;
758 int i;
759
760 /* we only need to do this once in a while */
761 if (self->created_statements++ < 200) {
762 return;
763 }
764
765 self->created_statements = 0;
766
767 new_list = PyList_New(0);
768 if (!new_list) {
769 return;
770 }
771
772 for (i = 0; i < PyList_Size(self->statements); i++) {
773 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000774 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000775 if (PyList_Append(new_list, weakref) != 0) {
776 Py_DECREF(new_list);
777 return;
778 }
779 }
780 }
781
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300782 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000783}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000784
Gerhard Häringf9cee222010-03-05 15:20:03 +0000785static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
786{
787 PyObject* new_list;
788 PyObject* weakref;
789 int i;
790
791 /* we only need to do this once in a while */
792 if (self->created_cursors++ < 200) {
793 return;
794 }
795
796 self->created_cursors = 0;
797
798 new_list = PyList_New(0);
799 if (!new_list) {
800 return;
801 }
802
803 for (i = 0; i < PyList_Size(self->cursors); i++) {
804 weakref = PyList_GetItem(self->cursors, i);
805 if (PyWeakref_GetObject(weakref) != Py_None) {
806 if (PyList_Append(new_list, weakref) != 0) {
807 Py_DECREF(new_list);
808 return;
809 }
810 }
811 }
812
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300813 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000814}
815
gescheitb9a03762019-07-13 06:15:49 +0300816static void _destructor(void* args)
817{
818 Py_DECREF((PyObject*)args);
819}
820
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100821/*[clinic input]
822_sqlite3.Connection.create_function as pysqlite_connection_create_function
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000823
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100824 name: str
825 narg: int
826 func: object
827 *
828 deterministic: bool = False
829
830Creates a new function. Non-standard.
831[clinic start generated code]*/
832
833static PyObject *
834pysqlite_connection_create_function_impl(pysqlite_Connection *self,
835 const char *name, int narg,
836 PyObject *func, int deterministic)
837/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
838{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500840 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000841
Gerhard Häringf9cee222010-03-05 15:20:03 +0000842 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
843 return NULL;
844 }
845
Sergey Fedoseev08308582018-07-08 12:09:20 +0500846 if (deterministic) {
847#if SQLITE_VERSION_NUMBER < 3008003
848 PyErr_SetString(pysqlite_NotSupportedError,
849 "deterministic=True requires SQLite 3.8.3 or higher");
850 return NULL;
851#else
852 if (sqlite3_libversion_number() < 3008003) {
853 PyErr_SetString(pysqlite_NotSupportedError,
854 "deterministic=True requires SQLite 3.8.3 or higher");
855 return NULL;
856 }
857 flags |= SQLITE_DETERMINISTIC;
858#endif
859 }
gescheitb9a03762019-07-13 06:15:49 +0300860 rc = sqlite3_create_function_v2(self->db,
861 name,
862 narg,
863 flags,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100864 (void*)Py_NewRef(func),
gescheitb9a03762019-07-13 06:15:49 +0300865 _pysqlite_func_callback,
866 NULL,
867 NULL,
868 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000869
Thomas Wouters477c8d52006-05-27 19:21:47 +0000870 if (rc != SQLITE_OK) {
871 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000872 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000873 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000874 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500875 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000876}
877
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100878/*[clinic input]
879_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100881 name: str
882 n_arg: int
883 aggregate_class: object
884
885Creates a new aggregate. Non-standard.
886[clinic start generated code]*/
887
888static PyObject *
889pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
890 const char *name, int n_arg,
891 PyObject *aggregate_class)
892/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
893{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000894 int rc;
895
Gerhard Häringf9cee222010-03-05 15:20:03 +0000896 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
897 return NULL;
898 }
899
gescheitb9a03762019-07-13 06:15:49 +0300900 rc = sqlite3_create_function_v2(self->db,
901 name,
902 n_arg,
903 SQLITE_UTF8,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100904 (void*)Py_NewRef(aggregate_class),
gescheitb9a03762019-07-13 06:15:49 +0300905 0,
906 &_pysqlite_step_callback,
907 &_pysqlite_final_callback,
908 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000909 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000910 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000911 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000912 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000913 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500914 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000915}
916
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000917static 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 +0000918{
919 PyObject *ret;
920 int rc;
921 PyGILState_STATE gilstate;
922
923 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000924
Victor Stinnerd4095d92013-07-26 22:23:33 +0200925 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000926
Victor Stinnerd4095d92013-07-26 22:23:33 +0200927 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700928 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200929 PyErr_Print();
930 else
931 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200932
Victor Stinnerd4095d92013-07-26 22:23:33 +0200933 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200934 }
935 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200936 if (PyLong_Check(ret)) {
937 rc = _PyLong_AsInt(ret);
938 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700939 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200940 PyErr_Print();
941 else
942 PyErr_Clear();
943 rc = SQLITE_DENY;
944 }
945 }
946 else {
947 rc = SQLITE_DENY;
948 }
949 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000950 }
951
952 PyGILState_Release(gilstate);
953 return rc;
954}
955
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000956static int _progress_handler(void* user_arg)
957{
958 int rc;
959 PyObject *ret;
960 PyGILState_STATE gilstate;
961
962 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100963 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000964
965 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700966 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000967 PyErr_Print();
968 } else {
969 PyErr_Clear();
970 }
971
Mark Dickinson934896d2009-02-21 20:59:32 +0000972 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000973 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000974 } else {
975 rc = (int)PyObject_IsTrue(ret);
976 Py_DECREF(ret);
977 }
978
979 PyGILState_Release(gilstate);
980 return rc;
981}
982
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200983#ifdef HAVE_TRACE_V2
984/*
985 * From https://sqlite.org/c3ref/trace_v2.html:
986 * The integer return value from the callback is currently ignored, though this
987 * may change in future releases. Callback implementations should return zero
988 * to ensure future compatibility.
989 */
990static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
991#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200992static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200993#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200994{
995 PyObject *py_statement = NULL;
996 PyObject *ret = NULL;
997
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200998 PyGILState_STATE gilstate;
999
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001000#ifdef HAVE_TRACE_V2
1001 if (type != SQLITE_TRACE_STMT) {
1002 return 0;
1003 }
1004#endif
1005
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001006 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001007 py_statement = PyUnicode_DecodeUTF8(statement_string,
1008 strlen(statement_string), "replace");
1009 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001010 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001011 Py_DECREF(py_statement);
1012 }
1013
1014 if (ret) {
1015 Py_DECREF(ret);
1016 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001017 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001018 PyErr_Print();
1019 } else {
1020 PyErr_Clear();
1021 }
1022 }
1023
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001024 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001025#ifdef HAVE_TRACE_V2
1026 return 0;
1027#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001028}
1029
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001030/*[clinic input]
1031_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001032
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001033 authorizer_callback as authorizer_cb: object
1034
1035Sets authorizer callback. Non-standard.
1036[clinic start generated code]*/
1037
1038static PyObject *
1039pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1040 PyObject *authorizer_cb)
1041/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1042{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001043 int rc;
1044
Gerhard Häringf9cee222010-03-05 15:20:03 +00001045 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1046 return NULL;
1047 }
1048
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001049 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001050 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001051 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001052 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001053 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001054 } else {
1055 Py_INCREF(authorizer_cb);
1056 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001057 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001058 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001059}
1060
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001061/*[clinic input]
1062_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1063
1064 progress_handler: object
1065 n: int
1066
1067Sets progress handler callback. Non-standard.
1068[clinic start generated code]*/
1069
1070static PyObject *
1071pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1072 PyObject *progress_handler,
1073 int n)
1074/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001075{
Gerhard Häringf9cee222010-03-05 15:20:03 +00001076 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1077 return NULL;
1078 }
1079
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001080 if (progress_handler == Py_None) {
1081 /* None clears the progress handler previously set */
1082 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001083 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001084 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001085 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001086 Py_INCREF(progress_handler);
1087 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001088 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001089 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001090}
1091
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001092/*[clinic input]
1093_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1094
1095 trace_callback: object
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001096
1097Sets a trace callback called for each SQL statement (passed as unicode).
1098
1099Non-standard.
1100[clinic start generated code]*/
1101
1102static PyObject *
Dong-hee Na21793492020-12-19 00:41:33 +09001103pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1104 PyObject *trace_callback)
1105/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001106{
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001107 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1108 return NULL;
1109 }
1110
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001111 if (trace_callback == Py_None) {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001112 /*
1113 * None clears the trace callback previously set
1114 *
1115 * Ref.
1116 * - https://sqlite.org/c3ref/c_trace.html
1117 * - https://sqlite.org/c3ref/trace_v2.html
1118 */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001119#ifdef HAVE_TRACE_V2
1120 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1121#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001122 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001123#endif
gescheitb9a03762019-07-13 06:15:49 +03001124 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001125 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001126#ifdef HAVE_TRACE_V2
1127 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1128#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001129 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001130#endif
gescheitb9a03762019-07-13 06:15:49 +03001131 Py_INCREF(trace_callback);
1132 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001133 }
1134
Berker Peksagfe21de92016-04-09 07:34:39 +03001135 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001136}
1137
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001138#ifndef SQLITE_OMIT_LOAD_EXTENSION
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001139/*[clinic input]
1140_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1141
Dong-hee Na21793492020-12-19 00:41:33 +09001142 enable as onoff: bool(accept={int})
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001143 /
1144
1145Enable dynamic loading of SQLite extension modules. Non-standard.
1146[clinic start generated code]*/
1147
1148static PyObject *
1149pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1150 int onoff)
Dong-hee Na21793492020-12-19 00:41:33 +09001151/*[clinic end generated code: output=9cac37190d388baf input=5c0da5b121121cbc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001152{
1153 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001154
1155 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1156 return NULL;
1157 }
1158
Gerhard Häringf9cee222010-03-05 15:20:03 +00001159 rc = sqlite3_enable_load_extension(self->db, onoff);
1160
1161 if (rc != SQLITE_OK) {
1162 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1163 return NULL;
1164 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001165 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001166 }
1167}
1168
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001169/*[clinic input]
1170_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1171
1172 name as extension_name: str
1173 /
1174
1175Load SQLite extension module. Non-standard.
1176[clinic start generated code]*/
1177
1178static PyObject *
1179pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1180 const char *extension_name)
1181/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001182{
1183 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001184 char* errmsg;
1185
1186 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1187 return NULL;
1188 }
1189
Gerhard Häringf9cee222010-03-05 15:20:03 +00001190 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1191 if (rc != 0) {
1192 PyErr_SetString(pysqlite_OperationalError, errmsg);
1193 return NULL;
1194 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001195 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001196 }
1197}
1198#endif
1199
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001200int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001201{
1202 if (self->check_same_thread) {
1203 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001204 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001205 "SQLite objects created in a thread can only be used in that same thread. "
1206 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001207 self->thread_ident, PyThread_get_thread_ident());
1208 return 0;
1209 }
1210
1211 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001212 return 1;
1213}
1214
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001215static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001216{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001217 return Py_NewRef(self->isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001218}
1219
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001220static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001221{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001222 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001223 return NULL;
1224 } else {
1225 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1226 }
1227}
1228
Berker Peksag59da4b32016-09-12 07:16:43 +03001229static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1230{
1231 if (!pysqlite_check_connection(self)) {
1232 return NULL;
1233 }
1234 if (!sqlite3_get_autocommit(self->db)) {
1235 Py_RETURN_TRUE;
1236 }
1237 Py_RETURN_FALSE;
1238}
1239
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001240static int
1241pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001243 if (isolation_level == NULL) {
1244 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1245 return -1;
1246 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001247 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001248 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001249 if (!res) {
1250 return -1;
1251 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 Py_DECREF(res);
1253
Serhiy Storchaka28914922016-09-01 22:18:03 +03001254 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001255 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001256 const char * const *candidate;
1257 PyObject *uppercase_level;
1258 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001259
Serhiy Storchaka28914922016-09-01 22:18:03 +03001260 if (!PyUnicode_Check(isolation_level)) {
1261 PyErr_Format(PyExc_TypeError,
1262 "isolation_level must be a string or None, not %.100s",
1263 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264 return -1;
1265 }
1266
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001267 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001268 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001269 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001270 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001271 return -1;
1272 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001273 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001274 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001275 break;
1276 }
1277 Py_DECREF(uppercase_level);
1278 if (!*candidate) {
1279 PyErr_SetString(PyExc_ValueError,
1280 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001281 return -1;
1282 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001283 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 }
1285
Serhiy Storchaka28914922016-09-01 22:18:03 +03001286 Py_INCREF(isolation_level);
1287 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001288 return 0;
1289}
1290
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001291PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001292{
1293 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001294 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001295 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001296 int rc;
1297
Gerhard Häringf9cee222010-03-05 15:20:03 +00001298 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1299 return NULL;
1300 }
1301
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001302 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001303 return NULL;
1304
Victor Stinnerc6a23202019-06-26 03:16:24 +02001305 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001306 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001307
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001308 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001309
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +02001310 statement = PyObject_New(pysqlite_Statement, pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001311 if (!statement) {
1312 return NULL;
1313 }
1314
Victor Stinner0201f442010-03-13 03:28:34 +00001315 statement->db = NULL;
1316 statement->st = NULL;
1317 statement->sql = NULL;
1318 statement->in_use = 0;
1319 statement->in_weakreflist = NULL;
1320
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001321 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001322 if (rc != SQLITE_OK) {
1323 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001324 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001325 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001326 if (PyErr_ExceptionMatches(PyExc_TypeError))
1327 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001328 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001329 (void)pysqlite_statement_reset(statement);
1330 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001331 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001332 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333 }
1334
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001335 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1336 if (weakref == NULL)
1337 goto error;
1338 if (PyList_Append(self->statements, weakref) != 0) {
1339 Py_DECREF(weakref);
1340 goto error;
1341 }
1342 Py_DECREF(weakref);
1343
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001344 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001345
1346error:
1347 Py_DECREF(statement);
1348 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001349}
1350
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001351/*[clinic input]
1352_sqlite3.Connection.execute as pysqlite_connection_execute
1353
1354 sql: unicode
1355 parameters: object = NULL
1356 /
1357
1358Executes a SQL statement. Non-standard.
1359[clinic start generated code]*/
1360
1361static PyObject *
1362pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
1363 PyObject *parameters)
1364/*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001365{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001366 _Py_IDENTIFIER(execute);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001367 PyObject* cursor = 0;
1368 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001370 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001371 if (!cursor) {
1372 goto error;
1373 }
1374
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001375 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001376 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001377 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001378 }
1379
1380error:
1381 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001382
1383 return cursor;
1384}
1385
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001386/*[clinic input]
1387_sqlite3.Connection.executemany as pysqlite_connection_executemany
1388
1389 sql: unicode
1390 parameters: object
1391 /
1392
1393Repeatedly executes a SQL statement. Non-standard.
1394[clinic start generated code]*/
1395
1396static PyObject *
1397pysqlite_connection_executemany_impl(pysqlite_Connection *self,
1398 PyObject *sql, PyObject *parameters)
1399/*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001400{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001401 _Py_IDENTIFIER(executemany);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001402 PyObject* cursor = 0;
1403 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001404
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001405 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001406 if (!cursor) {
1407 goto error;
1408 }
1409
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001410 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
1411 parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001412 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001413 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001414 }
1415
1416error:
1417 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001418
1419 return cursor;
1420}
1421
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001422/*[clinic input]
1423_sqlite3.Connection.executescript as pysqlite_connection_executescript
1424
1425 sql_script as script_obj: object
1426 /
1427
1428Executes a multiple SQL statements at once. Non-standard.
1429[clinic start generated code]*/
1430
1431static PyObject *
1432pysqlite_connection_executescript(pysqlite_Connection *self,
1433 PyObject *script_obj)
1434/*[clinic end generated code: output=4c4f9d77aa0ae37d input=c0b14695aa6c81d9]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001435{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001436 _Py_IDENTIFIER(executescript);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001437 PyObject* cursor = 0;
1438 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001439
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001440 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001441 if (!cursor) {
1442 goto error;
1443 }
1444
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001445 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
1446 script_obj, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001447 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001448 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001449 }
1450
1451error:
1452 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001453
1454 return cursor;
1455}
1456
1457/* ------------------------- COLLATION CODE ------------------------ */
1458
1459static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001460pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001461 void* context,
1462 int text1_length, const void* text1_data,
1463 int text2_length, const void* text2_data)
1464{
1465 PyObject* callback = (PyObject*)context;
1466 PyObject* string1 = 0;
1467 PyObject* string2 = 0;
1468 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001469 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001470 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001471 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001472 gilstate = PyGILState_Ensure();
1473
1474 if (PyErr_Occurred()) {
1475 goto finally;
1476 }
1477
Guido van Rossum98297ee2007-11-06 21:34:58 +00001478 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1479 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001480
1481 if (!string1 || !string2) {
1482 goto finally; /* failed to allocate strings */
1483 }
1484
1485 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1486
1487 if (!retval) {
1488 /* execution failed */
1489 goto finally;
1490 }
1491
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001492 longval = PyLong_AsLongAndOverflow(retval, &result);
1493 if (longval == -1 && PyErr_Occurred()) {
1494 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001495 result = 0;
1496 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001497 else if (!result) {
1498 if (longval > 0)
1499 result = 1;
1500 else if (longval < 0)
1501 result = -1;
1502 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001503
1504finally:
1505 Py_XDECREF(string1);
1506 Py_XDECREF(string2);
1507 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001508 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001509 return result;
1510}
1511
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001512/*[clinic input]
1513_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1514
1515Abort any pending database operation. Non-standard.
1516[clinic start generated code]*/
1517
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001518static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001519pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1520/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001521{
1522 PyObject* retval = NULL;
1523
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001524 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001525 goto finally;
1526 }
1527
1528 sqlite3_interrupt(self->db);
1529
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001530 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001531
1532finally:
1533 return retval;
1534}
1535
Christian Heimesbbe741d2008-03-28 10:53:29 +00001536/* Function author: Paul Kippes <kippesp@gmail.com>
1537 * Class method of Connection to call the Python function _iterdump
1538 * of the sqlite3 module.
1539 */
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001540/*[clinic input]
1541_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1542
1543Returns iterator to the dump of the database in an SQL text format.
1544
1545Non-standard.
1546[clinic start generated code]*/
1547
Christian Heimesbbe741d2008-03-28 10:53:29 +00001548static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001549pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1550/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001551{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001552 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001553 PyObject* retval = NULL;
1554 PyObject* module = NULL;
1555 PyObject* module_dict;
1556 PyObject* pyfn_iterdump;
1557
1558 if (!pysqlite_check_connection(self)) {
1559 goto finally;
1560 }
1561
1562 module = PyImport_ImportModule(MODULE_NAME ".dump");
1563 if (!module) {
1564 goto finally;
1565 }
1566
1567 module_dict = PyModule_GetDict(module);
1568 if (!module_dict) {
1569 goto finally;
1570 }
1571
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001572 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001573 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001574 if (!PyErr_Occurred()) {
1575 PyErr_SetString(pysqlite_OperationalError,
1576 "Failed to obtain _iterdump() reference");
1577 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001578 goto finally;
1579 }
1580
Petr Viktorinffd97532020-02-11 17:46:57 +01001581 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001582
1583finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001584 Py_XDECREF(module);
1585 return retval;
1586}
1587
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001588/*[clinic input]
1589_sqlite3.Connection.backup as pysqlite_connection_backup
1590
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001591 target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType')
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001592 *
1593 pages: int = -1
1594 progress: object = None
1595 name: str = "main"
1596 sleep: double = 0.250
1597
1598Makes a backup of the database. Non-standard.
1599[clinic start generated code]*/
1600
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001601static PyObject *
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001602pysqlite_connection_backup_impl(pysqlite_Connection *self,
1603 pysqlite_Connection *target, int pages,
1604 PyObject *progress, const char *name,
1605 double sleep)
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001606/*[clinic end generated code: output=306a3e6a38c36334 input=30ae45fc420bfd3b]*/
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001607{
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001608 int rc;
1609 int callback_error = 0;
Pablo Galindoa6d63a22020-12-29 00:28:09 +00001610 int sleep_ms = (int)(sleep * 1000.0);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001611 sqlite3 *bck_conn;
1612 sqlite3_backup *bck_handle;
Victor Stinnerca405012018-04-30 12:22:17 +02001613
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001614 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1615 return NULL;
1616 }
1617
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001618 if (!pysqlite_check_connection(target)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001619 return NULL;
1620 }
1621
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001622 if (target == self) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001623 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1624 return NULL;
1625 }
1626
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001627#if SQLITE_VERSION_NUMBER < 3008008
1628 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001629 https://www.sqlite.org/src/info/169b5505498c0a7e */
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001630 if (!sqlite3_get_autocommit(target->db)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001631 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1632 return NULL;
1633 }
1634#endif
1635
1636 if (progress != Py_None && !PyCallable_Check(progress)) {
1637 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1638 return NULL;
1639 }
1640
1641 if (pages == 0) {
1642 pages = -1;
1643 }
1644
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001645 bck_conn = target->db;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001646
1647 Py_BEGIN_ALLOW_THREADS
1648 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1649 Py_END_ALLOW_THREADS
1650
1651 if (bck_handle) {
1652 do {
1653 Py_BEGIN_ALLOW_THREADS
1654 rc = sqlite3_backup_step(bck_handle, pages);
1655 Py_END_ALLOW_THREADS
1656
1657 if (progress != Py_None) {
1658 PyObject *res;
1659
1660 res = PyObject_CallFunction(progress, "iii", rc,
1661 sqlite3_backup_remaining(bck_handle),
1662 sqlite3_backup_pagecount(bck_handle));
1663 if (res == NULL) {
1664 /* User's callback raised an error: interrupt the loop and
1665 propagate it. */
1666 callback_error = 1;
1667 rc = -1;
1668 } else {
1669 Py_DECREF(res);
1670 }
1671 }
1672
1673 /* Sleep for a while if there are still further pages to copy and
1674 the engine could not make any progress */
1675 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1676 Py_BEGIN_ALLOW_THREADS
Victor Stinnerca405012018-04-30 12:22:17 +02001677 sqlite3_sleep(sleep_ms);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001678 Py_END_ALLOW_THREADS
1679 }
1680 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1681
1682 Py_BEGIN_ALLOW_THREADS
1683 rc = sqlite3_backup_finish(bck_handle);
1684 Py_END_ALLOW_THREADS
1685 } else {
1686 rc = _pysqlite_seterror(bck_conn, NULL);
1687 }
1688
1689 if (!callback_error && rc != SQLITE_OK) {
1690 /* We cannot use _pysqlite_seterror() here because the backup APIs do
1691 not set the error status on the connection object, but rather on
1692 the backup handle. */
1693 if (rc == SQLITE_NOMEM) {
1694 (void)PyErr_NoMemory();
1695 } else {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001696 PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001697 }
1698 }
1699
1700 if (!callback_error && rc == SQLITE_OK) {
1701 Py_RETURN_NONE;
1702 } else {
1703 return NULL;
1704 }
1705}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001706
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001707/*[clinic input]
1708_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
1709
1710 name: unicode
1711 callback as callable: object
1712 /
1713
1714Creates a collation function. Non-standard.
1715[clinic start generated code]*/
1716
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001717static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001718pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1719 PyObject *name, PyObject *callable)
1720/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001721{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001722 PyObject* uppercase_name = 0;
Victor Stinner35466c52010-04-22 11:23:23 +00001723 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001724 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001725 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001726 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001727 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001728 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001729
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001730 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001731 goto finally;
1732 }
1733
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001734 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1735 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001736 if (!uppercase_name) {
1737 goto finally;
1738 }
1739
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001740 if (PyUnicode_READY(uppercase_name))
1741 goto finally;
1742 len = PyUnicode_GET_LENGTH(uppercase_name);
1743 kind = PyUnicode_KIND(uppercase_name);
1744 data = PyUnicode_DATA(uppercase_name);
1745 for (i=0; i<len; i++) {
1746 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1747 if ((ch >= '0' && ch <= '9')
1748 || (ch >= 'A' && ch <= 'Z')
1749 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001750 {
Victor Stinner35466c52010-04-22 11:23:23 +00001751 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001752 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001753 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001754 goto finally;
1755 }
1756 }
1757
Serhiy Storchaka06515832016-11-20 09:13:07 +02001758 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001759 if (!uppercase_name_str)
1760 goto finally;
1761
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762 if (callable != Py_None && !PyCallable_Check(callable)) {
1763 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1764 goto finally;
1765 }
1766
1767 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001768 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1769 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001770 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001771 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1772 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001773 }
1774
1775 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001776 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001777 SQLITE_UTF8,
1778 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001779 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001780 if (rc != SQLITE_OK) {
Serhiy Storchaka90731802021-01-31 17:42:38 +02001781 if (callable != Py_None) {
1782 if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1783 PyErr_Clear();
1784 }
1785 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001786 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001787 goto finally;
1788 }
1789
1790finally:
1791 Py_XDECREF(uppercase_name);
1792
1793 if (PyErr_Occurred()) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001794 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001795 }
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001796 return Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001797}
1798
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001799/*[clinic input]
1800_sqlite3.Connection.__enter__ as pysqlite_connection_enter
1801
1802Called when the connection is used as a context manager.
1803
1804Returns itself as a convenience to the caller.
1805[clinic start generated code]*/
1806
Christian Heimesbbe741d2008-03-28 10:53:29 +00001807static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001808pysqlite_connection_enter_impl(pysqlite_Connection *self)
1809/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001810{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001811 return Py_NewRef((PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001812}
1813
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001814/*[clinic input]
1815_sqlite3.Connection.__exit__ as pysqlite_connection_exit
1816
1817 type as exc_type: object
1818 value as exc_value: object
1819 traceback as exc_tb: object
1820 /
1821
1822Called when the connection is used as a context manager.
1823
1824If there was any exception, a rollback takes place; otherwise we commit.
1825[clinic start generated code]*/
1826
Christian Heimesbbe741d2008-03-28 10:53:29 +00001827static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001828pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
1829 PyObject *exc_value, PyObject *exc_tb)
1830/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001831{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001832 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001833 PyObject* result;
1834
Christian Heimesbbe741d2008-03-28 10:53:29 +00001835 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1836 method_name = "commit";
1837 } else {
1838 method_name = "rollback";
1839 }
1840
Victor Stinner3466bde2016-09-05 18:16:01 -07001841 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001842 if (!result) {
1843 return NULL;
1844 }
1845 Py_DECREF(result);
1846
1847 Py_RETURN_FALSE;
1848}
1849
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001850static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001851PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001852
1853static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001854 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1855 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001856 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001857 {NULL}
1858};
1859
1860static PyMethodDef connection_methods[] = {
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001861 PYSQLITE_CONNECTION_BACKUP_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001862 PYSQLITE_CONNECTION_CLOSE_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001863 PYSQLITE_CONNECTION_COMMIT_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001864 PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
1865 PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
1866 PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
1867 PYSQLITE_CONNECTION_CURSOR_METHODDEF
1868 PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
1869 PYSQLITE_CONNECTION_ENTER_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001870 PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
1871 PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
1872 PYSQLITE_CONNECTION_EXECUTE_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001873 PYSQLITE_CONNECTION_EXIT_METHODDEF
1874 PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
1875 PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
1876 PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
1877 PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
1878 PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
1879 PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
1880 PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001881 {NULL, NULL}
1882};
1883
1884static struct PyMemberDef connection_members[] =
1885{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001886 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1887 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1888 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1889 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1890 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1891 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1892 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1893 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1894 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1895 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001896 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1897 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001898 {NULL}
1899};
1900
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001901static PyType_Slot connection_slots[] = {
1902 {Py_tp_dealloc, pysqlite_connection_dealloc},
1903 {Py_tp_doc, (void *)connection_doc},
1904 {Py_tp_methods, connection_methods},
1905 {Py_tp_members, connection_members},
1906 {Py_tp_getset, connection_getset},
1907 {Py_tp_new, PyType_GenericNew},
1908 {Py_tp_init, pysqlite_connection_init},
1909 {Py_tp_call, pysqlite_connection_call},
1910 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001911};
1912
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001913static PyType_Spec connection_spec = {
1914 .name = MODULE_NAME ".Connection",
1915 .basicsize = sizeof(pysqlite_Connection),
1916 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1917 .slots = connection_slots,
1918};
1919
1920PyTypeObject *pysqlite_ConnectionType = NULL;
1921
1922extern int pysqlite_connection_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001923{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001924 pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
1925 if (pysqlite_ConnectionType == NULL) {
1926 return -1;
1927 }
1928 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001929}