blob: 75aec74e0aaa92074c2c57e8657c1d341ee542e4 [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) {
Aviv Palivoda86a67052017-03-03 12:58:17 +0200236 SQLITE3_CLOSE(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) {
Aviv Palivoda86a67052017-03-03 12:58:17 +0200341 rc = SQLITE3_CLOSE(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
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000410PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000411{
412 int rc;
413 const char* tail;
414 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
Benjamin Peterson52526942017-09-20 07:36:18 -0700423 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, &tail);
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;
463 const char* tail;
464 sqlite3_stmt* statement;
465
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000466 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000467 return NULL;
468 }
469
Berker Peksag59da4b32016-09-12 07:16:43 +0300470 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000471 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000472
473 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700474 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 Py_END_ALLOW_THREADS
476 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000477 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000478 goto error;
479 }
480
Benjamin Petersond7b03282008-09-13 15:58:53 +0000481 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300482 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000483 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000484 }
485
486 Py_BEGIN_ALLOW_THREADS
487 rc = sqlite3_finalize(statement);
488 Py_END_ALLOW_THREADS
489 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000490 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000491 }
492
493 }
494
495error:
496 if (PyErr_Occurred()) {
497 return NULL;
498 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200499 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000500 }
501}
502
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200503static int
504_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200506 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000508 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200509 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
510 if (value == -1 && PyErr_Occurred())
511 return -1;
512 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513 } else if (PyFloat_Check(py_val)) {
514 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000515 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200516 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200517 if (str == NULL)
518 return -1;
519 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000520 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200521 Py_buffer view;
522 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100523 PyErr_SetString(PyExc_ValueError,
524 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200525 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200527 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100528 PyErr_SetString(PyExc_OverflowError,
529 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200530 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100531 return -1;
532 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200533 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
534 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200536 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200538 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539}
540
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000541PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542{
543 PyObject* args;
544 int i;
545 sqlite3_value* cur_value;
546 PyObject* cur_py_value;
547 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548 Py_ssize_t buflen;
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();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000570 Py_INCREF(Py_None);
571 cur_py_value = Py_None;
572 }
573 break;
574 case SQLITE_BLOB:
575 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000576 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000577 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000578 break;
579 case SQLITE_NULL:
580 default:
581 Py_INCREF(Py_None);
582 cur_py_value = Py_None;
583 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000584
585 if (!cur_py_value) {
586 Py_DECREF(args);
587 return NULL;
588 }
589
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590 PyTuple_SetItem(args, i, cur_py_value);
591
592 }
593
594 return args;
595}
596
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000597void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598{
599 PyObject* args;
600 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000601 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200602 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000603
604 PyGILState_STATE threadstate;
605
606 threadstate = PyGILState_Ensure();
607
608 py_func = (PyObject*)sqlite3_user_data(context);
609
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000610 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611 if (args) {
612 py_retval = PyObject_CallObject(py_func, args);
613 Py_DECREF(args);
614 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000615
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200616 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200618 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000619 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200620 }
621 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700622 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000623 PyErr_Print();
624 } else {
625 PyErr_Clear();
626 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200627 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000628 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629
630 PyGILState_Release(threadstate);
631}
632
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000633static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634{
635 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000636 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637 PyObject* aggregate_class;
638 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000639 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000640
641 PyGILState_STATE threadstate;
642
643 threadstate = PyGILState_Ensure();
644
645 aggregate_class = (PyObject*)sqlite3_user_data(context);
646
647 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
648
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200649 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100650 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000653 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700654 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000655 PyErr_Print();
656 } else {
657 PyErr_Clear();
658 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200659 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661 }
662 }
663
664 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000665 if (!stepmethod) {
666 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000667 }
668
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000669 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000670 if (!args) {
671 goto error;
672 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000673
674 function_result = PyObject_CallObject(stepmethod, args);
675 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000676
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700678 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000679 PyErr_Print();
680 } else {
681 PyErr_Clear();
682 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200683 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 }
685
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686error:
687 Py_XDECREF(stepmethod);
688 Py_XDECREF(function_result);
689
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690 PyGILState_Release(threadstate);
691}
692
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000693void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200695 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000696 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200697 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200698 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200699 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000700
701 PyGILState_STATE threadstate;
702
703 threadstate = PyGILState_Ensure();
704
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
706 if (!*aggregate_instance) {
707 /* this branch is executed if there was an exception in the aggregate's
708 * __init__ */
709
Thomas Wouters477c8d52006-05-27 19:21:47 +0000710 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 }
712
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200713 /* Keep the exception (if any) of the last call to step() */
714 PyErr_Fetch(&exception, &value, &tb);
715
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200716 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200717
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200718 Py_DECREF(*aggregate_instance);
719
720 ok = 0;
721 if (function_result) {
722 ok = _pysqlite_set_result(context, function_result) == 0;
723 Py_DECREF(function_result);
724 }
725 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700726 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000727 PyErr_Print();
728 } else {
729 PyErr_Clear();
730 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200731 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000732 }
733
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200734 /* Restore the exception (if any) of the last call to step(),
735 but clear also the current exception if finalize() failed */
736 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200737
Thomas Wouters477c8d52006-05-27 19:21:47 +0000738error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000739 PyGILState_Release(threadstate);
740}
741
Gerhard Häringf9cee222010-03-05 15:20:03 +0000742static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000743{
744 PyObject* new_list;
745 PyObject* weakref;
746 int i;
747
748 /* we only need to do this once in a while */
749 if (self->created_statements++ < 200) {
750 return;
751 }
752
753 self->created_statements = 0;
754
755 new_list = PyList_New(0);
756 if (!new_list) {
757 return;
758 }
759
760 for (i = 0; i < PyList_Size(self->statements); i++) {
761 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000762 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000763 if (PyList_Append(new_list, weakref) != 0) {
764 Py_DECREF(new_list);
765 return;
766 }
767 }
768 }
769
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300770 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000771}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000772
Gerhard Häringf9cee222010-03-05 15:20:03 +0000773static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
774{
775 PyObject* new_list;
776 PyObject* weakref;
777 int i;
778
779 /* we only need to do this once in a while */
780 if (self->created_cursors++ < 200) {
781 return;
782 }
783
784 self->created_cursors = 0;
785
786 new_list = PyList_New(0);
787 if (!new_list) {
788 return;
789 }
790
791 for (i = 0; i < PyList_Size(self->cursors); i++) {
792 weakref = PyList_GetItem(self->cursors, i);
793 if (PyWeakref_GetObject(weakref) != Py_None) {
794 if (PyList_Append(new_list, weakref) != 0) {
795 Py_DECREF(new_list);
796 return;
797 }
798 }
799 }
800
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300801 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000802}
803
gescheitb9a03762019-07-13 06:15:49 +0300804static void _destructor(void* args)
805{
806 Py_DECREF((PyObject*)args);
807}
808
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100809/*[clinic input]
810_sqlite3.Connection.create_function as pysqlite_connection_create_function
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000811
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100812 name: str
813 narg: int
814 func: object
815 *
816 deterministic: bool = False
817
818Creates a new function. Non-standard.
819[clinic start generated code]*/
820
821static PyObject *
822pysqlite_connection_create_function_impl(pysqlite_Connection *self,
823 const char *name, int narg,
824 PyObject *func, int deterministic)
825/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
826{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500828 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000829
Gerhard Häringf9cee222010-03-05 15:20:03 +0000830 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
831 return NULL;
832 }
833
Sergey Fedoseev08308582018-07-08 12:09:20 +0500834 if (deterministic) {
835#if SQLITE_VERSION_NUMBER < 3008003
836 PyErr_SetString(pysqlite_NotSupportedError,
837 "deterministic=True requires SQLite 3.8.3 or higher");
838 return NULL;
839#else
840 if (sqlite3_libversion_number() < 3008003) {
841 PyErr_SetString(pysqlite_NotSupportedError,
842 "deterministic=True requires SQLite 3.8.3 or higher");
843 return NULL;
844 }
845 flags |= SQLITE_DETERMINISTIC;
846#endif
847 }
gescheitb9a03762019-07-13 06:15:49 +0300848 Py_INCREF(func);
849 rc = sqlite3_create_function_v2(self->db,
850 name,
851 narg,
852 flags,
853 (void*)func,
854 _pysqlite_func_callback,
855 NULL,
856 NULL,
857 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859 if (rc != SQLITE_OK) {
860 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000861 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500864 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865}
866
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100867/*[clinic input]
868_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000869
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100870 name: str
871 n_arg: int
872 aggregate_class: object
873
874Creates a new aggregate. Non-standard.
875[clinic start generated code]*/
876
877static PyObject *
878pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
879 const char *name, int n_arg,
880 PyObject *aggregate_class)
881/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
882{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883 int rc;
884
Gerhard Häringf9cee222010-03-05 15:20:03 +0000885 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
886 return NULL;
887 }
888
gescheitb9a03762019-07-13 06:15:49 +0300889 Py_INCREF(aggregate_class);
890 rc = sqlite3_create_function_v2(self->db,
891 name,
892 n_arg,
893 SQLITE_UTF8,
894 (void*)aggregate_class,
895 0,
896 &_pysqlite_step_callback,
897 &_pysqlite_final_callback,
898 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000899 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000901 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000902 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000903 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500904 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905}
906
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000907static 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 +0000908{
909 PyObject *ret;
910 int rc;
911 PyGILState_STATE gilstate;
912
913 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000914
Victor Stinnerd4095d92013-07-26 22:23:33 +0200915 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000916
Victor Stinnerd4095d92013-07-26 22:23:33 +0200917 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700918 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200919 PyErr_Print();
920 else
921 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200922
Victor Stinnerd4095d92013-07-26 22:23:33 +0200923 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200924 }
925 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200926 if (PyLong_Check(ret)) {
927 rc = _PyLong_AsInt(ret);
928 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700929 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200930 PyErr_Print();
931 else
932 PyErr_Clear();
933 rc = SQLITE_DENY;
934 }
935 }
936 else {
937 rc = SQLITE_DENY;
938 }
939 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000940 }
941
942 PyGILState_Release(gilstate);
943 return rc;
944}
945
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000946static int _progress_handler(void* user_arg)
947{
948 int rc;
949 PyObject *ret;
950 PyGILState_STATE gilstate;
951
952 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100953 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000954
955 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700956 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000957 PyErr_Print();
958 } else {
959 PyErr_Clear();
960 }
961
Mark Dickinson934896d2009-02-21 20:59:32 +0000962 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000963 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000964 } else {
965 rc = (int)PyObject_IsTrue(ret);
966 Py_DECREF(ret);
967 }
968
969 PyGILState_Release(gilstate);
970 return rc;
971}
972
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200973#ifdef HAVE_TRACE_V2
974/*
975 * From https://sqlite.org/c3ref/trace_v2.html:
976 * The integer return value from the callback is currently ignored, though this
977 * may change in future releases. Callback implementations should return zero
978 * to ensure future compatibility.
979 */
980static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
981#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200982static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200983#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200984{
985 PyObject *py_statement = NULL;
986 PyObject *ret = NULL;
987
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200988 PyGILState_STATE gilstate;
989
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200990#ifdef HAVE_TRACE_V2
991 if (type != SQLITE_TRACE_STMT) {
992 return 0;
993 }
994#endif
995
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200996 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200997 py_statement = PyUnicode_DecodeUTF8(statement_string,
998 strlen(statement_string), "replace");
999 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001000 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001001 Py_DECREF(py_statement);
1002 }
1003
1004 if (ret) {
1005 Py_DECREF(ret);
1006 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001007 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001008 PyErr_Print();
1009 } else {
1010 PyErr_Clear();
1011 }
1012 }
1013
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001014 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001015#ifdef HAVE_TRACE_V2
1016 return 0;
1017#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001018}
1019
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001020/*[clinic input]
1021_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001022
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001023 authorizer_callback as authorizer_cb: object
1024
1025Sets authorizer callback. Non-standard.
1026[clinic start generated code]*/
1027
1028static PyObject *
1029pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1030 PyObject *authorizer_cb)
1031/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1032{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001033 int rc;
1034
Gerhard Häringf9cee222010-03-05 15:20:03 +00001035 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1036 return NULL;
1037 }
1038
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001039 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001040 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001041 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001042 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001043 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001044 } else {
1045 Py_INCREF(authorizer_cb);
1046 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001047 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001048 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001049}
1050
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001051/*[clinic input]
1052_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1053
1054 progress_handler: object
1055 n: int
1056
1057Sets progress handler callback. Non-standard.
1058[clinic start generated code]*/
1059
1060static PyObject *
1061pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1062 PyObject *progress_handler,
1063 int n)
1064/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001065{
Gerhard Häringf9cee222010-03-05 15:20:03 +00001066 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1067 return NULL;
1068 }
1069
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001070 if (progress_handler == Py_None) {
1071 /* None clears the progress handler previously set */
1072 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001073 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001074 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001075 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001076 Py_INCREF(progress_handler);
1077 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001078 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001079 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001080}
1081
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001082/*[clinic input]
1083_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1084
1085 trace_callback: object
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001086
1087Sets a trace callback called for each SQL statement (passed as unicode).
1088
1089Non-standard.
1090[clinic start generated code]*/
1091
1092static PyObject *
Dong-hee Na21793492020-12-19 00:41:33 +09001093pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1094 PyObject *trace_callback)
1095/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001096{
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001097 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1098 return NULL;
1099 }
1100
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001101 if (trace_callback == Py_None) {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001102 /*
1103 * None clears the trace callback previously set
1104 *
1105 * Ref.
1106 * - https://sqlite.org/c3ref/c_trace.html
1107 * - https://sqlite.org/c3ref/trace_v2.html
1108 */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001109#ifdef HAVE_TRACE_V2
1110 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1111#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001112 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001113#endif
gescheitb9a03762019-07-13 06:15:49 +03001114 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001115 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001116#ifdef HAVE_TRACE_V2
1117 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1118#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001119 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001120#endif
gescheitb9a03762019-07-13 06:15:49 +03001121 Py_INCREF(trace_callback);
1122 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001123 }
1124
Berker Peksagfe21de92016-04-09 07:34:39 +03001125 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001126}
1127
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001128#ifndef SQLITE_OMIT_LOAD_EXTENSION
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001129/*[clinic input]
1130_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1131
Dong-hee Na21793492020-12-19 00:41:33 +09001132 enable as onoff: bool(accept={int})
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001133 /
1134
1135Enable dynamic loading of SQLite extension modules. Non-standard.
1136[clinic start generated code]*/
1137
1138static PyObject *
1139pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1140 int onoff)
Dong-hee Na21793492020-12-19 00:41:33 +09001141/*[clinic end generated code: output=9cac37190d388baf input=5c0da5b121121cbc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001142{
1143 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001144
1145 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1146 return NULL;
1147 }
1148
Gerhard Häringf9cee222010-03-05 15:20:03 +00001149 rc = sqlite3_enable_load_extension(self->db, onoff);
1150
1151 if (rc != SQLITE_OK) {
1152 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1153 return NULL;
1154 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001155 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001156 }
1157}
1158
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001159/*[clinic input]
1160_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1161
1162 name as extension_name: str
1163 /
1164
1165Load SQLite extension module. Non-standard.
1166[clinic start generated code]*/
1167
1168static PyObject *
1169pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1170 const char *extension_name)
1171/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001172{
1173 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001174 char* errmsg;
1175
1176 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1177 return NULL;
1178 }
1179
Gerhard Häringf9cee222010-03-05 15:20:03 +00001180 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1181 if (rc != 0) {
1182 PyErr_SetString(pysqlite_OperationalError, errmsg);
1183 return NULL;
1184 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001185 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001186 }
1187}
1188#endif
1189
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001190int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001191{
1192 if (self->check_same_thread) {
1193 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001194 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001195 "SQLite objects created in a thread can only be used in that same thread. "
1196 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197 self->thread_ident, PyThread_get_thread_ident());
1198 return 0;
1199 }
1200
1201 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001202 return 1;
1203}
1204
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001205static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001206{
1207 Py_INCREF(self->isolation_level);
1208 return self->isolation_level;
1209}
1210
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001211static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001212{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001213 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001214 return NULL;
1215 } else {
1216 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1217 }
1218}
1219
Berker Peksag59da4b32016-09-12 07:16:43 +03001220static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1221{
1222 if (!pysqlite_check_connection(self)) {
1223 return NULL;
1224 }
1225 if (!sqlite3_get_autocommit(self->db)) {
1226 Py_RETURN_TRUE;
1227 }
1228 Py_RETURN_FALSE;
1229}
1230
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001231static int
1232pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001233{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001234 if (isolation_level == NULL) {
1235 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1236 return -1;
1237 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001238 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001239 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001240 if (!res) {
1241 return -1;
1242 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001243 Py_DECREF(res);
1244
Serhiy Storchaka28914922016-09-01 22:18:03 +03001245 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001246 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001247 const char * const *candidate;
1248 PyObject *uppercase_level;
1249 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001250
Serhiy Storchaka28914922016-09-01 22:18:03 +03001251 if (!PyUnicode_Check(isolation_level)) {
1252 PyErr_Format(PyExc_TypeError,
1253 "isolation_level must be a string or None, not %.100s",
1254 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001255 return -1;
1256 }
1257
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001258 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001259 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001260 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001261 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001262 return -1;
1263 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001264 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001265 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001266 break;
1267 }
1268 Py_DECREF(uppercase_level);
1269 if (!*candidate) {
1270 PyErr_SetString(PyExc_ValueError,
1271 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001272 return -1;
1273 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001274 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275 }
1276
Serhiy Storchaka28914922016-09-01 22:18:03 +03001277 Py_INCREF(isolation_level);
1278 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001279 return 0;
1280}
1281
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001282PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001283{
1284 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001285 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001286 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287 int rc;
1288
Gerhard Häringf9cee222010-03-05 15:20:03 +00001289 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1290 return NULL;
1291 }
1292
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001293 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001294 return NULL;
1295
Victor Stinnerc6a23202019-06-26 03:16:24 +02001296 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001297 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001298
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001299 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001300
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +02001301 statement = PyObject_New(pysqlite_Statement, pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001302 if (!statement) {
1303 return NULL;
1304 }
1305
Victor Stinner0201f442010-03-13 03:28:34 +00001306 statement->db = NULL;
1307 statement->st = NULL;
1308 statement->sql = NULL;
1309 statement->in_use = 0;
1310 statement->in_weakreflist = NULL;
1311
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001312 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001313 if (rc != SQLITE_OK) {
1314 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001315 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001316 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001317 if (PyErr_ExceptionMatches(PyExc_TypeError))
1318 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001319 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001320 (void)pysqlite_statement_reset(statement);
1321 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001322 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001323 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001324 }
1325
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001326 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1327 if (weakref == NULL)
1328 goto error;
1329 if (PyList_Append(self->statements, weakref) != 0) {
1330 Py_DECREF(weakref);
1331 goto error;
1332 }
1333 Py_DECREF(weakref);
1334
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001335 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001336
1337error:
1338 Py_DECREF(statement);
1339 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001340}
1341
Larry Hastings01b08832015-05-08 07:37:49 -07001342PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343{
1344 PyObject* cursor = 0;
1345 PyObject* result = 0;
1346 PyObject* method = 0;
1347
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001348 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001349 if (!cursor) {
1350 goto error;
1351 }
1352
1353 method = PyObject_GetAttrString(cursor, "execute");
1354 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001355 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001356 goto error;
1357 }
1358
1359 result = PyObject_CallObject(method, args);
1360 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001361 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001362 }
1363
1364error:
1365 Py_XDECREF(result);
1366 Py_XDECREF(method);
1367
1368 return cursor;
1369}
1370
Larry Hastings01b08832015-05-08 07:37:49 -07001371PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001372{
1373 PyObject* cursor = 0;
1374 PyObject* result = 0;
1375 PyObject* method = 0;
1376
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001377 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001378 if (!cursor) {
1379 goto error;
1380 }
1381
1382 method = PyObject_GetAttrString(cursor, "executemany");
1383 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001384 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001385 goto error;
1386 }
1387
1388 result = PyObject_CallObject(method, args);
1389 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001390 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001391 }
1392
1393error:
1394 Py_XDECREF(result);
1395 Py_XDECREF(method);
1396
1397 return cursor;
1398}
1399
Larry Hastings01b08832015-05-08 07:37:49 -07001400PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001401{
1402 PyObject* cursor = 0;
1403 PyObject* result = 0;
1404 PyObject* method = 0;
1405
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001406 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001407 if (!cursor) {
1408 goto error;
1409 }
1410
1411 method = PyObject_GetAttrString(cursor, "executescript");
1412 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001413 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001414 goto error;
1415 }
1416
1417 result = PyObject_CallObject(method, args);
1418 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001419 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001420 }
1421
1422error:
1423 Py_XDECREF(result);
1424 Py_XDECREF(method);
1425
1426 return cursor;
1427}
1428
1429/* ------------------------- COLLATION CODE ------------------------ */
1430
1431static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001432pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001433 void* context,
1434 int text1_length, const void* text1_data,
1435 int text2_length, const void* text2_data)
1436{
1437 PyObject* callback = (PyObject*)context;
1438 PyObject* string1 = 0;
1439 PyObject* string2 = 0;
1440 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001441 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001442 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001443 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001444 gilstate = PyGILState_Ensure();
1445
1446 if (PyErr_Occurred()) {
1447 goto finally;
1448 }
1449
Guido van Rossum98297ee2007-11-06 21:34:58 +00001450 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1451 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001452
1453 if (!string1 || !string2) {
1454 goto finally; /* failed to allocate strings */
1455 }
1456
1457 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1458
1459 if (!retval) {
1460 /* execution failed */
1461 goto finally;
1462 }
1463
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001464 longval = PyLong_AsLongAndOverflow(retval, &result);
1465 if (longval == -1 && PyErr_Occurred()) {
1466 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001467 result = 0;
1468 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001469 else if (!result) {
1470 if (longval > 0)
1471 result = 1;
1472 else if (longval < 0)
1473 result = -1;
1474 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001475
1476finally:
1477 Py_XDECREF(string1);
1478 Py_XDECREF(string2);
1479 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001480 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001481 return result;
1482}
1483
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001484/*[clinic input]
1485_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1486
1487Abort any pending database operation. Non-standard.
1488[clinic start generated code]*/
1489
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001491pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1492/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001493{
1494 PyObject* retval = NULL;
1495
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001496 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001497 goto finally;
1498 }
1499
1500 sqlite3_interrupt(self->db);
1501
1502 Py_INCREF(Py_None);
1503 retval = Py_None;
1504
1505finally:
1506 return retval;
1507}
1508
Christian Heimesbbe741d2008-03-28 10:53:29 +00001509/* Function author: Paul Kippes <kippesp@gmail.com>
1510 * Class method of Connection to call the Python function _iterdump
1511 * of the sqlite3 module.
1512 */
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001513/*[clinic input]
1514_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1515
1516Returns iterator to the dump of the database in an SQL text format.
1517
1518Non-standard.
1519[clinic start generated code]*/
1520
Christian Heimesbbe741d2008-03-28 10:53:29 +00001521static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001522pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1523/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001524{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001525 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001526 PyObject* retval = NULL;
1527 PyObject* module = NULL;
1528 PyObject* module_dict;
1529 PyObject* pyfn_iterdump;
1530
1531 if (!pysqlite_check_connection(self)) {
1532 goto finally;
1533 }
1534
1535 module = PyImport_ImportModule(MODULE_NAME ".dump");
1536 if (!module) {
1537 goto finally;
1538 }
1539
1540 module_dict = PyModule_GetDict(module);
1541 if (!module_dict) {
1542 goto finally;
1543 }
1544
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001545 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001546 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001547 if (!PyErr_Occurred()) {
1548 PyErr_SetString(pysqlite_OperationalError,
1549 "Failed to obtain _iterdump() reference");
1550 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001551 goto finally;
1552 }
1553
Petr Viktorinffd97532020-02-11 17:46:57 +01001554 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001555
1556finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001557 Py_XDECREF(module);
1558 return retval;
1559}
1560
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001561static PyObject *
1562pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *kwds)
1563{
1564 PyObject *target = NULL;
1565 int pages = -1;
1566 PyObject *progress = Py_None;
1567 const char *name = "main";
1568 int rc;
1569 int callback_error = 0;
Victor Stinnerca405012018-04-30 12:22:17 +02001570 PyObject *sleep_obj = NULL;
1571 int sleep_ms = 250;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001572 sqlite3 *bck_conn;
1573 sqlite3_backup *bck_handle;
1574 static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL};
1575
Victor Stinnerca405012018-04-30 12:22:17 +02001576 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsO:backup", keywords,
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001577 pysqlite_ConnectionType, &target,
Victor Stinnerca405012018-04-30 12:22:17 +02001578 &pages, &progress, &name, &sleep_obj)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001579 return NULL;
1580 }
1581
Victor Stinnerca405012018-04-30 12:22:17 +02001582 if (sleep_obj != NULL) {
1583 _PyTime_t sleep_secs;
1584 if (_PyTime_FromSecondsObject(&sleep_secs, sleep_obj,
1585 _PyTime_ROUND_TIMEOUT)) {
1586 return NULL;
1587 }
1588 _PyTime_t ms = _PyTime_AsMilliseconds(sleep_secs,
1589 _PyTime_ROUND_TIMEOUT);
1590 if (ms < INT_MIN || ms > INT_MAX) {
1591 PyErr_SetString(PyExc_OverflowError, "sleep is too large");
1592 return NULL;
1593 }
1594 sleep_ms = (int)ms;
1595 }
1596
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001597 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1598 return NULL;
1599 }
1600
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001601 if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
1602 return NULL;
1603 }
1604
1605 if ((pysqlite_Connection *)target == self) {
1606 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1607 return NULL;
1608 }
1609
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001610#if SQLITE_VERSION_NUMBER < 3008008
1611 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001612 https://www.sqlite.org/src/info/169b5505498c0a7e */
1613 if (!sqlite3_get_autocommit(((pysqlite_Connection *)target)->db)) {
1614 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1615 return NULL;
1616 }
1617#endif
1618
1619 if (progress != Py_None && !PyCallable_Check(progress)) {
1620 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1621 return NULL;
1622 }
1623
1624 if (pages == 0) {
1625 pages = -1;
1626 }
1627
1628 bck_conn = ((pysqlite_Connection *)target)->db;
1629
1630 Py_BEGIN_ALLOW_THREADS
1631 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1632 Py_END_ALLOW_THREADS
1633
1634 if (bck_handle) {
1635 do {
1636 Py_BEGIN_ALLOW_THREADS
1637 rc = sqlite3_backup_step(bck_handle, pages);
1638 Py_END_ALLOW_THREADS
1639
1640 if (progress != Py_None) {
1641 PyObject *res;
1642
1643 res = PyObject_CallFunction(progress, "iii", rc,
1644 sqlite3_backup_remaining(bck_handle),
1645 sqlite3_backup_pagecount(bck_handle));
1646 if (res == NULL) {
1647 /* User's callback raised an error: interrupt the loop and
1648 propagate it. */
1649 callback_error = 1;
1650 rc = -1;
1651 } else {
1652 Py_DECREF(res);
1653 }
1654 }
1655
1656 /* Sleep for a while if there are still further pages to copy and
1657 the engine could not make any progress */
1658 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1659 Py_BEGIN_ALLOW_THREADS
Victor Stinnerca405012018-04-30 12:22:17 +02001660 sqlite3_sleep(sleep_ms);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001661 Py_END_ALLOW_THREADS
1662 }
1663 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1664
1665 Py_BEGIN_ALLOW_THREADS
1666 rc = sqlite3_backup_finish(bck_handle);
1667 Py_END_ALLOW_THREADS
1668 } else {
1669 rc = _pysqlite_seterror(bck_conn, NULL);
1670 }
1671
1672 if (!callback_error && rc != SQLITE_OK) {
1673 /* We cannot use _pysqlite_seterror() here because the backup APIs do
1674 not set the error status on the connection object, but rather on
1675 the backup handle. */
1676 if (rc == SQLITE_NOMEM) {
1677 (void)PyErr_NoMemory();
1678 } else {
1679#if SQLITE_VERSION_NUMBER > 3007015
1680 PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
1681#else
1682 switch (rc) {
Berker Peksagb10a64d2018-09-20 14:14:33 +03001683 case SQLITE_ERROR:
1684 /* Description of SQLITE_ERROR in SQLite 3.7.14 and older
1685 releases. */
1686 PyErr_SetString(pysqlite_OperationalError,
1687 "SQL logic error or missing database");
1688 break;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001689 case SQLITE_READONLY:
1690 PyErr_SetString(pysqlite_OperationalError,
1691 "attempt to write a readonly database");
1692 break;
1693 case SQLITE_BUSY:
1694 PyErr_SetString(pysqlite_OperationalError, "database is locked");
1695 break;
1696 case SQLITE_LOCKED:
1697 PyErr_SetString(pysqlite_OperationalError,
1698 "database table is locked");
1699 break;
1700 default:
1701 PyErr_Format(pysqlite_OperationalError,
1702 "unrecognized error code: %d", rc);
1703 break;
1704 }
1705#endif
1706 }
1707 }
1708
1709 if (!callback_error && rc == SQLITE_OK) {
1710 Py_RETURN_NONE;
1711 } else {
1712 return NULL;
1713 }
1714}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001715
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001716/*[clinic input]
1717_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
1718
1719 name: unicode
1720 callback as callable: object
1721 /
1722
1723Creates a collation function. Non-standard.
1724[clinic start generated code]*/
1725
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001726static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001727pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1728 PyObject *name, PyObject *callable)
1729/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001730{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001731 PyObject* uppercase_name = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001732 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001733 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001734 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001735 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001736 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001737 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001738 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001739
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001740 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001741 goto finally;
1742 }
1743
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001744 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1745 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001746 if (!uppercase_name) {
1747 goto finally;
1748 }
1749
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001750 if (PyUnicode_READY(uppercase_name))
1751 goto finally;
1752 len = PyUnicode_GET_LENGTH(uppercase_name);
1753 kind = PyUnicode_KIND(uppercase_name);
1754 data = PyUnicode_DATA(uppercase_name);
1755 for (i=0; i<len; i++) {
1756 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1757 if ((ch >= '0' && ch <= '9')
1758 || (ch >= 'A' && ch <= 'Z')
1759 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001760 {
Victor Stinner35466c52010-04-22 11:23:23 +00001761 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001763 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001764 goto finally;
1765 }
1766 }
1767
Serhiy Storchaka06515832016-11-20 09:13:07 +02001768 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001769 if (!uppercase_name_str)
1770 goto finally;
1771
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001772 if (callable != Py_None && !PyCallable_Check(callable)) {
1773 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1774 goto finally;
1775 }
1776
1777 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001778 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1779 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001780 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001781 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1782 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001783 }
1784
1785 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001786 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001787 SQLITE_UTF8,
1788 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001789 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001790 if (rc != SQLITE_OK) {
1791 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001792 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001793 goto finally;
1794 }
1795
1796finally:
1797 Py_XDECREF(uppercase_name);
1798
1799 if (PyErr_Occurred()) {
1800 retval = NULL;
1801 } else {
1802 Py_INCREF(Py_None);
1803 retval = Py_None;
1804 }
1805
1806 return retval;
1807}
1808
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001809/*[clinic input]
1810_sqlite3.Connection.__enter__ as pysqlite_connection_enter
1811
1812Called when the connection is used as a context manager.
1813
1814Returns itself as a convenience to the caller.
1815[clinic start generated code]*/
1816
Christian Heimesbbe741d2008-03-28 10:53:29 +00001817static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001818pysqlite_connection_enter_impl(pysqlite_Connection *self)
1819/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001820{
1821 Py_INCREF(self);
1822 return (PyObject*)self;
1823}
1824
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001825/*[clinic input]
1826_sqlite3.Connection.__exit__ as pysqlite_connection_exit
1827
1828 type as exc_type: object
1829 value as exc_value: object
1830 traceback as exc_tb: object
1831 /
1832
1833Called when the connection is used as a context manager.
1834
1835If there was any exception, a rollback takes place; otherwise we commit.
1836[clinic start generated code]*/
1837
Christian Heimesbbe741d2008-03-28 10:53:29 +00001838static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001839pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
1840 PyObject *exc_value, PyObject *exc_tb)
1841/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001842{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001843 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001844 PyObject* result;
1845
Christian Heimesbbe741d2008-03-28 10:53:29 +00001846 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1847 method_name = "commit";
1848 } else {
1849 method_name = "rollback";
1850 }
1851
Victor Stinner3466bde2016-09-05 18:16:01 -07001852 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001853 if (!result) {
1854 return NULL;
1855 }
1856 Py_DECREF(result);
1857
1858 Py_RETURN_FALSE;
1859}
1860
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001861static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001862PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001863
1864static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001865 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1866 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001867 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001868 {NULL}
1869};
1870
1871static PyMethodDef connection_methods[] = {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001872 PYSQLITE_CONNECTION_CLOSE_METHODDEF
1873 PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
1874 PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
1875 PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
1876 PYSQLITE_CONNECTION_CURSOR_METHODDEF
1877 PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
1878 PYSQLITE_CONNECTION_ENTER_METHODDEF
1879 PYSQLITE_CONNECTION_EXIT_METHODDEF
1880 PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
1881 PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
1882 PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
1883 PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
1884 PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
1885 PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
1886 PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001887 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001888 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001889 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001890 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001891 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001892 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001893 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001894 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001895 {"backup", (PyCFunction)(void(*)(void))pysqlite_connection_backup, METH_VARARGS | METH_KEYWORDS,
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001896 PyDoc_STR("Makes a backup of the database. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001897 {NULL, NULL}
1898};
1899
1900static struct PyMemberDef connection_members[] =
1901{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001902 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1903 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1904 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1905 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1906 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1907 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1908 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1909 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1910 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1911 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001912 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1913 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001914 {NULL}
1915};
1916
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001917static PyType_Slot connection_slots[] = {
1918 {Py_tp_dealloc, pysqlite_connection_dealloc},
1919 {Py_tp_doc, (void *)connection_doc},
1920 {Py_tp_methods, connection_methods},
1921 {Py_tp_members, connection_members},
1922 {Py_tp_getset, connection_getset},
1923 {Py_tp_new, PyType_GenericNew},
1924 {Py_tp_init, pysqlite_connection_init},
1925 {Py_tp_call, pysqlite_connection_call},
1926 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001927};
1928
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001929static PyType_Spec connection_spec = {
1930 .name = MODULE_NAME ".Connection",
1931 .basicsize = sizeof(pysqlite_Connection),
1932 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1933 .slots = connection_slots,
1934};
1935
1936PyTypeObject *pysqlite_ConnectionType = NULL;
1937
1938extern int pysqlite_connection_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001939{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001940 pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
1941 if (pysqlite_ConnectionType == NULL) {
1942 return -1;
1943 }
1944 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001945}