blob: e2ba758be684b7b433f5199e437eecac6893b366 [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
Martin v. Löwise75fc142013-11-07 18:46:53 +010044_Py_IDENTIFIER(cursor);
45
Serhiy Storchaka28914922016-09-01 22:18:03 +030046static const char * const begin_statements[] = {
47 "BEGIN ",
48 "BEGIN DEFERRED",
49 "BEGIN IMMEDIATE",
50 "BEGIN EXCLUSIVE",
51 NULL
52};
53
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000054static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Gerhard Häringf9cee222010-03-05 15:20:03 +000055static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057
Benjamin Petersond7b03282008-09-13 15:58:53 +000058static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059{
60 /* in older SQLite versions, calling sqlite3_result_error in callbacks
61 * triggers a bug in SQLite that leads either to irritating results or
62 * segfaults, depending on the SQLite version */
63#if SQLITE_VERSION_NUMBER >= 3003003
64 sqlite3_result_error(ctx, errmsg, len);
65#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000066 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067#endif
68}
69
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000070int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010072 static char *kwlist[] = {
73 "database", "timeout", "detect_types", "isolation_level",
74 "check_same_thread", "factory", "cached_statements", "uri",
75 NULL
76 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077
78 char* database;
79 int detect_types = 0;
80 PyObject* isolation_level = NULL;
81 PyObject* factory = NULL;
82 int check_same_thread = 1;
83 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010084 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000085 double timeout = 5.0;
86 int rc;
87
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010088 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOip", kwlist,
89 &database, &timeout, &detect_types,
90 &isolation_level, &check_same_thread,
91 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000093 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094 }
95
Gerhard Häringf9cee222010-03-05 15:20:03 +000096 self->initialized = 1;
97
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098 self->begin_statement = NULL;
99
100 self->statement_cache = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000101 self->statements = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000102 self->cursors = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000103
104 Py_INCREF(Py_None);
105 self->row_factory = Py_None;
106
107 Py_INCREF(&PyUnicode_Type);
108 self->text_factory = (PyObject*)&PyUnicode_Type;
109
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100110#ifdef SQLITE_OPEN_URI
111 Py_BEGIN_ALLOW_THREADS
112 rc = sqlite3_open_v2(database, &self->db,
113 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
114 (uri ? SQLITE_OPEN_URI : 0), NULL);
115#else
116 if (uri) {
117 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
118 return -1;
119 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200121 /* No need to use sqlite3_open_v2 as sqlite3_open(filename, db) is the
122 same as sqlite3_open_v2(filename, db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL). */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100124#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125 Py_END_ALLOW_THREADS
126
127 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000128 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000129 return -1;
130 }
131
132 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000133 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134 if (!isolation_level) {
135 return -1;
136 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000137 } else {
138 Py_INCREF(isolation_level);
139 }
140 self->isolation_level = NULL;
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100141 if (pysqlite_connection_set_isolation_level(self, isolation_level) < 0) {
142 Py_DECREF(isolation_level);
143 return -1;
144 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000145 Py_DECREF(isolation_level);
146
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000147 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 if (PyErr_Occurred()) {
149 return -1;
150 }
151
Gerhard Häringf9cee222010-03-05 15:20:03 +0000152 self->created_statements = 0;
153 self->created_cursors = 0;
154
155 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000156 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000157 self->cursors = PyList_New(0);
158 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000159 return -1;
160 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000161
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000162 /* By default, the Cache class INCREFs the factory in its initializer, and
163 * decrefs it in its deallocator method. Since this would create a circular
164 * reference here, we're breaking it by decrementing self, and telling the
165 * cache class to not decref the factory (self) in its deallocator.
166 */
167 self->statement_cache->decref_factory = 0;
168 Py_DECREF(self);
169
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000170 self->detect_types = detect_types;
171 self->timeout = timeout;
172 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000173#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000174 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000175#endif
Berker Peksag7bea2342016-06-12 14:09:51 +0300176 if (!check_same_thread && sqlite3_libversion_number() < 3003001) {
177 PyErr_SetString(pysqlite_NotSupportedError, "shared connections not available");
178 return -1;
179 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000180 self->check_same_thread = check_same_thread;
181
182 self->function_pinboard = PyDict_New();
183 if (!self->function_pinboard) {
184 return -1;
185 }
186
187 self->collations = PyDict_New();
188 if (!self->collations) {
189 return -1;
190 }
191
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000192 self->Warning = pysqlite_Warning;
193 self->Error = pysqlite_Error;
194 self->InterfaceError = pysqlite_InterfaceError;
195 self->DatabaseError = pysqlite_DatabaseError;
196 self->DataError = pysqlite_DataError;
197 self->OperationalError = pysqlite_OperationalError;
198 self->IntegrityError = pysqlite_IntegrityError;
199 self->InternalError = pysqlite_InternalError;
200 self->ProgrammingError = pysqlite_ProgrammingError;
201 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202
203 return 0;
204}
205
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000206/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000207void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000209 int i;
210 PyObject* weakref;
211 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000212 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213
Thomas Wouters477c8d52006-05-27 19:21:47 +0000214 for (i = 0; i < PyList_Size(self->statements); i++) {
215 weakref = PyList_GetItem(self->statements, i);
216 statement = PyWeakref_GetObject(weakref);
217 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500218 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000219 if (action == ACTION_RESET) {
220 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
221 } else {
222 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
223 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500224 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000225 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000226 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000227
228 if (reset_cursors) {
229 for (i = 0; i < PyList_Size(self->cursors); i++) {
230 weakref = PyList_GetItem(self->cursors, i);
231 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
232 if ((PyObject*)cursor != Py_None) {
233 cursor->reset = 1;
234 }
235 }
236 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237}
238
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000239void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240{
241 Py_XDECREF(self->statement_cache);
242
243 /* Clean up if user has not called .close() explicitly. */
244 if (self->db) {
245 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200246 SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247 Py_END_ALLOW_THREADS
248 }
249
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250 Py_XDECREF(self->isolation_level);
251 Py_XDECREF(self->function_pinboard);
252 Py_XDECREF(self->row_factory);
253 Py_XDECREF(self->text_factory);
254 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000255 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000256 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257
Christian Heimes90aa7642007-12-19 02:45:37 +0000258 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259}
260
Gerhard Häringf9cee222010-03-05 15:20:03 +0000261/*
262 * Registers a cursor with the connection.
263 *
264 * 0 => error; 1 => ok
265 */
266int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
267{
268 PyObject* weakref;
269
270 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
271 if (!weakref) {
272 goto error;
273 }
274
275 if (PyList_Append(connection->cursors, weakref) != 0) {
276 Py_CLEAR(weakref);
277 goto error;
278 }
279
280 Py_DECREF(weakref);
281
282 return 1;
283error:
284 return 0;
285}
286
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000287PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000288{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300289 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000290 PyObject* factory = NULL;
291 PyObject* cursor;
292
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
294 &factory)) {
295 return NULL;
296 }
297
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000298 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000299 return NULL;
300 }
301
302 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000303 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304 }
305
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300306 cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL);
307 if (cursor == NULL)
308 return NULL;
309 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
310 PyErr_Format(PyExc_TypeError,
311 "factory must return a cursor, not %.100s",
312 Py_TYPE(cursor)->tp_name);
313 Py_DECREF(cursor);
314 return NULL;
315 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316
Gerhard Häringf9cee222010-03-05 15:20:03 +0000317 _pysqlite_drop_unused_cursor_references(self);
318
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300321 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000322 }
323
324 return cursor;
325}
326
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000327PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000328{
329 int rc;
330
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000331 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000332 return NULL;
333 }
334
Gerhard Häringf9cee222010-03-05 15:20:03 +0000335 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000336
337 if (self->db) {
338 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200339 rc = SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000340 Py_END_ALLOW_THREADS
341
342 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000343 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344 return NULL;
345 } else {
346 self->db = NULL;
347 }
348 }
349
Berker Peksagfe21de92016-04-09 07:34:39 +0300350 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351}
352
353/*
354 * Checks if a connection object is usable (i. e. not closed).
355 *
356 * 0 => error; 1 => ok
357 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000358int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000360 if (!con->initialized) {
361 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
362 return 0;
363 }
364
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000365 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000366 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367 return 0;
368 } else {
369 return 1;
370 }
371}
372
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000373PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374{
375 int rc;
376 const char* tail;
377 sqlite3_stmt* statement;
378
379 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200380 rc = SQLITE3_PREPARE(self->db, self->begin_statement, -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381 Py_END_ALLOW_THREADS
382
383 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000384 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 goto error;
386 }
387
Benjamin Petersond7b03282008-09-13 15:58:53 +0000388 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300389 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000390 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000391 }
392
393 Py_BEGIN_ALLOW_THREADS
394 rc = sqlite3_finalize(statement);
395 Py_END_ALLOW_THREADS
396
397 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000398 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399 }
400
401error:
402 if (PyErr_Occurred()) {
403 return NULL;
404 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200405 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406 }
407}
408
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000409PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410{
411 int rc;
412 const char* tail;
413 sqlite3_stmt* statement;
414
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000415 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000416 return NULL;
417 }
418
Berker Peksag59da4b32016-09-12 07:16:43 +0300419 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000420
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000421 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200422 rc = SQLITE3_PREPARE(self->db, "COMMIT", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423 Py_END_ALLOW_THREADS
424 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000425 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426 goto error;
427 }
428
Benjamin Petersond7b03282008-09-13 15:58:53 +0000429 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300430 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000431 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432 }
433
434 Py_BEGIN_ALLOW_THREADS
435 rc = sqlite3_finalize(statement);
436 Py_END_ALLOW_THREADS
437 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000438 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439 }
440
441 }
442
443error:
444 if (PyErr_Occurred()) {
445 return NULL;
446 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200447 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 }
449}
450
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000451PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000452{
453 int rc;
454 const char* tail;
455 sqlite3_stmt* statement;
456
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000457 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000458 return NULL;
459 }
460
Berker Peksag59da4b32016-09-12 07:16:43 +0300461 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000462 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463
464 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200465 rc = SQLITE3_PREPARE(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000466 Py_END_ALLOW_THREADS
467 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000468 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469 goto error;
470 }
471
Benjamin Petersond7b03282008-09-13 15:58:53 +0000472 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300473 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000474 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 }
476
477 Py_BEGIN_ALLOW_THREADS
478 rc = sqlite3_finalize(statement);
479 Py_END_ALLOW_THREADS
480 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000481 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000482 }
483
484 }
485
486error:
487 if (PyErr_Occurred()) {
488 return NULL;
489 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200490 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000491 }
492}
493
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200494static int
495_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000496{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200497 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000498 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000499 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200500 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
501 if (value == -1 && PyErr_Occurred())
502 return -1;
503 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000504 } else if (PyFloat_Check(py_val)) {
505 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000506 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200507 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200508 if (str == NULL)
509 return -1;
510 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000511 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200512 Py_buffer view;
513 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100514 PyErr_SetString(PyExc_ValueError,
515 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200516 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200518 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100519 PyErr_SetString(PyExc_OverflowError,
520 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200521 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100522 return -1;
523 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200524 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
525 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200527 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000528 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200529 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000530}
531
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000532PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533{
534 PyObject* args;
535 int i;
536 sqlite3_value* cur_value;
537 PyObject* cur_py_value;
538 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000540
541 args = PyTuple_New(argc);
542 if (!args) {
543 return NULL;
544 }
545
546 for (i = 0; i < argc; i++) {
547 cur_value = argv[i];
548 switch (sqlite3_value_type(argv[i])) {
549 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200550 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551 break;
552 case SQLITE_FLOAT:
553 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
554 break;
555 case SQLITE_TEXT:
556 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000557 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000558 /* TODO: have a way to show errors here */
559 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000560 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000561 Py_INCREF(Py_None);
562 cur_py_value = Py_None;
563 }
564 break;
565 case SQLITE_BLOB:
566 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000567 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000568 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569 break;
570 case SQLITE_NULL:
571 default:
572 Py_INCREF(Py_None);
573 cur_py_value = Py_None;
574 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000575
576 if (!cur_py_value) {
577 Py_DECREF(args);
578 return NULL;
579 }
580
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000581 PyTuple_SetItem(args, i, cur_py_value);
582
583 }
584
585 return args;
586}
587
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000588void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589{
590 PyObject* args;
591 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000592 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200593 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000594
Georg Brandldfd73442009-04-05 11:47:34 +0000595#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596 PyGILState_STATE threadstate;
597
598 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000599#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000600
601 py_func = (PyObject*)sqlite3_user_data(context);
602
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000603 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604 if (args) {
605 py_retval = PyObject_CallObject(py_func, args);
606 Py_DECREF(args);
607 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000608
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200609 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000610 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200611 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200613 }
614 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000615 if (_enable_callback_tracebacks) {
616 PyErr_Print();
617 } else {
618 PyErr_Clear();
619 }
620 _sqlite3_result_error(context, "user-defined function raised exception", -1);
621 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622
Georg Brandldfd73442009-04-05 11:47:34 +0000623#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000625#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000626}
627
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000628static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629{
630 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000631 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000632 PyObject* aggregate_class;
633 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000634 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000635
Georg Brandldfd73442009-04-05 11:47:34 +0000636#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637 PyGILState_STATE threadstate;
638
639 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000640#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000641
642 aggregate_class = (PyObject*)sqlite3_user_data(context);
643
644 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
645
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200646 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100647 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000651 if (_enable_callback_tracebacks) {
652 PyErr_Print();
653 } else {
654 PyErr_Clear();
655 }
656 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000657 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658 }
659 }
660
661 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662 if (!stepmethod) {
663 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000664 }
665
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000666 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000667 if (!args) {
668 goto error;
669 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670
671 function_result = PyObject_CallObject(stepmethod, args);
672 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000673
Thomas Wouters477c8d52006-05-27 19:21:47 +0000674 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000675 if (_enable_callback_tracebacks) {
676 PyErr_Print();
677 } else {
678 PyErr_Clear();
679 }
680 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000681 }
682
Thomas Wouters477c8d52006-05-27 19:21:47 +0000683error:
684 Py_XDECREF(stepmethod);
685 Py_XDECREF(function_result);
686
Georg Brandldfd73442009-04-05 11:47:34 +0000687#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000689#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690}
691
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000692void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000693{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200694 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000695 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200696 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200697 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200698 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200699 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000700
Georg Brandldfd73442009-04-05 11:47:34 +0000701#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000702 PyGILState_STATE threadstate;
703
704 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000705#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
708 if (!*aggregate_instance) {
709 /* this branch is executed if there was an exception in the aggregate's
710 * __init__ */
711
Thomas Wouters477c8d52006-05-27 19:21:47 +0000712 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713 }
714
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200715 /* Keep the exception (if any) of the last call to step() */
716 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200717 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200718
Victor Stinner3466bde2016-09-05 18:16:01 -0700719 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200720
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200721 Py_DECREF(*aggregate_instance);
722
723 ok = 0;
724 if (function_result) {
725 ok = _pysqlite_set_result(context, function_result) == 0;
726 Py_DECREF(function_result);
727 }
728 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000729 if (_enable_callback_tracebacks) {
730 PyErr_Print();
731 } else {
732 PyErr_Clear();
733 }
734 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200735#if SQLITE_VERSION_NUMBER < 3003003
736 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
737 exception, so don't restore the previous exception */
738 restore = 0;
739#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000740 }
741
Victor Stinnerffff7632013-08-02 01:48:10 +0200742 if (restore) {
743 /* Restore the exception (if any) of the last call to step(),
744 but clear also the current exception if finalize() failed */
745 PyErr_Restore(exception, value, tb);
746 }
Victor Stinner3a857322013-07-22 08:34:32 +0200747
Thomas Wouters477c8d52006-05-27 19:21:47 +0000748error:
Georg Brandldfd73442009-04-05 11:47:34 +0000749#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000750 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000751#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200752 /* explicit return to avoid a compilation error if WITH_THREAD
753 is not defined */
754 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000755}
756
Gerhard Häringf9cee222010-03-05 15:20:03 +0000757static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000758{
759 PyObject* new_list;
760 PyObject* weakref;
761 int i;
762
763 /* we only need to do this once in a while */
764 if (self->created_statements++ < 200) {
765 return;
766 }
767
768 self->created_statements = 0;
769
770 new_list = PyList_New(0);
771 if (!new_list) {
772 return;
773 }
774
775 for (i = 0; i < PyList_Size(self->statements); i++) {
776 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000777 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000778 if (PyList_Append(new_list, weakref) != 0) {
779 Py_DECREF(new_list);
780 return;
781 }
782 }
783 }
784
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300785 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787
Gerhard Häringf9cee222010-03-05 15:20:03 +0000788static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
789{
790 PyObject* new_list;
791 PyObject* weakref;
792 int i;
793
794 /* we only need to do this once in a while */
795 if (self->created_cursors++ < 200) {
796 return;
797 }
798
799 self->created_cursors = 0;
800
801 new_list = PyList_New(0);
802 if (!new_list) {
803 return;
804 }
805
806 for (i = 0; i < PyList_Size(self->cursors); i++) {
807 weakref = PyList_GetItem(self->cursors, i);
808 if (PyWeakref_GetObject(weakref) != Py_None) {
809 if (PyList_Append(new_list, weakref) != 0) {
810 Py_DECREF(new_list);
811 return;
812 }
813 }
814 }
815
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300816 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000817}
818
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000819PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820{
821 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
822
823 PyObject* func;
824 char* name;
825 int narg;
826 int rc;
827
Gerhard Häringf9cee222010-03-05 15:20:03 +0000828 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
829 return NULL;
830 }
831
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000832 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
833 &name, &narg, &func))
834 {
835 return NULL;
836 }
837
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000838 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839
Thomas Wouters477c8d52006-05-27 19:21:47 +0000840 if (rc != SQLITE_OK) {
841 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000842 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000843 return NULL;
844 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000845 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
846 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000847
Berker Peksagfe21de92016-04-09 07:34:39 +0300848 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000849 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000850}
851
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000852PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000853{
854 PyObject* aggregate_class;
855
856 int n_arg;
857 char* name;
858 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
859 int rc;
860
Gerhard Häringf9cee222010-03-05 15:20:03 +0000861 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
862 return NULL;
863 }
864
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
866 kwlist, &name, &n_arg, &aggregate_class)) {
867 return NULL;
868 }
869
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000870 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 +0000871 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000873 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000874 return NULL;
875 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000876 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
877 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000878
Berker Peksagfe21de92016-04-09 07:34:39 +0300879 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880 }
881}
882
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000883static 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 +0000884{
885 PyObject *ret;
886 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000887#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000888 PyGILState_STATE gilstate;
889
890 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000891#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000892
Victor Stinnerd4095d92013-07-26 22:23:33 +0200893 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000894
Victor Stinnerd4095d92013-07-26 22:23:33 +0200895 if (ret == NULL) {
896 if (_enable_callback_tracebacks)
897 PyErr_Print();
898 else
899 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200900
Victor Stinnerd4095d92013-07-26 22:23:33 +0200901 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200902 }
903 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200904 if (PyLong_Check(ret)) {
905 rc = _PyLong_AsInt(ret);
906 if (rc == -1 && PyErr_Occurred()) {
907 if (_enable_callback_tracebacks)
908 PyErr_Print();
909 else
910 PyErr_Clear();
911 rc = SQLITE_DENY;
912 }
913 }
914 else {
915 rc = SQLITE_DENY;
916 }
917 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000918 }
919
Georg Brandldfd73442009-04-05 11:47:34 +0000920#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000921 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000922#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000923 return rc;
924}
925
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000926static int _progress_handler(void* user_arg)
927{
928 int rc;
929 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000930#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000931 PyGILState_STATE gilstate;
932
933 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000934#endif
Victor Stinner070c4d72016-12-09 12:29:18 +0100935 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000936
937 if (!ret) {
938 if (_enable_callback_tracebacks) {
939 PyErr_Print();
940 } else {
941 PyErr_Clear();
942 }
943
Mark Dickinson934896d2009-02-21 20:59:32 +0000944 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000945 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000946 } else {
947 rc = (int)PyObject_IsTrue(ret);
948 Py_DECREF(ret);
949 }
950
Georg Brandldfd73442009-04-05 11:47:34 +0000951#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000952 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000953#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000954 return rc;
955}
956
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200957static void _trace_callback(void* user_arg, const char* statement_string)
958{
959 PyObject *py_statement = NULL;
960 PyObject *ret = NULL;
961
962#ifdef WITH_THREAD
963 PyGILState_STATE gilstate;
964
965 gilstate = PyGILState_Ensure();
966#endif
967 py_statement = PyUnicode_DecodeUTF8(statement_string,
968 strlen(statement_string), "replace");
969 if (py_statement) {
970 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
971 Py_DECREF(py_statement);
972 }
973
974 if (ret) {
975 Py_DECREF(ret);
976 } else {
977 if (_enable_callback_tracebacks) {
978 PyErr_Print();
979 } else {
980 PyErr_Clear();
981 }
982 }
983
984#ifdef WITH_THREAD
985 PyGILState_Release(gilstate);
986#endif
987}
988
Gerhard Häringf9cee222010-03-05 15:20:03 +0000989static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000990{
991 PyObject* authorizer_cb;
992
993 static char *kwlist[] = { "authorizer_callback", NULL };
994 int rc;
995
Gerhard Häringf9cee222010-03-05 15:20:03 +0000996 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
997 return NULL;
998 }
999
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001000 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1001 kwlist, &authorizer_cb)) {
1002 return NULL;
1003 }
1004
1005 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1006
1007 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001008 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001009 return NULL;
1010 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001011 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1012 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001013
Berker Peksagfe21de92016-04-09 07:34:39 +03001014 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001015 }
1016}
1017
Gerhard Häringf9cee222010-03-05 15:20:03 +00001018static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001019{
1020 PyObject* progress_handler;
1021 int n;
1022
1023 static char *kwlist[] = { "progress_handler", "n", NULL };
1024
Gerhard Häringf9cee222010-03-05 15:20:03 +00001025 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1026 return NULL;
1027 }
1028
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001029 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1030 kwlist, &progress_handler, &n)) {
1031 return NULL;
1032 }
1033
1034 if (progress_handler == Py_None) {
1035 /* None clears the progress handler previously set */
1036 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1037 } else {
1038 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001039 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1040 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001041 }
1042
Berker Peksagfe21de92016-04-09 07:34:39 +03001043 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001044}
1045
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001046static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1047{
1048 PyObject* trace_callback;
1049
1050 static char *kwlist[] = { "trace_callback", NULL };
1051
1052 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1053 return NULL;
1054 }
1055
1056 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1057 kwlist, &trace_callback)) {
1058 return NULL;
1059 }
1060
1061 if (trace_callback == Py_None) {
1062 /* None clears the trace callback previously set */
1063 sqlite3_trace(self->db, 0, (void*)0);
1064 } else {
1065 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1066 return NULL;
1067 sqlite3_trace(self->db, _trace_callback, trace_callback);
1068 }
1069
Berker Peksagfe21de92016-04-09 07:34:39 +03001070 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001071}
1072
Gerhard Häringf9cee222010-03-05 15:20:03 +00001073#ifdef HAVE_LOAD_EXTENSION
1074static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1075{
1076 int rc;
1077 int onoff;
1078
1079 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1080 return NULL;
1081 }
1082
1083 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1084 return NULL;
1085 }
1086
1087 rc = sqlite3_enable_load_extension(self->db, onoff);
1088
1089 if (rc != SQLITE_OK) {
1090 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1091 return NULL;
1092 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001093 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001094 }
1095}
1096
1097static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1098{
1099 int rc;
1100 char* extension_name;
1101 char* errmsg;
1102
1103 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1104 return NULL;
1105 }
1106
1107 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1108 return NULL;
1109 }
1110
1111 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1112 if (rc != 0) {
1113 PyErr_SetString(pysqlite_OperationalError, errmsg);
1114 return NULL;
1115 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001116 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001117 }
1118}
1119#endif
1120
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001121int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001122{
Georg Brandldfd73442009-04-05 11:47:34 +00001123#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001124 if (self->check_same_thread) {
1125 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001126 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127 "SQLite objects created in a thread can only be used in that same thread."
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001128 "The object was created in thread id %lu and this is thread id %lu",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001129 self->thread_ident, PyThread_get_thread_ident());
1130 return 0;
1131 }
1132
1133 }
Georg Brandldfd73442009-04-05 11:47:34 +00001134#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135 return 1;
1136}
1137
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001138static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001139{
1140 Py_INCREF(self->isolation_level);
1141 return self->isolation_level;
1142}
1143
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001144static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001145{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001146 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147 return NULL;
1148 } else {
1149 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1150 }
1151}
1152
Berker Peksag59da4b32016-09-12 07:16:43 +03001153static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1154{
1155 if (!pysqlite_check_connection(self)) {
1156 return NULL;
1157 }
1158 if (!sqlite3_get_autocommit(self->db)) {
1159 Py_RETURN_TRUE;
1160 }
1161 Py_RETURN_FALSE;
1162}
1163
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001164static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001165{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001167 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001168 if (!res) {
1169 return -1;
1170 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001171 Py_DECREF(res);
1172
Serhiy Storchaka28914922016-09-01 22:18:03 +03001173 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001174 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001175 const char * const *candidate;
1176 PyObject *uppercase_level;
1177 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001178
Serhiy Storchaka28914922016-09-01 22:18:03 +03001179 if (!PyUnicode_Check(isolation_level)) {
1180 PyErr_Format(PyExc_TypeError,
1181 "isolation_level must be a string or None, not %.100s",
1182 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001183 return -1;
1184 }
1185
Serhiy Storchaka28914922016-09-01 22:18:03 +03001186 uppercase_level = _PyObject_CallMethodIdObjArgs(
1187 (PyObject *)&PyUnicode_Type, &PyId_upper,
1188 isolation_level, NULL);
1189 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001190 return -1;
1191 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001192 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001193 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001194 break;
1195 }
1196 Py_DECREF(uppercase_level);
1197 if (!*candidate) {
1198 PyErr_SetString(PyExc_ValueError,
1199 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001200 return -1;
1201 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001202 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001203 }
1204
Serhiy Storchaka28914922016-09-01 22:18:03 +03001205 Py_INCREF(isolation_level);
1206 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001207 return 0;
1208}
1209
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001210PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001211{
1212 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001213 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001214 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001215 int rc;
1216
Gerhard Häringf9cee222010-03-05 15:20:03 +00001217 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1218 return NULL;
1219 }
1220
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001221 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001222 return NULL;
1223
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001224 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001225 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001226
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001227 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001228
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001229 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001230 if (!statement) {
1231 return NULL;
1232 }
1233
Victor Stinner0201f442010-03-13 03:28:34 +00001234 statement->db = NULL;
1235 statement->st = NULL;
1236 statement->sql = NULL;
1237 statement->in_use = 0;
1238 statement->in_weakreflist = NULL;
1239
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001240 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001241 if (rc != SQLITE_OK) {
1242 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001243 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001244 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001245 if (PyErr_ExceptionMatches(PyExc_TypeError))
1246 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001247 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001248 (void)pysqlite_statement_reset(statement);
1249 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001251 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 }
1253
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001254 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1255 if (weakref == NULL)
1256 goto error;
1257 if (PyList_Append(self->statements, weakref) != 0) {
1258 Py_DECREF(weakref);
1259 goto error;
1260 }
1261 Py_DECREF(weakref);
1262
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001263 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001264
1265error:
1266 Py_DECREF(statement);
1267 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268}
1269
Larry Hastings01b08832015-05-08 07:37:49 -07001270PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271{
1272 PyObject* cursor = 0;
1273 PyObject* result = 0;
1274 PyObject* method = 0;
1275
Victor Stinner3466bde2016-09-05 18:16:01 -07001276 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001277 if (!cursor) {
1278 goto error;
1279 }
1280
1281 method = PyObject_GetAttrString(cursor, "execute");
1282 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001283 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 goto error;
1285 }
1286
1287 result = PyObject_CallObject(method, args);
1288 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001289 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001290 }
1291
1292error:
1293 Py_XDECREF(result);
1294 Py_XDECREF(method);
1295
1296 return cursor;
1297}
1298
Larry Hastings01b08832015-05-08 07:37:49 -07001299PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001300{
1301 PyObject* cursor = 0;
1302 PyObject* result = 0;
1303 PyObject* method = 0;
1304
Victor Stinner3466bde2016-09-05 18:16:01 -07001305 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001306 if (!cursor) {
1307 goto error;
1308 }
1309
1310 method = PyObject_GetAttrString(cursor, "executemany");
1311 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001312 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001313 goto error;
1314 }
1315
1316 result = PyObject_CallObject(method, args);
1317 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001318 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001319 }
1320
1321error:
1322 Py_XDECREF(result);
1323 Py_XDECREF(method);
1324
1325 return cursor;
1326}
1327
Larry Hastings01b08832015-05-08 07:37:49 -07001328PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001329{
1330 PyObject* cursor = 0;
1331 PyObject* result = 0;
1332 PyObject* method = 0;
1333
Victor Stinner3466bde2016-09-05 18:16:01 -07001334 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001335 if (!cursor) {
1336 goto error;
1337 }
1338
1339 method = PyObject_GetAttrString(cursor, "executescript");
1340 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001341 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001342 goto error;
1343 }
1344
1345 result = PyObject_CallObject(method, args);
1346 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001347 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001348 }
1349
1350error:
1351 Py_XDECREF(result);
1352 Py_XDECREF(method);
1353
1354 return cursor;
1355}
1356
1357/* ------------------------- COLLATION CODE ------------------------ */
1358
1359static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001360pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001361 void* context,
1362 int text1_length, const void* text1_data,
1363 int text2_length, const void* text2_data)
1364{
1365 PyObject* callback = (PyObject*)context;
1366 PyObject* string1 = 0;
1367 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001368#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001370#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001371 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001372 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001374#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001375 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001376#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001377
1378 if (PyErr_Occurred()) {
1379 goto finally;
1380 }
1381
Guido van Rossum98297ee2007-11-06 21:34:58 +00001382 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1383 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001384
1385 if (!string1 || !string2) {
1386 goto finally; /* failed to allocate strings */
1387 }
1388
1389 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1390
1391 if (!retval) {
1392 /* execution failed */
1393 goto finally;
1394 }
1395
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001396 longval = PyLong_AsLongAndOverflow(retval, &result);
1397 if (longval == -1 && PyErr_Occurred()) {
1398 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001399 result = 0;
1400 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001401 else if (!result) {
1402 if (longval > 0)
1403 result = 1;
1404 else if (longval < 0)
1405 result = -1;
1406 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001407
1408finally:
1409 Py_XDECREF(string1);
1410 Py_XDECREF(string2);
1411 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001412#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001413 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001414#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001415 return result;
1416}
1417
1418static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001419pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001420{
1421 PyObject* retval = NULL;
1422
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001423 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001424 goto finally;
1425 }
1426
1427 sqlite3_interrupt(self->db);
1428
1429 Py_INCREF(Py_None);
1430 retval = Py_None;
1431
1432finally:
1433 return retval;
1434}
1435
Christian Heimesbbe741d2008-03-28 10:53:29 +00001436/* Function author: Paul Kippes <kippesp@gmail.com>
1437 * Class method of Connection to call the Python function _iterdump
1438 * of the sqlite3 module.
1439 */
1440static PyObject *
1441pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1442{
1443 PyObject* retval = NULL;
1444 PyObject* module = NULL;
1445 PyObject* module_dict;
1446 PyObject* pyfn_iterdump;
1447
1448 if (!pysqlite_check_connection(self)) {
1449 goto finally;
1450 }
1451
1452 module = PyImport_ImportModule(MODULE_NAME ".dump");
1453 if (!module) {
1454 goto finally;
1455 }
1456
1457 module_dict = PyModule_GetDict(module);
1458 if (!module_dict) {
1459 goto finally;
1460 }
1461
1462 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1463 if (!pyfn_iterdump) {
1464 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1465 goto finally;
1466 }
1467
1468 args = PyTuple_New(1);
1469 if (!args) {
1470 goto finally;
1471 }
1472 Py_INCREF(self);
1473 PyTuple_SetItem(args, 0, (PyObject*)self);
1474 retval = PyObject_CallObject(pyfn_iterdump, args);
1475
1476finally:
1477 Py_XDECREF(args);
1478 Py_XDECREF(module);
1479 return retval;
1480}
1481
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001482static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001483pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001484{
1485 PyObject* callable;
1486 PyObject* uppercase_name = 0;
1487 PyObject* name;
1488 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001489 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001490 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001491 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001492 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001493 unsigned int kind;
1494 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001495
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001496 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001497 goto finally;
1498 }
1499
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001500 if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1501 &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001502 goto finally;
1503 }
1504
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001505 uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type,
1506 &PyId_upper, name, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001507 if (!uppercase_name) {
1508 goto finally;
1509 }
1510
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001511 if (PyUnicode_READY(uppercase_name))
1512 goto finally;
1513 len = PyUnicode_GET_LENGTH(uppercase_name);
1514 kind = PyUnicode_KIND(uppercase_name);
1515 data = PyUnicode_DATA(uppercase_name);
1516 for (i=0; i<len; i++) {
1517 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1518 if ((ch >= '0' && ch <= '9')
1519 || (ch >= 'A' && ch <= 'Z')
1520 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 {
Victor Stinner35466c52010-04-22 11:23:23 +00001522 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001524 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001525 goto finally;
1526 }
1527 }
1528
Serhiy Storchaka06515832016-11-20 09:13:07 +02001529 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001530 if (!uppercase_name_str)
1531 goto finally;
1532
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001533 if (callable != Py_None && !PyCallable_Check(callable)) {
1534 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1535 goto finally;
1536 }
1537
1538 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001539 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1540 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001541 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001542 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1543 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001544 }
1545
1546 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001547 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001548 SQLITE_UTF8,
1549 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001550 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001551 if (rc != SQLITE_OK) {
1552 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001553 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001554 goto finally;
1555 }
1556
1557finally:
1558 Py_XDECREF(uppercase_name);
1559
1560 if (PyErr_Occurred()) {
1561 retval = NULL;
1562 } else {
1563 Py_INCREF(Py_None);
1564 retval = Py_None;
1565 }
1566
1567 return retval;
1568}
1569
Christian Heimesbbe741d2008-03-28 10:53:29 +00001570/* Called when the connection is used as a context manager. Returns itself as a
1571 * convenience to the caller. */
1572static PyObject *
1573pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1574{
1575 Py_INCREF(self);
1576 return (PyObject*)self;
1577}
1578
1579/** Called when the connection is used as a context manager. If there was any
1580 * exception, a rollback takes place; otherwise we commit. */
1581static PyObject *
1582pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1583{
1584 PyObject* exc_type, *exc_value, *exc_tb;
1585 char* method_name;
1586 PyObject* result;
1587
1588 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1589 return NULL;
1590 }
1591
1592 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1593 method_name = "commit";
1594 } else {
1595 method_name = "rollback";
1596 }
1597
Victor Stinner3466bde2016-09-05 18:16:01 -07001598 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001599 if (!result) {
1600 return NULL;
1601 }
1602 Py_DECREF(result);
1603
1604 Py_RETURN_FALSE;
1605}
1606
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001607static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001608PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001609
1610static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001611 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1612 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001613 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001614 {NULL}
1615};
1616
1617static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001618 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001619 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001620 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001621 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001622 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001623 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001624 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001625 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001626 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001627 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001628 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001629 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001630 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001631 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001632 #ifdef HAVE_LOAD_EXTENSION
1633 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1634 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1635 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1636 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1637 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001638 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1639 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001640 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1641 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001642 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001643 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001644 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001645 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001646 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001647 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001648 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001649 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001650 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001651 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001652 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001653 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001654 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1655 PyDoc_STR("For context manager. Non-standard.")},
1656 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1657 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001658 {NULL, NULL}
1659};
1660
1661static struct PyMemberDef connection_members[] =
1662{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001663 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1664 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1665 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1666 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1667 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1668 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1669 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1670 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1671 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1672 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001673 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1674 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001675 {NULL}
1676};
1677
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001678PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001679 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001680 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001681 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001682 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001683 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001684 0, /* tp_print */
1685 0, /* tp_getattr */
1686 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001687 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001688 0, /* tp_repr */
1689 0, /* tp_as_number */
1690 0, /* tp_as_sequence */
1691 0, /* tp_as_mapping */
1692 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001693 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001694 0, /* tp_str */
1695 0, /* tp_getattro */
1696 0, /* tp_setattro */
1697 0, /* tp_as_buffer */
1698 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1699 connection_doc, /* tp_doc */
1700 0, /* tp_traverse */
1701 0, /* tp_clear */
1702 0, /* tp_richcompare */
1703 0, /* tp_weaklistoffset */
1704 0, /* tp_iter */
1705 0, /* tp_iternext */
1706 connection_methods, /* tp_methods */
1707 connection_members, /* tp_members */
1708 connection_getset, /* tp_getset */
1709 0, /* tp_base */
1710 0, /* tp_dict */
1711 0, /* tp_descr_get */
1712 0, /* tp_descr_set */
1713 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001714 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001715 0, /* tp_alloc */
1716 0, /* tp_new */
1717 0 /* tp_free */
1718};
1719
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001720extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001721{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001722 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1723 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001724}