blob: 697295da9ce3978bc9098b84b9f0343cd5670e9f [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"
R. David Murrayd35251d2010-06-01 01:32:12 +000026#include "structmember.h"
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
33#include "pythread.h"
34
Gerhard Häringe7ea7452008-03-29 00:45:29 +000035#define ACTION_FINALIZE 1
36#define ACTION_RESET 2
37
Gerhard Häringf9cee222010-03-05 15:20:03 +000038#if SQLITE_VERSION_NUMBER >= 3003008
39#ifndef SQLITE_OMIT_LOAD_EXTENSION
40#define HAVE_LOAD_EXTENSION
41#endif
42#endif
43
Emanuele Gaifasd7aed412018-03-10 23:08:31 +010044#if SQLITE_VERSION_NUMBER >= 3006011
45#define HAVE_BACKUP_API
46#endif
47
Martin v. Löwise75fc142013-11-07 18:46:53 +010048_Py_IDENTIFIER(cursor);
49
Serhiy Storchaka28914922016-09-01 22:18:03 +030050static const char * const begin_statements[] = {
51 "BEGIN ",
52 "BEGIN DEFERRED",
53 "BEGIN IMMEDIATE",
54 "BEGIN EXCLUSIVE",
55 NULL
56};
57
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +020058static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored));
Gerhard Häringf9cee222010-03-05 15:20:03 +000059static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061
Benjamin Petersond7b03282008-09-13 15:58:53 +000062static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063{
64 /* in older SQLite versions, calling sqlite3_result_error in callbacks
65 * triggers a bug in SQLite that leads either to irritating results or
66 * segfaults, depending on the SQLite version */
67#if SQLITE_VERSION_NUMBER >= 3003003
68 sqlite3_result_error(ctx, errmsg, len);
69#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000070 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071#endif
72}
73
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000074int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000075{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010076 static char *kwlist[] = {
77 "database", "timeout", "detect_types", "isolation_level",
78 "check_same_thread", "factory", "cached_statements", "uri",
79 NULL
80 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081
82 char* database;
Anders Lorentsena22a1272017-11-07 01:47:43 +010083 PyObject* database_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 int detect_types = 0;
85 PyObject* isolation_level = NULL;
86 PyObject* factory = NULL;
87 int check_same_thread = 1;
88 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010089 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000090 double timeout = 5.0;
91 int rc;
92
Anders Lorentsena22a1272017-11-07 01:47:43 +010093 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
94 PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010095 &isolation_level, &check_same_thread,
96 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000098 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000099 }
100
Anders Lorentsena22a1272017-11-07 01:47:43 +0100101 database = PyBytes_AsString(database_obj);
102
Gerhard Häringf9cee222010-03-05 15:20:03 +0000103 self->initialized = 1;
104
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105 self->begin_statement = NULL;
106
Oren Milman93c5a5d2017-10-10 22:27:46 +0300107 Py_CLEAR(self->statement_cache);
108 Py_CLEAR(self->statements);
109 Py_CLEAR(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110
111 Py_INCREF(Py_None);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300112 Py_XSETREF(self->row_factory, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113
114 Py_INCREF(&PyUnicode_Type);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300115 Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100117#ifdef SQLITE_OPEN_URI
118 Py_BEGIN_ALLOW_THREADS
119 rc = sqlite3_open_v2(database, &self->db,
120 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
121 (uri ? SQLITE_OPEN_URI : 0), NULL);
122#else
123 if (uri) {
124 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
125 return -1;
126 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200128 /* No need to use sqlite3_open_v2 as sqlite3_open(filename, db) is the
129 same as sqlite3_open_v2(filename, db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL). */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100131#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 Py_END_ALLOW_THREADS
133
Anders Lorentsena22a1272017-11-07 01:47:43 +0100134 Py_DECREF(database_obj);
135
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000137 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 return -1;
139 }
140
141 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000142 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000143 if (!isolation_level) {
144 return -1;
145 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 } else {
147 Py_INCREF(isolation_level);
148 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300149 Py_CLEAR(self->isolation_level);
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200150 if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100151 Py_DECREF(isolation_level);
152 return -1;
153 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154 Py_DECREF(isolation_level);
155
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000156 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000157 if (PyErr_Occurred()) {
158 return -1;
159 }
160
Gerhard Häringf9cee222010-03-05 15:20:03 +0000161 self->created_statements = 0;
162 self->created_cursors = 0;
163
164 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000165 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000166 self->cursors = PyList_New(0);
167 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000168 return -1;
169 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000170
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 /* By default, the Cache class INCREFs the factory in its initializer, and
172 * decrefs it in its deallocator method. Since this would create a circular
173 * reference here, we're breaking it by decrementing self, and telling the
174 * cache class to not decref the factory (self) in its deallocator.
175 */
176 self->statement_cache->decref_factory = 0;
177 Py_DECREF(self);
178
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179 self->detect_types = detect_types;
180 self->timeout = timeout;
181 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182 self->thread_ident = PyThread_get_thread_ident();
Berker Peksag7bea2342016-06-12 14:09:51 +0300183 if (!check_same_thread && sqlite3_libversion_number() < 3003001) {
184 PyErr_SetString(pysqlite_NotSupportedError, "shared connections not available");
185 return -1;
186 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000187 self->check_same_thread = check_same_thread;
188
gescheitb9a03762019-07-13 06:15:49 +0300189 self->function_pinboard_trace_callback = NULL;
190 self->function_pinboard_progress_handler = NULL;
191 self->function_pinboard_authorizer_cb = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000192
Oren Milman93c5a5d2017-10-10 22:27:46 +0300193 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194 if (!self->collations) {
195 return -1;
196 }
197
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000198 self->Warning = pysqlite_Warning;
199 self->Error = pysqlite_Error;
200 self->InterfaceError = pysqlite_InterfaceError;
201 self->DatabaseError = pysqlite_DatabaseError;
202 self->DataError = pysqlite_DataError;
203 self->OperationalError = pysqlite_OperationalError;
204 self->IntegrityError = pysqlite_IntegrityError;
205 self->InternalError = pysqlite_InternalError;
206 self->ProgrammingError = pysqlite_ProgrammingError;
207 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208
209 return 0;
210}
211
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000212/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000213void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000214{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000215 int i;
216 PyObject* weakref;
217 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000218 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000219
Thomas Wouters477c8d52006-05-27 19:21:47 +0000220 for (i = 0; i < PyList_Size(self->statements); i++) {
221 weakref = PyList_GetItem(self->statements, i);
222 statement = PyWeakref_GetObject(weakref);
223 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500224 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000225 if (action == ACTION_RESET) {
226 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
227 } else {
228 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
229 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500230 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000231 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000233
234 if (reset_cursors) {
235 for (i = 0; i < PyList_Size(self->cursors); i++) {
236 weakref = PyList_GetItem(self->cursors, i);
237 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
238 if ((PyObject*)cursor != Py_None) {
239 cursor->reset = 1;
240 }
241 }
242 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243}
244
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000245void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246{
247 Py_XDECREF(self->statement_cache);
248
249 /* Clean up if user has not called .close() explicitly. */
250 if (self->db) {
Aviv Palivoda86a67052017-03-03 12:58:17 +0200251 SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252 }
253
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254 Py_XDECREF(self->isolation_level);
gescheitb9a03762019-07-13 06:15:49 +0300255 Py_XDECREF(self->function_pinboard_trace_callback);
256 Py_XDECREF(self->function_pinboard_progress_handler);
257 Py_XDECREF(self->function_pinboard_authorizer_cb);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000258 Py_XDECREF(self->row_factory);
259 Py_XDECREF(self->text_factory);
260 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000261 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000262 Py_XDECREF(self->cursors);
Christian Heimes90aa7642007-12-19 02:45:37 +0000263 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264}
265
Gerhard Häringf9cee222010-03-05 15:20:03 +0000266/*
267 * Registers a cursor with the connection.
268 *
269 * 0 => error; 1 => ok
270 */
271int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
272{
273 PyObject* weakref;
274
275 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
276 if (!weakref) {
277 goto error;
278 }
279
280 if (PyList_Append(connection->cursors, weakref) != 0) {
281 Py_CLEAR(weakref);
282 goto error;
283 }
284
285 Py_DECREF(weakref);
286
287 return 1;
288error:
289 return 0;
290}
291
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000292PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300294 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000295 PyObject* factory = NULL;
296 PyObject* cursor;
297
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
299 &factory)) {
300 return NULL;
301 }
302
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000303 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304 return NULL;
305 }
306
307 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000308 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000309 }
310
Petr Viktorinffd97532020-02-11 17:46:57 +0100311 cursor = PyObject_CallOneArg(factory, (PyObject *)self);
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300312 if (cursor == NULL)
313 return NULL;
314 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
315 PyErr_Format(PyExc_TypeError,
316 "factory must return a cursor, not %.100s",
317 Py_TYPE(cursor)->tp_name);
318 Py_DECREF(cursor);
319 return NULL;
320 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000321
Gerhard Häringf9cee222010-03-05 15:20:03 +0000322 _pysqlite_drop_unused_cursor_references(self);
323
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300326 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327 }
328
329 return cursor;
330}
331
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000332PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333{
334 int rc;
335
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000336 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337 return NULL;
338 }
339
Gerhard Häringf9cee222010-03-05 15:20:03 +0000340 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341
342 if (self->db) {
Aviv Palivoda86a67052017-03-03 12:58:17 +0200343 rc = SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344
345 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000346 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347 return NULL;
348 } else {
349 self->db = NULL;
350 }
351 }
352
Berker Peksagfe21de92016-04-09 07:34:39 +0300353 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000354}
355
356/*
357 * Checks if a connection object is usable (i. e. not closed).
358 *
359 * 0 => error; 1 => ok
360 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000361int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000362{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000363 if (!con->initialized) {
364 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
365 return 0;
366 }
367
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000369 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 return 0;
371 } else {
372 return 1;
373 }
374}
375
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000376PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377{
378 int rc;
379 const char* tail;
380 sqlite3_stmt* statement;
381
382 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700383 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384 Py_END_ALLOW_THREADS
385
386 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000387 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388 goto error;
389 }
390
Benjamin Petersond7b03282008-09-13 15:58:53 +0000391 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300392 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000393 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394 }
395
396 Py_BEGIN_ALLOW_THREADS
397 rc = sqlite3_finalize(statement);
398 Py_END_ALLOW_THREADS
399
400 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000401 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402 }
403
404error:
405 if (PyErr_Occurred()) {
406 return NULL;
407 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200408 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 }
410}
411
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000412PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413{
414 int rc;
415 const char* tail;
416 sqlite3_stmt* statement;
417
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000418 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419 return NULL;
420 }
421
Berker Peksag59da4b32016-09-12 07:16:43 +0300422 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000423
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700425 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426 Py_END_ALLOW_THREADS
427 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000428 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000429 goto error;
430 }
431
Benjamin Petersond7b03282008-09-13 15:58:53 +0000432 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300433 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000434 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000435 }
436
437 Py_BEGIN_ALLOW_THREADS
438 rc = sqlite3_finalize(statement);
439 Py_END_ALLOW_THREADS
440 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000441 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442 }
443
444 }
445
446error:
447 if (PyErr_Occurred()) {
448 return NULL;
449 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200450 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451 }
452}
453
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000454PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455{
456 int rc;
457 const char* tail;
458 sqlite3_stmt* statement;
459
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000460 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461 return NULL;
462 }
463
Berker Peksag59da4b32016-09-12 07:16:43 +0300464 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000465 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000466
467 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700468 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469 Py_END_ALLOW_THREADS
470 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000471 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000472 goto error;
473 }
474
Benjamin Petersond7b03282008-09-13 15:58:53 +0000475 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300476 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000477 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000478 }
479
480 Py_BEGIN_ALLOW_THREADS
481 rc = sqlite3_finalize(statement);
482 Py_END_ALLOW_THREADS
483 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000484 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000485 }
486
487 }
488
489error:
490 if (PyErr_Occurred()) {
491 return NULL;
492 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200493 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000494 }
495}
496
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200497static int
498_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200500 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000501 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000502 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200503 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
504 if (value == -1 && PyErr_Occurred())
505 return -1;
506 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507 } else if (PyFloat_Check(py_val)) {
508 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000509 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200510 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200511 if (str == NULL)
512 return -1;
513 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000514 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200515 Py_buffer view;
516 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100517 PyErr_SetString(PyExc_ValueError,
518 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200519 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200521 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100522 PyErr_SetString(PyExc_OverflowError,
523 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200524 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100525 return -1;
526 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200527 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
528 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000529 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200530 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000531 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200532 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533}
534
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000535PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536{
537 PyObject* args;
538 int i;
539 sqlite3_value* cur_value;
540 PyObject* cur_py_value;
541 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543
544 args = PyTuple_New(argc);
545 if (!args) {
546 return NULL;
547 }
548
549 for (i = 0; i < argc; i++) {
550 cur_value = argv[i];
551 switch (sqlite3_value_type(argv[i])) {
552 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500553 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554 break;
555 case SQLITE_FLOAT:
556 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
557 break;
558 case SQLITE_TEXT:
559 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000560 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000561 /* TODO: have a way to show errors here */
562 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000563 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000564 Py_INCREF(Py_None);
565 cur_py_value = Py_None;
566 }
567 break;
568 case SQLITE_BLOB:
569 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000570 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000571 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000572 break;
573 case SQLITE_NULL:
574 default:
575 Py_INCREF(Py_None);
576 cur_py_value = Py_None;
577 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000578
579 if (!cur_py_value) {
580 Py_DECREF(args);
581 return NULL;
582 }
583
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000584 PyTuple_SetItem(args, i, cur_py_value);
585
586 }
587
588 return args;
589}
590
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000591void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592{
593 PyObject* args;
594 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000595 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200596 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597
598 PyGILState_STATE threadstate;
599
600 threadstate = PyGILState_Ensure();
601
602 py_func = (PyObject*)sqlite3_user_data(context);
603
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000604 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000605 if (args) {
606 py_retval = PyObject_CallObject(py_func, args);
607 Py_DECREF(args);
608 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000609
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200610 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000611 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200612 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000613 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200614 }
615 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700616 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617 PyErr_Print();
618 } else {
619 PyErr_Clear();
620 }
621 _sqlite3_result_error(context, "user-defined function raised exception", -1);
622 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623
624 PyGILState_Release(threadstate);
625}
626
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000627static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628{
629 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000630 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000631 PyObject* aggregate_class;
632 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000633 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634
635 PyGILState_STATE threadstate;
636
637 threadstate = PyGILState_Ensure();
638
639 aggregate_class = (PyObject*)sqlite3_user_data(context);
640
641 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
642
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200643 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100644 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000645
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700648 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000649 PyErr_Print();
650 } else {
651 PyErr_Clear();
652 }
653 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655 }
656 }
657
658 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659 if (!stepmethod) {
660 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661 }
662
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000663 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664 if (!args) {
665 goto error;
666 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000667
668 function_result = PyObject_CallObject(stepmethod, args);
669 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670
Thomas Wouters477c8d52006-05-27 19:21:47 +0000671 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700672 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000673 PyErr_Print();
674 } else {
675 PyErr_Clear();
676 }
677 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678 }
679
Thomas Wouters477c8d52006-05-27 19:21:47 +0000680error:
681 Py_XDECREF(stepmethod);
682 Py_XDECREF(function_result);
683
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 PyGILState_Release(threadstate);
685}
686
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000687void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200689 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200691 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200692 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200693 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200694 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000695
696 PyGILState_STATE threadstate;
697
698 threadstate = PyGILState_Ensure();
699
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000700 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
701 if (!*aggregate_instance) {
702 /* this branch is executed if there was an exception in the aggregate's
703 * __init__ */
704
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706 }
707
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200708 /* Keep the exception (if any) of the last call to step() */
709 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200710 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200711
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200712 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200713
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200714 Py_DECREF(*aggregate_instance);
715
716 ok = 0;
717 if (function_result) {
718 ok = _pysqlite_set_result(context, function_result) == 0;
719 Py_DECREF(function_result);
720 }
721 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700722 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000723 PyErr_Print();
724 } else {
725 PyErr_Clear();
726 }
727 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200728#if SQLITE_VERSION_NUMBER < 3003003
729 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
730 exception, so don't restore the previous exception */
731 restore = 0;
732#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000733 }
734
Victor Stinnerffff7632013-08-02 01:48:10 +0200735 if (restore) {
736 /* Restore the exception (if any) of the last call to step(),
737 but clear also the current exception if finalize() failed */
738 PyErr_Restore(exception, value, tb);
739 }
Victor Stinner3a857322013-07-22 08:34:32 +0200740
Thomas Wouters477c8d52006-05-27 19:21:47 +0000741error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000742 PyGILState_Release(threadstate);
743}
744
Gerhard Häringf9cee222010-03-05 15:20:03 +0000745static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000746{
747 PyObject* new_list;
748 PyObject* weakref;
749 int i;
750
751 /* we only need to do this once in a while */
752 if (self->created_statements++ < 200) {
753 return;
754 }
755
756 self->created_statements = 0;
757
758 new_list = PyList_New(0);
759 if (!new_list) {
760 return;
761 }
762
763 for (i = 0; i < PyList_Size(self->statements); i++) {
764 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000765 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766 if (PyList_Append(new_list, weakref) != 0) {
767 Py_DECREF(new_list);
768 return;
769 }
770 }
771 }
772
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300773 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000774}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775
Gerhard Häringf9cee222010-03-05 15:20:03 +0000776static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
777{
778 PyObject* new_list;
779 PyObject* weakref;
780 int i;
781
782 /* we only need to do this once in a while */
783 if (self->created_cursors++ < 200) {
784 return;
785 }
786
787 self->created_cursors = 0;
788
789 new_list = PyList_New(0);
790 if (!new_list) {
791 return;
792 }
793
794 for (i = 0; i < PyList_Size(self->cursors); i++) {
795 weakref = PyList_GetItem(self->cursors, i);
796 if (PyWeakref_GetObject(weakref) != Py_None) {
797 if (PyList_Append(new_list, weakref) != 0) {
798 Py_DECREF(new_list);
799 return;
800 }
801 }
802 }
803
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300804 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000805}
806
gescheitb9a03762019-07-13 06:15:49 +0300807static void _destructor(void* args)
808{
809 Py_DECREF((PyObject*)args);
810}
811
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000812PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813{
Sergey Fedoseev08308582018-07-08 12:09:20 +0500814 static char *kwlist[] = {"name", "narg", "func", "deterministic", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815
816 PyObject* func;
817 char* name;
818 int narg;
819 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500820 int deterministic = 0;
821 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000822
Gerhard Häringf9cee222010-03-05 15:20:03 +0000823 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
824 return NULL;
825 }
826
Sergey Fedoseev08308582018-07-08 12:09:20 +0500827 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|$p", kwlist,
828 &name, &narg, &func, &deterministic))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000829 {
830 return NULL;
831 }
832
Sergey Fedoseev08308582018-07-08 12:09:20 +0500833 if (deterministic) {
834#if SQLITE_VERSION_NUMBER < 3008003
835 PyErr_SetString(pysqlite_NotSupportedError,
836 "deterministic=True requires SQLite 3.8.3 or higher");
837 return NULL;
838#else
839 if (sqlite3_libversion_number() < 3008003) {
840 PyErr_SetString(pysqlite_NotSupportedError,
841 "deterministic=True requires SQLite 3.8.3 or higher");
842 return NULL;
843 }
844 flags |= SQLITE_DETERMINISTIC;
845#endif
846 }
gescheitb9a03762019-07-13 06:15:49 +0300847 Py_INCREF(func);
848 rc = sqlite3_create_function_v2(self->db,
849 name,
850 narg,
851 flags,
852 (void*)func,
853 _pysqlite_func_callback,
854 NULL,
855 NULL,
856 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000857
Thomas Wouters477c8d52006-05-27 19:21:47 +0000858 if (rc != SQLITE_OK) {
859 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000860 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000861 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500863 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864}
865
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000866PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867{
868 PyObject* aggregate_class;
869
870 int n_arg;
871 char* name;
872 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
873 int rc;
874
Gerhard Häringf9cee222010-03-05 15:20:03 +0000875 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
876 return NULL;
877 }
878
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000879 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
880 kwlist, &name, &n_arg, &aggregate_class)) {
881 return NULL;
882 }
gescheitb9a03762019-07-13 06:15:49 +0300883 Py_INCREF(aggregate_class);
884 rc = sqlite3_create_function_v2(self->db,
885 name,
886 n_arg,
887 SQLITE_UTF8,
888 (void*)aggregate_class,
889 0,
890 &_pysqlite_step_callback,
891 &_pysqlite_final_callback,
892 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000893 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000894 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000895 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000896 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000897 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500898 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000899}
900
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000901static 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 +0000902{
903 PyObject *ret;
904 int rc;
905 PyGILState_STATE gilstate;
906
907 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000908
Victor Stinnerd4095d92013-07-26 22:23:33 +0200909 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000910
Victor Stinnerd4095d92013-07-26 22:23:33 +0200911 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700912 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200913 PyErr_Print();
914 else
915 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200916
Victor Stinnerd4095d92013-07-26 22:23:33 +0200917 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200918 }
919 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200920 if (PyLong_Check(ret)) {
921 rc = _PyLong_AsInt(ret);
922 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700923 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200924 PyErr_Print();
925 else
926 PyErr_Clear();
927 rc = SQLITE_DENY;
928 }
929 }
930 else {
931 rc = SQLITE_DENY;
932 }
933 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000934 }
935
936 PyGILState_Release(gilstate);
937 return rc;
938}
939
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000940static int _progress_handler(void* user_arg)
941{
942 int rc;
943 PyObject *ret;
944 PyGILState_STATE gilstate;
945
946 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100947 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000948
949 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700950 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000951 PyErr_Print();
952 } else {
953 PyErr_Clear();
954 }
955
Mark Dickinson934896d2009-02-21 20:59:32 +0000956 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000957 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000958 } else {
959 rc = (int)PyObject_IsTrue(ret);
960 Py_DECREF(ret);
961 }
962
963 PyGILState_Release(gilstate);
964 return rc;
965}
966
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200967static void _trace_callback(void* user_arg, const char* statement_string)
968{
969 PyObject *py_statement = NULL;
970 PyObject *ret = NULL;
971
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200972 PyGILState_STATE gilstate;
973
974 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200975 py_statement = PyUnicode_DecodeUTF8(statement_string,
976 strlen(statement_string), "replace");
977 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100978 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200979 Py_DECREF(py_statement);
980 }
981
982 if (ret) {
983 Py_DECREF(ret);
984 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700985 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200986 PyErr_Print();
987 } else {
988 PyErr_Clear();
989 }
990 }
991
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200992 PyGILState_Release(gilstate);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200993}
994
Gerhard Häringf9cee222010-03-05 15:20:03 +0000995static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000996{
997 PyObject* authorizer_cb;
998
999 static char *kwlist[] = { "authorizer_callback", NULL };
1000 int rc;
1001
Gerhard Häringf9cee222010-03-05 15:20:03 +00001002 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1003 return NULL;
1004 }
1005
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001006 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1007 kwlist, &authorizer_cb)) {
1008 return NULL;
1009 }
1010
1011 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001012 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001013 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001014 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001015 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001016 } else {
1017 Py_INCREF(authorizer_cb);
1018 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001019 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001020 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001021}
1022
Gerhard Häringf9cee222010-03-05 15:20:03 +00001023static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001024{
1025 PyObject* progress_handler;
1026 int n;
1027
1028 static char *kwlist[] = { "progress_handler", "n", NULL };
1029
Gerhard Häringf9cee222010-03-05 15:20:03 +00001030 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1031 return NULL;
1032 }
1033
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001034 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1035 kwlist, &progress_handler, &n)) {
1036 return NULL;
1037 }
1038
1039 if (progress_handler == Py_None) {
1040 /* None clears the progress handler previously set */
1041 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001042 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001043 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001044 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001045 Py_INCREF(progress_handler);
1046 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001047 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001048 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001049}
1050
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001051static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1052{
1053 PyObject* trace_callback;
1054
1055 static char *kwlist[] = { "trace_callback", NULL };
1056
1057 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1058 return NULL;
1059 }
1060
1061 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1062 kwlist, &trace_callback)) {
1063 return NULL;
1064 }
1065
1066 if (trace_callback == Py_None) {
1067 /* None clears the trace callback previously set */
1068 sqlite3_trace(self->db, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001069 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001070 } else {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001071 sqlite3_trace(self->db, _trace_callback, trace_callback);
gescheitb9a03762019-07-13 06:15:49 +03001072 Py_INCREF(trace_callback);
1073 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001074 }
1075
Berker Peksagfe21de92016-04-09 07:34:39 +03001076 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001077}
1078
Gerhard Häringf9cee222010-03-05 15:20:03 +00001079#ifdef HAVE_LOAD_EXTENSION
1080static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1081{
1082 int rc;
1083 int onoff;
1084
1085 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1086 return NULL;
1087 }
1088
1089 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1090 return NULL;
1091 }
1092
1093 rc = sqlite3_enable_load_extension(self->db, onoff);
1094
1095 if (rc != SQLITE_OK) {
1096 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1097 return NULL;
1098 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001099 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001100 }
1101}
1102
1103static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1104{
1105 int rc;
1106 char* extension_name;
1107 char* errmsg;
1108
1109 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1110 return NULL;
1111 }
1112
1113 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1114 return NULL;
1115 }
1116
1117 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1118 if (rc != 0) {
1119 PyErr_SetString(pysqlite_OperationalError, errmsg);
1120 return NULL;
1121 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001122 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001123 }
1124}
1125#endif
1126
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001127int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001128{
1129 if (self->check_same_thread) {
1130 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001131 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001132 "SQLite objects created in a thread can only be used in that same thread. "
1133 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001134 self->thread_ident, PyThread_get_thread_ident());
1135 return 0;
1136 }
1137
1138 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001139 return 1;
1140}
1141
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001142static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143{
1144 Py_INCREF(self->isolation_level);
1145 return self->isolation_level;
1146}
1147
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001148static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001150 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001151 return NULL;
1152 } else {
1153 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1154 }
1155}
1156
Berker Peksag59da4b32016-09-12 07:16:43 +03001157static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1158{
1159 if (!pysqlite_check_connection(self)) {
1160 return NULL;
1161 }
1162 if (!sqlite3_get_autocommit(self->db)) {
1163 Py_RETURN_TRUE;
1164 }
1165 Py_RETURN_FALSE;
1166}
1167
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001168static int
1169pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001171 if (isolation_level == NULL) {
1172 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1173 return -1;
1174 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001175 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001176 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001177 if (!res) {
1178 return -1;
1179 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001180 Py_DECREF(res);
1181
Serhiy Storchaka28914922016-09-01 22:18:03 +03001182 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001183 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001184 const char * const *candidate;
1185 PyObject *uppercase_level;
1186 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001187
Serhiy Storchaka28914922016-09-01 22:18:03 +03001188 if (!PyUnicode_Check(isolation_level)) {
1189 PyErr_Format(PyExc_TypeError,
1190 "isolation_level must be a string or None, not %.100s",
1191 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001192 return -1;
1193 }
1194
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001195 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001196 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001197 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001198 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001199 return -1;
1200 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001201 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001202 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001203 break;
1204 }
1205 Py_DECREF(uppercase_level);
1206 if (!*candidate) {
1207 PyErr_SetString(PyExc_ValueError,
1208 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001209 return -1;
1210 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001211 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001212 }
1213
Serhiy Storchaka28914922016-09-01 22:18:03 +03001214 Py_INCREF(isolation_level);
1215 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001216 return 0;
1217}
1218
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001219PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001220{
1221 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001222 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001223 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224 int rc;
1225
Gerhard Häringf9cee222010-03-05 15:20:03 +00001226 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1227 return NULL;
1228 }
1229
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001230 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001231 return NULL;
1232
Victor Stinnerc6a23202019-06-26 03:16:24 +02001233 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001234 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001235
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001236 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001237
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001238 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001239 if (!statement) {
1240 return NULL;
1241 }
1242
Victor Stinner0201f442010-03-13 03:28:34 +00001243 statement->db = NULL;
1244 statement->st = NULL;
1245 statement->sql = NULL;
1246 statement->in_use = 0;
1247 statement->in_weakreflist = NULL;
1248
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001249 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 if (rc != SQLITE_OK) {
1251 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001252 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001253 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001254 if (PyErr_ExceptionMatches(PyExc_TypeError))
1255 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001256 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001257 (void)pysqlite_statement_reset(statement);
1258 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001260 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001261 }
1262
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001263 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1264 if (weakref == NULL)
1265 goto error;
1266 if (PyList_Append(self->statements, weakref) != 0) {
1267 Py_DECREF(weakref);
1268 goto error;
1269 }
1270 Py_DECREF(weakref);
1271
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001272 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001273
1274error:
1275 Py_DECREF(statement);
1276 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001277}
1278
Larry Hastings01b08832015-05-08 07:37:49 -07001279PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280{
1281 PyObject* cursor = 0;
1282 PyObject* result = 0;
1283 PyObject* method = 0;
1284
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001285 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001286 if (!cursor) {
1287 goto error;
1288 }
1289
1290 method = PyObject_GetAttrString(cursor, "execute");
1291 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001292 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001293 goto error;
1294 }
1295
1296 result = PyObject_CallObject(method, args);
1297 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001298 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001299 }
1300
1301error:
1302 Py_XDECREF(result);
1303 Py_XDECREF(method);
1304
1305 return cursor;
1306}
1307
Larry Hastings01b08832015-05-08 07:37:49 -07001308PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001309{
1310 PyObject* cursor = 0;
1311 PyObject* result = 0;
1312 PyObject* method = 0;
1313
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001314 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001315 if (!cursor) {
1316 goto error;
1317 }
1318
1319 method = PyObject_GetAttrString(cursor, "executemany");
1320 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001321 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001322 goto error;
1323 }
1324
1325 result = PyObject_CallObject(method, args);
1326 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001327 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001328 }
1329
1330error:
1331 Py_XDECREF(result);
1332 Py_XDECREF(method);
1333
1334 return cursor;
1335}
1336
Larry Hastings01b08832015-05-08 07:37:49 -07001337PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001338{
1339 PyObject* cursor = 0;
1340 PyObject* result = 0;
1341 PyObject* method = 0;
1342
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001343 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001344 if (!cursor) {
1345 goto error;
1346 }
1347
1348 method = PyObject_GetAttrString(cursor, "executescript");
1349 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001350 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001351 goto error;
1352 }
1353
1354 result = PyObject_CallObject(method, args);
1355 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001356 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001357 }
1358
1359error:
1360 Py_XDECREF(result);
1361 Py_XDECREF(method);
1362
1363 return cursor;
1364}
1365
1366/* ------------------------- COLLATION CODE ------------------------ */
1367
1368static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001369pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370 void* context,
1371 int text1_length, const void* text1_data,
1372 int text2_length, const void* text2_data)
1373{
1374 PyObject* callback = (PyObject*)context;
1375 PyObject* string1 = 0;
1376 PyObject* string2 = 0;
1377 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001378 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001379 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001380 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001381 gilstate = PyGILState_Ensure();
1382
1383 if (PyErr_Occurred()) {
1384 goto finally;
1385 }
1386
Guido van Rossum98297ee2007-11-06 21:34:58 +00001387 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1388 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001389
1390 if (!string1 || !string2) {
1391 goto finally; /* failed to allocate strings */
1392 }
1393
1394 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1395
1396 if (!retval) {
1397 /* execution failed */
1398 goto finally;
1399 }
1400
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001401 longval = PyLong_AsLongAndOverflow(retval, &result);
1402 if (longval == -1 && PyErr_Occurred()) {
1403 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001404 result = 0;
1405 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001406 else if (!result) {
1407 if (longval > 0)
1408 result = 1;
1409 else if (longval < 0)
1410 result = -1;
1411 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001412
1413finally:
1414 Py_XDECREF(string1);
1415 Py_XDECREF(string2);
1416 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001417 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001418 return result;
1419}
1420
1421static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001422pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001423{
1424 PyObject* retval = NULL;
1425
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001426 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001427 goto finally;
1428 }
1429
1430 sqlite3_interrupt(self->db);
1431
1432 Py_INCREF(Py_None);
1433 retval = Py_None;
1434
1435finally:
1436 return retval;
1437}
1438
Christian Heimesbbe741d2008-03-28 10:53:29 +00001439/* Function author: Paul Kippes <kippesp@gmail.com>
1440 * Class method of Connection to call the Python function _iterdump
1441 * of the sqlite3 module.
1442 */
1443static PyObject *
1444pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1445{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001446 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001447 PyObject* retval = NULL;
1448 PyObject* module = NULL;
1449 PyObject* module_dict;
1450 PyObject* pyfn_iterdump;
1451
1452 if (!pysqlite_check_connection(self)) {
1453 goto finally;
1454 }
1455
1456 module = PyImport_ImportModule(MODULE_NAME ".dump");
1457 if (!module) {
1458 goto finally;
1459 }
1460
1461 module_dict = PyModule_GetDict(module);
1462 if (!module_dict) {
1463 goto finally;
1464 }
1465
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001466 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001467 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001468 if (!PyErr_Occurred()) {
1469 PyErr_SetString(pysqlite_OperationalError,
1470 "Failed to obtain _iterdump() reference");
1471 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001472 goto finally;
1473 }
1474
Petr Viktorinffd97532020-02-11 17:46:57 +01001475 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001476
1477finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001478 Py_XDECREF(module);
1479 return retval;
1480}
1481
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001482#ifdef HAVE_BACKUP_API
1483static PyObject *
1484pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *kwds)
1485{
1486 PyObject *target = NULL;
1487 int pages = -1;
1488 PyObject *progress = Py_None;
1489 const char *name = "main";
1490 int rc;
1491 int callback_error = 0;
Victor Stinnerca405012018-04-30 12:22:17 +02001492 PyObject *sleep_obj = NULL;
1493 int sleep_ms = 250;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001494 sqlite3 *bck_conn;
1495 sqlite3_backup *bck_handle;
1496 static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL};
1497
Victor Stinnerca405012018-04-30 12:22:17 +02001498 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsO:backup", keywords,
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001499 &pysqlite_ConnectionType, &target,
Victor Stinnerca405012018-04-30 12:22:17 +02001500 &pages, &progress, &name, &sleep_obj)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001501 return NULL;
1502 }
1503
Victor Stinnerca405012018-04-30 12:22:17 +02001504 if (sleep_obj != NULL) {
1505 _PyTime_t sleep_secs;
1506 if (_PyTime_FromSecondsObject(&sleep_secs, sleep_obj,
1507 _PyTime_ROUND_TIMEOUT)) {
1508 return NULL;
1509 }
1510 _PyTime_t ms = _PyTime_AsMilliseconds(sleep_secs,
1511 _PyTime_ROUND_TIMEOUT);
1512 if (ms < INT_MIN || ms > INT_MAX) {
1513 PyErr_SetString(PyExc_OverflowError, "sleep is too large");
1514 return NULL;
1515 }
1516 sleep_ms = (int)ms;
1517 }
1518
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001519 if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
1520 return NULL;
1521 }
1522
1523 if ((pysqlite_Connection *)target == self) {
1524 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1525 return NULL;
1526 }
1527
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001528#if SQLITE_VERSION_NUMBER < 3008008
1529 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001530 https://www.sqlite.org/src/info/169b5505498c0a7e */
1531 if (!sqlite3_get_autocommit(((pysqlite_Connection *)target)->db)) {
1532 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1533 return NULL;
1534 }
1535#endif
1536
1537 if (progress != Py_None && !PyCallable_Check(progress)) {
1538 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1539 return NULL;
1540 }
1541
1542 if (pages == 0) {
1543 pages = -1;
1544 }
1545
1546 bck_conn = ((pysqlite_Connection *)target)->db;
1547
1548 Py_BEGIN_ALLOW_THREADS
1549 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1550 Py_END_ALLOW_THREADS
1551
1552 if (bck_handle) {
1553 do {
1554 Py_BEGIN_ALLOW_THREADS
1555 rc = sqlite3_backup_step(bck_handle, pages);
1556 Py_END_ALLOW_THREADS
1557
1558 if (progress != Py_None) {
1559 PyObject *res;
1560
1561 res = PyObject_CallFunction(progress, "iii", rc,
1562 sqlite3_backup_remaining(bck_handle),
1563 sqlite3_backup_pagecount(bck_handle));
1564 if (res == NULL) {
1565 /* User's callback raised an error: interrupt the loop and
1566 propagate it. */
1567 callback_error = 1;
1568 rc = -1;
1569 } else {
1570 Py_DECREF(res);
1571 }
1572 }
1573
1574 /* Sleep for a while if there are still further pages to copy and
1575 the engine could not make any progress */
1576 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1577 Py_BEGIN_ALLOW_THREADS
Victor Stinnerca405012018-04-30 12:22:17 +02001578 sqlite3_sleep(sleep_ms);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001579 Py_END_ALLOW_THREADS
1580 }
1581 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1582
1583 Py_BEGIN_ALLOW_THREADS
1584 rc = sqlite3_backup_finish(bck_handle);
1585 Py_END_ALLOW_THREADS
1586 } else {
1587 rc = _pysqlite_seterror(bck_conn, NULL);
1588 }
1589
1590 if (!callback_error && rc != SQLITE_OK) {
1591 /* We cannot use _pysqlite_seterror() here because the backup APIs do
1592 not set the error status on the connection object, but rather on
1593 the backup handle. */
1594 if (rc == SQLITE_NOMEM) {
1595 (void)PyErr_NoMemory();
1596 } else {
1597#if SQLITE_VERSION_NUMBER > 3007015
1598 PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
1599#else
1600 switch (rc) {
Berker Peksagb10a64d2018-09-20 14:14:33 +03001601 case SQLITE_ERROR:
1602 /* Description of SQLITE_ERROR in SQLite 3.7.14 and older
1603 releases. */
1604 PyErr_SetString(pysqlite_OperationalError,
1605 "SQL logic error or missing database");
1606 break;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001607 case SQLITE_READONLY:
1608 PyErr_SetString(pysqlite_OperationalError,
1609 "attempt to write a readonly database");
1610 break;
1611 case SQLITE_BUSY:
1612 PyErr_SetString(pysqlite_OperationalError, "database is locked");
1613 break;
1614 case SQLITE_LOCKED:
1615 PyErr_SetString(pysqlite_OperationalError,
1616 "database table is locked");
1617 break;
1618 default:
1619 PyErr_Format(pysqlite_OperationalError,
1620 "unrecognized error code: %d", rc);
1621 break;
1622 }
1623#endif
1624 }
1625 }
1626
1627 if (!callback_error && rc == SQLITE_OK) {
1628 Py_RETURN_NONE;
1629 } else {
1630 return NULL;
1631 }
1632}
1633#endif
1634
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001635static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001636pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001637{
1638 PyObject* callable;
1639 PyObject* uppercase_name = 0;
1640 PyObject* name;
1641 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001642 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001643 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001644 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001645 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001646 unsigned int kind;
1647 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001648
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001649 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001650 goto finally;
1651 }
1652
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001653 if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1654 &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001655 goto finally;
1656 }
1657
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001658 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1659 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001660 if (!uppercase_name) {
1661 goto finally;
1662 }
1663
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001664 if (PyUnicode_READY(uppercase_name))
1665 goto finally;
1666 len = PyUnicode_GET_LENGTH(uppercase_name);
1667 kind = PyUnicode_KIND(uppercase_name);
1668 data = PyUnicode_DATA(uppercase_name);
1669 for (i=0; i<len; i++) {
1670 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1671 if ((ch >= '0' && ch <= '9')
1672 || (ch >= 'A' && ch <= 'Z')
1673 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001674 {
Victor Stinner35466c52010-04-22 11:23:23 +00001675 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001676 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001677 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001678 goto finally;
1679 }
1680 }
1681
Serhiy Storchaka06515832016-11-20 09:13:07 +02001682 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001683 if (!uppercase_name_str)
1684 goto finally;
1685
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001686 if (callable != Py_None && !PyCallable_Check(callable)) {
1687 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1688 goto finally;
1689 }
1690
1691 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001692 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1693 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001694 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001695 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1696 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001697 }
1698
1699 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001700 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001701 SQLITE_UTF8,
1702 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001703 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001704 if (rc != SQLITE_OK) {
1705 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001706 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001707 goto finally;
1708 }
1709
1710finally:
1711 Py_XDECREF(uppercase_name);
1712
1713 if (PyErr_Occurred()) {
1714 retval = NULL;
1715 } else {
1716 Py_INCREF(Py_None);
1717 retval = Py_None;
1718 }
1719
1720 return retval;
1721}
1722
Christian Heimesbbe741d2008-03-28 10:53:29 +00001723/* Called when the connection is used as a context manager. Returns itself as a
1724 * convenience to the caller. */
1725static PyObject *
1726pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1727{
1728 Py_INCREF(self);
1729 return (PyObject*)self;
1730}
1731
1732/** Called when the connection is used as a context manager. If there was any
1733 * exception, a rollback takes place; otherwise we commit. */
1734static PyObject *
1735pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1736{
1737 PyObject* exc_type, *exc_value, *exc_tb;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001738 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001739 PyObject* result;
1740
1741 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1742 return NULL;
1743 }
1744
1745 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1746 method_name = "commit";
1747 } else {
1748 method_name = "rollback";
1749 }
1750
Victor Stinner3466bde2016-09-05 18:16:01 -07001751 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001752 if (!result) {
1753 return NULL;
1754 }
1755 Py_DECREF(result);
1756
1757 Py_RETURN_FALSE;
1758}
1759
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001760static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001761PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762
1763static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001764 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1765 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001766 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001767 {NULL}
1768};
1769
1770static PyMethodDef connection_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001771 {"cursor", (PyCFunction)(void(*)(void))pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001772 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001773 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001774 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001775 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001776 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001777 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001778 PyDoc_STR("Roll back the current transaction.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001779 {"create_function", (PyCFunction)(void(*)(void))pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001780 PyDoc_STR("Creates a new function. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001781 {"create_aggregate", (PyCFunction)(void(*)(void))pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001782 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001783 {"set_authorizer", (PyCFunction)(void(*)(void))pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001784 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001785 #ifdef HAVE_LOAD_EXTENSION
1786 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1787 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1788 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1789 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1790 #endif
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001791 {"set_progress_handler", (PyCFunction)(void(*)(void))pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001792 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001793 {"set_trace_callback", (PyCFunction)(void(*)(void))pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001794 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001795 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001796 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001797 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001798 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001799 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001800 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001801 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001802 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001803 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001804 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001805 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001806 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001807 #ifdef HAVE_BACKUP_API
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001808 {"backup", (PyCFunction)(void(*)(void))pysqlite_connection_backup, METH_VARARGS | METH_KEYWORDS,
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001809 PyDoc_STR("Makes a backup of the database. Non-standard.")},
1810 #endif
Christian Heimesbbe741d2008-03-28 10:53:29 +00001811 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1812 PyDoc_STR("For context manager. Non-standard.")},
1813 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1814 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001815 {NULL, NULL}
1816};
1817
1818static struct PyMemberDef connection_members[] =
1819{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001820 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1821 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1822 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1823 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1824 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1825 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1826 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1827 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1828 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1829 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001830 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1831 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001832 {NULL}
1833};
1834
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001835PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001836 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001837 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001838 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001839 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001840 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001841 0, /* tp_vectorcall_offset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001842 0, /* tp_getattr */
1843 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001844 0, /* tp_as_async */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001845 0, /* tp_repr */
1846 0, /* tp_as_number */
1847 0, /* tp_as_sequence */
1848 0, /* tp_as_mapping */
1849 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001850 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001851 0, /* tp_str */
1852 0, /* tp_getattro */
1853 0, /* tp_setattro */
1854 0, /* tp_as_buffer */
1855 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1856 connection_doc, /* tp_doc */
1857 0, /* tp_traverse */
1858 0, /* tp_clear */
1859 0, /* tp_richcompare */
1860 0, /* tp_weaklistoffset */
1861 0, /* tp_iter */
1862 0, /* tp_iternext */
1863 connection_methods, /* tp_methods */
1864 connection_members, /* tp_members */
1865 connection_getset, /* tp_getset */
1866 0, /* tp_base */
1867 0, /* tp_dict */
1868 0, /* tp_descr_get */
1869 0, /* tp_descr_set */
1870 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001871 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001872 0, /* tp_alloc */
1873 0, /* tp_new */
1874 0 /* tp_free */
1875};
1876
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001877extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001878{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001879 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1880 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001881}