blob: 3c52108c10ea4f4beb728b9c132166bc788fc26f [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
Thomas Wouters477c8d52006-05-27 19:21:47 +0000205/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000206void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000208 pysqlite_Node* node;
209 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000210
211 node = self->statement_cache->first;
212
213 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000214 statement = (pysqlite_Statement*)(node->data);
215 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000216 node = node->next;
217 }
218
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300219 Py_SETREF(self->statement_cache,
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200220 (pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221 Py_DECREF(self);
222 self->statement_cache->decref_factory = 0;
223}
224
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000225/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000226void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000227{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000228 int i;
229 PyObject* weakref;
230 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000231 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232
Thomas Wouters477c8d52006-05-27 19:21:47 +0000233 for (i = 0; i < PyList_Size(self->statements); i++) {
234 weakref = PyList_GetItem(self->statements, i);
235 statement = PyWeakref_GetObject(weakref);
236 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500237 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000238 if (action == ACTION_RESET) {
239 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
240 } else {
241 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
242 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500243 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000244 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000246
247 if (reset_cursors) {
248 for (i = 0; i < PyList_Size(self->cursors); i++) {
249 weakref = PyList_GetItem(self->cursors, i);
250 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
251 if ((PyObject*)cursor != Py_None) {
252 cursor->reset = 1;
253 }
254 }
255 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256}
257
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000258void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259{
260 Py_XDECREF(self->statement_cache);
261
262 /* Clean up if user has not called .close() explicitly. */
263 if (self->db) {
264 Py_BEGIN_ALLOW_THREADS
265 sqlite3_close(self->db);
266 Py_END_ALLOW_THREADS
267 }
268
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269 Py_XDECREF(self->isolation_level);
270 Py_XDECREF(self->function_pinboard);
271 Py_XDECREF(self->row_factory);
272 Py_XDECREF(self->text_factory);
273 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000274 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000275 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000276
Christian Heimes90aa7642007-12-19 02:45:37 +0000277 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000278}
279
Gerhard Häringf9cee222010-03-05 15:20:03 +0000280/*
281 * Registers a cursor with the connection.
282 *
283 * 0 => error; 1 => ok
284 */
285int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
286{
287 PyObject* weakref;
288
289 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
290 if (!weakref) {
291 goto error;
292 }
293
294 if (PyList_Append(connection->cursors, weakref) != 0) {
295 Py_CLEAR(weakref);
296 goto error;
297 }
298
299 Py_DECREF(weakref);
300
301 return 1;
302error:
303 return 0;
304}
305
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000306PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300308 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000309 PyObject* factory = NULL;
310 PyObject* cursor;
311
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
313 &factory)) {
314 return NULL;
315 }
316
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000317 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 return NULL;
319 }
320
321 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000322 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 }
324
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300325 cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL);
326 if (cursor == NULL)
327 return NULL;
328 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
329 PyErr_Format(PyExc_TypeError,
330 "factory must return a cursor, not %.100s",
331 Py_TYPE(cursor)->tp_name);
332 Py_DECREF(cursor);
333 return NULL;
334 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335
Gerhard Häringf9cee222010-03-05 15:20:03 +0000336 _pysqlite_drop_unused_cursor_references(self);
337
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300340 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341 }
342
343 return cursor;
344}
345
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000346PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347{
348 int rc;
349
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000350 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351 return NULL;
352 }
353
Gerhard Häringf9cee222010-03-05 15:20:03 +0000354 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355
356 if (self->db) {
357 Py_BEGIN_ALLOW_THREADS
358 rc = sqlite3_close(self->db);
359 Py_END_ALLOW_THREADS
360
361 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000362 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 return NULL;
364 } else {
365 self->db = NULL;
366 }
367 }
368
369 Py_INCREF(Py_None);
370 return Py_None;
371}
372
373/*
374 * Checks if a connection object is usable (i. e. not closed).
375 *
376 * 0 => error; 1 => ok
377 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000378int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000380 if (!con->initialized) {
381 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
382 return 0;
383 }
384
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000386 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000387 return 0;
388 } else {
389 return 1;
390 }
391}
392
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000393PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394{
395 int rc;
396 const char* tail;
397 sqlite3_stmt* statement;
398
399 Py_BEGIN_ALLOW_THREADS
400 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
401 Py_END_ALLOW_THREADS
402
403 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000404 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405 goto error;
406 }
407
Benjamin Petersond7b03282008-09-13 15:58:53 +0000408 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 if (rc == SQLITE_DONE) {
410 self->inTransaction = 1;
411 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000412 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413 }
414
415 Py_BEGIN_ALLOW_THREADS
416 rc = sqlite3_finalize(statement);
417 Py_END_ALLOW_THREADS
418
419 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000420 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000421 }
422
423error:
424 if (PyErr_Occurred()) {
425 return NULL;
426 } else {
427 Py_INCREF(Py_None);
428 return Py_None;
429 }
430}
431
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000432PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000433{
434 int rc;
435 const char* tail;
436 sqlite3_stmt* statement;
437
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000438 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439 return NULL;
440 }
441
442 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000443
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000444 Py_BEGIN_ALLOW_THREADS
445 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
446 Py_END_ALLOW_THREADS
447 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000448 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000449 goto error;
450 }
451
Benjamin Petersond7b03282008-09-13 15:58:53 +0000452 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000453 if (rc == SQLITE_DONE) {
454 self->inTransaction = 0;
455 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000456 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457 }
458
459 Py_BEGIN_ALLOW_THREADS
460 rc = sqlite3_finalize(statement);
461 Py_END_ALLOW_THREADS
462 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000463 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 }
465
466 }
467
468error:
469 if (PyErr_Occurred()) {
470 return NULL;
471 } else {
472 Py_INCREF(Py_None);
473 return Py_None;
474 }
475}
476
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000477PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000478{
479 int rc;
480 const char* tail;
481 sqlite3_stmt* statement;
482
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000483 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000484 return NULL;
485 }
486
487 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000488 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000489
490 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000491 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492 Py_END_ALLOW_THREADS
493 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000494 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000495 goto error;
496 }
497
Benjamin Petersond7b03282008-09-13 15:58:53 +0000498 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499 if (rc == SQLITE_DONE) {
500 self->inTransaction = 0;
501 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000502 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503 }
504
505 Py_BEGIN_ALLOW_THREADS
506 rc = sqlite3_finalize(statement);
507 Py_END_ALLOW_THREADS
508 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000509 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000510 }
511
512 }
513
514error:
515 if (PyErr_Occurred()) {
516 return NULL;
517 } else {
518 Py_INCREF(Py_None);
519 return Py_None;
520 }
521}
522
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200523static int
524_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000525{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200526 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000528 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200529 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
530 if (value == -1 && PyErr_Occurred())
531 return -1;
532 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533 } else if (PyFloat_Check(py_val)) {
534 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000535 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200536 const char *str = _PyUnicode_AsString(py_val);
537 if (str == NULL)
538 return -1;
539 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000540 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200541 Py_buffer view;
542 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100543 PyErr_SetString(PyExc_ValueError,
544 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200545 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200547 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100548 PyErr_SetString(PyExc_OverflowError,
549 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200550 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100551 return -1;
552 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200553 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
554 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000555 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200556 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000557 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200558 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000559}
560
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000561PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562{
563 PyObject* args;
564 int i;
565 sqlite3_value* cur_value;
566 PyObject* cur_py_value;
567 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000568 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569
570 args = PyTuple_New(argc);
571 if (!args) {
572 return NULL;
573 }
574
575 for (i = 0; i < argc; i++) {
576 cur_value = argv[i];
577 switch (sqlite3_value_type(argv[i])) {
578 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200579 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000580 break;
581 case SQLITE_FLOAT:
582 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
583 break;
584 case SQLITE_TEXT:
585 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000586 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587 /* TODO: have a way to show errors here */
588 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000589 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590 Py_INCREF(Py_None);
591 cur_py_value = Py_None;
592 }
593 break;
594 case SQLITE_BLOB:
595 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000596 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000597 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598 break;
599 case SQLITE_NULL:
600 default:
601 Py_INCREF(Py_None);
602 cur_py_value = Py_None;
603 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604
605 if (!cur_py_value) {
606 Py_DECREF(args);
607 return NULL;
608 }
609
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610 PyTuple_SetItem(args, i, cur_py_value);
611
612 }
613
614 return args;
615}
616
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000617void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000618{
619 PyObject* args;
620 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000621 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200622 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623
Georg Brandldfd73442009-04-05 11:47:34 +0000624#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000625 PyGILState_STATE threadstate;
626
627 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000628#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629
630 py_func = (PyObject*)sqlite3_user_data(context);
631
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000632 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000633 if (args) {
634 py_retval = PyObject_CallObject(py_func, args);
635 Py_DECREF(args);
636 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200638 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000639 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200640 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200642 }
643 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000644 if (_enable_callback_tracebacks) {
645 PyErr_Print();
646 } else {
647 PyErr_Clear();
648 }
649 _sqlite3_result_error(context, "user-defined function raised exception", -1);
650 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651
Georg Brandldfd73442009-04-05 11:47:34 +0000652#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000653 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000654#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655}
656
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000657static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658{
659 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661 PyObject* aggregate_class;
662 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000663 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000664
Georg Brandldfd73442009-04-05 11:47:34 +0000665#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666 PyGILState_STATE threadstate;
667
668 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000669#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670
671 aggregate_class = (PyObject*)sqlite3_user_data(context);
672
673 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
674
675 if (*aggregate_instance == 0) {
676 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
677
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000679 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000680 if (_enable_callback_tracebacks) {
681 PyErr_Print();
682 } else {
683 PyErr_Clear();
684 }
685 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000687 }
688 }
689
690 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691 if (!stepmethod) {
692 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000693 }
694
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000695 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000696 if (!args) {
697 goto error;
698 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000699
700 function_result = PyObject_CallObject(stepmethod, args);
701 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000702
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000704 if (_enable_callback_tracebacks) {
705 PyErr_Print();
706 } else {
707 PyErr_Clear();
708 }
709 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 }
711
Thomas Wouters477c8d52006-05-27 19:21:47 +0000712error:
713 Py_XDECREF(stepmethod);
714 Py_XDECREF(function_result);
715
Georg Brandldfd73442009-04-05 11:47:34 +0000716#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000717 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000718#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719}
720
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000721void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200723 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000724 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200725 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200726 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200727 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200728 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729
Georg Brandldfd73442009-04-05 11:47:34 +0000730#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000731 PyGILState_STATE threadstate;
732
733 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000734#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000736 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
737 if (!*aggregate_instance) {
738 /* this branch is executed if there was an exception in the aggregate's
739 * __init__ */
740
Thomas Wouters477c8d52006-05-27 19:21:47 +0000741 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000742 }
743
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200744 /* Keep the exception (if any) of the last call to step() */
745 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200746 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200747
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200748 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200749
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200750 Py_DECREF(*aggregate_instance);
751
752 ok = 0;
753 if (function_result) {
754 ok = _pysqlite_set_result(context, function_result) == 0;
755 Py_DECREF(function_result);
756 }
757 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000758 if (_enable_callback_tracebacks) {
759 PyErr_Print();
760 } else {
761 PyErr_Clear();
762 }
763 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200764#if SQLITE_VERSION_NUMBER < 3003003
765 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
766 exception, so don't restore the previous exception */
767 restore = 0;
768#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000769 }
770
Victor Stinnerffff7632013-08-02 01:48:10 +0200771 if (restore) {
772 /* Restore the exception (if any) of the last call to step(),
773 but clear also the current exception if finalize() failed */
774 PyErr_Restore(exception, value, tb);
775 }
Victor Stinner3a857322013-07-22 08:34:32 +0200776
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777error:
Georg Brandldfd73442009-04-05 11:47:34 +0000778#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000779 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000780#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200781 /* explicit return to avoid a compilation error if WITH_THREAD
782 is not defined */
783 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000784}
785
Gerhard Häringf9cee222010-03-05 15:20:03 +0000786static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787{
788 PyObject* new_list;
789 PyObject* weakref;
790 int i;
791
792 /* we only need to do this once in a while */
793 if (self->created_statements++ < 200) {
794 return;
795 }
796
797 self->created_statements = 0;
798
799 new_list = PyList_New(0);
800 if (!new_list) {
801 return;
802 }
803
804 for (i = 0; i < PyList_Size(self->statements); i++) {
805 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000806 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807 if (PyList_Append(new_list, weakref) != 0) {
808 Py_DECREF(new_list);
809 return;
810 }
811 }
812 }
813
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300814 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000815}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000816
Gerhard Häringf9cee222010-03-05 15:20:03 +0000817static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
818{
819 PyObject* new_list;
820 PyObject* weakref;
821 int i;
822
823 /* we only need to do this once in a while */
824 if (self->created_cursors++ < 200) {
825 return;
826 }
827
828 self->created_cursors = 0;
829
830 new_list = PyList_New(0);
831 if (!new_list) {
832 return;
833 }
834
835 for (i = 0; i < PyList_Size(self->cursors); i++) {
836 weakref = PyList_GetItem(self->cursors, i);
837 if (PyWeakref_GetObject(weakref) != Py_None) {
838 if (PyList_Append(new_list, weakref) != 0) {
839 Py_DECREF(new_list);
840 return;
841 }
842 }
843 }
844
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300845 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000846}
847
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000848PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000849{
850 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
851
852 PyObject* func;
853 char* name;
854 int narg;
855 int rc;
856
Gerhard Häringf9cee222010-03-05 15:20:03 +0000857 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
858 return NULL;
859 }
860
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
862 &name, &narg, &func))
863 {
864 return NULL;
865 }
866
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000867 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868
Thomas Wouters477c8d52006-05-27 19:21:47 +0000869 if (rc != SQLITE_OK) {
870 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000871 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872 return NULL;
873 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000874 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
875 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000876
Thomas Wouters477c8d52006-05-27 19:21:47 +0000877 Py_INCREF(Py_None);
878 return Py_None;
879 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880}
881
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000882PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883{
884 PyObject* aggregate_class;
885
886 int n_arg;
887 char* name;
888 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
889 int rc;
890
Gerhard Häringf9cee222010-03-05 15:20:03 +0000891 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
892 return NULL;
893 }
894
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000895 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
896 kwlist, &name, &n_arg, &aggregate_class)) {
897 return NULL;
898 }
899
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000900 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 +0000901 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000902 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000903 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000904 return NULL;
905 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000906 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
907 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000908
909 Py_INCREF(Py_None);
910 return Py_None;
911 }
912}
913
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000914static 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 +0000915{
916 PyObject *ret;
917 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000918#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000919 PyGILState_STATE gilstate;
920
921 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000922#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000923
Victor Stinnerd4095d92013-07-26 22:23:33 +0200924 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000925
Victor Stinnerd4095d92013-07-26 22:23:33 +0200926 if (ret == NULL) {
927 if (_enable_callback_tracebacks)
928 PyErr_Print();
929 else
930 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200931
Victor Stinnerd4095d92013-07-26 22:23:33 +0200932 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200933 }
934 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200935 if (PyLong_Check(ret)) {
936 rc = _PyLong_AsInt(ret);
937 if (rc == -1 && PyErr_Occurred()) {
938 if (_enable_callback_tracebacks)
939 PyErr_Print();
940 else
941 PyErr_Clear();
942 rc = SQLITE_DENY;
943 }
944 }
945 else {
946 rc = SQLITE_DENY;
947 }
948 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000949 }
950
Georg Brandldfd73442009-04-05 11:47:34 +0000951#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000952 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000953#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000954 return rc;
955}
956
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000957static int _progress_handler(void* user_arg)
958{
959 int rc;
960 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000961#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000962 PyGILState_STATE gilstate;
963
964 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000965#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000966 ret = PyObject_CallFunction((PyObject*)user_arg, "");
967
968 if (!ret) {
969 if (_enable_callback_tracebacks) {
970 PyErr_Print();
971 } else {
972 PyErr_Clear();
973 }
974
Mark Dickinson934896d2009-02-21 20:59:32 +0000975 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000976 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000977 } else {
978 rc = (int)PyObject_IsTrue(ret);
979 Py_DECREF(ret);
980 }
981
Georg Brandldfd73442009-04-05 11:47:34 +0000982#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000983 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000984#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000985 return rc;
986}
987
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200988static void _trace_callback(void* user_arg, const char* statement_string)
989{
990 PyObject *py_statement = NULL;
991 PyObject *ret = NULL;
992
993#ifdef WITH_THREAD
994 PyGILState_STATE gilstate;
995
996 gilstate = PyGILState_Ensure();
997#endif
998 py_statement = PyUnicode_DecodeUTF8(statement_string,
999 strlen(statement_string), "replace");
1000 if (py_statement) {
1001 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
1002 Py_DECREF(py_statement);
1003 }
1004
1005 if (ret) {
1006 Py_DECREF(ret);
1007 } else {
1008 if (_enable_callback_tracebacks) {
1009 PyErr_Print();
1010 } else {
1011 PyErr_Clear();
1012 }
1013 }
1014
1015#ifdef WITH_THREAD
1016 PyGILState_Release(gilstate);
1017#endif
1018}
1019
Gerhard Häringf9cee222010-03-05 15:20:03 +00001020static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001021{
1022 PyObject* authorizer_cb;
1023
1024 static char *kwlist[] = { "authorizer_callback", NULL };
1025 int rc;
1026
Gerhard Häringf9cee222010-03-05 15:20:03 +00001027 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1028 return NULL;
1029 }
1030
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001031 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1032 kwlist, &authorizer_cb)) {
1033 return NULL;
1034 }
1035
1036 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1037
1038 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001039 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001040 return NULL;
1041 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001042 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1043 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001044
1045 Py_INCREF(Py_None);
1046 return Py_None;
1047 }
1048}
1049
Gerhard Häringf9cee222010-03-05 15:20:03 +00001050static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001051{
1052 PyObject* progress_handler;
1053 int n;
1054
1055 static char *kwlist[] = { "progress_handler", "n", NULL };
1056
Gerhard Häringf9cee222010-03-05 15:20:03 +00001057 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1058 return NULL;
1059 }
1060
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001061 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1062 kwlist, &progress_handler, &n)) {
1063 return NULL;
1064 }
1065
1066 if (progress_handler == Py_None) {
1067 /* None clears the progress handler previously set */
1068 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1069 } else {
1070 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001071 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1072 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001073 }
1074
1075 Py_INCREF(Py_None);
1076 return Py_None;
1077}
1078
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001079static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1080{
1081 PyObject* trace_callback;
1082
1083 static char *kwlist[] = { "trace_callback", NULL };
1084
1085 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1086 return NULL;
1087 }
1088
1089 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1090 kwlist, &trace_callback)) {
1091 return NULL;
1092 }
1093
1094 if (trace_callback == Py_None) {
1095 /* None clears the trace callback previously set */
1096 sqlite3_trace(self->db, 0, (void*)0);
1097 } else {
1098 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1099 return NULL;
1100 sqlite3_trace(self->db, _trace_callback, trace_callback);
1101 }
1102
1103 Py_INCREF(Py_None);
1104 return Py_None;
1105}
1106
Gerhard Häringf9cee222010-03-05 15:20:03 +00001107#ifdef HAVE_LOAD_EXTENSION
1108static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1109{
1110 int rc;
1111 int onoff;
1112
1113 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1114 return NULL;
1115 }
1116
1117 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1118 return NULL;
1119 }
1120
1121 rc = sqlite3_enable_load_extension(self->db, onoff);
1122
1123 if (rc != SQLITE_OK) {
1124 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1125 return NULL;
1126 } else {
1127 Py_INCREF(Py_None);
1128 return Py_None;
1129 }
1130}
1131
1132static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1133{
1134 int rc;
1135 char* extension_name;
1136 char* errmsg;
1137
1138 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1139 return NULL;
1140 }
1141
1142 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1143 return NULL;
1144 }
1145
1146 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1147 if (rc != 0) {
1148 PyErr_SetString(pysqlite_OperationalError, errmsg);
1149 return NULL;
1150 } else {
1151 Py_INCREF(Py_None);
1152 return Py_None;
1153 }
1154}
1155#endif
1156
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001157int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001158{
Georg Brandldfd73442009-04-05 11:47:34 +00001159#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001160 if (self->check_same_thread) {
1161 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001162 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163 "SQLite objects created in a thread can only be used in that same thread."
1164 "The object was created in thread id %ld and this is thread id %ld",
1165 self->thread_ident, PyThread_get_thread_ident());
1166 return 0;
1167 }
1168
1169 }
Georg Brandldfd73442009-04-05 11:47:34 +00001170#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001171 return 1;
1172}
1173
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001174static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001175{
1176 Py_INCREF(self->isolation_level);
1177 return self->isolation_level;
1178}
1179
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001180static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001182 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001183 return NULL;
1184 } else {
1185 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1186 }
1187}
1188
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001189static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001190{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001191 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001192 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001193 if (!res) {
1194 return -1;
1195 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001196 Py_DECREF(res);
1197
Serhiy Storchaka28914922016-09-01 22:18:03 +03001198 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001199 self->inTransaction = 0;
1200 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001201 const char * const *candidate;
1202 PyObject *uppercase_level;
1203 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001204
Serhiy Storchaka28914922016-09-01 22:18:03 +03001205 if (!PyUnicode_Check(isolation_level)) {
1206 PyErr_Format(PyExc_TypeError,
1207 "isolation_level must be a string or None, not %.100s",
1208 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001209 return -1;
1210 }
1211
Serhiy Storchaka28914922016-09-01 22:18:03 +03001212 uppercase_level = _PyObject_CallMethodIdObjArgs(
1213 (PyObject *)&PyUnicode_Type, &PyId_upper,
1214 isolation_level, NULL);
1215 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001216 return -1;
1217 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001218 for (candidate = begin_statements; *candidate; candidate++) {
1219 if (!PyUnicode_CompareWithASCIIString(uppercase_level, *candidate + 6))
1220 break;
1221 }
1222 Py_DECREF(uppercase_level);
1223 if (!*candidate) {
1224 PyErr_SetString(PyExc_ValueError,
1225 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001226 return -1;
1227 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001228 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001229 }
1230
Serhiy Storchaka28914922016-09-01 22:18:03 +03001231 Py_INCREF(isolation_level);
1232 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001233 return 0;
1234}
1235
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001236PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001237{
1238 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001239 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001240 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001241 int rc;
1242
Gerhard Häringf9cee222010-03-05 15:20:03 +00001243 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1244 return NULL;
1245 }
1246
Larry Hastings3b12e952015-05-08 07:45:10 -07001247 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection()", kwargs))
1248 return NULL;
1249
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001250 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001251 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001253 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001254
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001255 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001256 if (!statement) {
1257 return NULL;
1258 }
1259
Victor Stinner0201f442010-03-13 03:28:34 +00001260 statement->db = NULL;
1261 statement->st = NULL;
1262 statement->sql = NULL;
1263 statement->in_use = 0;
1264 statement->in_weakreflist = NULL;
1265
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001266 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001267 if (rc != SQLITE_OK) {
1268 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001269 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001271 if (PyErr_ExceptionMatches(PyExc_TypeError))
1272 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001273 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001274 (void)pysqlite_statement_reset(statement);
1275 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001276 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001277 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001278 }
1279
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001280 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1281 if (weakref == NULL)
1282 goto error;
1283 if (PyList_Append(self->statements, weakref) != 0) {
1284 Py_DECREF(weakref);
1285 goto error;
1286 }
1287 Py_DECREF(weakref);
1288
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001289 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001290
1291error:
1292 Py_DECREF(statement);
1293 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001294}
1295
Larry Hastings01b08832015-05-08 07:37:49 -07001296PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001297{
1298 PyObject* cursor = 0;
1299 PyObject* result = 0;
1300 PyObject* method = 0;
1301
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001302 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001303 if (!cursor) {
1304 goto error;
1305 }
1306
1307 method = PyObject_GetAttrString(cursor, "execute");
1308 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001309 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001310 goto error;
1311 }
1312
1313 result = PyObject_CallObject(method, args);
1314 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001315 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001316 }
1317
1318error:
1319 Py_XDECREF(result);
1320 Py_XDECREF(method);
1321
1322 return cursor;
1323}
1324
Larry Hastings01b08832015-05-08 07:37:49 -07001325PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001326{
1327 PyObject* cursor = 0;
1328 PyObject* result = 0;
1329 PyObject* method = 0;
1330
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001331 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001332 if (!cursor) {
1333 goto error;
1334 }
1335
1336 method = PyObject_GetAttrString(cursor, "executemany");
1337 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001338 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001339 goto error;
1340 }
1341
1342 result = PyObject_CallObject(method, args);
1343 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001344 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001345 }
1346
1347error:
1348 Py_XDECREF(result);
1349 Py_XDECREF(method);
1350
1351 return cursor;
1352}
1353
Larry Hastings01b08832015-05-08 07:37:49 -07001354PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001355{
1356 PyObject* cursor = 0;
1357 PyObject* result = 0;
1358 PyObject* method = 0;
1359
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001360 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001361 if (!cursor) {
1362 goto error;
1363 }
1364
1365 method = PyObject_GetAttrString(cursor, "executescript");
1366 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001367 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001368 goto error;
1369 }
1370
1371 result = PyObject_CallObject(method, args);
1372 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001373 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001374 }
1375
1376error:
1377 Py_XDECREF(result);
1378 Py_XDECREF(method);
1379
1380 return cursor;
1381}
1382
1383/* ------------------------- COLLATION CODE ------------------------ */
1384
1385static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001386pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001387 void* context,
1388 int text1_length, const void* text1_data,
1389 int text2_length, const void* text2_data)
1390{
1391 PyObject* callback = (PyObject*)context;
1392 PyObject* string1 = 0;
1393 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001394#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001395 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001396#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001397 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001398 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001399 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001400#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001401 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001402#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001403
1404 if (PyErr_Occurred()) {
1405 goto finally;
1406 }
1407
Guido van Rossum98297ee2007-11-06 21:34:58 +00001408 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1409 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001410
1411 if (!string1 || !string2) {
1412 goto finally; /* failed to allocate strings */
1413 }
1414
1415 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1416
1417 if (!retval) {
1418 /* execution failed */
1419 goto finally;
1420 }
1421
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001422 longval = PyLong_AsLongAndOverflow(retval, &result);
1423 if (longval == -1 && PyErr_Occurred()) {
1424 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001425 result = 0;
1426 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001427 else if (!result) {
1428 if (longval > 0)
1429 result = 1;
1430 else if (longval < 0)
1431 result = -1;
1432 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001433
1434finally:
1435 Py_XDECREF(string1);
1436 Py_XDECREF(string2);
1437 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001438#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001439 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001440#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001441 return result;
1442}
1443
1444static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001445pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001446{
1447 PyObject* retval = NULL;
1448
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001449 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001450 goto finally;
1451 }
1452
1453 sqlite3_interrupt(self->db);
1454
1455 Py_INCREF(Py_None);
1456 retval = Py_None;
1457
1458finally:
1459 return retval;
1460}
1461
Christian Heimesbbe741d2008-03-28 10:53:29 +00001462/* Function author: Paul Kippes <kippesp@gmail.com>
1463 * Class method of Connection to call the Python function _iterdump
1464 * of the sqlite3 module.
1465 */
1466static PyObject *
1467pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1468{
1469 PyObject* retval = NULL;
1470 PyObject* module = NULL;
1471 PyObject* module_dict;
1472 PyObject* pyfn_iterdump;
1473
1474 if (!pysqlite_check_connection(self)) {
1475 goto finally;
1476 }
1477
1478 module = PyImport_ImportModule(MODULE_NAME ".dump");
1479 if (!module) {
1480 goto finally;
1481 }
1482
1483 module_dict = PyModule_GetDict(module);
1484 if (!module_dict) {
1485 goto finally;
1486 }
1487
1488 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1489 if (!pyfn_iterdump) {
1490 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1491 goto finally;
1492 }
1493
1494 args = PyTuple_New(1);
1495 if (!args) {
1496 goto finally;
1497 }
1498 Py_INCREF(self);
1499 PyTuple_SetItem(args, 0, (PyObject*)self);
1500 retval = PyObject_CallObject(pyfn_iterdump, args);
1501
1502finally:
1503 Py_XDECREF(args);
1504 Py_XDECREF(module);
1505 return retval;
1506}
1507
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001508static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001509pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001510{
1511 PyObject* callable;
1512 PyObject* uppercase_name = 0;
1513 PyObject* name;
1514 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001515 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001516 _Py_IDENTIFIER(upper);
Victor Stinner35466c52010-04-22 11:23:23 +00001517 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001518 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001519 unsigned int kind;
1520 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001522 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523 goto finally;
1524 }
1525
Gerhard Häring6d214562007-08-10 18:15:11 +00001526 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001527 goto finally;
1528 }
1529
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001530 uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001531 if (!uppercase_name) {
1532 goto finally;
1533 }
1534
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001535 if (PyUnicode_READY(uppercase_name))
1536 goto finally;
1537 len = PyUnicode_GET_LENGTH(uppercase_name);
1538 kind = PyUnicode_KIND(uppercase_name);
1539 data = PyUnicode_DATA(uppercase_name);
1540 for (i=0; i<len; i++) {
1541 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1542 if ((ch >= '0' && ch <= '9')
1543 || (ch >= 'A' && ch <= 'Z')
1544 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001545 {
Victor Stinner35466c52010-04-22 11:23:23 +00001546 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001547 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001548 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001549 goto finally;
1550 }
1551 }
1552
Victor Stinner35466c52010-04-22 11:23:23 +00001553 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1554 if (!uppercase_name_str)
1555 goto finally;
1556
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001557 if (callable != Py_None && !PyCallable_Check(callable)) {
1558 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1559 goto finally;
1560 }
1561
1562 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001563 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1564 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001565 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001566 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1567 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001568 }
1569
1570 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001571 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001572 SQLITE_UTF8,
1573 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001574 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001575 if (rc != SQLITE_OK) {
1576 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001577 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001578 goto finally;
1579 }
1580
1581finally:
1582 Py_XDECREF(uppercase_name);
1583
1584 if (PyErr_Occurred()) {
1585 retval = NULL;
1586 } else {
1587 Py_INCREF(Py_None);
1588 retval = Py_None;
1589 }
1590
1591 return retval;
1592}
1593
Christian Heimesbbe741d2008-03-28 10:53:29 +00001594/* Called when the connection is used as a context manager. Returns itself as a
1595 * convenience to the caller. */
1596static PyObject *
1597pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1598{
1599 Py_INCREF(self);
1600 return (PyObject*)self;
1601}
1602
1603/** Called when the connection is used as a context manager. If there was any
1604 * exception, a rollback takes place; otherwise we commit. */
1605static PyObject *
1606pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1607{
1608 PyObject* exc_type, *exc_value, *exc_tb;
1609 char* method_name;
1610 PyObject* result;
1611
1612 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1613 return NULL;
1614 }
1615
1616 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1617 method_name = "commit";
1618 } else {
1619 method_name = "rollback";
1620 }
1621
1622 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1623 if (!result) {
1624 return NULL;
1625 }
1626 Py_DECREF(result);
1627
1628 Py_RETURN_FALSE;
1629}
1630
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001631static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001632PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001633
1634static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001635 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1636 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001637 {NULL}
1638};
1639
1640static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001641 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001642 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001643 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001644 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001645 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001646 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001647 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001648 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001649 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001650 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001651 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001652 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001653 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001654 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001655 #ifdef HAVE_LOAD_EXTENSION
1656 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1657 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1658 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1659 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1660 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001661 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1662 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001663 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1664 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001665 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001666 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001667 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001668 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001669 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001670 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001671 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001672 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001673 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001674 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001675 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001676 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001677 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1678 PyDoc_STR("For context manager. Non-standard.")},
1679 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1680 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001681 {NULL, NULL}
1682};
1683
1684static struct PyMemberDef connection_members[] =
1685{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001686 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1687 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1688 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1689 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1690 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1691 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1692 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1693 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1694 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1695 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001696 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1697 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001698 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001699 {NULL}
1700};
1701
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001702PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001703 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001704 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001705 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001706 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001707 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001708 0, /* tp_print */
1709 0, /* tp_getattr */
1710 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001711 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001712 0, /* tp_repr */
1713 0, /* tp_as_number */
1714 0, /* tp_as_sequence */
1715 0, /* tp_as_mapping */
1716 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001717 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001718 0, /* tp_str */
1719 0, /* tp_getattro */
1720 0, /* tp_setattro */
1721 0, /* tp_as_buffer */
1722 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1723 connection_doc, /* tp_doc */
1724 0, /* tp_traverse */
1725 0, /* tp_clear */
1726 0, /* tp_richcompare */
1727 0, /* tp_weaklistoffset */
1728 0, /* tp_iter */
1729 0, /* tp_iternext */
1730 connection_methods, /* tp_methods */
1731 connection_members, /* tp_members */
1732 connection_getset, /* tp_getset */
1733 0, /* tp_base */
1734 0, /* tp_dict */
1735 0, /* tp_descr_get */
1736 0, /* tp_descr_set */
1737 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001738 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001739 0, /* tp_alloc */
1740 0, /* tp_new */
1741 0 /* tp_free */
1742};
1743
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001744extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001745{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001746 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1747 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001748}