blob: 351317e78db4fc285995a981b33799ad980e41e7 [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
Miss Islington (bot)e8a5a922018-03-10 14:29:19 -080044#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
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -080058static 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);
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -0800150 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) {
Miss Islington (bot)c3bdea42018-07-09 21:48:02 -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;
Miss Islington (bot)c3bdea42018-07-09 21:48:02 -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) {
Miss Islington (bot)c3bdea42018-07-09 21:48:02 -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) {
Miss Islington (bot)c3bdea42018-07-09 21:48:02 -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{
813 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
814
815 PyObject* func;
816 char* name;
817 int narg;
818 int rc;
819
Gerhard Häringf9cee222010-03-05 15:20:03 +0000820 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
821 return NULL;
822 }
823
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
825 &name, &narg, &func))
826 {
827 return NULL;
828 }
829
Serhiy Storchaka1de91a02018-12-05 23:09:56 +0200830 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1) {
831 return NULL;
832 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000833 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834
Thomas Wouters477c8d52006-05-27 19:21:47 +0000835 if (rc != SQLITE_OK) {
836 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000837 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000838 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839 }
Serhiy Storchaka1de91a02018-12-05 23:09:56 +0200840 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000841}
842
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000843PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000844{
845 PyObject* aggregate_class;
846
847 int n_arg;
848 char* name;
849 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
850 int rc;
851
Gerhard Häringf9cee222010-03-05 15:20:03 +0000852 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
853 return NULL;
854 }
855
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000856 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
857 kwlist, &name, &n_arg, &aggregate_class)) {
858 return NULL;
859 }
860
Serhiy Storchaka1de91a02018-12-05 23:09:56 +0200861 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1) {
862 return NULL;
863 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000864 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 +0000865 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000866 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000867 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000869 }
Serhiy Storchaka1de91a02018-12-05 23:09:56 +0200870 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000871}
872
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000873static 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 +0000874{
875 PyObject *ret;
876 int rc;
877 PyGILState_STATE gilstate;
878
879 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000880
Victor Stinnerd4095d92013-07-26 22:23:33 +0200881 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000882
Victor Stinnerd4095d92013-07-26 22:23:33 +0200883 if (ret == NULL) {
Miss Islington (bot)c3bdea42018-07-09 21:48:02 -0700884 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200885 PyErr_Print();
886 else
887 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200888
Victor Stinnerd4095d92013-07-26 22:23:33 +0200889 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200890 }
891 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200892 if (PyLong_Check(ret)) {
893 rc = _PyLong_AsInt(ret);
894 if (rc == -1 && PyErr_Occurred()) {
Miss Islington (bot)c3bdea42018-07-09 21:48:02 -0700895 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200896 PyErr_Print();
897 else
898 PyErr_Clear();
899 rc = SQLITE_DENY;
900 }
901 }
902 else {
903 rc = SQLITE_DENY;
904 }
905 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000906 }
907
908 PyGILState_Release(gilstate);
909 return rc;
910}
911
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000912static int _progress_handler(void* user_arg)
913{
914 int rc;
915 PyObject *ret;
916 PyGILState_STATE gilstate;
917
918 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100919 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000920
921 if (!ret) {
Miss Islington (bot)c3bdea42018-07-09 21:48:02 -0700922 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000923 PyErr_Print();
924 } else {
925 PyErr_Clear();
926 }
927
Mark Dickinson934896d2009-02-21 20:59:32 +0000928 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000929 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000930 } else {
931 rc = (int)PyObject_IsTrue(ret);
932 Py_DECREF(ret);
933 }
934
935 PyGILState_Release(gilstate);
936 return rc;
937}
938
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200939static void _trace_callback(void* user_arg, const char* statement_string)
940{
941 PyObject *py_statement = NULL;
942 PyObject *ret = NULL;
943
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200944 PyGILState_STATE gilstate;
945
946 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200947 py_statement = PyUnicode_DecodeUTF8(statement_string,
948 strlen(statement_string), "replace");
949 if (py_statement) {
950 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
951 Py_DECREF(py_statement);
952 }
953
954 if (ret) {
955 Py_DECREF(ret);
956 } else {
Miss Islington (bot)c3bdea42018-07-09 21:48:02 -0700957 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200958 PyErr_Print();
959 } else {
960 PyErr_Clear();
961 }
962 }
963
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200964 PyGILState_Release(gilstate);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200965}
966
Gerhard Häringf9cee222010-03-05 15:20:03 +0000967static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000968{
969 PyObject* authorizer_cb;
970
971 static char *kwlist[] = { "authorizer_callback", NULL };
972 int rc;
973
Gerhard Häringf9cee222010-03-05 15:20:03 +0000974 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
975 return NULL;
976 }
977
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000978 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
979 kwlist, &authorizer_cb)) {
980 return NULL;
981 }
982
Serhiy Storchaka1de91a02018-12-05 23:09:56 +0200983 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1) {
984 return NULL;
985 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000986 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000987 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000988 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000989 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000990 }
Serhiy Storchaka1de91a02018-12-05 23:09:56 +0200991 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000992}
993
Gerhard Häringf9cee222010-03-05 15:20:03 +0000994static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000995{
996 PyObject* progress_handler;
997 int n;
998
999 static char *kwlist[] = { "progress_handler", "n", NULL };
1000
Gerhard Häringf9cee222010-03-05 15:20:03 +00001001 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1002 return NULL;
1003 }
1004
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001005 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1006 kwlist, &progress_handler, &n)) {
1007 return NULL;
1008 }
1009
1010 if (progress_handler == Py_None) {
1011 /* None clears the progress handler previously set */
1012 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1013 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001014 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1015 return NULL;
Serhiy Storchaka1de91a02018-12-05 23:09:56 +02001016 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001017 }
1018
Berker Peksagfe21de92016-04-09 07:34:39 +03001019 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001020}
1021
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001022static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1023{
1024 PyObject* trace_callback;
1025
1026 static char *kwlist[] = { "trace_callback", NULL };
1027
1028 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1029 return NULL;
1030 }
1031
1032 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1033 kwlist, &trace_callback)) {
1034 return NULL;
1035 }
1036
1037 if (trace_callback == Py_None) {
1038 /* None clears the trace callback previously set */
1039 sqlite3_trace(self->db, 0, (void*)0);
1040 } else {
1041 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1042 return NULL;
1043 sqlite3_trace(self->db, _trace_callback, trace_callback);
1044 }
1045
Berker Peksagfe21de92016-04-09 07:34:39 +03001046 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001047}
1048
Gerhard Häringf9cee222010-03-05 15:20:03 +00001049#ifdef HAVE_LOAD_EXTENSION
1050static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1051{
1052 int rc;
1053 int onoff;
1054
1055 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1056 return NULL;
1057 }
1058
1059 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1060 return NULL;
1061 }
1062
1063 rc = sqlite3_enable_load_extension(self->db, onoff);
1064
1065 if (rc != SQLITE_OK) {
1066 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1067 return NULL;
1068 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001069 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001070 }
1071}
1072
1073static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1074{
1075 int rc;
1076 char* extension_name;
1077 char* errmsg;
1078
1079 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1080 return NULL;
1081 }
1082
1083 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1084 return NULL;
1085 }
1086
1087 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1088 if (rc != 0) {
1089 PyErr_SetString(pysqlite_OperationalError, errmsg);
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#endif
1096
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001097int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098{
1099 if (self->check_same_thread) {
1100 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001101 PyErr_Format(pysqlite_ProgrammingError,
Miss Islington (bot)00765bb2018-03-26 09:01:22 -07001102 "SQLite objects created in a thread can only be used in that same thread. "
1103 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001104 self->thread_ident, PyThread_get_thread_ident());
1105 return 0;
1106 }
1107
1108 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001109 return 1;
1110}
1111
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001112static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001113{
1114 Py_INCREF(self->isolation_level);
1115 return self->isolation_level;
1116}
1117
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001118static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001119{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001120 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001121 return NULL;
1122 } else {
1123 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1124 }
1125}
1126
Berker Peksag59da4b32016-09-12 07:16:43 +03001127static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1128{
1129 if (!pysqlite_check_connection(self)) {
1130 return NULL;
1131 }
1132 if (!sqlite3_get_autocommit(self->db)) {
1133 Py_RETURN_TRUE;
1134 }
1135 Py_RETURN_FALSE;
1136}
1137
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -08001138static int
1139pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001140{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001141 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001142 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143 if (!res) {
1144 return -1;
1145 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 Py_DECREF(res);
1147
Serhiy Storchaka28914922016-09-01 22:18:03 +03001148 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001150 const char * const *candidate;
1151 PyObject *uppercase_level;
1152 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001153
Serhiy Storchaka28914922016-09-01 22:18:03 +03001154 if (!PyUnicode_Check(isolation_level)) {
1155 PyErr_Format(PyExc_TypeError,
1156 "isolation_level must be a string or None, not %.100s",
1157 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001158 return -1;
1159 }
1160
Serhiy Storchaka28914922016-09-01 22:18:03 +03001161 uppercase_level = _PyObject_CallMethodIdObjArgs(
1162 (PyObject *)&PyUnicode_Type, &PyId_upper,
1163 isolation_level, NULL);
1164 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001165 return -1;
1166 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001167 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001168 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001169 break;
1170 }
1171 Py_DECREF(uppercase_level);
1172 if (!*candidate) {
1173 PyErr_SetString(PyExc_ValueError,
1174 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001175 return -1;
1176 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001177 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001178 }
1179
Serhiy Storchaka28914922016-09-01 22:18:03 +03001180 Py_INCREF(isolation_level);
1181 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001182 return 0;
1183}
1184
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001185PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001186{
1187 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001188 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001189 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001190 int rc;
1191
Gerhard Häringf9cee222010-03-05 15:20:03 +00001192 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1193 return NULL;
1194 }
1195
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001196 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001197 return NULL;
1198
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001199 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001200 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001201
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001202 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001203
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001204 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001205 if (!statement) {
1206 return NULL;
1207 }
1208
Victor Stinner0201f442010-03-13 03:28:34 +00001209 statement->db = NULL;
1210 statement->st = NULL;
1211 statement->sql = NULL;
1212 statement->in_use = 0;
1213 statement->in_weakreflist = NULL;
1214
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001215 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001216 if (rc != SQLITE_OK) {
1217 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001218 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001219 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001220 if (PyErr_ExceptionMatches(PyExc_TypeError))
1221 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001222 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001223 (void)pysqlite_statement_reset(statement);
1224 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001225 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001226 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227 }
1228
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001229 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1230 if (weakref == NULL)
1231 goto error;
1232 if (PyList_Append(self->statements, weakref) != 0) {
1233 Py_DECREF(weakref);
1234 goto error;
1235 }
1236 Py_DECREF(weakref);
1237
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001238 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001239
1240error:
1241 Py_DECREF(statement);
1242 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001243}
1244
Larry Hastings01b08832015-05-08 07:37:49 -07001245PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001246{
1247 PyObject* cursor = 0;
1248 PyObject* result = 0;
1249 PyObject* method = 0;
1250
Victor Stinner3466bde2016-09-05 18:16:01 -07001251 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 if (!cursor) {
1253 goto error;
1254 }
1255
1256 method = PyObject_GetAttrString(cursor, "execute");
1257 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001258 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259 goto error;
1260 }
1261
1262 result = PyObject_CallObject(method, args);
1263 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001264 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001265 }
1266
1267error:
1268 Py_XDECREF(result);
1269 Py_XDECREF(method);
1270
1271 return cursor;
1272}
1273
Larry Hastings01b08832015-05-08 07:37:49 -07001274PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275{
1276 PyObject* cursor = 0;
1277 PyObject* result = 0;
1278 PyObject* method = 0;
1279
Victor Stinner3466bde2016-09-05 18:16:01 -07001280 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001281 if (!cursor) {
1282 goto error;
1283 }
1284
1285 method = PyObject_GetAttrString(cursor, "executemany");
1286 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001287 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001288 goto error;
1289 }
1290
1291 result = PyObject_CallObject(method, args);
1292 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001293 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001294 }
1295
1296error:
1297 Py_XDECREF(result);
1298 Py_XDECREF(method);
1299
1300 return cursor;
1301}
1302
Larry Hastings01b08832015-05-08 07:37:49 -07001303PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001304{
1305 PyObject* cursor = 0;
1306 PyObject* result = 0;
1307 PyObject* method = 0;
1308
Victor Stinner3466bde2016-09-05 18:16:01 -07001309 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001310 if (!cursor) {
1311 goto error;
1312 }
1313
1314 method = PyObject_GetAttrString(cursor, "executescript");
1315 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001316 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001317 goto error;
1318 }
1319
1320 result = PyObject_CallObject(method, args);
1321 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001322 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001323 }
1324
1325error:
1326 Py_XDECREF(result);
1327 Py_XDECREF(method);
1328
1329 return cursor;
1330}
1331
1332/* ------------------------- COLLATION CODE ------------------------ */
1333
1334static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001335pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001336 void* context,
1337 int text1_length, const void* text1_data,
1338 int text2_length, const void* text2_data)
1339{
1340 PyObject* callback = (PyObject*)context;
1341 PyObject* string1 = 0;
1342 PyObject* string2 = 0;
1343 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001344 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001345 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001346 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001347 gilstate = PyGILState_Ensure();
1348
1349 if (PyErr_Occurred()) {
1350 goto finally;
1351 }
1352
Guido van Rossum98297ee2007-11-06 21:34:58 +00001353 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1354 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001355
1356 if (!string1 || !string2) {
1357 goto finally; /* failed to allocate strings */
1358 }
1359
1360 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1361
1362 if (!retval) {
1363 /* execution failed */
1364 goto finally;
1365 }
1366
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001367 longval = PyLong_AsLongAndOverflow(retval, &result);
1368 if (longval == -1 && PyErr_Occurred()) {
1369 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370 result = 0;
1371 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001372 else if (!result) {
1373 if (longval > 0)
1374 result = 1;
1375 else if (longval < 0)
1376 result = -1;
1377 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001378
1379finally:
1380 Py_XDECREF(string1);
1381 Py_XDECREF(string2);
1382 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001383 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001384 return result;
1385}
1386
1387static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001388pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001389{
1390 PyObject* retval = NULL;
1391
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001392 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001393 goto finally;
1394 }
1395
1396 sqlite3_interrupt(self->db);
1397
1398 Py_INCREF(Py_None);
1399 retval = Py_None;
1400
1401finally:
1402 return retval;
1403}
1404
Christian Heimesbbe741d2008-03-28 10:53:29 +00001405/* Function author: Paul Kippes <kippesp@gmail.com>
1406 * Class method of Connection to call the Python function _iterdump
1407 * of the sqlite3 module.
1408 */
1409static PyObject *
1410pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1411{
1412 PyObject* retval = NULL;
1413 PyObject* module = NULL;
1414 PyObject* module_dict;
1415 PyObject* pyfn_iterdump;
1416
1417 if (!pysqlite_check_connection(self)) {
1418 goto finally;
1419 }
1420
1421 module = PyImport_ImportModule(MODULE_NAME ".dump");
1422 if (!module) {
1423 goto finally;
1424 }
1425
1426 module_dict = PyModule_GetDict(module);
1427 if (!module_dict) {
1428 goto finally;
1429 }
1430
1431 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1432 if (!pyfn_iterdump) {
1433 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1434 goto finally;
1435 }
1436
1437 args = PyTuple_New(1);
1438 if (!args) {
1439 goto finally;
1440 }
1441 Py_INCREF(self);
1442 PyTuple_SetItem(args, 0, (PyObject*)self);
1443 retval = PyObject_CallObject(pyfn_iterdump, args);
1444
1445finally:
1446 Py_XDECREF(args);
1447 Py_XDECREF(module);
1448 return retval;
1449}
1450
Miss Islington (bot)e8a5a922018-03-10 14:29:19 -08001451#ifdef HAVE_BACKUP_API
1452static PyObject *
1453pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *kwds)
1454{
1455 PyObject *target = NULL;
1456 int pages = -1;
1457 PyObject *progress = Py_None;
1458 const char *name = "main";
1459 int rc;
1460 int callback_error = 0;
1461 double sleep_secs = 0.250;
1462 sqlite3 *bck_conn;
1463 sqlite3_backup *bck_handle;
1464 static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL};
1465
1466 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsd:backup", keywords,
1467 &pysqlite_ConnectionType, &target,
1468 &pages, &progress, &name, &sleep_secs)) {
1469 return NULL;
1470 }
1471
1472 if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
1473 return NULL;
1474 }
1475
1476 if ((pysqlite_Connection *)target == self) {
1477 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1478 return NULL;
1479 }
1480
Miss Islington (bot)429ca442018-03-17 23:24:33 -07001481#if SQLITE_VERSION_NUMBER < 3008008
1482 /* Since 3.8.8 this is already done, per commit
Miss Islington (bot)e8a5a922018-03-10 14:29:19 -08001483 https://www.sqlite.org/src/info/169b5505498c0a7e */
1484 if (!sqlite3_get_autocommit(((pysqlite_Connection *)target)->db)) {
1485 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1486 return NULL;
1487 }
1488#endif
1489
1490 if (progress != Py_None && !PyCallable_Check(progress)) {
1491 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1492 return NULL;
1493 }
1494
1495 if (pages == 0) {
1496 pages = -1;
1497 }
1498
1499 bck_conn = ((pysqlite_Connection *)target)->db;
1500
1501 Py_BEGIN_ALLOW_THREADS
1502 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1503 Py_END_ALLOW_THREADS
1504
1505 if (bck_handle) {
1506 do {
1507 Py_BEGIN_ALLOW_THREADS
1508 rc = sqlite3_backup_step(bck_handle, pages);
1509 Py_END_ALLOW_THREADS
1510
1511 if (progress != Py_None) {
1512 PyObject *res;
1513
1514 res = PyObject_CallFunction(progress, "iii", rc,
1515 sqlite3_backup_remaining(bck_handle),
1516 sqlite3_backup_pagecount(bck_handle));
1517 if (res == NULL) {
1518 /* User's callback raised an error: interrupt the loop and
1519 propagate it. */
1520 callback_error = 1;
1521 rc = -1;
1522 } else {
1523 Py_DECREF(res);
1524 }
1525 }
1526
1527 /* Sleep for a while if there are still further pages to copy and
1528 the engine could not make any progress */
1529 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1530 Py_BEGIN_ALLOW_THREADS
1531 sqlite3_sleep(sleep_secs * 1000.0);
1532 Py_END_ALLOW_THREADS
1533 }
1534 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1535
1536 Py_BEGIN_ALLOW_THREADS
1537 rc = sqlite3_backup_finish(bck_handle);
1538 Py_END_ALLOW_THREADS
1539 } else {
1540 rc = _pysqlite_seterror(bck_conn, NULL);
1541 }
1542
1543 if (!callback_error && rc != SQLITE_OK) {
1544 /* We cannot use _pysqlite_seterror() here because the backup APIs do
1545 not set the error status on the connection object, but rather on
1546 the backup handle. */
1547 if (rc == SQLITE_NOMEM) {
1548 (void)PyErr_NoMemory();
1549 } else {
1550#if SQLITE_VERSION_NUMBER > 3007015
1551 PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
1552#else
1553 switch (rc) {
Miss Islington (bot)c56bbae2018-09-20 10:20:32 -07001554 case SQLITE_ERROR:
1555 /* Description of SQLITE_ERROR in SQLite 3.7.14 and older
1556 releases. */
1557 PyErr_SetString(pysqlite_OperationalError,
1558 "SQL logic error or missing database");
1559 break;
Miss Islington (bot)e8a5a922018-03-10 14:29:19 -08001560 case SQLITE_READONLY:
1561 PyErr_SetString(pysqlite_OperationalError,
1562 "attempt to write a readonly database");
1563 break;
1564 case SQLITE_BUSY:
1565 PyErr_SetString(pysqlite_OperationalError, "database is locked");
1566 break;
1567 case SQLITE_LOCKED:
1568 PyErr_SetString(pysqlite_OperationalError,
1569 "database table is locked");
1570 break;
1571 default:
1572 PyErr_Format(pysqlite_OperationalError,
1573 "unrecognized error code: %d", rc);
1574 break;
1575 }
1576#endif
1577 }
1578 }
1579
1580 if (!callback_error && rc == SQLITE_OK) {
1581 Py_RETURN_NONE;
1582 } else {
1583 return NULL;
1584 }
1585}
1586#endif
1587
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001588static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001589pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001590{
1591 PyObject* callable;
1592 PyObject* uppercase_name = 0;
1593 PyObject* name;
1594 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001595 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001596 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001597 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001598 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001599 unsigned int kind;
1600 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001601
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001602 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001603 goto finally;
1604 }
1605
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001606 if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1607 &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001608 goto finally;
1609 }
1610
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001611 uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type,
1612 &PyId_upper, name, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001613 if (!uppercase_name) {
1614 goto finally;
1615 }
1616
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001617 if (PyUnicode_READY(uppercase_name))
1618 goto finally;
1619 len = PyUnicode_GET_LENGTH(uppercase_name);
1620 kind = PyUnicode_KIND(uppercase_name);
1621 data = PyUnicode_DATA(uppercase_name);
1622 for (i=0; i<len; i++) {
1623 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1624 if ((ch >= '0' && ch <= '9')
1625 || (ch >= 'A' && ch <= 'Z')
1626 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001627 {
Victor Stinner35466c52010-04-22 11:23:23 +00001628 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001629 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001630 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001631 goto finally;
1632 }
1633 }
1634
Serhiy Storchaka06515832016-11-20 09:13:07 +02001635 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001636 if (!uppercase_name_str)
1637 goto finally;
1638
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001639 if (callable != Py_None && !PyCallable_Check(callable)) {
1640 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1641 goto finally;
1642 }
1643
1644 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001645 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1646 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001647 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001648 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1649 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001650 }
1651
1652 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001653 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001654 SQLITE_UTF8,
1655 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001656 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001657 if (rc != SQLITE_OK) {
1658 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001659 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001660 goto finally;
1661 }
1662
1663finally:
1664 Py_XDECREF(uppercase_name);
1665
1666 if (PyErr_Occurred()) {
1667 retval = NULL;
1668 } else {
1669 Py_INCREF(Py_None);
1670 retval = Py_None;
1671 }
1672
1673 return retval;
1674}
1675
Christian Heimesbbe741d2008-03-28 10:53:29 +00001676/* Called when the connection is used as a context manager. Returns itself as a
1677 * convenience to the caller. */
1678static PyObject *
1679pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1680{
1681 Py_INCREF(self);
1682 return (PyObject*)self;
1683}
1684
1685/** Called when the connection is used as a context manager. If there was any
1686 * exception, a rollback takes place; otherwise we commit. */
1687static PyObject *
1688pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1689{
1690 PyObject* exc_type, *exc_value, *exc_tb;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001691 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001692 PyObject* result;
1693
1694 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1695 return NULL;
1696 }
1697
1698 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1699 method_name = "commit";
1700 } else {
1701 method_name = "rollback";
1702 }
1703
Victor Stinner3466bde2016-09-05 18:16:01 -07001704 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001705 if (!result) {
1706 return NULL;
1707 }
1708 Py_DECREF(result);
1709
1710 Py_RETURN_FALSE;
1711}
1712
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001713static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001714PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001715
1716static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001717 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1718 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001719 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001720 {NULL}
1721};
1722
1723static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001724 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001725 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001726 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001727 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001728 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001729 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001730 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001731 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001732 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001733 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001734 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001735 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001736 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001737 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001738 #ifdef HAVE_LOAD_EXTENSION
1739 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1740 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1741 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1742 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1743 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001744 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1745 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001746 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1747 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001748 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001749 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001750 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001751 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001752 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001753 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001754 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001755 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001756 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001757 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001758 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001759 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Miss Islington (bot)e8a5a922018-03-10 14:29:19 -08001760 #ifdef HAVE_BACKUP_API
1761 {"backup", (PyCFunction)pysqlite_connection_backup, METH_VARARGS | METH_KEYWORDS,
1762 PyDoc_STR("Makes a backup of the database. Non-standard.")},
1763 #endif
Christian Heimesbbe741d2008-03-28 10:53:29 +00001764 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1765 PyDoc_STR("For context manager. Non-standard.")},
1766 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1767 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001768 {NULL, NULL}
1769};
1770
1771static struct PyMemberDef connection_members[] =
1772{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001773 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1774 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1775 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1776 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1777 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1778 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1779 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1780 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1781 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1782 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001783 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1784 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001785 {NULL}
1786};
1787
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001788PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001789 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001790 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001791 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001792 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001793 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001794 0, /* tp_print */
1795 0, /* tp_getattr */
1796 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001797 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001798 0, /* tp_repr */
1799 0, /* tp_as_number */
1800 0, /* tp_as_sequence */
1801 0, /* tp_as_mapping */
1802 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001803 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001804 0, /* tp_str */
1805 0, /* tp_getattro */
1806 0, /* tp_setattro */
1807 0, /* tp_as_buffer */
1808 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1809 connection_doc, /* tp_doc */
1810 0, /* tp_traverse */
1811 0, /* tp_clear */
1812 0, /* tp_richcompare */
1813 0, /* tp_weaklistoffset */
1814 0, /* tp_iter */
1815 0, /* tp_iternext */
1816 connection_methods, /* tp_methods */
1817 connection_members, /* tp_members */
1818 connection_getset, /* tp_getset */
1819 0, /* tp_base */
1820 0, /* tp_dict */
1821 0, /* tp_descr_get */
1822 0, /* tp_descr_set */
1823 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001824 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001825 0, /* tp_alloc */
1826 0, /* tp_new */
1827 0 /* tp_free */
1828};
1829
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001830extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001831{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001832 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1833 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001834}