blob: ef2daeb0c25763ea2261871a7dc1ce2ba52af662 [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
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000058static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
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);
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100150 if (pysqlite_connection_set_isolation_level(self, isolation_level) < 0) {
151 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) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000620 if (_enable_callback_tracebacks) {
621 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;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000652 if (_enable_callback_tracebacks) {
653 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) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000676 if (_enable_callback_tracebacks) {
677 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) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000726 if (_enable_callback_tracebacks) {
727 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
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000830 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832 if (rc != SQLITE_OK) {
833 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000834 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000835 return NULL;
836 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000837 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
838 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839
Berker Peksagfe21de92016-04-09 07:34:39 +0300840 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000842}
843
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000844PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000845{
846 PyObject* aggregate_class;
847
848 int n_arg;
849 char* name;
850 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
851 int rc;
852
Gerhard Häringf9cee222010-03-05 15:20:03 +0000853 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
854 return NULL;
855 }
856
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000857 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
858 kwlist, &name, &n_arg, &aggregate_class)) {
859 return NULL;
860 }
861
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000862 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 +0000863 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000864 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000865 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866 return NULL;
867 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000868 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
869 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000870
Berker Peksagfe21de92016-04-09 07:34:39 +0300871 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000872 }
873}
874
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000875static 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 +0000876{
877 PyObject *ret;
878 int rc;
879 PyGILState_STATE gilstate;
880
881 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000882
Victor Stinnerd4095d92013-07-26 22:23:33 +0200883 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000884
Victor Stinnerd4095d92013-07-26 22:23:33 +0200885 if (ret == NULL) {
886 if (_enable_callback_tracebacks)
887 PyErr_Print();
888 else
889 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200890
Victor Stinnerd4095d92013-07-26 22:23:33 +0200891 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200892 }
893 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200894 if (PyLong_Check(ret)) {
895 rc = _PyLong_AsInt(ret);
896 if (rc == -1 && PyErr_Occurred()) {
897 if (_enable_callback_tracebacks)
898 PyErr_Print();
899 else
900 PyErr_Clear();
901 rc = SQLITE_DENY;
902 }
903 }
904 else {
905 rc = SQLITE_DENY;
906 }
907 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000908 }
909
910 PyGILState_Release(gilstate);
911 return rc;
912}
913
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000914static int _progress_handler(void* user_arg)
915{
916 int rc;
917 PyObject *ret;
918 PyGILState_STATE gilstate;
919
920 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100921 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000922
923 if (!ret) {
924 if (_enable_callback_tracebacks) {
925 PyErr_Print();
926 } else {
927 PyErr_Clear();
928 }
929
Mark Dickinson934896d2009-02-21 20:59:32 +0000930 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000931 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000932 } else {
933 rc = (int)PyObject_IsTrue(ret);
934 Py_DECREF(ret);
935 }
936
937 PyGILState_Release(gilstate);
938 return rc;
939}
940
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200941static void _trace_callback(void* user_arg, const char* statement_string)
942{
943 PyObject *py_statement = NULL;
944 PyObject *ret = NULL;
945
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200946 PyGILState_STATE gilstate;
947
948 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200949 py_statement = PyUnicode_DecodeUTF8(statement_string,
950 strlen(statement_string), "replace");
951 if (py_statement) {
952 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
953 Py_DECREF(py_statement);
954 }
955
956 if (ret) {
957 Py_DECREF(ret);
958 } else {
959 if (_enable_callback_tracebacks) {
960 PyErr_Print();
961 } else {
962 PyErr_Clear();
963 }
964 }
965
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200966 PyGILState_Release(gilstate);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200967}
968
Gerhard Häringf9cee222010-03-05 15:20:03 +0000969static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000970{
971 PyObject* authorizer_cb;
972
973 static char *kwlist[] = { "authorizer_callback", NULL };
974 int rc;
975
Gerhard Häringf9cee222010-03-05 15:20:03 +0000976 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
977 return NULL;
978 }
979
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000980 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
981 kwlist, &authorizer_cb)) {
982 return NULL;
983 }
984
985 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
986
987 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;
990 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000991 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
992 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000993
Berker Peksagfe21de92016-04-09 07:34:39 +0300994 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000995 }
996}
997
Gerhard Häringf9cee222010-03-05 15:20:03 +0000998static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000999{
1000 PyObject* progress_handler;
1001 int n;
1002
1003 static char *kwlist[] = { "progress_handler", "n", NULL };
1004
Gerhard Häringf9cee222010-03-05 15:20:03 +00001005 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1006 return NULL;
1007 }
1008
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001009 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1010 kwlist, &progress_handler, &n)) {
1011 return NULL;
1012 }
1013
1014 if (progress_handler == Py_None) {
1015 /* None clears the progress handler previously set */
1016 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1017 } else {
1018 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001019 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1020 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001021 }
1022
Berker Peksagfe21de92016-04-09 07:34:39 +03001023 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001024}
1025
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001026static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1027{
1028 PyObject* trace_callback;
1029
1030 static char *kwlist[] = { "trace_callback", NULL };
1031
1032 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1033 return NULL;
1034 }
1035
1036 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1037 kwlist, &trace_callback)) {
1038 return NULL;
1039 }
1040
1041 if (trace_callback == Py_None) {
1042 /* None clears the trace callback previously set */
1043 sqlite3_trace(self->db, 0, (void*)0);
1044 } else {
1045 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1046 return NULL;
1047 sqlite3_trace(self->db, _trace_callback, trace_callback);
1048 }
1049
Berker Peksagfe21de92016-04-09 07:34:39 +03001050 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001051}
1052
Gerhard Häringf9cee222010-03-05 15:20:03 +00001053#ifdef HAVE_LOAD_EXTENSION
1054static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1055{
1056 int rc;
1057 int onoff;
1058
1059 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1060 return NULL;
1061 }
1062
1063 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1064 return NULL;
1065 }
1066
1067 rc = sqlite3_enable_load_extension(self->db, onoff);
1068
1069 if (rc != SQLITE_OK) {
1070 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1071 return NULL;
1072 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001073 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001074 }
1075}
1076
1077static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1078{
1079 int rc;
1080 char* extension_name;
1081 char* errmsg;
1082
1083 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1084 return NULL;
1085 }
1086
1087 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1088 return NULL;
1089 }
1090
1091 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1092 if (rc != 0) {
1093 PyErr_SetString(pysqlite_OperationalError, errmsg);
1094 return NULL;
1095 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001096 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001097 }
1098}
1099#endif
1100
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001101int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001102{
1103 if (self->check_same_thread) {
1104 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001105 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001106 "SQLite objects created in a thread can only be used in that same thread. "
1107 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001108 self->thread_ident, PyThread_get_thread_ident());
1109 return 0;
1110 }
1111
1112 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001113 return 1;
1114}
1115
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001116static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001117{
1118 Py_INCREF(self->isolation_level);
1119 return self->isolation_level;
1120}
1121
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001122static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001123{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001124 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 return NULL;
1126 } else {
1127 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1128 }
1129}
1130
Berker Peksag59da4b32016-09-12 07:16:43 +03001131static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1132{
1133 if (!pysqlite_check_connection(self)) {
1134 return NULL;
1135 }
1136 if (!sqlite3_get_autocommit(self->db)) {
1137 Py_RETURN_TRUE;
1138 }
1139 Py_RETURN_FALSE;
1140}
1141
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001142static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001145 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 if (!res) {
1147 return -1;
1148 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149 Py_DECREF(res);
1150
Serhiy Storchaka28914922016-09-01 22:18:03 +03001151 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001153 const char * const *candidate;
1154 PyObject *uppercase_level;
1155 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001156
Serhiy Storchaka28914922016-09-01 22:18:03 +03001157 if (!PyUnicode_Check(isolation_level)) {
1158 PyErr_Format(PyExc_TypeError,
1159 "isolation_level must be a string or None, not %.100s",
1160 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001161 return -1;
1162 }
1163
Serhiy Storchaka28914922016-09-01 22:18:03 +03001164 uppercase_level = _PyObject_CallMethodIdObjArgs(
1165 (PyObject *)&PyUnicode_Type, &PyId_upper,
1166 isolation_level, NULL);
1167 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001168 return -1;
1169 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001170 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001171 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001172 break;
1173 }
1174 Py_DECREF(uppercase_level);
1175 if (!*candidate) {
1176 PyErr_SetString(PyExc_ValueError,
1177 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001178 return -1;
1179 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001180 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181 }
1182
Serhiy Storchaka28914922016-09-01 22:18:03 +03001183 Py_INCREF(isolation_level);
1184 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185 return 0;
1186}
1187
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001188PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001189{
1190 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001191 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001192 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001193 int rc;
1194
Gerhard Häringf9cee222010-03-05 15:20:03 +00001195 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1196 return NULL;
1197 }
1198
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001199 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001200 return NULL;
1201
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001202 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001203 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001204
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001205 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001206
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001207 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001208 if (!statement) {
1209 return NULL;
1210 }
1211
Victor Stinner0201f442010-03-13 03:28:34 +00001212 statement->db = NULL;
1213 statement->st = NULL;
1214 statement->sql = NULL;
1215 statement->in_use = 0;
1216 statement->in_weakreflist = NULL;
1217
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001218 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001219 if (rc != SQLITE_OK) {
1220 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001221 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001222 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001223 if (PyErr_ExceptionMatches(PyExc_TypeError))
1224 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001225 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001226 (void)pysqlite_statement_reset(statement);
1227 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001229 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001230 }
1231
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001232 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1233 if (weakref == NULL)
1234 goto error;
1235 if (PyList_Append(self->statements, weakref) != 0) {
1236 Py_DECREF(weakref);
1237 goto error;
1238 }
1239 Py_DECREF(weakref);
1240
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001241 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001242
1243error:
1244 Py_DECREF(statement);
1245 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001246}
1247
Larry Hastings01b08832015-05-08 07:37:49 -07001248PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001249{
1250 PyObject* cursor = 0;
1251 PyObject* result = 0;
1252 PyObject* method = 0;
1253
Victor Stinner3466bde2016-09-05 18:16:01 -07001254 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001255 if (!cursor) {
1256 goto error;
1257 }
1258
1259 method = PyObject_GetAttrString(cursor, "execute");
1260 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001261 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001262 goto error;
1263 }
1264
1265 result = PyObject_CallObject(method, args);
1266 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001267 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268 }
1269
1270error:
1271 Py_XDECREF(result);
1272 Py_XDECREF(method);
1273
1274 return cursor;
1275}
1276
Larry Hastings01b08832015-05-08 07:37:49 -07001277PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001278{
1279 PyObject* cursor = 0;
1280 PyObject* result = 0;
1281 PyObject* method = 0;
1282
Victor Stinner3466bde2016-09-05 18:16:01 -07001283 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 if (!cursor) {
1285 goto error;
1286 }
1287
1288 method = PyObject_GetAttrString(cursor, "executemany");
1289 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001290 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001291 goto error;
1292 }
1293
1294 result = PyObject_CallObject(method, args);
1295 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001296 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001297 }
1298
1299error:
1300 Py_XDECREF(result);
1301 Py_XDECREF(method);
1302
1303 return cursor;
1304}
1305
Larry Hastings01b08832015-05-08 07:37:49 -07001306PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001307{
1308 PyObject* cursor = 0;
1309 PyObject* result = 0;
1310 PyObject* method = 0;
1311
Victor Stinner3466bde2016-09-05 18:16:01 -07001312 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001313 if (!cursor) {
1314 goto error;
1315 }
1316
1317 method = PyObject_GetAttrString(cursor, "executescript");
1318 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001319 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001320 goto error;
1321 }
1322
1323 result = PyObject_CallObject(method, args);
1324 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001325 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001326 }
1327
1328error:
1329 Py_XDECREF(result);
1330 Py_XDECREF(method);
1331
1332 return cursor;
1333}
1334
1335/* ------------------------- COLLATION CODE ------------------------ */
1336
1337static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001338pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001339 void* context,
1340 int text1_length, const void* text1_data,
1341 int text2_length, const void* text2_data)
1342{
1343 PyObject* callback = (PyObject*)context;
1344 PyObject* string1 = 0;
1345 PyObject* string2 = 0;
1346 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001347 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001348 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001349 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001350 gilstate = PyGILState_Ensure();
1351
1352 if (PyErr_Occurred()) {
1353 goto finally;
1354 }
1355
Guido van Rossum98297ee2007-11-06 21:34:58 +00001356 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1357 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001358
1359 if (!string1 || !string2) {
1360 goto finally; /* failed to allocate strings */
1361 }
1362
1363 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1364
1365 if (!retval) {
1366 /* execution failed */
1367 goto finally;
1368 }
1369
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001370 longval = PyLong_AsLongAndOverflow(retval, &result);
1371 if (longval == -1 && PyErr_Occurred()) {
1372 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373 result = 0;
1374 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001375 else if (!result) {
1376 if (longval > 0)
1377 result = 1;
1378 else if (longval < 0)
1379 result = -1;
1380 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001381
1382finally:
1383 Py_XDECREF(string1);
1384 Py_XDECREF(string2);
1385 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001386 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001387 return result;
1388}
1389
1390static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001391pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001392{
1393 PyObject* retval = NULL;
1394
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001395 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001396 goto finally;
1397 }
1398
1399 sqlite3_interrupt(self->db);
1400
1401 Py_INCREF(Py_None);
1402 retval = Py_None;
1403
1404finally:
1405 return retval;
1406}
1407
Christian Heimesbbe741d2008-03-28 10:53:29 +00001408/* Function author: Paul Kippes <kippesp@gmail.com>
1409 * Class method of Connection to call the Python function _iterdump
1410 * of the sqlite3 module.
1411 */
1412static PyObject *
1413pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1414{
1415 PyObject* retval = NULL;
1416 PyObject* module = NULL;
1417 PyObject* module_dict;
1418 PyObject* pyfn_iterdump;
1419
1420 if (!pysqlite_check_connection(self)) {
1421 goto finally;
1422 }
1423
1424 module = PyImport_ImportModule(MODULE_NAME ".dump");
1425 if (!module) {
1426 goto finally;
1427 }
1428
1429 module_dict = PyModule_GetDict(module);
1430 if (!module_dict) {
1431 goto finally;
1432 }
1433
1434 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1435 if (!pyfn_iterdump) {
1436 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1437 goto finally;
1438 }
1439
1440 args = PyTuple_New(1);
1441 if (!args) {
1442 goto finally;
1443 }
1444 Py_INCREF(self);
1445 PyTuple_SetItem(args, 0, (PyObject*)self);
1446 retval = PyObject_CallObject(pyfn_iterdump, args);
1447
1448finally:
1449 Py_XDECREF(args);
1450 Py_XDECREF(module);
1451 return retval;
1452}
1453
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001454#ifdef HAVE_BACKUP_API
1455static PyObject *
1456pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *kwds)
1457{
1458 PyObject *target = NULL;
1459 int pages = -1;
1460 PyObject *progress = Py_None;
1461 const char *name = "main";
1462 int rc;
1463 int callback_error = 0;
Victor Stinnerca405012018-04-30 12:22:17 +02001464 PyObject *sleep_obj = NULL;
1465 int sleep_ms = 250;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001466 sqlite3 *bck_conn;
1467 sqlite3_backup *bck_handle;
1468 static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL};
1469
Victor Stinnerca405012018-04-30 12:22:17 +02001470 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsO:backup", keywords,
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001471 &pysqlite_ConnectionType, &target,
Victor Stinnerca405012018-04-30 12:22:17 +02001472 &pages, &progress, &name, &sleep_obj)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001473 return NULL;
1474 }
1475
Victor Stinnerca405012018-04-30 12:22:17 +02001476 if (sleep_obj != NULL) {
1477 _PyTime_t sleep_secs;
1478 if (_PyTime_FromSecondsObject(&sleep_secs, sleep_obj,
1479 _PyTime_ROUND_TIMEOUT)) {
1480 return NULL;
1481 }
1482 _PyTime_t ms = _PyTime_AsMilliseconds(sleep_secs,
1483 _PyTime_ROUND_TIMEOUT);
1484 if (ms < INT_MIN || ms > INT_MAX) {
1485 PyErr_SetString(PyExc_OverflowError, "sleep is too large");
1486 return NULL;
1487 }
1488 sleep_ms = (int)ms;
1489 }
1490
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001491 if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
1492 return NULL;
1493 }
1494
1495 if ((pysqlite_Connection *)target == self) {
1496 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1497 return NULL;
1498 }
1499
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001500#if SQLITE_VERSION_NUMBER < 3008008
1501 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001502 https://www.sqlite.org/src/info/169b5505498c0a7e */
1503 if (!sqlite3_get_autocommit(((pysqlite_Connection *)target)->db)) {
1504 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1505 return NULL;
1506 }
1507#endif
1508
1509 if (progress != Py_None && !PyCallable_Check(progress)) {
1510 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1511 return NULL;
1512 }
1513
1514 if (pages == 0) {
1515 pages = -1;
1516 }
1517
1518 bck_conn = ((pysqlite_Connection *)target)->db;
1519
1520 Py_BEGIN_ALLOW_THREADS
1521 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1522 Py_END_ALLOW_THREADS
1523
1524 if (bck_handle) {
1525 do {
1526 Py_BEGIN_ALLOW_THREADS
1527 rc = sqlite3_backup_step(bck_handle, pages);
1528 Py_END_ALLOW_THREADS
1529
1530 if (progress != Py_None) {
1531 PyObject *res;
1532
1533 res = PyObject_CallFunction(progress, "iii", rc,
1534 sqlite3_backup_remaining(bck_handle),
1535 sqlite3_backup_pagecount(bck_handle));
1536 if (res == NULL) {
1537 /* User's callback raised an error: interrupt the loop and
1538 propagate it. */
1539 callback_error = 1;
1540 rc = -1;
1541 } else {
1542 Py_DECREF(res);
1543 }
1544 }
1545
1546 /* Sleep for a while if there are still further pages to copy and
1547 the engine could not make any progress */
1548 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1549 Py_BEGIN_ALLOW_THREADS
Victor Stinnerca405012018-04-30 12:22:17 +02001550 sqlite3_sleep(sleep_ms);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001551 Py_END_ALLOW_THREADS
1552 }
1553 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1554
1555 Py_BEGIN_ALLOW_THREADS
1556 rc = sqlite3_backup_finish(bck_handle);
1557 Py_END_ALLOW_THREADS
1558 } else {
1559 rc = _pysqlite_seterror(bck_conn, NULL);
1560 }
1561
1562 if (!callback_error && rc != SQLITE_OK) {
1563 /* We cannot use _pysqlite_seterror() here because the backup APIs do
1564 not set the error status on the connection object, but rather on
1565 the backup handle. */
1566 if (rc == SQLITE_NOMEM) {
1567 (void)PyErr_NoMemory();
1568 } else {
1569#if SQLITE_VERSION_NUMBER > 3007015
1570 PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
1571#else
1572 switch (rc) {
1573 case SQLITE_READONLY:
1574 PyErr_SetString(pysqlite_OperationalError,
1575 "attempt to write a readonly database");
1576 break;
1577 case SQLITE_BUSY:
1578 PyErr_SetString(pysqlite_OperationalError, "database is locked");
1579 break;
1580 case SQLITE_LOCKED:
1581 PyErr_SetString(pysqlite_OperationalError,
1582 "database table is locked");
1583 break;
1584 default:
1585 PyErr_Format(pysqlite_OperationalError,
1586 "unrecognized error code: %d", rc);
1587 break;
1588 }
1589#endif
1590 }
1591 }
1592
1593 if (!callback_error && rc == SQLITE_OK) {
1594 Py_RETURN_NONE;
1595 } else {
1596 return NULL;
1597 }
1598}
1599#endif
1600
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001601static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001602pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001603{
1604 PyObject* callable;
1605 PyObject* uppercase_name = 0;
1606 PyObject* name;
1607 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001608 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001609 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001610 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001611 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001612 unsigned int kind;
1613 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001614
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001615 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001616 goto finally;
1617 }
1618
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001619 if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1620 &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001621 goto finally;
1622 }
1623
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001624 uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type,
1625 &PyId_upper, name, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001626 if (!uppercase_name) {
1627 goto finally;
1628 }
1629
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001630 if (PyUnicode_READY(uppercase_name))
1631 goto finally;
1632 len = PyUnicode_GET_LENGTH(uppercase_name);
1633 kind = PyUnicode_KIND(uppercase_name);
1634 data = PyUnicode_DATA(uppercase_name);
1635 for (i=0; i<len; i++) {
1636 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1637 if ((ch >= '0' && ch <= '9')
1638 || (ch >= 'A' && ch <= 'Z')
1639 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001640 {
Victor Stinner35466c52010-04-22 11:23:23 +00001641 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001642 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001643 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001644 goto finally;
1645 }
1646 }
1647
Serhiy Storchaka06515832016-11-20 09:13:07 +02001648 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001649 if (!uppercase_name_str)
1650 goto finally;
1651
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001652 if (callable != Py_None && !PyCallable_Check(callable)) {
1653 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1654 goto finally;
1655 }
1656
1657 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001658 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1659 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001660 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001661 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1662 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001663 }
1664
1665 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001666 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001667 SQLITE_UTF8,
1668 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001669 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001670 if (rc != SQLITE_OK) {
1671 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001672 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001673 goto finally;
1674 }
1675
1676finally:
1677 Py_XDECREF(uppercase_name);
1678
1679 if (PyErr_Occurred()) {
1680 retval = NULL;
1681 } else {
1682 Py_INCREF(Py_None);
1683 retval = Py_None;
1684 }
1685
1686 return retval;
1687}
1688
Christian Heimesbbe741d2008-03-28 10:53:29 +00001689/* Called when the connection is used as a context manager. Returns itself as a
1690 * convenience to the caller. */
1691static PyObject *
1692pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1693{
1694 Py_INCREF(self);
1695 return (PyObject*)self;
1696}
1697
1698/** Called when the connection is used as a context manager. If there was any
1699 * exception, a rollback takes place; otherwise we commit. */
1700static PyObject *
1701pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1702{
1703 PyObject* exc_type, *exc_value, *exc_tb;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001704 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001705 PyObject* result;
1706
1707 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1708 return NULL;
1709 }
1710
1711 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1712 method_name = "commit";
1713 } else {
1714 method_name = "rollback";
1715 }
1716
Victor Stinner3466bde2016-09-05 18:16:01 -07001717 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001718 if (!result) {
1719 return NULL;
1720 }
1721 Py_DECREF(result);
1722
1723 Py_RETURN_FALSE;
1724}
1725
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001726static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001727PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001728
1729static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001730 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1731 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001732 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001733 {NULL}
1734};
1735
1736static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001737 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001738 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001739 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001740 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001741 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001742 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001743 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001744 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001745 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001746 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001747 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001748 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001749 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001750 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001751 #ifdef HAVE_LOAD_EXTENSION
1752 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1753 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1754 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1755 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1756 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001757 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1758 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001759 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1760 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001761 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001763 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001764 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001765 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001766 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001767 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001768 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001769 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001770 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001771 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001772 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001773 #ifdef HAVE_BACKUP_API
1774 {"backup", (PyCFunction)pysqlite_connection_backup, METH_VARARGS | METH_KEYWORDS,
1775 PyDoc_STR("Makes a backup of the database. Non-standard.")},
1776 #endif
Christian Heimesbbe741d2008-03-28 10:53:29 +00001777 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1778 PyDoc_STR("For context manager. Non-standard.")},
1779 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1780 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001781 {NULL, NULL}
1782};
1783
1784static struct PyMemberDef connection_members[] =
1785{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001786 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1787 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1788 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1789 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1790 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1791 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1792 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1793 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1794 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1795 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001796 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1797 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001798 {NULL}
1799};
1800
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001801PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001802 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001803 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001804 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001805 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001806 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001807 0, /* tp_print */
1808 0, /* tp_getattr */
1809 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001810 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001811 0, /* tp_repr */
1812 0, /* tp_as_number */
1813 0, /* tp_as_sequence */
1814 0, /* tp_as_mapping */
1815 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001816 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001817 0, /* tp_str */
1818 0, /* tp_getattro */
1819 0, /* tp_setattro */
1820 0, /* tp_as_buffer */
1821 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1822 connection_doc, /* tp_doc */
1823 0, /* tp_traverse */
1824 0, /* tp_clear */
1825 0, /* tp_richcompare */
1826 0, /* tp_weaklistoffset */
1827 0, /* tp_iter */
1828 0, /* tp_iternext */
1829 connection_methods, /* tp_methods */
1830 connection_members, /* tp_members */
1831 connection_getset, /* tp_getset */
1832 0, /* tp_base */
1833 0, /* tp_dict */
1834 0, /* tp_descr_get */
1835 0, /* tp_descr_set */
1836 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001837 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001838 0, /* tp_alloc */
1839 0, /* tp_new */
1840 0 /* tp_free */
1841};
1842
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001843extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001844{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001845 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1846 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001847}