blob: 958be7d869794afab9664930dd8b329576a305a2 [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
Gerhard Häringf9cee222010-03-05 15:20:03 +000036#if SQLITE_VERSION_NUMBER >= 3003008
37#ifndef SQLITE_OMIT_LOAD_EXTENSION
38#define HAVE_LOAD_EXTENSION
39#endif
40#endif
41
Emanuele Gaifasd7aed412018-03-10 23:08:31 +010042#if SQLITE_VERSION_NUMBER >= 3006011
43#define HAVE_BACKUP_API
44#endif
45
Martin v. Löwise75fc142013-11-07 18:46:53 +010046_Py_IDENTIFIER(cursor);
47
Serhiy Storchaka28914922016-09-01 22:18:03 +030048static const char * const begin_statements[] = {
49 "BEGIN ",
50 "BEGIN DEFERRED",
51 "BEGIN IMMEDIATE",
52 "BEGIN EXCLUSIVE",
53 NULL
54};
55
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +020056static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored));
Gerhard Häringf9cee222010-03-05 15:20:03 +000057static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059
Benjamin Petersond7b03282008-09-13 15:58:53 +000060static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061{
62 /* in older SQLite versions, calling sqlite3_result_error in callbacks
63 * triggers a bug in SQLite that leads either to irritating results or
64 * segfaults, depending on the SQLite version */
65#if SQLITE_VERSION_NUMBER >= 3003003
66 sqlite3_result_error(ctx, errmsg, len);
67#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000068 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif
70}
71
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000072int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010074 static char *kwlist[] = {
75 "database", "timeout", "detect_types", "isolation_level",
76 "check_same_thread", "factory", "cached_statements", "uri",
77 NULL
78 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000079
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +030080 const char* database;
Anders Lorentsena22a1272017-11-07 01:47:43 +010081 PyObject* database_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 int detect_types = 0;
83 PyObject* isolation_level = NULL;
84 PyObject* factory = NULL;
85 int check_same_thread = 1;
86 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010087 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088 double timeout = 5.0;
89 int rc;
90
Anders Lorentsena22a1272017-11-07 01:47:43 +010091 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
92 PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010093 &isolation_level, &check_same_thread,
94 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000095 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000096 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097 }
98
Anders Lorentsena22a1272017-11-07 01:47:43 +010099 database = PyBytes_AsString(database_obj);
100
Gerhard Häringf9cee222010-03-05 15:20:03 +0000101 self->initialized = 1;
102
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000103 self->begin_statement = NULL;
104
Oren Milman93c5a5d2017-10-10 22:27:46 +0300105 Py_CLEAR(self->statement_cache);
106 Py_CLEAR(self->statements);
107 Py_CLEAR(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108
109 Py_INCREF(Py_None);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300110 Py_XSETREF(self->row_factory, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111
112 Py_INCREF(&PyUnicode_Type);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300113 Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100115#ifdef SQLITE_OPEN_URI
116 Py_BEGIN_ALLOW_THREADS
117 rc = sqlite3_open_v2(database, &self->db,
118 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
119 (uri ? SQLITE_OPEN_URI : 0), NULL);
120#else
121 if (uri) {
122 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
123 return -1;
124 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200126 /* No need to use sqlite3_open_v2 as sqlite3_open(filename, db) is the
127 same as sqlite3_open_v2(filename, db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL). */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100129#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 Py_END_ALLOW_THREADS
131
Anders Lorentsena22a1272017-11-07 01:47:43 +0100132 Py_DECREF(database_obj);
133
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000135 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136 return -1;
137 }
138
139 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000140 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000141 if (!isolation_level) {
142 return -1;
143 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000144 } else {
145 Py_INCREF(isolation_level);
146 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300147 Py_CLEAR(self->isolation_level);
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200148 if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100149 Py_DECREF(isolation_level);
150 return -1;
151 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 Py_DECREF(isolation_level);
153
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000154 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000155 if (PyErr_Occurred()) {
156 return -1;
157 }
158
Gerhard Häringf9cee222010-03-05 15:20:03 +0000159 self->created_statements = 0;
160 self->created_cursors = 0;
161
162 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000163 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000164 self->cursors = PyList_New(0);
165 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000166 return -1;
167 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000168
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169 /* By default, the Cache class INCREFs the factory in its initializer, and
170 * decrefs it in its deallocator method. Since this would create a circular
171 * reference here, we're breaking it by decrementing self, and telling the
172 * cache class to not decref the factory (self) in its deallocator.
173 */
174 self->statement_cache->decref_factory = 0;
175 Py_DECREF(self);
176
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000177 self->detect_types = detect_types;
178 self->timeout = timeout;
179 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000180 self->thread_ident = PyThread_get_thread_ident();
Berker Peksag7bea2342016-06-12 14:09:51 +0300181 if (!check_same_thread && sqlite3_libversion_number() < 3003001) {
182 PyErr_SetString(pysqlite_NotSupportedError, "shared connections not available");
183 return -1;
184 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000185 self->check_same_thread = check_same_thread;
186
gescheitb9a03762019-07-13 06:15:49 +0300187 self->function_pinboard_trace_callback = NULL;
188 self->function_pinboard_progress_handler = NULL;
189 self->function_pinboard_authorizer_cb = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190
Oren Milman93c5a5d2017-10-10 22:27:46 +0300191 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000192 if (!self->collations) {
193 return -1;
194 }
195
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000196 self->Warning = pysqlite_Warning;
197 self->Error = pysqlite_Error;
198 self->InterfaceError = pysqlite_InterfaceError;
199 self->DatabaseError = pysqlite_DatabaseError;
200 self->DataError = pysqlite_DataError;
201 self->OperationalError = pysqlite_OperationalError;
202 self->IntegrityError = pysqlite_IntegrityError;
203 self->InternalError = pysqlite_InternalError;
204 self->ProgrammingError = pysqlite_ProgrammingError;
205 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206
207 return 0;
208}
209
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000210/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000211void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000213 int i;
214 PyObject* weakref;
215 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000216 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000217
Thomas Wouters477c8d52006-05-27 19:21:47 +0000218 for (i = 0; i < PyList_Size(self->statements); i++) {
219 weakref = PyList_GetItem(self->statements, i);
220 statement = PyWeakref_GetObject(weakref);
221 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500222 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000223 if (action == ACTION_RESET) {
224 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
225 } else {
226 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
227 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500228 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000229 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000231
232 if (reset_cursors) {
233 for (i = 0; i < PyList_Size(self->cursors); i++) {
234 weakref = PyList_GetItem(self->cursors, i);
235 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
236 if ((PyObject*)cursor != Py_None) {
237 cursor->reset = 1;
238 }
239 }
240 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241}
242
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000243void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244{
245 Py_XDECREF(self->statement_cache);
246
247 /* Clean up if user has not called .close() explicitly. */
248 if (self->db) {
Aviv Palivoda86a67052017-03-03 12:58:17 +0200249 SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250 }
251
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252 Py_XDECREF(self->isolation_level);
gescheitb9a03762019-07-13 06:15:49 +0300253 Py_XDECREF(self->function_pinboard_trace_callback);
254 Py_XDECREF(self->function_pinboard_progress_handler);
255 Py_XDECREF(self->function_pinboard_authorizer_cb);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256 Py_XDECREF(self->row_factory);
257 Py_XDECREF(self->text_factory);
258 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000259 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000260 Py_XDECREF(self->cursors);
Christian Heimes90aa7642007-12-19 02:45:37 +0000261 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000262}
263
Gerhard Häringf9cee222010-03-05 15:20:03 +0000264/*
265 * Registers a cursor with the connection.
266 *
267 * 0 => error; 1 => ok
268 */
269int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
270{
271 PyObject* weakref;
272
273 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
274 if (!weakref) {
275 goto error;
276 }
277
278 if (PyList_Append(connection->cursors, weakref) != 0) {
279 Py_CLEAR(weakref);
280 goto error;
281 }
282
283 Py_DECREF(weakref);
284
285 return 1;
286error:
287 return 0;
288}
289
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000290PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300292 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293 PyObject* factory = NULL;
294 PyObject* cursor;
295
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
297 &factory)) {
298 return NULL;
299 }
300
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000301 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302 return NULL;
303 }
304
305 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000306 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307 }
308
Petr Viktorinffd97532020-02-11 17:46:57 +0100309 cursor = PyObject_CallOneArg(factory, (PyObject *)self);
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300310 if (cursor == NULL)
311 return NULL;
312 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
313 PyErr_Format(PyExc_TypeError,
314 "factory must return a cursor, not %.100s",
315 Py_TYPE(cursor)->tp_name);
316 Py_DECREF(cursor);
317 return NULL;
318 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319
Gerhard Häringf9cee222010-03-05 15:20:03 +0000320 _pysqlite_drop_unused_cursor_references(self);
321
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000322 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300324 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 }
326
327 return cursor;
328}
329
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000330PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
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
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000452PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000453{
454 int rc;
455 const char* tail;
456 sqlite3_stmt* statement;
457
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000458 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000459 return NULL;
460 }
461
Berker Peksag59da4b32016-09-12 07:16:43 +0300462 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000463 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464
465 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700466 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000467 Py_END_ALLOW_THREADS
468 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000469 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000470 goto error;
471 }
472
Benjamin Petersond7b03282008-09-13 15:58:53 +0000473 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300474 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000475 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000476 }
477
478 Py_BEGIN_ALLOW_THREADS
479 rc = sqlite3_finalize(statement);
480 Py_END_ALLOW_THREADS
481 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000482 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483 }
484
485 }
486
487error:
488 if (PyErr_Occurred()) {
489 return NULL;
490 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200491 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492 }
493}
494
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200495static int
496_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000497{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200498 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000500 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200501 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
502 if (value == -1 && PyErr_Occurred())
503 return -1;
504 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505 } else if (PyFloat_Check(py_val)) {
506 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000507 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200508 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200509 if (str == NULL)
510 return -1;
511 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000512 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200513 Py_buffer view;
514 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100515 PyErr_SetString(PyExc_ValueError,
516 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200517 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000518 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200519 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100520 PyErr_SetString(PyExc_OverflowError,
521 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200522 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100523 return -1;
524 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200525 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
526 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200528 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000529 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200530 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000531}
532
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000533PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000534{
535 PyObject* args;
536 int i;
537 sqlite3_value* cur_value;
538 PyObject* cur_py_value;
539 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000540 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541
542 args = PyTuple_New(argc);
543 if (!args) {
544 return NULL;
545 }
546
547 for (i = 0; i < argc; i++) {
548 cur_value = argv[i];
549 switch (sqlite3_value_type(argv[i])) {
550 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500551 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000552 break;
553 case SQLITE_FLOAT:
554 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
555 break;
556 case SQLITE_TEXT:
557 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000558 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000559 /* TODO: have a way to show errors here */
560 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000561 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562 Py_INCREF(Py_None);
563 cur_py_value = Py_None;
564 }
565 break;
566 case SQLITE_BLOB:
567 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000568 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000569 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000570 break;
571 case SQLITE_NULL:
572 default:
573 Py_INCREF(Py_None);
574 cur_py_value = Py_None;
575 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000576
577 if (!cur_py_value) {
578 Py_DECREF(args);
579 return NULL;
580 }
581
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000582 PyTuple_SetItem(args, i, cur_py_value);
583
584 }
585
586 return args;
587}
588
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000589void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590{
591 PyObject* args;
592 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000593 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200594 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595
596 PyGILState_STATE threadstate;
597
598 threadstate = PyGILState_Ensure();
599
600 py_func = (PyObject*)sqlite3_user_data(context);
601
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000602 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000603 if (args) {
604 py_retval = PyObject_CallObject(py_func, args);
605 Py_DECREF(args);
606 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000607
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200608 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000609 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200610 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000611 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200612 }
613 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700614 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000615 PyErr_Print();
616 } else {
617 PyErr_Clear();
618 }
619 _sqlite3_result_error(context, "user-defined function raised exception", -1);
620 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621
622 PyGILState_Release(threadstate);
623}
624
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000625static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000626{
627 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000628 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629 PyObject* aggregate_class;
630 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000631 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000632
633 PyGILState_STATE threadstate;
634
635 threadstate = PyGILState_Ensure();
636
637 aggregate_class = (PyObject*)sqlite3_user_data(context);
638
639 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
640
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200641 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100642 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000645 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700646 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000647 PyErr_Print();
648 } else {
649 PyErr_Clear();
650 }
651 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000653 }
654 }
655
656 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000657 if (!stepmethod) {
658 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000659 }
660
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000661 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662 if (!args) {
663 goto error;
664 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000665
666 function_result = PyObject_CallObject(stepmethod, args);
667 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668
Thomas Wouters477c8d52006-05-27 19:21:47 +0000669 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700670 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000671 PyErr_Print();
672 } else {
673 PyErr_Clear();
674 }
675 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000676 }
677
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678error:
679 Py_XDECREF(stepmethod);
680 Py_XDECREF(function_result);
681
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000682 PyGILState_Release(threadstate);
683}
684
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000685void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000686{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200687 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200689 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200690 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200691 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200692 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000693
694 PyGILState_STATE threadstate;
695
696 threadstate = PyGILState_Ensure();
697
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000698 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
699 if (!*aggregate_instance) {
700 /* this branch is executed if there was an exception in the aggregate's
701 * __init__ */
702
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 }
705
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200706 /* Keep the exception (if any) of the last call to step() */
707 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200708 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200709
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200710 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200711
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200712 Py_DECREF(*aggregate_instance);
713
714 ok = 0;
715 if (function_result) {
716 ok = _pysqlite_set_result(context, function_result) == 0;
717 Py_DECREF(function_result);
718 }
719 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700720 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000721 PyErr_Print();
722 } else {
723 PyErr_Clear();
724 }
725 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200726#if SQLITE_VERSION_NUMBER < 3003003
727 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
728 exception, so don't restore the previous exception */
729 restore = 0;
730#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000731 }
732
Victor Stinnerffff7632013-08-02 01:48:10 +0200733 if (restore) {
734 /* 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);
737 }
Victor Stinner3a857322013-07-22 08:34:32 +0200738
Thomas Wouters477c8d52006-05-27 19:21:47 +0000739error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000740 PyGILState_Release(threadstate);
741}
742
Gerhard Häringf9cee222010-03-05 15:20:03 +0000743static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000744{
745 PyObject* new_list;
746 PyObject* weakref;
747 int i;
748
749 /* we only need to do this once in a while */
750 if (self->created_statements++ < 200) {
751 return;
752 }
753
754 self->created_statements = 0;
755
756 new_list = PyList_New(0);
757 if (!new_list) {
758 return;
759 }
760
761 for (i = 0; i < PyList_Size(self->statements); i++) {
762 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000763 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000764 if (PyList_Append(new_list, weakref) != 0) {
765 Py_DECREF(new_list);
766 return;
767 }
768 }
769 }
770
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300771 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773
Gerhard Häringf9cee222010-03-05 15:20:03 +0000774static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
775{
776 PyObject* new_list;
777 PyObject* weakref;
778 int i;
779
780 /* we only need to do this once in a while */
781 if (self->created_cursors++ < 200) {
782 return;
783 }
784
785 self->created_cursors = 0;
786
787 new_list = PyList_New(0);
788 if (!new_list) {
789 return;
790 }
791
792 for (i = 0; i < PyList_Size(self->cursors); i++) {
793 weakref = PyList_GetItem(self->cursors, i);
794 if (PyWeakref_GetObject(weakref) != Py_None) {
795 if (PyList_Append(new_list, weakref) != 0) {
796 Py_DECREF(new_list);
797 return;
798 }
799 }
800 }
801
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300802 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000803}
804
gescheitb9a03762019-07-13 06:15:49 +0300805static void _destructor(void* args)
806{
807 Py_DECREF((PyObject*)args);
808}
809
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000810PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000811{
Sergey Fedoseev08308582018-07-08 12:09:20 +0500812 static char *kwlist[] = {"name", "narg", "func", "deterministic", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813
814 PyObject* func;
815 char* name;
816 int narg;
817 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500818 int deterministic = 0;
819 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820
Gerhard Häringf9cee222010-03-05 15:20:03 +0000821 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
822 return NULL;
823 }
824
Sergey Fedoseev08308582018-07-08 12:09:20 +0500825 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|$p", kwlist,
826 &name, &narg, &func, &deterministic))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827 {
828 return NULL;
829 }
830
Sergey Fedoseev08308582018-07-08 12:09:20 +0500831 if (deterministic) {
832#if SQLITE_VERSION_NUMBER < 3008003
833 PyErr_SetString(pysqlite_NotSupportedError,
834 "deterministic=True requires SQLite 3.8.3 or higher");
835 return NULL;
836#else
837 if (sqlite3_libversion_number() < 3008003) {
838 PyErr_SetString(pysqlite_NotSupportedError,
839 "deterministic=True requires SQLite 3.8.3 or higher");
840 return NULL;
841 }
842 flags |= SQLITE_DETERMINISTIC;
843#endif
844 }
gescheitb9a03762019-07-13 06:15:49 +0300845 Py_INCREF(func);
846 rc = sqlite3_create_function_v2(self->db,
847 name,
848 narg,
849 flags,
850 (void*)func,
851 _pysqlite_func_callback,
852 NULL,
853 NULL,
854 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000855
Thomas Wouters477c8d52006-05-27 19:21:47 +0000856 if (rc != SQLITE_OK) {
857 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000858 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000860 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500861 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862}
863
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000864PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865{
866 PyObject* aggregate_class;
867
868 int n_arg;
869 char* name;
870 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
871 int rc;
872
Gerhard Häringf9cee222010-03-05 15:20:03 +0000873 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
874 return NULL;
875 }
876
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000877 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
878 kwlist, &name, &n_arg, &aggregate_class)) {
879 return NULL;
880 }
gescheitb9a03762019-07-13 06:15:49 +0300881 Py_INCREF(aggregate_class);
882 rc = sqlite3_create_function_v2(self->db,
883 name,
884 n_arg,
885 SQLITE_UTF8,
886 (void*)aggregate_class,
887 0,
888 &_pysqlite_step_callback,
889 &_pysqlite_final_callback,
890 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000891 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000893 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000894 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000895 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500896 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000897}
898
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000899static 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 +0000900{
901 PyObject *ret;
902 int rc;
903 PyGILState_STATE gilstate;
904
905 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000906
Victor Stinnerd4095d92013-07-26 22:23:33 +0200907 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000908
Victor Stinnerd4095d92013-07-26 22:23:33 +0200909 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700910 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200911 PyErr_Print();
912 else
913 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200914
Victor Stinnerd4095d92013-07-26 22:23:33 +0200915 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200916 }
917 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200918 if (PyLong_Check(ret)) {
919 rc = _PyLong_AsInt(ret);
920 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700921 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200922 PyErr_Print();
923 else
924 PyErr_Clear();
925 rc = SQLITE_DENY;
926 }
927 }
928 else {
929 rc = SQLITE_DENY;
930 }
931 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000932 }
933
934 PyGILState_Release(gilstate);
935 return rc;
936}
937
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000938static int _progress_handler(void* user_arg)
939{
940 int rc;
941 PyObject *ret;
942 PyGILState_STATE gilstate;
943
944 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100945 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000946
947 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700948 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000949 PyErr_Print();
950 } else {
951 PyErr_Clear();
952 }
953
Mark Dickinson934896d2009-02-21 20:59:32 +0000954 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000955 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000956 } else {
957 rc = (int)PyObject_IsTrue(ret);
958 Py_DECREF(ret);
959 }
960
961 PyGILState_Release(gilstate);
962 return rc;
963}
964
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200965static void _trace_callback(void* user_arg, const char* statement_string)
966{
967 PyObject *py_statement = NULL;
968 PyObject *ret = NULL;
969
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200970 PyGILState_STATE gilstate;
971
972 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200973 py_statement = PyUnicode_DecodeUTF8(statement_string,
974 strlen(statement_string), "replace");
975 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100976 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200977 Py_DECREF(py_statement);
978 }
979
980 if (ret) {
981 Py_DECREF(ret);
982 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700983 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200984 PyErr_Print();
985 } else {
986 PyErr_Clear();
987 }
988 }
989
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200990 PyGILState_Release(gilstate);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200991}
992
Gerhard Häringf9cee222010-03-05 15:20:03 +0000993static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000994{
995 PyObject* authorizer_cb;
996
997 static char *kwlist[] = { "authorizer_callback", NULL };
998 int rc;
999
Gerhard Häringf9cee222010-03-05 15:20:03 +00001000 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1001 return NULL;
1002 }
1003
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001004 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1005 kwlist, &authorizer_cb)) {
1006 return NULL;
1007 }
1008
1009 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001010 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001011 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001012 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001013 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001014 } else {
1015 Py_INCREF(authorizer_cb);
1016 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001017 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001018 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001019}
1020
Gerhard Häringf9cee222010-03-05 15:20:03 +00001021static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001022{
1023 PyObject* progress_handler;
1024 int n;
1025
1026 static char *kwlist[] = { "progress_handler", "n", NULL };
1027
Gerhard Häringf9cee222010-03-05 15:20:03 +00001028 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1029 return NULL;
1030 }
1031
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001032 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1033 kwlist, &progress_handler, &n)) {
1034 return NULL;
1035 }
1036
1037 if (progress_handler == Py_None) {
1038 /* None clears the progress handler previously set */
1039 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001040 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001041 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001042 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001043 Py_INCREF(progress_handler);
1044 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001045 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001046 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001047}
1048
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001049static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1050{
1051 PyObject* trace_callback;
1052
1053 static char *kwlist[] = { "trace_callback", NULL };
1054
1055 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1056 return NULL;
1057 }
1058
1059 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1060 kwlist, &trace_callback)) {
1061 return NULL;
1062 }
1063
1064 if (trace_callback == Py_None) {
1065 /* None clears the trace callback previously set */
1066 sqlite3_trace(self->db, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001067 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001068 } else {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001069 sqlite3_trace(self->db, _trace_callback, trace_callback);
gescheitb9a03762019-07-13 06:15:49 +03001070 Py_INCREF(trace_callback);
1071 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001072 }
1073
Berker Peksagfe21de92016-04-09 07:34:39 +03001074 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001075}
1076
Gerhard Häringf9cee222010-03-05 15:20:03 +00001077#ifdef HAVE_LOAD_EXTENSION
1078static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1079{
1080 int rc;
1081 int onoff;
1082
1083 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1084 return NULL;
1085 }
1086
1087 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1088 return NULL;
1089 }
1090
1091 rc = sqlite3_enable_load_extension(self->db, onoff);
1092
1093 if (rc != SQLITE_OK) {
1094 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1095 return NULL;
1096 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001097 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001098 }
1099}
1100
1101static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1102{
1103 int rc;
1104 char* extension_name;
1105 char* errmsg;
1106
1107 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1108 return NULL;
1109 }
1110
1111 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1112 return NULL;
1113 }
1114
1115 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1116 if (rc != 0) {
1117 PyErr_SetString(pysqlite_OperationalError, errmsg);
1118 return NULL;
1119 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001120 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001121 }
1122}
1123#endif
1124
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001125int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001126{
1127 if (self->check_same_thread) {
1128 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001129 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001130 "SQLite objects created in a thread can only be used in that same thread. "
1131 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001132 self->thread_ident, PyThread_get_thread_ident());
1133 return 0;
1134 }
1135
1136 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001137 return 1;
1138}
1139
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001140static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001141{
1142 Py_INCREF(self->isolation_level);
1143 return self->isolation_level;
1144}
1145
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001146static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001148 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149 return NULL;
1150 } else {
1151 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1152 }
1153}
1154
Berker Peksag59da4b32016-09-12 07:16:43 +03001155static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1156{
1157 if (!pysqlite_check_connection(self)) {
1158 return NULL;
1159 }
1160 if (!sqlite3_get_autocommit(self->db)) {
1161 Py_RETURN_TRUE;
1162 }
1163 Py_RETURN_FALSE;
1164}
1165
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001166static int
1167pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001168{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001169 if (isolation_level == NULL) {
1170 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1171 return -1;
1172 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001174 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001175 if (!res) {
1176 return -1;
1177 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001178 Py_DECREF(res);
1179
Serhiy Storchaka28914922016-09-01 22:18:03 +03001180 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001182 const char * const *candidate;
1183 PyObject *uppercase_level;
1184 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001185
Serhiy Storchaka28914922016-09-01 22:18:03 +03001186 if (!PyUnicode_Check(isolation_level)) {
1187 PyErr_Format(PyExc_TypeError,
1188 "isolation_level must be a string or None, not %.100s",
1189 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001190 return -1;
1191 }
1192
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001193 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001194 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001195 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001196 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001197 return -1;
1198 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001199 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001200 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001201 break;
1202 }
1203 Py_DECREF(uppercase_level);
1204 if (!*candidate) {
1205 PyErr_SetString(PyExc_ValueError,
1206 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001207 return -1;
1208 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001209 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001210 }
1211
Serhiy Storchaka28914922016-09-01 22:18:03 +03001212 Py_INCREF(isolation_level);
1213 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001214 return 0;
1215}
1216
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001217PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001218{
1219 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001220 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001221 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001222 int rc;
1223
Gerhard Häringf9cee222010-03-05 15:20:03 +00001224 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1225 return NULL;
1226 }
1227
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001228 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001229 return NULL;
1230
Victor Stinnerc6a23202019-06-26 03:16:24 +02001231 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001232 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001233
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001234 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001235
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001236 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001237 if (!statement) {
1238 return NULL;
1239 }
1240
Victor Stinner0201f442010-03-13 03:28:34 +00001241 statement->db = NULL;
1242 statement->st = NULL;
1243 statement->sql = NULL;
1244 statement->in_use = 0;
1245 statement->in_weakreflist = NULL;
1246
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001247 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001248 if (rc != SQLITE_OK) {
1249 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001250 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001251 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001252 if (PyErr_ExceptionMatches(PyExc_TypeError))
1253 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001254 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001255 (void)pysqlite_statement_reset(statement);
1256 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001257 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001258 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259 }
1260
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001261 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1262 if (weakref == NULL)
1263 goto error;
1264 if (PyList_Append(self->statements, weakref) != 0) {
1265 Py_DECREF(weakref);
1266 goto error;
1267 }
1268 Py_DECREF(weakref);
1269
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001271
1272error:
1273 Py_DECREF(statement);
1274 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275}
1276
Larry Hastings01b08832015-05-08 07:37:49 -07001277PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001278{
1279 PyObject* cursor = 0;
1280 PyObject* result = 0;
1281 PyObject* method = 0;
1282
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001283 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 if (!cursor) {
1285 goto error;
1286 }
1287
1288 method = PyObject_GetAttrString(cursor, "execute");
1289 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001290 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001291 goto error;
1292 }
1293
1294 result = PyObject_CallObject(method, args);
1295 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001296 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001297 }
1298
1299error:
1300 Py_XDECREF(result);
1301 Py_XDECREF(method);
1302
1303 return cursor;
1304}
1305
Larry Hastings01b08832015-05-08 07:37:49 -07001306PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001307{
1308 PyObject* cursor = 0;
1309 PyObject* result = 0;
1310 PyObject* method = 0;
1311
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001312 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001313 if (!cursor) {
1314 goto error;
1315 }
1316
1317 method = PyObject_GetAttrString(cursor, "executemany");
1318 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001319 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001320 goto error;
1321 }
1322
1323 result = PyObject_CallObject(method, args);
1324 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001325 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001326 }
1327
1328error:
1329 Py_XDECREF(result);
1330 Py_XDECREF(method);
1331
1332 return cursor;
1333}
1334
Larry Hastings01b08832015-05-08 07:37:49 -07001335PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001336{
1337 PyObject* cursor = 0;
1338 PyObject* result = 0;
1339 PyObject* method = 0;
1340
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001341 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001342 if (!cursor) {
1343 goto error;
1344 }
1345
1346 method = PyObject_GetAttrString(cursor, "executescript");
1347 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001348 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001349 goto error;
1350 }
1351
1352 result = PyObject_CallObject(method, args);
1353 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001354 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001355 }
1356
1357error:
1358 Py_XDECREF(result);
1359 Py_XDECREF(method);
1360
1361 return cursor;
1362}
1363
1364/* ------------------------- COLLATION CODE ------------------------ */
1365
1366static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001367pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001368 void* context,
1369 int text1_length, const void* text1_data,
1370 int text2_length, const void* text2_data)
1371{
1372 PyObject* callback = (PyObject*)context;
1373 PyObject* string1 = 0;
1374 PyObject* string2 = 0;
1375 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001376 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001377 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001378 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001379 gilstate = PyGILState_Ensure();
1380
1381 if (PyErr_Occurred()) {
1382 goto finally;
1383 }
1384
Guido van Rossum98297ee2007-11-06 21:34:58 +00001385 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1386 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001387
1388 if (!string1 || !string2) {
1389 goto finally; /* failed to allocate strings */
1390 }
1391
1392 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1393
1394 if (!retval) {
1395 /* execution failed */
1396 goto finally;
1397 }
1398
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001399 longval = PyLong_AsLongAndOverflow(retval, &result);
1400 if (longval == -1 && PyErr_Occurred()) {
1401 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001402 result = 0;
1403 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001404 else if (!result) {
1405 if (longval > 0)
1406 result = 1;
1407 else if (longval < 0)
1408 result = -1;
1409 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001410
1411finally:
1412 Py_XDECREF(string1);
1413 Py_XDECREF(string2);
1414 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001415 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001416 return result;
1417}
1418
1419static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001420pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001421{
1422 PyObject* retval = NULL;
1423
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001424 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001425 goto finally;
1426 }
1427
1428 sqlite3_interrupt(self->db);
1429
1430 Py_INCREF(Py_None);
1431 retval = Py_None;
1432
1433finally:
1434 return retval;
1435}
1436
Christian Heimesbbe741d2008-03-28 10:53:29 +00001437/* Function author: Paul Kippes <kippesp@gmail.com>
1438 * Class method of Connection to call the Python function _iterdump
1439 * of the sqlite3 module.
1440 */
1441static PyObject *
1442pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1443{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001444 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001445 PyObject* retval = NULL;
1446 PyObject* module = NULL;
1447 PyObject* module_dict;
1448 PyObject* pyfn_iterdump;
1449
1450 if (!pysqlite_check_connection(self)) {
1451 goto finally;
1452 }
1453
1454 module = PyImport_ImportModule(MODULE_NAME ".dump");
1455 if (!module) {
1456 goto finally;
1457 }
1458
1459 module_dict = PyModule_GetDict(module);
1460 if (!module_dict) {
1461 goto finally;
1462 }
1463
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001464 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001465 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001466 if (!PyErr_Occurred()) {
1467 PyErr_SetString(pysqlite_OperationalError,
1468 "Failed to obtain _iterdump() reference");
1469 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001470 goto finally;
1471 }
1472
Petr Viktorinffd97532020-02-11 17:46:57 +01001473 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001474
1475finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001476 Py_XDECREF(module);
1477 return retval;
1478}
1479
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001480#ifdef HAVE_BACKUP_API
1481static PyObject *
1482pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *kwds)
1483{
1484 PyObject *target = NULL;
1485 int pages = -1;
1486 PyObject *progress = Py_None;
1487 const char *name = "main";
1488 int rc;
1489 int callback_error = 0;
Victor Stinnerca405012018-04-30 12:22:17 +02001490 PyObject *sleep_obj = NULL;
1491 int sleep_ms = 250;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001492 sqlite3 *bck_conn;
1493 sqlite3_backup *bck_handle;
1494 static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL};
1495
Victor Stinnerca405012018-04-30 12:22:17 +02001496 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsO:backup", keywords,
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001497 &pysqlite_ConnectionType, &target,
Victor Stinnerca405012018-04-30 12:22:17 +02001498 &pages, &progress, &name, &sleep_obj)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001499 return NULL;
1500 }
1501
Victor Stinnerca405012018-04-30 12:22:17 +02001502 if (sleep_obj != NULL) {
1503 _PyTime_t sleep_secs;
1504 if (_PyTime_FromSecondsObject(&sleep_secs, sleep_obj,
1505 _PyTime_ROUND_TIMEOUT)) {
1506 return NULL;
1507 }
1508 _PyTime_t ms = _PyTime_AsMilliseconds(sleep_secs,
1509 _PyTime_ROUND_TIMEOUT);
1510 if (ms < INT_MIN || ms > INT_MAX) {
1511 PyErr_SetString(PyExc_OverflowError, "sleep is too large");
1512 return NULL;
1513 }
1514 sleep_ms = (int)ms;
1515 }
1516
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001517 if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
1518 return NULL;
1519 }
1520
1521 if ((pysqlite_Connection *)target == self) {
1522 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1523 return NULL;
1524 }
1525
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001526#if SQLITE_VERSION_NUMBER < 3008008
1527 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001528 https://www.sqlite.org/src/info/169b5505498c0a7e */
1529 if (!sqlite3_get_autocommit(((pysqlite_Connection *)target)->db)) {
1530 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1531 return NULL;
1532 }
1533#endif
1534
1535 if (progress != Py_None && !PyCallable_Check(progress)) {
1536 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1537 return NULL;
1538 }
1539
1540 if (pages == 0) {
1541 pages = -1;
1542 }
1543
1544 bck_conn = ((pysqlite_Connection *)target)->db;
1545
1546 Py_BEGIN_ALLOW_THREADS
1547 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1548 Py_END_ALLOW_THREADS
1549
1550 if (bck_handle) {
1551 do {
1552 Py_BEGIN_ALLOW_THREADS
1553 rc = sqlite3_backup_step(bck_handle, pages);
1554 Py_END_ALLOW_THREADS
1555
1556 if (progress != Py_None) {
1557 PyObject *res;
1558
1559 res = PyObject_CallFunction(progress, "iii", rc,
1560 sqlite3_backup_remaining(bck_handle),
1561 sqlite3_backup_pagecount(bck_handle));
1562 if (res == NULL) {
1563 /* User's callback raised an error: interrupt the loop and
1564 propagate it. */
1565 callback_error = 1;
1566 rc = -1;
1567 } else {
1568 Py_DECREF(res);
1569 }
1570 }
1571
1572 /* Sleep for a while if there are still further pages to copy and
1573 the engine could not make any progress */
1574 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1575 Py_BEGIN_ALLOW_THREADS
Victor Stinnerca405012018-04-30 12:22:17 +02001576 sqlite3_sleep(sleep_ms);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001577 Py_END_ALLOW_THREADS
1578 }
1579 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1580
1581 Py_BEGIN_ALLOW_THREADS
1582 rc = sqlite3_backup_finish(bck_handle);
1583 Py_END_ALLOW_THREADS
1584 } else {
1585 rc = _pysqlite_seterror(bck_conn, NULL);
1586 }
1587
1588 if (!callback_error && rc != SQLITE_OK) {
1589 /* We cannot use _pysqlite_seterror() here because the backup APIs do
1590 not set the error status on the connection object, but rather on
1591 the backup handle. */
1592 if (rc == SQLITE_NOMEM) {
1593 (void)PyErr_NoMemory();
1594 } else {
1595#if SQLITE_VERSION_NUMBER > 3007015
1596 PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
1597#else
1598 switch (rc) {
Berker Peksagb10a64d2018-09-20 14:14:33 +03001599 case SQLITE_ERROR:
1600 /* Description of SQLITE_ERROR in SQLite 3.7.14 and older
1601 releases. */
1602 PyErr_SetString(pysqlite_OperationalError,
1603 "SQL logic error or missing database");
1604 break;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001605 case SQLITE_READONLY:
1606 PyErr_SetString(pysqlite_OperationalError,
1607 "attempt to write a readonly database");
1608 break;
1609 case SQLITE_BUSY:
1610 PyErr_SetString(pysqlite_OperationalError, "database is locked");
1611 break;
1612 case SQLITE_LOCKED:
1613 PyErr_SetString(pysqlite_OperationalError,
1614 "database table is locked");
1615 break;
1616 default:
1617 PyErr_Format(pysqlite_OperationalError,
1618 "unrecognized error code: %d", rc);
1619 break;
1620 }
1621#endif
1622 }
1623 }
1624
1625 if (!callback_error && rc == SQLITE_OK) {
1626 Py_RETURN_NONE;
1627 } else {
1628 return NULL;
1629 }
1630}
1631#endif
1632
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001633static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001634pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001635{
1636 PyObject* callable;
1637 PyObject* uppercase_name = 0;
1638 PyObject* name;
1639 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001640 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001641 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001642 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001643 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001644 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001645 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001646
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001647 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001648 goto finally;
1649 }
1650
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001651 if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1652 &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001653 goto finally;
1654 }
1655
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001656 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1657 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001658 if (!uppercase_name) {
1659 goto finally;
1660 }
1661
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001662 if (PyUnicode_READY(uppercase_name))
1663 goto finally;
1664 len = PyUnicode_GET_LENGTH(uppercase_name);
1665 kind = PyUnicode_KIND(uppercase_name);
1666 data = PyUnicode_DATA(uppercase_name);
1667 for (i=0; i<len; i++) {
1668 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1669 if ((ch >= '0' && ch <= '9')
1670 || (ch >= 'A' && ch <= 'Z')
1671 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001672 {
Victor Stinner35466c52010-04-22 11:23:23 +00001673 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001674 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001675 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001676 goto finally;
1677 }
1678 }
1679
Serhiy Storchaka06515832016-11-20 09:13:07 +02001680 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001681 if (!uppercase_name_str)
1682 goto finally;
1683
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001684 if (callable != Py_None && !PyCallable_Check(callable)) {
1685 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1686 goto finally;
1687 }
1688
1689 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001690 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1691 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001692 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001693 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1694 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001695 }
1696
1697 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001698 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001699 SQLITE_UTF8,
1700 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001701 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001702 if (rc != SQLITE_OK) {
1703 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001704 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001705 goto finally;
1706 }
1707
1708finally:
1709 Py_XDECREF(uppercase_name);
1710
1711 if (PyErr_Occurred()) {
1712 retval = NULL;
1713 } else {
1714 Py_INCREF(Py_None);
1715 retval = Py_None;
1716 }
1717
1718 return retval;
1719}
1720
Christian Heimesbbe741d2008-03-28 10:53:29 +00001721/* Called when the connection is used as a context manager. Returns itself as a
1722 * convenience to the caller. */
1723static PyObject *
1724pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1725{
1726 Py_INCREF(self);
1727 return (PyObject*)self;
1728}
1729
1730/** Called when the connection is used as a context manager. If there was any
1731 * exception, a rollback takes place; otherwise we commit. */
1732static PyObject *
1733pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1734{
1735 PyObject* exc_type, *exc_value, *exc_tb;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001736 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001737 PyObject* result;
1738
1739 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1740 return NULL;
1741 }
1742
1743 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1744 method_name = "commit";
1745 } else {
1746 method_name = "rollback";
1747 }
1748
Victor Stinner3466bde2016-09-05 18:16:01 -07001749 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001750 if (!result) {
1751 return NULL;
1752 }
1753 Py_DECREF(result);
1754
1755 Py_RETURN_FALSE;
1756}
1757
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001758static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001759PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001760
1761static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001762 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1763 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001764 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001765 {NULL}
1766};
1767
1768static PyMethodDef connection_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001769 {"cursor", (PyCFunction)(void(*)(void))pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001770 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001771 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001772 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001773 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001774 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001775 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001776 PyDoc_STR("Roll back the current transaction.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001777 {"create_function", (PyCFunction)(void(*)(void))pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001778 PyDoc_STR("Creates a new function. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001779 {"create_aggregate", (PyCFunction)(void(*)(void))pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001780 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001781 {"set_authorizer", (PyCFunction)(void(*)(void))pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001782 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001783 #ifdef HAVE_LOAD_EXTENSION
1784 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1785 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1786 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1787 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1788 #endif
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001789 {"set_progress_handler", (PyCFunction)(void(*)(void))pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001790 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001791 {"set_trace_callback", (PyCFunction)(void(*)(void))pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001792 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001793 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001794 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001795 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001796 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001797 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001798 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001799 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001800 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001801 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001802 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001803 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001804 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001805 #ifdef HAVE_BACKUP_API
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001806 {"backup", (PyCFunction)(void(*)(void))pysqlite_connection_backup, METH_VARARGS | METH_KEYWORDS,
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001807 PyDoc_STR("Makes a backup of the database. Non-standard.")},
1808 #endif
Christian Heimesbbe741d2008-03-28 10:53:29 +00001809 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1810 PyDoc_STR("For context manager. Non-standard.")},
1811 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1812 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001813 {NULL, NULL}
1814};
1815
1816static struct PyMemberDef connection_members[] =
1817{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001818 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1819 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1820 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1821 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1822 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1823 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1824 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1825 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1826 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1827 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001828 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1829 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001830 {NULL}
1831};
1832
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001833PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001834 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001835 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001836 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001837 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001838 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001839 0, /* tp_vectorcall_offset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001840 0, /* tp_getattr */
1841 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001842 0, /* tp_as_async */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001843 0, /* tp_repr */
1844 0, /* tp_as_number */
1845 0, /* tp_as_sequence */
1846 0, /* tp_as_mapping */
1847 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001848 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001849 0, /* tp_str */
1850 0, /* tp_getattro */
1851 0, /* tp_setattro */
1852 0, /* tp_as_buffer */
1853 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1854 connection_doc, /* tp_doc */
1855 0, /* tp_traverse */
1856 0, /* tp_clear */
1857 0, /* tp_richcompare */
1858 0, /* tp_weaklistoffset */
1859 0, /* tp_iter */
1860 0, /* tp_iternext */
1861 connection_methods, /* tp_methods */
1862 connection_members, /* tp_members */
1863 connection_getset, /* tp_getset */
1864 0, /* tp_base */
1865 0, /* tp_dict */
1866 0, /* tp_descr_get */
1867 0, /* tp_descr_set */
1868 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001869 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001870 0, /* tp_alloc */
1871 0, /* tp_new */
1872 0 /* tp_free */
1873};
1874
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001875extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001876{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001877 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1878 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001879}