blob: e3340bf19f7bd369a022b3a315e38c15d20e8e28 [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
Oren Milman93c5a5d2017-10-10 22:27:46 +0300189 Py_XSETREF(self->function_pinboard, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 if (!self->function_pinboard) {
191 return -1;
192 }
193
Oren Milman93c5a5d2017-10-10 22:27:46 +0300194 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195 if (!self->collations) {
196 return -1;
197 }
198
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000199 self->Warning = pysqlite_Warning;
200 self->Error = pysqlite_Error;
201 self->InterfaceError = pysqlite_InterfaceError;
202 self->DatabaseError = pysqlite_DatabaseError;
203 self->DataError = pysqlite_DataError;
204 self->OperationalError = pysqlite_OperationalError;
205 self->IntegrityError = pysqlite_IntegrityError;
206 self->InternalError = pysqlite_InternalError;
207 self->ProgrammingError = pysqlite_ProgrammingError;
208 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209
210 return 0;
211}
212
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000213/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000214void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000216 int i;
217 PyObject* weakref;
218 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000219 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220
Thomas Wouters477c8d52006-05-27 19:21:47 +0000221 for (i = 0; i < PyList_Size(self->statements); i++) {
222 weakref = PyList_GetItem(self->statements, i);
223 statement = PyWeakref_GetObject(weakref);
224 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500225 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000226 if (action == ACTION_RESET) {
227 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
228 } else {
229 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
230 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500231 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000232 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000233 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000234
235 if (reset_cursors) {
236 for (i = 0; i < PyList_Size(self->cursors); i++) {
237 weakref = PyList_GetItem(self->cursors, i);
238 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
239 if ((PyObject*)cursor != Py_None) {
240 cursor->reset = 1;
241 }
242 }
243 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244}
245
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000246void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247{
248 Py_XDECREF(self->statement_cache);
249
250 /* Clean up if user has not called .close() explicitly. */
251 if (self->db) {
252 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200253 SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254 Py_END_ALLOW_THREADS
255 }
256
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257 Py_XDECREF(self->isolation_level);
258 Py_XDECREF(self->function_pinboard);
259 Py_XDECREF(self->row_factory);
260 Py_XDECREF(self->text_factory);
261 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000262 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000263 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264
Christian Heimes90aa7642007-12-19 02:45:37 +0000265 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266}
267
Gerhard Häringf9cee222010-03-05 15:20:03 +0000268/*
269 * Registers a cursor with the connection.
270 *
271 * 0 => error; 1 => ok
272 */
273int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
274{
275 PyObject* weakref;
276
277 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
278 if (!weakref) {
279 goto error;
280 }
281
282 if (PyList_Append(connection->cursors, weakref) != 0) {
283 Py_CLEAR(weakref);
284 goto error;
285 }
286
287 Py_DECREF(weakref);
288
289 return 1;
290error:
291 return 0;
292}
293
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000294PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000295{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300296 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000297 PyObject* factory = NULL;
298 PyObject* cursor;
299
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000300 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
301 &factory)) {
302 return NULL;
303 }
304
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000305 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000306 return NULL;
307 }
308
309 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000310 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000311 }
312
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300313 cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL);
314 if (cursor == NULL)
315 return NULL;
316 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
317 PyErr_Format(PyExc_TypeError,
318 "factory must return a cursor, not %.100s",
319 Py_TYPE(cursor)->tp_name);
320 Py_DECREF(cursor);
321 return NULL;
322 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323
Gerhard Häringf9cee222010-03-05 15:20:03 +0000324 _pysqlite_drop_unused_cursor_references(self);
325
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300328 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000329 }
330
331 return cursor;
332}
333
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000334PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335{
336 int rc;
337
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000338 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 return NULL;
340 }
341
Gerhard Häringf9cee222010-03-05 15:20:03 +0000342 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343
344 if (self->db) {
345 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200346 rc = SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347 Py_END_ALLOW_THREADS
348
349 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000350 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351 return NULL;
352 } else {
353 self->db = NULL;
354 }
355 }
356
Berker Peksagfe21de92016-04-09 07:34:39 +0300357 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358}
359
360/*
361 * Checks if a connection object is usable (i. e. not closed).
362 *
363 * 0 => error; 1 => ok
364 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000365int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000367 if (!con->initialized) {
368 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
369 return 0;
370 }
371
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000373 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374 return 0;
375 } else {
376 return 1;
377 }
378}
379
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000380PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381{
382 int rc;
383 const char* tail;
384 sqlite3_stmt* statement;
385
386 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700387 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388 Py_END_ALLOW_THREADS
389
390 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000391 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 goto error;
393 }
394
Benjamin Petersond7b03282008-09-13 15:58:53 +0000395 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300396 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000397 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000398 }
399
400 Py_BEGIN_ALLOW_THREADS
401 rc = sqlite3_finalize(statement);
402 Py_END_ALLOW_THREADS
403
404 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000405 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406 }
407
408error:
409 if (PyErr_Occurred()) {
410 return NULL;
411 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200412 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413 }
414}
415
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000416PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000417{
418 int rc;
419 const char* tail;
420 sqlite3_stmt* statement;
421
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000422 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423 return NULL;
424 }
425
Berker Peksag59da4b32016-09-12 07:16:43 +0300426 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000427
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000428 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700429 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430 Py_END_ALLOW_THREADS
431 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000432 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000433 goto error;
434 }
435
Benjamin Petersond7b03282008-09-13 15:58:53 +0000436 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300437 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000438 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439 }
440
441 Py_BEGIN_ALLOW_THREADS
442 rc = sqlite3_finalize(statement);
443 Py_END_ALLOW_THREADS
444 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000445 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446 }
447
448 }
449
450error:
451 if (PyErr_Occurred()) {
452 return NULL;
453 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200454 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455 }
456}
457
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000458PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000459{
460 int rc;
461 const char* tail;
462 sqlite3_stmt* statement;
463
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000464 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000465 return NULL;
466 }
467
Berker Peksag59da4b32016-09-12 07:16:43 +0300468 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000469 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000470
471 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700472 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000473 Py_END_ALLOW_THREADS
474 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000475 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000476 goto error;
477 }
478
Benjamin Petersond7b03282008-09-13 15:58:53 +0000479 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300480 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000481 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000482 }
483
484 Py_BEGIN_ALLOW_THREADS
485 rc = sqlite3_finalize(statement);
486 Py_END_ALLOW_THREADS
487 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000488 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000489 }
490
491 }
492
493error:
494 if (PyErr_Occurred()) {
495 return NULL;
496 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200497 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000498 }
499}
500
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200501static int
502_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200504 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000506 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200507 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
508 if (value == -1 && PyErr_Occurred())
509 return -1;
510 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511 } else if (PyFloat_Check(py_val)) {
512 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000513 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200514 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200515 if (str == NULL)
516 return -1;
517 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000518 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200519 Py_buffer view;
520 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100521 PyErr_SetString(PyExc_ValueError,
522 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200523 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000524 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200525 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100526 PyErr_SetString(PyExc_OverflowError,
527 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200528 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100529 return -1;
530 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200531 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
532 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200534 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200536 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537}
538
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000539PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000540{
541 PyObject* args;
542 int i;
543 sqlite3_value* cur_value;
544 PyObject* cur_py_value;
545 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000547
548 args = PyTuple_New(argc);
549 if (!args) {
550 return NULL;
551 }
552
553 for (i = 0; i < argc; i++) {
554 cur_value = argv[i];
555 switch (sqlite3_value_type(argv[i])) {
556 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200557 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000558 break;
559 case SQLITE_FLOAT:
560 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
561 break;
562 case SQLITE_TEXT:
563 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000564 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000565 /* TODO: have a way to show errors here */
566 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000567 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000568 Py_INCREF(Py_None);
569 cur_py_value = Py_None;
570 }
571 break;
572 case SQLITE_BLOB:
573 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000574 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000575 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000576 break;
577 case SQLITE_NULL:
578 default:
579 Py_INCREF(Py_None);
580 cur_py_value = Py_None;
581 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000582
583 if (!cur_py_value) {
584 Py_DECREF(args);
585 return NULL;
586 }
587
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000588 PyTuple_SetItem(args, i, cur_py_value);
589
590 }
591
592 return args;
593}
594
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000595void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596{
597 PyObject* args;
598 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200600 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000601
602 PyGILState_STATE threadstate;
603
604 threadstate = PyGILState_Ensure();
605
606 py_func = (PyObject*)sqlite3_user_data(context);
607
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000608 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000609 if (args) {
610 py_retval = PyObject_CallObject(py_func, args);
611 Py_DECREF(args);
612 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000613
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200614 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000615 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200616 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200618 }
619 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700620 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000621 PyErr_Print();
622 } else {
623 PyErr_Clear();
624 }
625 _sqlite3_result_error(context, "user-defined function raised exception", -1);
626 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000627
628 PyGILState_Release(threadstate);
629}
630
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000631static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000632{
633 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000634 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000635 PyObject* aggregate_class;
636 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000637 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000638
639 PyGILState_STATE threadstate;
640
641 threadstate = PyGILState_Ensure();
642
643 aggregate_class = (PyObject*)sqlite3_user_data(context);
644
645 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
646
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200647 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100648 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000649
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700652 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000653 PyErr_Print();
654 } else {
655 PyErr_Clear();
656 }
657 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000659 }
660 }
661
662 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000663 if (!stepmethod) {
664 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000665 }
666
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000667 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000668 if (!args) {
669 goto error;
670 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671
672 function_result = PyObject_CallObject(stepmethod, args);
673 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700676 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000677 PyErr_Print();
678 } else {
679 PyErr_Clear();
680 }
681 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000682 }
683
Thomas Wouters477c8d52006-05-27 19:21:47 +0000684error:
685 Py_XDECREF(stepmethod);
686 Py_XDECREF(function_result);
687
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688 PyGILState_Release(threadstate);
689}
690
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000691void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200693 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200695 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200696 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200697 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200698 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000699
700 PyGILState_STATE threadstate;
701
702 threadstate = PyGILState_Ensure();
703
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
705 if (!*aggregate_instance) {
706 /* this branch is executed if there was an exception in the aggregate's
707 * __init__ */
708
Thomas Wouters477c8d52006-05-27 19:21:47 +0000709 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 }
711
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200712 /* Keep the exception (if any) of the last call to step() */
713 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200714 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200715
Victor Stinner3466bde2016-09-05 18:16:01 -0700716 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200717
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200718 Py_DECREF(*aggregate_instance);
719
720 ok = 0;
721 if (function_result) {
722 ok = _pysqlite_set_result(context, function_result) == 0;
723 Py_DECREF(function_result);
724 }
725 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700726 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000727 PyErr_Print();
728 } else {
729 PyErr_Clear();
730 }
731 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200732#if SQLITE_VERSION_NUMBER < 3003003
733 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
734 exception, so don't restore the previous exception */
735 restore = 0;
736#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000737 }
738
Victor Stinnerffff7632013-08-02 01:48:10 +0200739 if (restore) {
740 /* Restore the exception (if any) of the last call to step(),
741 but clear also the current exception if finalize() failed */
742 PyErr_Restore(exception, value, tb);
743 }
Victor Stinner3a857322013-07-22 08:34:32 +0200744
Thomas Wouters477c8d52006-05-27 19:21:47 +0000745error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000746 PyGILState_Release(threadstate);
747}
748
Gerhard Häringf9cee222010-03-05 15:20:03 +0000749static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000750{
751 PyObject* new_list;
752 PyObject* weakref;
753 int i;
754
755 /* we only need to do this once in a while */
756 if (self->created_statements++ < 200) {
757 return;
758 }
759
760 self->created_statements = 0;
761
762 new_list = PyList_New(0);
763 if (!new_list) {
764 return;
765 }
766
767 for (i = 0; i < PyList_Size(self->statements); i++) {
768 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000769 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000770 if (PyList_Append(new_list, weakref) != 0) {
771 Py_DECREF(new_list);
772 return;
773 }
774 }
775 }
776
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300777 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000778}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000779
Gerhard Häringf9cee222010-03-05 15:20:03 +0000780static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
781{
782 PyObject* new_list;
783 PyObject* weakref;
784 int i;
785
786 /* we only need to do this once in a while */
787 if (self->created_cursors++ < 200) {
788 return;
789 }
790
791 self->created_cursors = 0;
792
793 new_list = PyList_New(0);
794 if (!new_list) {
795 return;
796 }
797
798 for (i = 0; i < PyList_Size(self->cursors); i++) {
799 weakref = PyList_GetItem(self->cursors, i);
800 if (PyWeakref_GetObject(weakref) != Py_None) {
801 if (PyList_Append(new_list, weakref) != 0) {
802 Py_DECREF(new_list);
803 return;
804 }
805 }
806 }
807
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300808 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000809}
810
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000811PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000812{
Sergey Fedoseev08308582018-07-08 12:09:20 +0500813 static char *kwlist[] = {"name", "narg", "func", "deterministic", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000814
815 PyObject* func;
816 char* name;
817 int narg;
818 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500819 int deterministic = 0;
820 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000821
Gerhard Häringf9cee222010-03-05 15:20:03 +0000822 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
823 return NULL;
824 }
825
Sergey Fedoseev08308582018-07-08 12:09:20 +0500826 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|$p", kwlist,
827 &name, &narg, &func, &deterministic))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000828 {
829 return NULL;
830 }
831
Sergey Fedoseev08308582018-07-08 12:09:20 +0500832 if (deterministic) {
833#if SQLITE_VERSION_NUMBER < 3008003
834 PyErr_SetString(pysqlite_NotSupportedError,
835 "deterministic=True requires SQLite 3.8.3 or higher");
836 return NULL;
837#else
838 if (sqlite3_libversion_number() < 3008003) {
839 PyErr_SetString(pysqlite_NotSupportedError,
840 "deterministic=True requires SQLite 3.8.3 or higher");
841 return NULL;
842 }
843 flags |= SQLITE_DETERMINISTIC;
844#endif
845 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500846 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1) {
847 return NULL;
848 }
Sergey Fedoseev08308582018-07-08 12:09:20 +0500849 rc = sqlite3_create_function(self->db,
850 name,
851 narg,
852 flags,
853 (void*)func,
854 _pysqlite_func_callback,
855 NULL,
856 NULL);
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 }
883
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500884 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1) {
885 return NULL;
886 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000887 rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000888 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000889 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000890 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000891 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000892 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500893 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000894}
895
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000896static 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 +0000897{
898 PyObject *ret;
899 int rc;
900 PyGILState_STATE gilstate;
901
902 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000903
Victor Stinnerd4095d92013-07-26 22:23:33 +0200904 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000905
Victor Stinnerd4095d92013-07-26 22:23:33 +0200906 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700907 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200908 PyErr_Print();
909 else
910 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200911
Victor Stinnerd4095d92013-07-26 22:23:33 +0200912 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200913 }
914 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200915 if (PyLong_Check(ret)) {
916 rc = _PyLong_AsInt(ret);
917 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700918 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200919 PyErr_Print();
920 else
921 PyErr_Clear();
922 rc = SQLITE_DENY;
923 }
924 }
925 else {
926 rc = SQLITE_DENY;
927 }
928 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000929 }
930
931 PyGILState_Release(gilstate);
932 return rc;
933}
934
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000935static int _progress_handler(void* user_arg)
936{
937 int rc;
938 PyObject *ret;
939 PyGILState_STATE gilstate;
940
941 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100942 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000943
944 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700945 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000946 PyErr_Print();
947 } else {
948 PyErr_Clear();
949 }
950
Mark Dickinson934896d2009-02-21 20:59:32 +0000951 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000952 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000953 } else {
954 rc = (int)PyObject_IsTrue(ret);
955 Py_DECREF(ret);
956 }
957
958 PyGILState_Release(gilstate);
959 return rc;
960}
961
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200962static void _trace_callback(void* user_arg, const char* statement_string)
963{
964 PyObject *py_statement = NULL;
965 PyObject *ret = NULL;
966
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200967 PyGILState_STATE gilstate;
968
969 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200970 py_statement = PyUnicode_DecodeUTF8(statement_string,
971 strlen(statement_string), "replace");
972 if (py_statement) {
973 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
974 Py_DECREF(py_statement);
975 }
976
977 if (ret) {
978 Py_DECREF(ret);
979 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700980 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200981 PyErr_Print();
982 } else {
983 PyErr_Clear();
984 }
985 }
986
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200987 PyGILState_Release(gilstate);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200988}
989
Gerhard Häringf9cee222010-03-05 15:20:03 +0000990static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000991{
992 PyObject* authorizer_cb;
993
994 static char *kwlist[] = { "authorizer_callback", NULL };
995 int rc;
996
Gerhard Häringf9cee222010-03-05 15:20:03 +0000997 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
998 return NULL;
999 }
1000
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001001 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1002 kwlist, &authorizer_cb)) {
1003 return NULL;
1004 }
1005
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001006 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1) {
1007 return NULL;
1008 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001009 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");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001012 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001013 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001014 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001015}
1016
Gerhard Häringf9cee222010-03-05 15:20:03 +00001017static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001018{
1019 PyObject* progress_handler;
1020 int n;
1021
1022 static char *kwlist[] = { "progress_handler", "n", NULL };
1023
Gerhard Häringf9cee222010-03-05 15:20:03 +00001024 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1025 return NULL;
1026 }
1027
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001028 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1029 kwlist, &progress_handler, &n)) {
1030 return NULL;
1031 }
1032
1033 if (progress_handler == Py_None) {
1034 /* None clears the progress handler previously set */
1035 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1036 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001037 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1038 return NULL;
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001039 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001040 }
1041
Berker Peksagfe21de92016-04-09 07:34:39 +03001042 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001043}
1044
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001045static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1046{
1047 PyObject* trace_callback;
1048
1049 static char *kwlist[] = { "trace_callback", NULL };
1050
1051 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1052 return NULL;
1053 }
1054
1055 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1056 kwlist, &trace_callback)) {
1057 return NULL;
1058 }
1059
1060 if (trace_callback == Py_None) {
1061 /* None clears the trace callback previously set */
1062 sqlite3_trace(self->db, 0, (void*)0);
1063 } else {
1064 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1065 return NULL;
1066 sqlite3_trace(self->db, _trace_callback, trace_callback);
1067 }
1068
Berker Peksagfe21de92016-04-09 07:34:39 +03001069 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001070}
1071
Gerhard Häringf9cee222010-03-05 15:20:03 +00001072#ifdef HAVE_LOAD_EXTENSION
1073static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1074{
1075 int rc;
1076 int onoff;
1077
1078 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1079 return NULL;
1080 }
1081
1082 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1083 return NULL;
1084 }
1085
1086 rc = sqlite3_enable_load_extension(self->db, onoff);
1087
1088 if (rc != SQLITE_OK) {
1089 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1090 return NULL;
1091 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001092 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001093 }
1094}
1095
1096static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1097{
1098 int rc;
1099 char* extension_name;
1100 char* errmsg;
1101
1102 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1103 return NULL;
1104 }
1105
1106 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1107 return NULL;
1108 }
1109
1110 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1111 if (rc != 0) {
1112 PyErr_SetString(pysqlite_OperationalError, errmsg);
1113 return NULL;
1114 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001115 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001116 }
1117}
1118#endif
1119
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001120int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001121{
1122 if (self->check_same_thread) {
1123 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001124 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001125 "SQLite objects created in a thread can only be used in that same thread. "
1126 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127 self->thread_ident, PyThread_get_thread_ident());
1128 return 0;
1129 }
1130
1131 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001132 return 1;
1133}
1134
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001135static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001136{
1137 Py_INCREF(self->isolation_level);
1138 return self->isolation_level;
1139}
1140
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001141static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001142{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001143 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 return NULL;
1145 } else {
1146 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1147 }
1148}
1149
Berker Peksag59da4b32016-09-12 07:16:43 +03001150static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1151{
1152 if (!pysqlite_check_connection(self)) {
1153 return NULL;
1154 }
1155 if (!sqlite3_get_autocommit(self->db)) {
1156 Py_RETURN_TRUE;
1157 }
1158 Py_RETURN_FALSE;
1159}
1160
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001161static int
1162pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001164 if (isolation_level == NULL) {
1165 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1166 return -1;
1167 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001168 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001169 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170 if (!res) {
1171 return -1;
1172 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173 Py_DECREF(res);
1174
Serhiy Storchaka28914922016-09-01 22:18:03 +03001175 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001176 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001177 const char * const *candidate;
1178 PyObject *uppercase_level;
1179 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001180
Serhiy Storchaka28914922016-09-01 22:18:03 +03001181 if (!PyUnicode_Check(isolation_level)) {
1182 PyErr_Format(PyExc_TypeError,
1183 "isolation_level must be a string or None, not %.100s",
1184 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185 return -1;
1186 }
1187
Serhiy Storchaka28914922016-09-01 22:18:03 +03001188 uppercase_level = _PyObject_CallMethodIdObjArgs(
1189 (PyObject *)&PyUnicode_Type, &PyId_upper,
1190 isolation_level, NULL);
1191 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001192 return -1;
1193 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001194 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001195 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001196 break;
1197 }
1198 Py_DECREF(uppercase_level);
1199 if (!*candidate) {
1200 PyErr_SetString(PyExc_ValueError,
1201 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001202 return -1;
1203 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001204 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001205 }
1206
Serhiy Storchaka28914922016-09-01 22:18:03 +03001207 Py_INCREF(isolation_level);
1208 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001209 return 0;
1210}
1211
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001212PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001213{
1214 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001215 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001216 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001217 int rc;
1218
Gerhard Häringf9cee222010-03-05 15:20:03 +00001219 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1220 return NULL;
1221 }
1222
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001223 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001224 return NULL;
1225
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001226 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001229 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001230
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001231 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001232 if (!statement) {
1233 return NULL;
1234 }
1235
Victor Stinner0201f442010-03-13 03:28:34 +00001236 statement->db = NULL;
1237 statement->st = NULL;
1238 statement->sql = NULL;
1239 statement->in_use = 0;
1240 statement->in_weakreflist = NULL;
1241
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001242 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001243 if (rc != SQLITE_OK) {
1244 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001245 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001246 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001247 if (PyErr_ExceptionMatches(PyExc_TypeError))
1248 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001249 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001250 (void)pysqlite_statement_reset(statement);
1251 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001253 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001254 }
1255
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001256 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1257 if (weakref == NULL)
1258 goto error;
1259 if (PyList_Append(self->statements, weakref) != 0) {
1260 Py_DECREF(weakref);
1261 goto error;
1262 }
1263 Py_DECREF(weakref);
1264
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001265 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001266
1267error:
1268 Py_DECREF(statement);
1269 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270}
1271
Larry Hastings01b08832015-05-08 07:37:49 -07001272PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001273{
1274 PyObject* cursor = 0;
1275 PyObject* result = 0;
1276 PyObject* method = 0;
1277
Victor Stinner3466bde2016-09-05 18:16:01 -07001278 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001279 if (!cursor) {
1280 goto error;
1281 }
1282
1283 method = PyObject_GetAttrString(cursor, "execute");
1284 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001285 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001286 goto error;
1287 }
1288
1289 result = PyObject_CallObject(method, args);
1290 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001291 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001292 }
1293
1294error:
1295 Py_XDECREF(result);
1296 Py_XDECREF(method);
1297
1298 return cursor;
1299}
1300
Larry Hastings01b08832015-05-08 07:37:49 -07001301PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001302{
1303 PyObject* cursor = 0;
1304 PyObject* result = 0;
1305 PyObject* method = 0;
1306
Victor Stinner3466bde2016-09-05 18:16:01 -07001307 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001308 if (!cursor) {
1309 goto error;
1310 }
1311
1312 method = PyObject_GetAttrString(cursor, "executemany");
1313 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001314 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001315 goto error;
1316 }
1317
1318 result = PyObject_CallObject(method, args);
1319 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001320 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001321 }
1322
1323error:
1324 Py_XDECREF(result);
1325 Py_XDECREF(method);
1326
1327 return cursor;
1328}
1329
Larry Hastings01b08832015-05-08 07:37:49 -07001330PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001331{
1332 PyObject* cursor = 0;
1333 PyObject* result = 0;
1334 PyObject* method = 0;
1335
Victor Stinner3466bde2016-09-05 18:16:01 -07001336 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001337 if (!cursor) {
1338 goto error;
1339 }
1340
1341 method = PyObject_GetAttrString(cursor, "executescript");
1342 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001343 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001344 goto error;
1345 }
1346
1347 result = PyObject_CallObject(method, args);
1348 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001349 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001350 }
1351
1352error:
1353 Py_XDECREF(result);
1354 Py_XDECREF(method);
1355
1356 return cursor;
1357}
1358
1359/* ------------------------- COLLATION CODE ------------------------ */
1360
1361static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001362pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001363 void* context,
1364 int text1_length, const void* text1_data,
1365 int text2_length, const void* text2_data)
1366{
1367 PyObject* callback = (PyObject*)context;
1368 PyObject* string1 = 0;
1369 PyObject* string2 = 0;
1370 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001371 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001372 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001374 gilstate = PyGILState_Ensure();
1375
1376 if (PyErr_Occurred()) {
1377 goto finally;
1378 }
1379
Guido van Rossum98297ee2007-11-06 21:34:58 +00001380 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1381 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001382
1383 if (!string1 || !string2) {
1384 goto finally; /* failed to allocate strings */
1385 }
1386
1387 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1388
1389 if (!retval) {
1390 /* execution failed */
1391 goto finally;
1392 }
1393
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001394 longval = PyLong_AsLongAndOverflow(retval, &result);
1395 if (longval == -1 && PyErr_Occurred()) {
1396 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001397 result = 0;
1398 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001399 else if (!result) {
1400 if (longval > 0)
1401 result = 1;
1402 else if (longval < 0)
1403 result = -1;
1404 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001405
1406finally:
1407 Py_XDECREF(string1);
1408 Py_XDECREF(string2);
1409 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001410 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411 return result;
1412}
1413
1414static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001415pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001416{
1417 PyObject* retval = NULL;
1418
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001419 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001420 goto finally;
1421 }
1422
1423 sqlite3_interrupt(self->db);
1424
1425 Py_INCREF(Py_None);
1426 retval = Py_None;
1427
1428finally:
1429 return retval;
1430}
1431
Christian Heimesbbe741d2008-03-28 10:53:29 +00001432/* Function author: Paul Kippes <kippesp@gmail.com>
1433 * Class method of Connection to call the Python function _iterdump
1434 * of the sqlite3 module.
1435 */
1436static PyObject *
1437pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1438{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001439 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001440 PyObject* retval = NULL;
1441 PyObject* module = NULL;
1442 PyObject* module_dict;
1443 PyObject* pyfn_iterdump;
1444
1445 if (!pysqlite_check_connection(self)) {
1446 goto finally;
1447 }
1448
1449 module = PyImport_ImportModule(MODULE_NAME ".dump");
1450 if (!module) {
1451 goto finally;
1452 }
1453
1454 module_dict = PyModule_GetDict(module);
1455 if (!module_dict) {
1456 goto finally;
1457 }
1458
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001459 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001460 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001461 if (!PyErr_Occurred()) {
1462 PyErr_SetString(pysqlite_OperationalError,
1463 "Failed to obtain _iterdump() reference");
1464 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001465 goto finally;
1466 }
1467
1468 args = PyTuple_New(1);
1469 if (!args) {
1470 goto finally;
1471 }
1472 Py_INCREF(self);
1473 PyTuple_SetItem(args, 0, (PyObject*)self);
1474 retval = PyObject_CallObject(pyfn_iterdump, args);
1475
1476finally:
1477 Py_XDECREF(args);
1478 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
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001658 uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type,
1659 &PyId_upper, name, NULL);
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 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001841 0, /* tp_print */
1842 0, /* tp_getattr */
1843 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001844 0, /* tp_reserved */
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}