blob: aca66fed08edc7fa7a0e4ad63aa4e30eb7cab05d [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
121 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100122#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 Py_END_ALLOW_THREADS
124
125 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000126 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127 return -1;
128 }
129
130 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000131 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000132 if (!isolation_level) {
133 return -1;
134 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135 } else {
136 Py_INCREF(isolation_level);
137 }
138 self->isolation_level = NULL;
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100139 if (pysqlite_connection_set_isolation_level(self, isolation_level) < 0) {
140 Py_DECREF(isolation_level);
141 return -1;
142 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143 Py_DECREF(isolation_level);
144
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000145 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 if (PyErr_Occurred()) {
147 return -1;
148 }
149
Gerhard Häringf9cee222010-03-05 15:20:03 +0000150 self->created_statements = 0;
151 self->created_cursors = 0;
152
153 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000154 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000155 self->cursors = PyList_New(0);
156 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157 return -1;
158 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000159
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160 /* By default, the Cache class INCREFs the factory in its initializer, and
161 * decrefs it in its deallocator method. Since this would create a circular
162 * reference here, we're breaking it by decrementing self, and telling the
163 * cache class to not decref the factory (self) in its deallocator.
164 */
165 self->statement_cache->decref_factory = 0;
166 Py_DECREF(self);
167
168 self->inTransaction = 0;
169 self->detect_types = detect_types;
170 self->timeout = timeout;
171 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000172#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000174#endif
Berker Peksag7bea2342016-06-12 14:09:51 +0300175 if (!check_same_thread && sqlite3_libversion_number() < 3003001) {
176 PyErr_SetString(pysqlite_NotSupportedError, "shared connections not available");
177 return -1;
178 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179 self->check_same_thread = check_same_thread;
180
181 self->function_pinboard = PyDict_New();
182 if (!self->function_pinboard) {
183 return -1;
184 }
185
186 self->collations = PyDict_New();
187 if (!self->collations) {
188 return -1;
189 }
190
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000191 self->Warning = pysqlite_Warning;
192 self->Error = pysqlite_Error;
193 self->InterfaceError = pysqlite_InterfaceError;
194 self->DatabaseError = pysqlite_DatabaseError;
195 self->DataError = pysqlite_DataError;
196 self->OperationalError = pysqlite_OperationalError;
197 self->IntegrityError = pysqlite_IntegrityError;
198 self->InternalError = pysqlite_InternalError;
199 self->ProgrammingError = pysqlite_ProgrammingError;
200 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201
202 return 0;
203}
204
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000205/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000206void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000208 int i;
209 PyObject* weakref;
210 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000211 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212
Thomas Wouters477c8d52006-05-27 19:21:47 +0000213 for (i = 0; i < PyList_Size(self->statements); i++) {
214 weakref = PyList_GetItem(self->statements, i);
215 statement = PyWeakref_GetObject(weakref);
216 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500217 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000218 if (action == ACTION_RESET) {
219 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
220 } else {
221 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
222 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500223 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000224 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000226
227 if (reset_cursors) {
228 for (i = 0; i < PyList_Size(self->cursors); i++) {
229 weakref = PyList_GetItem(self->cursors, i);
230 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
231 if ((PyObject*)cursor != Py_None) {
232 cursor->reset = 1;
233 }
234 }
235 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000236}
237
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000238void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000239{
240 Py_XDECREF(self->statement_cache);
241
242 /* Clean up if user has not called .close() explicitly. */
243 if (self->db) {
244 Py_BEGIN_ALLOW_THREADS
245 sqlite3_close(self->db);
246 Py_END_ALLOW_THREADS
247 }
248
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000249 Py_XDECREF(self->isolation_level);
250 Py_XDECREF(self->function_pinboard);
251 Py_XDECREF(self->row_factory);
252 Py_XDECREF(self->text_factory);
253 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000254 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000255 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256
Christian Heimes90aa7642007-12-19 02:45:37 +0000257 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000258}
259
Gerhard Häringf9cee222010-03-05 15:20:03 +0000260/*
261 * Registers a cursor with the connection.
262 *
263 * 0 => error; 1 => ok
264 */
265int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
266{
267 PyObject* weakref;
268
269 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
270 if (!weakref) {
271 goto error;
272 }
273
274 if (PyList_Append(connection->cursors, weakref) != 0) {
275 Py_CLEAR(weakref);
276 goto error;
277 }
278
279 Py_DECREF(weakref);
280
281 return 1;
282error:
283 return 0;
284}
285
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000286PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300288 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000289 PyObject* factory = NULL;
290 PyObject* cursor;
291
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
293 &factory)) {
294 return NULL;
295 }
296
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000297 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298 return NULL;
299 }
300
301 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000302 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303 }
304
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300305 cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL);
306 if (cursor == NULL)
307 return NULL;
308 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
309 PyErr_Format(PyExc_TypeError,
310 "factory must return a cursor, not %.100s",
311 Py_TYPE(cursor)->tp_name);
312 Py_DECREF(cursor);
313 return NULL;
314 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315
Gerhard Häringf9cee222010-03-05 15:20:03 +0000316 _pysqlite_drop_unused_cursor_references(self);
317
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300320 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000321 }
322
323 return cursor;
324}
325
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000326PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327{
328 int rc;
329
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000330 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331 return NULL;
332 }
333
Gerhard Häringf9cee222010-03-05 15:20:03 +0000334 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335
336 if (self->db) {
337 Py_BEGIN_ALLOW_THREADS
338 rc = sqlite3_close(self->db);
339 Py_END_ALLOW_THREADS
340
341 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000342 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343 return NULL;
344 } else {
345 self->db = NULL;
346 }
347 }
348
Berker Peksagfe21de92016-04-09 07:34:39 +0300349 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000350}
351
352/*
353 * Checks if a connection object is usable (i. e. not closed).
354 *
355 * 0 => error; 1 => ok
356 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000357int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000359 if (!con->initialized) {
360 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
361 return 0;
362 }
363
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000365 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 return 0;
367 } else {
368 return 1;
369 }
370}
371
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000372PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000373{
374 int rc;
375 const char* tail;
376 sqlite3_stmt* statement;
377
378 Py_BEGIN_ALLOW_THREADS
379 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
380 Py_END_ALLOW_THREADS
381
382 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000383 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384 goto error;
385 }
386
Benjamin Petersond7b03282008-09-13 15:58:53 +0000387 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388 if (rc == SQLITE_DONE) {
389 self->inTransaction = 1;
390 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000391 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 }
393
394 Py_BEGIN_ALLOW_THREADS
395 rc = sqlite3_finalize(statement);
396 Py_END_ALLOW_THREADS
397
398 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000399 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 }
401
402error:
403 if (PyErr_Occurred()) {
404 return NULL;
405 } else {
406 Py_INCREF(Py_None);
407 return Py_None;
408 }
409}
410
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000411PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412{
413 int rc;
414 const char* tail;
415 sqlite3_stmt* statement;
416
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000417 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000418 return NULL;
419 }
420
421 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000422
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423 Py_BEGIN_ALLOW_THREADS
424 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
425 Py_END_ALLOW_THREADS
426 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000427 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000428 goto error;
429 }
430
Benjamin Petersond7b03282008-09-13 15:58:53 +0000431 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432 if (rc == SQLITE_DONE) {
433 self->inTransaction = 0;
434 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000435 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000436 }
437
438 Py_BEGIN_ALLOW_THREADS
439 rc = sqlite3_finalize(statement);
440 Py_END_ALLOW_THREADS
441 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000442 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443 }
444
445 }
446
447error:
448 if (PyErr_Occurred()) {
449 return NULL;
450 } else {
451 Py_INCREF(Py_None);
452 return Py_None;
453 }
454}
455
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000456PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457{
458 int rc;
459 const char* tail;
460 sqlite3_stmt* statement;
461
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000462 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463 return NULL;
464 }
465
466 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000467 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000468
469 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000470 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000471 Py_END_ALLOW_THREADS
472 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000473 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474 goto error;
475 }
476
Benjamin Petersond7b03282008-09-13 15:58:53 +0000477 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000478 if (rc == SQLITE_DONE) {
479 self->inTransaction = 0;
480 } else {
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 {
497 Py_INCREF(Py_None);
498 return Py_None;
499 }
500}
501
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200502static int
503_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000504{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200505 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000506 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000507 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200508 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
509 if (value == -1 && PyErr_Occurred())
510 return -1;
511 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512 } else if (PyFloat_Check(py_val)) {
513 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000514 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200515 const char *str = _PyUnicode_AsString(py_val);
516 if (str == NULL)
517 return -1;
518 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000519 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200520 Py_buffer view;
521 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100522 PyErr_SetString(PyExc_ValueError,
523 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200524 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000525 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200526 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100527 PyErr_SetString(PyExc_OverflowError,
528 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200529 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100530 return -1;
531 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200532 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
533 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000534 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200535 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200537 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538}
539
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000540PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541{
542 PyObject* args;
543 int i;
544 sqlite3_value* cur_value;
545 PyObject* cur_py_value;
546 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000547 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548
549 args = PyTuple_New(argc);
550 if (!args) {
551 return NULL;
552 }
553
554 for (i = 0; i < argc; i++) {
555 cur_value = argv[i];
556 switch (sqlite3_value_type(argv[i])) {
557 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200558 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000559 break;
560 case SQLITE_FLOAT:
561 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
562 break;
563 case SQLITE_TEXT:
564 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000565 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000566 /* TODO: have a way to show errors here */
567 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000568 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569 Py_INCREF(Py_None);
570 cur_py_value = Py_None;
571 }
572 break;
573 case SQLITE_BLOB:
574 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000575 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000576 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000577 break;
578 case SQLITE_NULL:
579 default:
580 Py_INCREF(Py_None);
581 cur_py_value = Py_None;
582 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000583
584 if (!cur_py_value) {
585 Py_DECREF(args);
586 return NULL;
587 }
588
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589 PyTuple_SetItem(args, i, cur_py_value);
590
591 }
592
593 return args;
594}
595
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000596void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597{
598 PyObject* args;
599 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000600 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200601 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602
Georg Brandldfd73442009-04-05 11:47:34 +0000603#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604 PyGILState_STATE threadstate;
605
606 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000607#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000608
609 py_func = (PyObject*)sqlite3_user_data(context);
610
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000611 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000612 if (args) {
613 py_retval = PyObject_CallObject(py_func, args);
614 Py_DECREF(args);
615 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200617 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000618 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200619 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000620 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200621 }
622 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000623 if (_enable_callback_tracebacks) {
624 PyErr_Print();
625 } else {
626 PyErr_Clear();
627 }
628 _sqlite3_result_error(context, "user-defined function raised exception", -1);
629 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000630
Georg Brandldfd73442009-04-05 11:47:34 +0000631#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000632 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000633#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634}
635
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000636static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637{
638 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000639 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000640 PyObject* aggregate_class;
641 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000642 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643
Georg Brandldfd73442009-04-05 11:47:34 +0000644#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000645 PyGILState_STATE threadstate;
646
647 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000648#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000649
650 aggregate_class = (PyObject*)sqlite3_user_data(context);
651
652 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
653
654 if (*aggregate_instance == 0) {
Victor Stinner3466bde2016-09-05 18:16:01 -0700655 *aggregate_instance = PyObject_CallFunction(aggregate_class, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000656
Thomas Wouters477c8d52006-05-27 19:21:47 +0000657 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000659 if (_enable_callback_tracebacks) {
660 PyErr_Print();
661 } else {
662 PyErr_Clear();
663 }
664 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000665 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666 }
667 }
668
669 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000670 if (!stepmethod) {
671 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672 }
673
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000674 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675 if (!args) {
676 goto error;
677 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678
679 function_result = PyObject_CallObject(stepmethod, args);
680 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000681
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000683 if (_enable_callback_tracebacks) {
684 PyErr_Print();
685 } else {
686 PyErr_Clear();
687 }
688 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000689 }
690
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691error:
692 Py_XDECREF(stepmethod);
693 Py_XDECREF(function_result);
694
Georg Brandldfd73442009-04-05 11:47:34 +0000695#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000696 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000697#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000698}
699
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000700void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000701{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200702 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000703 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200704 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200705 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200706 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200707 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000708
Georg Brandldfd73442009-04-05 11:47:34 +0000709#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 PyGILState_STATE threadstate;
711
712 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000713#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000714
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
716 if (!*aggregate_instance) {
717 /* this branch is executed if there was an exception in the aggregate's
718 * __init__ */
719
Thomas Wouters477c8d52006-05-27 19:21:47 +0000720 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000721 }
722
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200723 /* Keep the exception (if any) of the last call to step() */
724 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200725 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200726
Victor Stinner3466bde2016-09-05 18:16:01 -0700727 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200728
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200729 Py_DECREF(*aggregate_instance);
730
731 ok = 0;
732 if (function_result) {
733 ok = _pysqlite_set_result(context, function_result) == 0;
734 Py_DECREF(function_result);
735 }
736 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000737 if (_enable_callback_tracebacks) {
738 PyErr_Print();
739 } else {
740 PyErr_Clear();
741 }
742 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200743#if SQLITE_VERSION_NUMBER < 3003003
744 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
745 exception, so don't restore the previous exception */
746 restore = 0;
747#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000748 }
749
Victor Stinnerffff7632013-08-02 01:48:10 +0200750 if (restore) {
751 /* Restore the exception (if any) of the last call to step(),
752 but clear also the current exception if finalize() failed */
753 PyErr_Restore(exception, value, tb);
754 }
Victor Stinner3a857322013-07-22 08:34:32 +0200755
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756error:
Georg Brandldfd73442009-04-05 11:47:34 +0000757#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000758 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000759#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200760 /* explicit return to avoid a compilation error if WITH_THREAD
761 is not defined */
762 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000763}
764
Gerhard Häringf9cee222010-03-05 15:20:03 +0000765static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766{
767 PyObject* new_list;
768 PyObject* weakref;
769 int i;
770
771 /* we only need to do this once in a while */
772 if (self->created_statements++ < 200) {
773 return;
774 }
775
776 self->created_statements = 0;
777
778 new_list = PyList_New(0);
779 if (!new_list) {
780 return;
781 }
782
783 for (i = 0; i < PyList_Size(self->statements); i++) {
784 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000785 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786 if (PyList_Append(new_list, weakref) != 0) {
787 Py_DECREF(new_list);
788 return;
789 }
790 }
791 }
792
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300793 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000794}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000795
Gerhard Häringf9cee222010-03-05 15:20:03 +0000796static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
797{
798 PyObject* new_list;
799 PyObject* weakref;
800 int i;
801
802 /* we only need to do this once in a while */
803 if (self->created_cursors++ < 200) {
804 return;
805 }
806
807 self->created_cursors = 0;
808
809 new_list = PyList_New(0);
810 if (!new_list) {
811 return;
812 }
813
814 for (i = 0; i < PyList_Size(self->cursors); i++) {
815 weakref = PyList_GetItem(self->cursors, i);
816 if (PyWeakref_GetObject(weakref) != Py_None) {
817 if (PyList_Append(new_list, weakref) != 0) {
818 Py_DECREF(new_list);
819 return;
820 }
821 }
822 }
823
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300824 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000825}
826
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000827PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000828{
829 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
830
831 PyObject* func;
832 char* name;
833 int narg;
834 int rc;
835
Gerhard Häringf9cee222010-03-05 15:20:03 +0000836 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
837 return NULL;
838 }
839
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
841 &name, &narg, &func))
842 {
843 return NULL;
844 }
845
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000846 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000847
Thomas Wouters477c8d52006-05-27 19:21:47 +0000848 if (rc != SQLITE_OK) {
849 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000850 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000851 return NULL;
852 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000853 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
854 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000855
Berker Peksagfe21de92016-04-09 07:34:39 +0300856 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000857 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858}
859
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000860PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861{
862 PyObject* aggregate_class;
863
864 int n_arg;
865 char* name;
866 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
867 int rc;
868
Gerhard Häringf9cee222010-03-05 15:20:03 +0000869 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
870 return NULL;
871 }
872
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000873 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
874 kwlist, &name, &n_arg, &aggregate_class)) {
875 return NULL;
876 }
877
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000878 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 +0000879 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000880 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000881 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882 return NULL;
883 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000884 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
885 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000886
Berker Peksagfe21de92016-04-09 07:34:39 +0300887 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000888 }
889}
890
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000891static 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 +0000892{
893 PyObject *ret;
894 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000895#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000896 PyGILState_STATE gilstate;
897
898 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000899#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000900
Victor Stinnerd4095d92013-07-26 22:23:33 +0200901 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000902
Victor Stinnerd4095d92013-07-26 22:23:33 +0200903 if (ret == NULL) {
904 if (_enable_callback_tracebacks)
905 PyErr_Print();
906 else
907 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200908
Victor Stinnerd4095d92013-07-26 22:23:33 +0200909 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200910 }
911 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200912 if (PyLong_Check(ret)) {
913 rc = _PyLong_AsInt(ret);
914 if (rc == -1 && PyErr_Occurred()) {
915 if (_enable_callback_tracebacks)
916 PyErr_Print();
917 else
918 PyErr_Clear();
919 rc = SQLITE_DENY;
920 }
921 }
922 else {
923 rc = SQLITE_DENY;
924 }
925 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000926 }
927
Georg Brandldfd73442009-04-05 11:47:34 +0000928#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000929 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000930#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000931 return rc;
932}
933
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000934static int _progress_handler(void* user_arg)
935{
936 int rc;
937 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000938#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000939 PyGILState_STATE gilstate;
940
941 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000942#endif
Victor Stinner3466bde2016-09-05 18:16:01 -0700943 ret = PyObject_CallFunction((PyObject*)user_arg, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000944
945 if (!ret) {
946 if (_enable_callback_tracebacks) {
947 PyErr_Print();
948 } else {
949 PyErr_Clear();
950 }
951
Mark Dickinson934896d2009-02-21 20:59:32 +0000952 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000953 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000954 } else {
955 rc = (int)PyObject_IsTrue(ret);
956 Py_DECREF(ret);
957 }
958
Georg Brandldfd73442009-04-05 11:47:34 +0000959#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000960 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000961#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000962 return rc;
963}
964
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200965static void _trace_callback(void* user_arg, const char* statement_string)
966{
967 PyObject *py_statement = NULL;
968 PyObject *ret = NULL;
969
970#ifdef WITH_THREAD
971 PyGILState_STATE gilstate;
972
973 gilstate = PyGILState_Ensure();
974#endif
975 py_statement = PyUnicode_DecodeUTF8(statement_string,
976 strlen(statement_string), "replace");
977 if (py_statement) {
978 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
979 Py_DECREF(py_statement);
980 }
981
982 if (ret) {
983 Py_DECREF(ret);
984 } else {
985 if (_enable_callback_tracebacks) {
986 PyErr_Print();
987 } else {
988 PyErr_Clear();
989 }
990 }
991
992#ifdef WITH_THREAD
993 PyGILState_Release(gilstate);
994#endif
995}
996
Gerhard Häringf9cee222010-03-05 15:20:03 +0000997static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000998{
999 PyObject* authorizer_cb;
1000
1001 static char *kwlist[] = { "authorizer_callback", NULL };
1002 int rc;
1003
Gerhard Häringf9cee222010-03-05 15:20:03 +00001004 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1005 return NULL;
1006 }
1007
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001008 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1009 kwlist, &authorizer_cb)) {
1010 return NULL;
1011 }
1012
1013 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1014
1015 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001016 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001017 return NULL;
1018 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001019 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1020 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001021
Berker Peksagfe21de92016-04-09 07:34:39 +03001022 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001023 }
1024}
1025
Gerhard Häringf9cee222010-03-05 15:20:03 +00001026static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001027{
1028 PyObject* progress_handler;
1029 int n;
1030
1031 static char *kwlist[] = { "progress_handler", "n", NULL };
1032
Gerhard Häringf9cee222010-03-05 15:20:03 +00001033 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1034 return NULL;
1035 }
1036
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001037 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1038 kwlist, &progress_handler, &n)) {
1039 return NULL;
1040 }
1041
1042 if (progress_handler == Py_None) {
1043 /* None clears the progress handler previously set */
1044 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1045 } else {
1046 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001047 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1048 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001049 }
1050
Berker Peksagfe21de92016-04-09 07:34:39 +03001051 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001052}
1053
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001054static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1055{
1056 PyObject* trace_callback;
1057
1058 static char *kwlist[] = { "trace_callback", NULL };
1059
1060 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1061 return NULL;
1062 }
1063
1064 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1065 kwlist, &trace_callback)) {
1066 return NULL;
1067 }
1068
1069 if (trace_callback == Py_None) {
1070 /* None clears the trace callback previously set */
1071 sqlite3_trace(self->db, 0, (void*)0);
1072 } else {
1073 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1074 return NULL;
1075 sqlite3_trace(self->db, _trace_callback, trace_callback);
1076 }
1077
Berker Peksagfe21de92016-04-09 07:34:39 +03001078 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001079}
1080
Gerhard Häringf9cee222010-03-05 15:20:03 +00001081#ifdef HAVE_LOAD_EXTENSION
1082static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1083{
1084 int rc;
1085 int onoff;
1086
1087 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1088 return NULL;
1089 }
1090
1091 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1092 return NULL;
1093 }
1094
1095 rc = sqlite3_enable_load_extension(self->db, onoff);
1096
1097 if (rc != SQLITE_OK) {
1098 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1099 return NULL;
1100 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001101 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001102 }
1103}
1104
1105static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1106{
1107 int rc;
1108 char* extension_name;
1109 char* errmsg;
1110
1111 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1112 return NULL;
1113 }
1114
1115 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1116 return NULL;
1117 }
1118
1119 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1120 if (rc != 0) {
1121 PyErr_SetString(pysqlite_OperationalError, errmsg);
1122 return NULL;
1123 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001124 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001125 }
1126}
1127#endif
1128
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001129int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001130{
Georg Brandldfd73442009-04-05 11:47:34 +00001131#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001132 if (self->check_same_thread) {
1133 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001134 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135 "SQLite objects created in a thread can only be used in that same thread."
1136 "The object was created in thread id %ld and this is thread id %ld",
1137 self->thread_ident, PyThread_get_thread_ident());
1138 return 0;
1139 }
1140
1141 }
Georg Brandldfd73442009-04-05 11:47:34 +00001142#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143 return 1;
1144}
1145
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001146static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147{
1148 Py_INCREF(self->isolation_level);
1149 return self->isolation_level;
1150}
1151
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001152static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001153{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001154 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155 return NULL;
1156 } else {
1157 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1158 }
1159}
1160
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001161static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001164 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001165 if (!res) {
1166 return -1;
1167 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001168 Py_DECREF(res);
1169
Serhiy Storchaka28914922016-09-01 22:18:03 +03001170 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001171 self->inTransaction = 0;
1172 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001173 const char * const *candidate;
1174 PyObject *uppercase_level;
1175 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001176
Serhiy Storchaka28914922016-09-01 22:18:03 +03001177 if (!PyUnicode_Check(isolation_level)) {
1178 PyErr_Format(PyExc_TypeError,
1179 "isolation_level must be a string or None, not %.100s",
1180 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181 return -1;
1182 }
1183
Serhiy Storchaka28914922016-09-01 22:18:03 +03001184 uppercase_level = _PyObject_CallMethodIdObjArgs(
1185 (PyObject *)&PyUnicode_Type, &PyId_upper,
1186 isolation_level, NULL);
1187 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001188 return -1;
1189 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001190 for (candidate = begin_statements; *candidate; candidate++) {
1191 if (!PyUnicode_CompareWithASCIIString(uppercase_level, *candidate + 6))
1192 break;
1193 }
1194 Py_DECREF(uppercase_level);
1195 if (!*candidate) {
1196 PyErr_SetString(PyExc_ValueError,
1197 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001198 return -1;
1199 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001200 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001201 }
1202
Serhiy Storchaka28914922016-09-01 22:18:03 +03001203 Py_INCREF(isolation_level);
1204 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001205 return 0;
1206}
1207
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001208PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001209{
1210 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001211 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001212 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001213 int rc;
1214
Gerhard Häringf9cee222010-03-05 15:20:03 +00001215 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1216 return NULL;
1217 }
1218
Larry Hastings3b12e952015-05-08 07:45:10 -07001219 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection()", kwargs))
1220 return NULL;
1221
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001222 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001223 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001225 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001226
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001227 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228 if (!statement) {
1229 return NULL;
1230 }
1231
Victor Stinner0201f442010-03-13 03:28:34 +00001232 statement->db = NULL;
1233 statement->st = NULL;
1234 statement->sql = NULL;
1235 statement->in_use = 0;
1236 statement->in_weakreflist = NULL;
1237
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001238 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001239 if (rc != SQLITE_OK) {
1240 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001241 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001243 if (PyErr_ExceptionMatches(PyExc_TypeError))
1244 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001245 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001246 (void)pysqlite_statement_reset(statement);
1247 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001248 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001249 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 }
1251
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001252 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1253 if (weakref == NULL)
1254 goto error;
1255 if (PyList_Append(self->statements, weakref) != 0) {
1256 Py_DECREF(weakref);
1257 goto error;
1258 }
1259 Py_DECREF(weakref);
1260
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001261 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001262
1263error:
1264 Py_DECREF(statement);
1265 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266}
1267
Larry Hastings01b08832015-05-08 07:37:49 -07001268PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001269{
1270 PyObject* cursor = 0;
1271 PyObject* result = 0;
1272 PyObject* method = 0;
1273
Victor Stinner3466bde2016-09-05 18:16:01 -07001274 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275 if (!cursor) {
1276 goto error;
1277 }
1278
1279 method = PyObject_GetAttrString(cursor, "execute");
1280 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001281 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001282 goto error;
1283 }
1284
1285 result = PyObject_CallObject(method, args);
1286 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001287 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001288 }
1289
1290error:
1291 Py_XDECREF(result);
1292 Py_XDECREF(method);
1293
1294 return cursor;
1295}
1296
Larry Hastings01b08832015-05-08 07:37:49 -07001297PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001298{
1299 PyObject* cursor = 0;
1300 PyObject* result = 0;
1301 PyObject* method = 0;
1302
Victor Stinner3466bde2016-09-05 18:16:01 -07001303 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001304 if (!cursor) {
1305 goto error;
1306 }
1307
1308 method = PyObject_GetAttrString(cursor, "executemany");
1309 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001310 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001311 goto error;
1312 }
1313
1314 result = PyObject_CallObject(method, args);
1315 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001316 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001317 }
1318
1319error:
1320 Py_XDECREF(result);
1321 Py_XDECREF(method);
1322
1323 return cursor;
1324}
1325
Larry Hastings01b08832015-05-08 07:37:49 -07001326PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327{
1328 PyObject* cursor = 0;
1329 PyObject* result = 0;
1330 PyObject* method = 0;
1331
Victor Stinner3466bde2016-09-05 18:16:01 -07001332 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333 if (!cursor) {
1334 goto error;
1335 }
1336
1337 method = PyObject_GetAttrString(cursor, "executescript");
1338 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001339 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001340 goto error;
1341 }
1342
1343 result = PyObject_CallObject(method, args);
1344 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001345 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001346 }
1347
1348error:
1349 Py_XDECREF(result);
1350 Py_XDECREF(method);
1351
1352 return cursor;
1353}
1354
1355/* ------------------------- COLLATION CODE ------------------------ */
1356
1357static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001358pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001359 void* context,
1360 int text1_length, const void* text1_data,
1361 int text2_length, const void* text2_data)
1362{
1363 PyObject* callback = (PyObject*)context;
1364 PyObject* string1 = 0;
1365 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001366#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001367 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001368#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001370 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001371 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001372#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001374#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001375
1376 if (PyErr_Occurred()) {
1377 goto finally;
1378 }
1379
Guido van Rossum98297ee2007-11-06 21:34:58 +00001380 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1381 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001382
1383 if (!string1 || !string2) {
1384 goto finally; /* failed to allocate strings */
1385 }
1386
1387 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1388
1389 if (!retval) {
1390 /* execution failed */
1391 goto finally;
1392 }
1393
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001394 longval = PyLong_AsLongAndOverflow(retval, &result);
1395 if (longval == -1 && PyErr_Occurred()) {
1396 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001397 result = 0;
1398 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001399 else if (!result) {
1400 if (longval > 0)
1401 result = 1;
1402 else if (longval < 0)
1403 result = -1;
1404 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001405
1406finally:
1407 Py_XDECREF(string1);
1408 Py_XDECREF(string2);
1409 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001410#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001412#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001413 return result;
1414}
1415
1416static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001417pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001418{
1419 PyObject* retval = NULL;
1420
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001421 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001422 goto finally;
1423 }
1424
1425 sqlite3_interrupt(self->db);
1426
1427 Py_INCREF(Py_None);
1428 retval = Py_None;
1429
1430finally:
1431 return retval;
1432}
1433
Christian Heimesbbe741d2008-03-28 10:53:29 +00001434/* Function author: Paul Kippes <kippesp@gmail.com>
1435 * Class method of Connection to call the Python function _iterdump
1436 * of the sqlite3 module.
1437 */
1438static PyObject *
1439pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1440{
1441 PyObject* retval = NULL;
1442 PyObject* module = NULL;
1443 PyObject* module_dict;
1444 PyObject* pyfn_iterdump;
1445
1446 if (!pysqlite_check_connection(self)) {
1447 goto finally;
1448 }
1449
1450 module = PyImport_ImportModule(MODULE_NAME ".dump");
1451 if (!module) {
1452 goto finally;
1453 }
1454
1455 module_dict = PyModule_GetDict(module);
1456 if (!module_dict) {
1457 goto finally;
1458 }
1459
1460 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1461 if (!pyfn_iterdump) {
1462 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1463 goto finally;
1464 }
1465
1466 args = PyTuple_New(1);
1467 if (!args) {
1468 goto finally;
1469 }
1470 Py_INCREF(self);
1471 PyTuple_SetItem(args, 0, (PyObject*)self);
1472 retval = PyObject_CallObject(pyfn_iterdump, args);
1473
1474finally:
1475 Py_XDECREF(args);
1476 Py_XDECREF(module);
1477 return retval;
1478}
1479
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001480static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001481pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001482{
1483 PyObject* callable;
1484 PyObject* uppercase_name = 0;
1485 PyObject* name;
1486 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001487 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001488 _Py_IDENTIFIER(upper);
Victor Stinner35466c52010-04-22 11:23:23 +00001489 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001491 unsigned int kind;
1492 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001493
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001494 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001495 goto finally;
1496 }
1497
Gerhard Häring6d214562007-08-10 18:15:11 +00001498 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001499 goto finally;
1500 }
1501
Victor Stinner3466bde2016-09-05 18:16:01 -07001502 uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001503 if (!uppercase_name) {
1504 goto finally;
1505 }
1506
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001507 if (PyUnicode_READY(uppercase_name))
1508 goto finally;
1509 len = PyUnicode_GET_LENGTH(uppercase_name);
1510 kind = PyUnicode_KIND(uppercase_name);
1511 data = PyUnicode_DATA(uppercase_name);
1512 for (i=0; i<len; i++) {
1513 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1514 if ((ch >= '0' && ch <= '9')
1515 || (ch >= 'A' && ch <= 'Z')
1516 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001517 {
Victor Stinner35466c52010-04-22 11:23:23 +00001518 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001519 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001520 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 goto finally;
1522 }
1523 }
1524
Victor Stinner35466c52010-04-22 11:23:23 +00001525 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1526 if (!uppercase_name_str)
1527 goto finally;
1528
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001529 if (callable != Py_None && !PyCallable_Check(callable)) {
1530 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1531 goto finally;
1532 }
1533
1534 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001535 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1536 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001538 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1539 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001540 }
1541
1542 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001543 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001544 SQLITE_UTF8,
1545 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001546 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001547 if (rc != SQLITE_OK) {
1548 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001549 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001550 goto finally;
1551 }
1552
1553finally:
1554 Py_XDECREF(uppercase_name);
1555
1556 if (PyErr_Occurred()) {
1557 retval = NULL;
1558 } else {
1559 Py_INCREF(Py_None);
1560 retval = Py_None;
1561 }
1562
1563 return retval;
1564}
1565
Christian Heimesbbe741d2008-03-28 10:53:29 +00001566/* Called when the connection is used as a context manager. Returns itself as a
1567 * convenience to the caller. */
1568static PyObject *
1569pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1570{
1571 Py_INCREF(self);
1572 return (PyObject*)self;
1573}
1574
1575/** Called when the connection is used as a context manager. If there was any
1576 * exception, a rollback takes place; otherwise we commit. */
1577static PyObject *
1578pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1579{
1580 PyObject* exc_type, *exc_value, *exc_tb;
1581 char* method_name;
1582 PyObject* result;
1583
1584 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1585 return NULL;
1586 }
1587
1588 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1589 method_name = "commit";
1590 } else {
1591 method_name = "rollback";
1592 }
1593
Victor Stinner3466bde2016-09-05 18:16:01 -07001594 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001595 if (!result) {
1596 return NULL;
1597 }
1598 Py_DECREF(result);
1599
1600 Py_RETURN_FALSE;
1601}
1602
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001603static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001604PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001605
1606static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001607 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1608 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001609 {NULL}
1610};
1611
1612static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001613 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001614 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001615 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001616 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001617 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001618 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001619 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001620 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001621 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001622 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001623 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001624 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001625 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001626 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001627 #ifdef HAVE_LOAD_EXTENSION
1628 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1629 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1630 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1631 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1632 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001633 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1634 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001635 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1636 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001637 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001638 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001639 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001640 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001641 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001642 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001643 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001644 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001645 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001646 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001647 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001648 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001649 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1650 PyDoc_STR("For context manager. Non-standard.")},
1651 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1652 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001653 {NULL, NULL}
1654};
1655
1656static struct PyMemberDef connection_members[] =
1657{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001658 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1659 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1660 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1661 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1662 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1663 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1664 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1665 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1666 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1667 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001668 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1669 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001670 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001671 {NULL}
1672};
1673
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001674PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001675 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001676 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001677 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001678 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001679 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001680 0, /* tp_print */
1681 0, /* tp_getattr */
1682 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001683 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001684 0, /* tp_repr */
1685 0, /* tp_as_number */
1686 0, /* tp_as_sequence */
1687 0, /* tp_as_mapping */
1688 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001689 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001690 0, /* tp_str */
1691 0, /* tp_getattro */
1692 0, /* tp_setattro */
1693 0, /* tp_as_buffer */
1694 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1695 connection_doc, /* tp_doc */
1696 0, /* tp_traverse */
1697 0, /* tp_clear */
1698 0, /* tp_richcompare */
1699 0, /* tp_weaklistoffset */
1700 0, /* tp_iter */
1701 0, /* tp_iternext */
1702 connection_methods, /* tp_methods */
1703 connection_members, /* tp_members */
1704 connection_getset, /* tp_getset */
1705 0, /* tp_base */
1706 0, /* tp_dict */
1707 0, /* tp_descr_get */
1708 0, /* tp_descr_set */
1709 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001710 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001711 0, /* tp_alloc */
1712 0, /* tp_new */
1713 0 /* tp_free */
1714};
1715
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001716extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001717{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001718 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1719 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001720}