blob: 6aa4764b6c6b0b01cfbc772bdb462caefd73e0b0 [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
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000046static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Gerhard Häringf9cee222010-03-05 15:20:03 +000047static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048
Thomas Wouters0e3f5912006-08-11 14:57:12 +000049
Benjamin Petersond7b03282008-09-13 15:58:53 +000050static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000051{
52 /* in older SQLite versions, calling sqlite3_result_error in callbacks
53 * triggers a bug in SQLite that leads either to irritating results or
54 * segfaults, depending on the SQLite version */
55#if SQLITE_VERSION_NUMBER >= 3003003
56 sqlite3_result_error(ctx, errmsg, len);
57#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000058 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059#endif
60}
61
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000062int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010064 static char *kwlist[] = {
65 "database", "timeout", "detect_types", "isolation_level",
66 "check_same_thread", "factory", "cached_statements", "uri",
67 NULL
68 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069
70 char* database;
71 int detect_types = 0;
72 PyObject* isolation_level = NULL;
73 PyObject* factory = NULL;
74 int check_same_thread = 1;
75 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010076 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077 double timeout = 5.0;
78 int rc;
79
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010080 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOip", kwlist,
81 &database, &timeout, &detect_types,
82 &isolation_level, &check_same_thread,
83 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000085 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 }
87
Gerhard Häringf9cee222010-03-05 15:20:03 +000088 self->initialized = 1;
89
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000090 self->begin_statement = NULL;
91
92 self->statement_cache = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000093 self->statements = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +000094 self->cursors = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000095
96 Py_INCREF(Py_None);
97 self->row_factory = Py_None;
98
99 Py_INCREF(&PyUnicode_Type);
100 self->text_factory = (PyObject*)&PyUnicode_Type;
101
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100102#ifdef SQLITE_OPEN_URI
103 Py_BEGIN_ALLOW_THREADS
104 rc = sqlite3_open_v2(database, &self->db,
105 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
106 (uri ? SQLITE_OPEN_URI : 0), NULL);
107#else
108 if (uri) {
109 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
110 return -1;
111 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112 Py_BEGIN_ALLOW_THREADS
113 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100114#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 Py_END_ALLOW_THREADS
116
117 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000118 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 return -1;
120 }
121
122 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000123 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 if (!isolation_level) {
125 return -1;
126 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127 } else {
128 Py_INCREF(isolation_level);
129 }
130 self->isolation_level = NULL;
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100131 if (pysqlite_connection_set_isolation_level(self, isolation_level) < 0) {
132 Py_DECREF(isolation_level);
133 return -1;
134 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135 Py_DECREF(isolation_level);
136
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000137 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 if (PyErr_Occurred()) {
139 return -1;
140 }
141
Gerhard Häringf9cee222010-03-05 15:20:03 +0000142 self->created_statements = 0;
143 self->created_cursors = 0;
144
145 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000146 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000147 self->cursors = PyList_New(0);
148 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000149 return -1;
150 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000151
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 /* By default, the Cache class INCREFs the factory in its initializer, and
153 * decrefs it in its deallocator method. Since this would create a circular
154 * reference here, we're breaking it by decrementing self, and telling the
155 * cache class to not decref the factory (self) in its deallocator.
156 */
157 self->statement_cache->decref_factory = 0;
158 Py_DECREF(self);
159
160 self->inTransaction = 0;
161 self->detect_types = detect_types;
162 self->timeout = timeout;
163 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000164#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000165 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000166#endif
Berker Peksag7bea2342016-06-12 14:09:51 +0300167 if (!check_same_thread && sqlite3_libversion_number() < 3003001) {
168 PyErr_SetString(pysqlite_NotSupportedError, "shared connections not available");
169 return -1;
170 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 self->check_same_thread = check_same_thread;
172
173 self->function_pinboard = PyDict_New();
174 if (!self->function_pinboard) {
175 return -1;
176 }
177
178 self->collations = PyDict_New();
179 if (!self->collations) {
180 return -1;
181 }
182
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000183 self->Warning = pysqlite_Warning;
184 self->Error = pysqlite_Error;
185 self->InterfaceError = pysqlite_InterfaceError;
186 self->DatabaseError = pysqlite_DatabaseError;
187 self->DataError = pysqlite_DataError;
188 self->OperationalError = pysqlite_OperationalError;
189 self->IntegrityError = pysqlite_IntegrityError;
190 self->InternalError = pysqlite_InternalError;
191 self->ProgrammingError = pysqlite_ProgrammingError;
192 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193
194 return 0;
195}
196
Thomas Wouters477c8d52006-05-27 19:21:47 +0000197/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000198void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000199{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000200 pysqlite_Node* node;
201 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202
203 node = self->statement_cache->first;
204
205 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000206 statement = (pysqlite_Statement*)(node->data);
207 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208 node = node->next;
209 }
210
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300211 Py_SETREF(self->statement_cache,
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200212 (pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213 Py_DECREF(self);
214 self->statement_cache->decref_factory = 0;
215}
216
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000217/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000218void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000219{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000220 int i;
221 PyObject* weakref;
222 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000223 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000224
Thomas Wouters477c8d52006-05-27 19:21:47 +0000225 for (i = 0; i < PyList_Size(self->statements); i++) {
226 weakref = PyList_GetItem(self->statements, i);
227 statement = PyWeakref_GetObject(weakref);
228 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500229 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000230 if (action == ACTION_RESET) {
231 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
232 } else {
233 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
234 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500235 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000236 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000238
239 if (reset_cursors) {
240 for (i = 0; i < PyList_Size(self->cursors); i++) {
241 weakref = PyList_GetItem(self->cursors, i);
242 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
243 if ((PyObject*)cursor != Py_None) {
244 cursor->reset = 1;
245 }
246 }
247 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248}
249
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000250void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000251{
252 Py_XDECREF(self->statement_cache);
253
254 /* Clean up if user has not called .close() explicitly. */
255 if (self->db) {
256 Py_BEGIN_ALLOW_THREADS
257 sqlite3_close(self->db);
258 Py_END_ALLOW_THREADS
259 }
260
261 if (self->begin_statement) {
262 PyMem_Free(self->begin_statement);
263 }
264 Py_XDECREF(self->isolation_level);
265 Py_XDECREF(self->function_pinboard);
266 Py_XDECREF(self->row_factory);
267 Py_XDECREF(self->text_factory);
268 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000269 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000270 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000271
Christian Heimes90aa7642007-12-19 02:45:37 +0000272 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000273}
274
Gerhard Häringf9cee222010-03-05 15:20:03 +0000275/*
276 * Registers a cursor with the connection.
277 *
278 * 0 => error; 1 => ok
279 */
280int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
281{
282 PyObject* weakref;
283
284 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
285 if (!weakref) {
286 goto error;
287 }
288
289 if (PyList_Append(connection->cursors, weakref) != 0) {
290 Py_CLEAR(weakref);
291 goto error;
292 }
293
294 Py_DECREF(weakref);
295
296 return 1;
297error:
298 return 0;
299}
300
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000301PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302{
303 static char *kwlist[] = {"factory", NULL, NULL};
304 PyObject* factory = NULL;
305 PyObject* cursor;
306
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
308 &factory)) {
309 return NULL;
310 }
311
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000312 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313 return NULL;
314 }
315
316 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000317 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 }
319
320 cursor = PyObject_CallFunction(factory, "O", self);
321
Gerhard Häringf9cee222010-03-05 15:20:03 +0000322 _pysqlite_drop_unused_cursor_references(self);
323
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300326 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327 }
328
329 return cursor;
330}
331
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000332PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333{
334 int rc;
335
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000336 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337 return NULL;
338 }
339
Gerhard Häringf9cee222010-03-05 15:20:03 +0000340 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341
342 if (self->db) {
343 Py_BEGIN_ALLOW_THREADS
344 rc = sqlite3_close(self->db);
345 Py_END_ALLOW_THREADS
346
347 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000348 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000349 return NULL;
350 } else {
351 self->db = NULL;
352 }
353 }
354
355 Py_INCREF(Py_None);
356 return Py_None;
357}
358
359/*
360 * Checks if a connection object is usable (i. e. not closed).
361 *
362 * 0 => error; 1 => ok
363 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000364int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000365{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000366 if (!con->initialized) {
367 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
368 return 0;
369 }
370
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000371 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000372 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000373 return 0;
374 } else {
375 return 1;
376 }
377}
378
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000379PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000380{
381 int rc;
382 const char* tail;
383 sqlite3_stmt* statement;
384
385 Py_BEGIN_ALLOW_THREADS
386 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
387 Py_END_ALLOW_THREADS
388
389 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000390 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000391 goto error;
392 }
393
Benjamin Petersond7b03282008-09-13 15:58:53 +0000394 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000395 if (rc == SQLITE_DONE) {
396 self->inTransaction = 1;
397 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000398 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399 }
400
401 Py_BEGIN_ALLOW_THREADS
402 rc = sqlite3_finalize(statement);
403 Py_END_ALLOW_THREADS
404
405 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000406 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000407 }
408
409error:
410 if (PyErr_Occurred()) {
411 return NULL;
412 } else {
413 Py_INCREF(Py_None);
414 return Py_None;
415 }
416}
417
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000418PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419{
420 int rc;
421 const char* tail;
422 sqlite3_stmt* statement;
423
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000424 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425 return NULL;
426 }
427
428 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000429 pysqlite_do_all_statements(self, ACTION_RESET, 0);
430
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431 Py_BEGIN_ALLOW_THREADS
432 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
433 Py_END_ALLOW_THREADS
434 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000435 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000436 goto error;
437 }
438
Benjamin Petersond7b03282008-09-13 15:58:53 +0000439 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000440 if (rc == SQLITE_DONE) {
441 self->inTransaction = 0;
442 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000443 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000444 }
445
446 Py_BEGIN_ALLOW_THREADS
447 rc = sqlite3_finalize(statement);
448 Py_END_ALLOW_THREADS
449 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000450 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451 }
452
453 }
454
455error:
456 if (PyErr_Occurred()) {
457 return NULL;
458 } else {
459 Py_INCREF(Py_None);
460 return Py_None;
461 }
462}
463
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000464PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000465{
466 int rc;
467 const char* tail;
468 sqlite3_stmt* statement;
469
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000470 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000471 return NULL;
472 }
473
474 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000475 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000476
477 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000478 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000479 Py_END_ALLOW_THREADS
480 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000481 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000482 goto error;
483 }
484
Benjamin Petersond7b03282008-09-13 15:58:53 +0000485 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000486 if (rc == SQLITE_DONE) {
487 self->inTransaction = 0;
488 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000489 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490 }
491
492 Py_BEGIN_ALLOW_THREADS
493 rc = sqlite3_finalize(statement);
494 Py_END_ALLOW_THREADS
495 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000496 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000497 }
498
499 }
500
501error:
502 if (PyErr_Occurred()) {
503 return NULL;
504 } else {
505 Py_INCREF(Py_None);
506 return Py_None;
507 }
508}
509
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200510static int
511_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200513 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000514 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000515 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200516 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
517 if (value == -1 && PyErr_Occurred())
518 return -1;
519 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520 } else if (PyFloat_Check(py_val)) {
521 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000522 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200523 const char *str = _PyUnicode_AsString(py_val);
524 if (str == NULL)
525 return -1;
526 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000527 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200528 Py_buffer view;
529 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100530 PyErr_SetString(PyExc_ValueError,
531 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200532 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200534 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100535 PyErr_SetString(PyExc_OverflowError,
536 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200537 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100538 return -1;
539 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200540 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
541 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200543 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200545 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546}
547
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000548PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549{
550 PyObject* args;
551 int i;
552 sqlite3_value* cur_value;
553 PyObject* cur_py_value;
554 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000555 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556
557 args = PyTuple_New(argc);
558 if (!args) {
559 return NULL;
560 }
561
562 for (i = 0; i < argc; i++) {
563 cur_value = argv[i];
564 switch (sqlite3_value_type(argv[i])) {
565 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200566 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567 break;
568 case SQLITE_FLOAT:
569 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
570 break;
571 case SQLITE_TEXT:
572 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000573 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000574 /* TODO: have a way to show errors here */
575 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000576 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000577 Py_INCREF(Py_None);
578 cur_py_value = Py_None;
579 }
580 break;
581 case SQLITE_BLOB:
582 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000583 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000584 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000585 break;
586 case SQLITE_NULL:
587 default:
588 Py_INCREF(Py_None);
589 cur_py_value = Py_None;
590 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000591
592 if (!cur_py_value) {
593 Py_DECREF(args);
594 return NULL;
595 }
596
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597 PyTuple_SetItem(args, i, cur_py_value);
598
599 }
600
601 return args;
602}
603
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000604void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000605{
606 PyObject* args;
607 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000608 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200609 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610
Georg Brandldfd73442009-04-05 11:47:34 +0000611#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000612 PyGILState_STATE threadstate;
613
614 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000615#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616
617 py_func = (PyObject*)sqlite3_user_data(context);
618
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000619 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000620 if (args) {
621 py_retval = PyObject_CallObject(py_func, args);
622 Py_DECREF(args);
623 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200625 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200627 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000628 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200629 }
630 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631 if (_enable_callback_tracebacks) {
632 PyErr_Print();
633 } else {
634 PyErr_Clear();
635 }
636 _sqlite3_result_error(context, "user-defined function raised exception", -1);
637 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000638
Georg Brandldfd73442009-04-05 11:47:34 +0000639#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000640 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000641#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000642}
643
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000644static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000645{
646 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000647 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648 PyObject* aggregate_class;
649 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650 PyObject* stepmethod = NULL;
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_STATE threadstate;
654
655 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000656#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000657
658 aggregate_class = (PyObject*)sqlite3_user_data(context);
659
660 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
661
662 if (*aggregate_instance == 0) {
663 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
664
Thomas Wouters477c8d52006-05-27 19:21:47 +0000665 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000667 if (_enable_callback_tracebacks) {
668 PyErr_Print();
669 } else {
670 PyErr_Clear();
671 }
672 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000673 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674 }
675 }
676
677 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678 if (!stepmethod) {
679 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 }
681
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000682 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000683 if (!args) {
684 goto error;
685 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000686
687 function_result = PyObject_CallObject(stepmethod, args);
688 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000689
Thomas Wouters477c8d52006-05-27 19:21:47 +0000690 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000691 if (_enable_callback_tracebacks) {
692 PyErr_Print();
693 } else {
694 PyErr_Clear();
695 }
696 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000697 }
698
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699error:
700 Py_XDECREF(stepmethod);
701 Py_XDECREF(function_result);
702
Georg Brandldfd73442009-04-05 11:47:34 +0000703#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000705#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706}
707
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000708void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200710 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200712 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200713 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200714 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200715 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000716
Georg Brandldfd73442009-04-05 11:47:34 +0000717#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000718 PyGILState_STATE threadstate;
719
720 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000721#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000723 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
724 if (!*aggregate_instance) {
725 /* this branch is executed if there was an exception in the aggregate's
726 * __init__ */
727
Thomas Wouters477c8d52006-05-27 19:21:47 +0000728 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729 }
730
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200731 /* Keep the exception (if any) of the last call to step() */
732 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200733 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200734
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200735 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200736
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200737 Py_DECREF(*aggregate_instance);
738
739 ok = 0;
740 if (function_result) {
741 ok = _pysqlite_set_result(context, function_result) == 0;
742 Py_DECREF(function_result);
743 }
744 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000745 if (_enable_callback_tracebacks) {
746 PyErr_Print();
747 } else {
748 PyErr_Clear();
749 }
750 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200751#if SQLITE_VERSION_NUMBER < 3003003
752 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
753 exception, so don't restore the previous exception */
754 restore = 0;
755#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000756 }
757
Victor Stinnerffff7632013-08-02 01:48:10 +0200758 if (restore) {
759 /* Restore the exception (if any) of the last call to step(),
760 but clear also the current exception if finalize() failed */
761 PyErr_Restore(exception, value, tb);
762 }
Victor Stinner3a857322013-07-22 08:34:32 +0200763
Thomas Wouters477c8d52006-05-27 19:21:47 +0000764error:
Georg Brandldfd73442009-04-05 11:47:34 +0000765#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000767#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200768 /* explicit return to avoid a compilation error if WITH_THREAD
769 is not defined */
770 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000771}
772
Gerhard Häringf9cee222010-03-05 15:20:03 +0000773static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000774{
775 PyObject* new_list;
776 PyObject* weakref;
777 int i;
778
779 /* we only need to do this once in a while */
780 if (self->created_statements++ < 200) {
781 return;
782 }
783
784 self->created_statements = 0;
785
786 new_list = PyList_New(0);
787 if (!new_list) {
788 return;
789 }
790
791 for (i = 0; i < PyList_Size(self->statements); i++) {
792 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000793 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000794 if (PyList_Append(new_list, weakref) != 0) {
795 Py_DECREF(new_list);
796 return;
797 }
798 }
799 }
800
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300801 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000803
Gerhard Häringf9cee222010-03-05 15:20:03 +0000804static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
805{
806 PyObject* new_list;
807 PyObject* weakref;
808 int i;
809
810 /* we only need to do this once in a while */
811 if (self->created_cursors++ < 200) {
812 return;
813 }
814
815 self->created_cursors = 0;
816
817 new_list = PyList_New(0);
818 if (!new_list) {
819 return;
820 }
821
822 for (i = 0; i < PyList_Size(self->cursors); i++) {
823 weakref = PyList_GetItem(self->cursors, i);
824 if (PyWeakref_GetObject(weakref) != Py_None) {
825 if (PyList_Append(new_list, weakref) != 0) {
826 Py_DECREF(new_list);
827 return;
828 }
829 }
830 }
831
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300832 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000833}
834
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000835PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000836{
837 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
838
839 PyObject* func;
840 char* name;
841 int narg;
842 int rc;
843
Gerhard Häringf9cee222010-03-05 15:20:03 +0000844 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
845 return NULL;
846 }
847
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
849 &name, &narg, &func))
850 {
851 return NULL;
852 }
853
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000854 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000855
Thomas Wouters477c8d52006-05-27 19:21:47 +0000856 if (rc != SQLITE_OK) {
857 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000858 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859 return NULL;
860 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000861 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
862 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000863
Thomas Wouters477c8d52006-05-27 19:21:47 +0000864 Py_INCREF(Py_None);
865 return Py_None;
866 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867}
868
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000869PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000870{
871 PyObject* aggregate_class;
872
873 int n_arg;
874 char* name;
875 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
876 int rc;
877
Gerhard Häringf9cee222010-03-05 15:20:03 +0000878 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
879 return NULL;
880 }
881
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
883 kwlist, &name, &n_arg, &aggregate_class)) {
884 return NULL;
885 }
886
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000887 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 +0000888 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000889 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000890 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000891 return NULL;
892 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000893 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
894 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000895
896 Py_INCREF(Py_None);
897 return Py_None;
898 }
899}
900
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000901static 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 +0000902{
903 PyObject *ret;
904 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000905#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000906 PyGILState_STATE gilstate;
907
908 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000909#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000910
Victor Stinnerd4095d92013-07-26 22:23:33 +0200911 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000912
Victor Stinnerd4095d92013-07-26 22:23:33 +0200913 if (ret == NULL) {
914 if (_enable_callback_tracebacks)
915 PyErr_Print();
916 else
917 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200918
Victor Stinnerd4095d92013-07-26 22:23:33 +0200919 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200920 }
921 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200922 if (PyLong_Check(ret)) {
923 rc = _PyLong_AsInt(ret);
924 if (rc == -1 && PyErr_Occurred()) {
925 if (_enable_callback_tracebacks)
926 PyErr_Print();
927 else
928 PyErr_Clear();
929 rc = SQLITE_DENY;
930 }
931 }
932 else {
933 rc = SQLITE_DENY;
934 }
935 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000936 }
937
Georg Brandldfd73442009-04-05 11:47:34 +0000938#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000939 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000940#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000941 return rc;
942}
943
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000944static int _progress_handler(void* user_arg)
945{
946 int rc;
947 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000948#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000949 PyGILState_STATE gilstate;
950
951 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000952#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000953 ret = PyObject_CallFunction((PyObject*)user_arg, "");
954
955 if (!ret) {
956 if (_enable_callback_tracebacks) {
957 PyErr_Print();
958 } else {
959 PyErr_Clear();
960 }
961
Mark Dickinson934896d2009-02-21 20:59:32 +0000962 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000963 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000964 } else {
965 rc = (int)PyObject_IsTrue(ret);
966 Py_DECREF(ret);
967 }
968
Georg Brandldfd73442009-04-05 11:47:34 +0000969#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000970 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000971#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000972 return rc;
973}
974
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200975static void _trace_callback(void* user_arg, const char* statement_string)
976{
977 PyObject *py_statement = NULL;
978 PyObject *ret = NULL;
979
980#ifdef WITH_THREAD
981 PyGILState_STATE gilstate;
982
983 gilstate = PyGILState_Ensure();
984#endif
985 py_statement = PyUnicode_DecodeUTF8(statement_string,
986 strlen(statement_string), "replace");
987 if (py_statement) {
988 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
989 Py_DECREF(py_statement);
990 }
991
992 if (ret) {
993 Py_DECREF(ret);
994 } else {
995 if (_enable_callback_tracebacks) {
996 PyErr_Print();
997 } else {
998 PyErr_Clear();
999 }
1000 }
1001
1002#ifdef WITH_THREAD
1003 PyGILState_Release(gilstate);
1004#endif
1005}
1006
Gerhard Häringf9cee222010-03-05 15:20:03 +00001007static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001008{
1009 PyObject* authorizer_cb;
1010
1011 static char *kwlist[] = { "authorizer_callback", NULL };
1012 int rc;
1013
Gerhard Häringf9cee222010-03-05 15:20:03 +00001014 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1015 return NULL;
1016 }
1017
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001018 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1019 kwlist, &authorizer_cb)) {
1020 return NULL;
1021 }
1022
1023 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1024
1025 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001026 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001027 return NULL;
1028 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001029 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1030 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001031
1032 Py_INCREF(Py_None);
1033 return Py_None;
1034 }
1035}
1036
Gerhard Häringf9cee222010-03-05 15:20:03 +00001037static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001038{
1039 PyObject* progress_handler;
1040 int n;
1041
1042 static char *kwlist[] = { "progress_handler", "n", NULL };
1043
Gerhard Häringf9cee222010-03-05 15:20:03 +00001044 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1045 return NULL;
1046 }
1047
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001048 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1049 kwlist, &progress_handler, &n)) {
1050 return NULL;
1051 }
1052
1053 if (progress_handler == Py_None) {
1054 /* None clears the progress handler previously set */
1055 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1056 } else {
1057 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001058 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1059 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001060 }
1061
1062 Py_INCREF(Py_None);
1063 return Py_None;
1064}
1065
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001066static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1067{
1068 PyObject* trace_callback;
1069
1070 static char *kwlist[] = { "trace_callback", NULL };
1071
1072 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1073 return NULL;
1074 }
1075
1076 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1077 kwlist, &trace_callback)) {
1078 return NULL;
1079 }
1080
1081 if (trace_callback == Py_None) {
1082 /* None clears the trace callback previously set */
1083 sqlite3_trace(self->db, 0, (void*)0);
1084 } else {
1085 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1086 return NULL;
1087 sqlite3_trace(self->db, _trace_callback, trace_callback);
1088 }
1089
1090 Py_INCREF(Py_None);
1091 return Py_None;
1092}
1093
Gerhard Häringf9cee222010-03-05 15:20:03 +00001094#ifdef HAVE_LOAD_EXTENSION
1095static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1096{
1097 int rc;
1098 int onoff;
1099
1100 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1101 return NULL;
1102 }
1103
1104 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1105 return NULL;
1106 }
1107
1108 rc = sqlite3_enable_load_extension(self->db, onoff);
1109
1110 if (rc != SQLITE_OK) {
1111 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1112 return NULL;
1113 } else {
1114 Py_INCREF(Py_None);
1115 return Py_None;
1116 }
1117}
1118
1119static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1120{
1121 int rc;
1122 char* extension_name;
1123 char* errmsg;
1124
1125 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1126 return NULL;
1127 }
1128
1129 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1130 return NULL;
1131 }
1132
1133 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1134 if (rc != 0) {
1135 PyErr_SetString(pysqlite_OperationalError, errmsg);
1136 return NULL;
1137 } else {
1138 Py_INCREF(Py_None);
1139 return Py_None;
1140 }
1141}
1142#endif
1143
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001144int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001145{
Georg Brandldfd73442009-04-05 11:47:34 +00001146#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147 if (self->check_same_thread) {
1148 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001149 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001150 "SQLite objects created in a thread can only be used in that same thread."
1151 "The object was created in thread id %ld and this is thread id %ld",
1152 self->thread_ident, PyThread_get_thread_ident());
1153 return 0;
1154 }
1155
1156 }
Georg Brandldfd73442009-04-05 11:47:34 +00001157#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001158 return 1;
1159}
1160
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001161static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162{
1163 Py_INCREF(self->isolation_level);
1164 return self->isolation_level;
1165}
1166
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001167static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001168{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001169 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170 return NULL;
1171 } else {
1172 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1173 }
1174}
1175
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001176static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001177{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001178 PyObject* res;
1179 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001180 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181
1182 Py_XDECREF(self->isolation_level);
1183
1184 if (self->begin_statement) {
1185 PyMem_Free(self->begin_statement);
1186 self->begin_statement = NULL;
1187 }
1188
1189 if (isolation_level == Py_None) {
1190 Py_INCREF(Py_None);
1191 self->isolation_level = Py_None;
1192
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001193 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001194 if (!res) {
1195 return -1;
1196 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197 Py_DECREF(res);
1198
1199 self->inTransaction = 0;
1200 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001201 const char *statement;
1202 Py_ssize_t size;
1203
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001204 Py_INCREF(isolation_level);
1205 self->isolation_level = isolation_level;
1206
Georg Brandlceab6102007-11-25 00:45:05 +00001207 if (!begin_word) {
1208 begin_word = PyUnicode_FromString("BEGIN ");
1209 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001210 }
Georg Brandlceab6102007-11-25 00:45:05 +00001211 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001212 if (!begin_statement) {
1213 return -1;
1214 }
1215
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001216 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001217 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001218 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001219 return -1;
1220 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001221 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001222 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001223 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224 return -1;
1225 }
1226
Neal Norwitzefee9f52007-10-27 02:50:52 +00001227 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228 Py_DECREF(begin_statement);
1229 }
1230
1231 return 0;
1232}
1233
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001234PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001235{
1236 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001237 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001238 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001239 int rc;
1240
Gerhard Häringf9cee222010-03-05 15:20:03 +00001241 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1242 return NULL;
1243 }
1244
Larry Hastings3b12e952015-05-08 07:45:10 -07001245 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection()", kwargs))
1246 return NULL;
1247
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001248 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001249 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001251 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001252
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001253 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001254 if (!statement) {
1255 return NULL;
1256 }
1257
Victor Stinner0201f442010-03-13 03:28:34 +00001258 statement->db = NULL;
1259 statement->st = NULL;
1260 statement->sql = NULL;
1261 statement->in_use = 0;
1262 statement->in_weakreflist = NULL;
1263
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001264 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001265 if (rc != SQLITE_OK) {
1266 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001267 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001269 if (PyErr_ExceptionMatches(PyExc_TypeError))
1270 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001272 (void)pysqlite_statement_reset(statement);
1273 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001274 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001275 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001276 }
1277
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001278 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1279 if (weakref == NULL)
1280 goto error;
1281 if (PyList_Append(self->statements, weakref) != 0) {
1282 Py_DECREF(weakref);
1283 goto error;
1284 }
1285 Py_DECREF(weakref);
1286
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001288
1289error:
1290 Py_DECREF(statement);
1291 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001292}
1293
Larry Hastings01b08832015-05-08 07:37:49 -07001294PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001295{
1296 PyObject* cursor = 0;
1297 PyObject* result = 0;
1298 PyObject* method = 0;
1299
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001300 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001301 if (!cursor) {
1302 goto error;
1303 }
1304
1305 method = PyObject_GetAttrString(cursor, "execute");
1306 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001307 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001308 goto error;
1309 }
1310
1311 result = PyObject_CallObject(method, args);
1312 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001313 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001314 }
1315
1316error:
1317 Py_XDECREF(result);
1318 Py_XDECREF(method);
1319
1320 return cursor;
1321}
1322
Larry Hastings01b08832015-05-08 07:37:49 -07001323PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001324{
1325 PyObject* cursor = 0;
1326 PyObject* result = 0;
1327 PyObject* method = 0;
1328
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001329 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001330 if (!cursor) {
1331 goto error;
1332 }
1333
1334 method = PyObject_GetAttrString(cursor, "executemany");
1335 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001336 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001337 goto error;
1338 }
1339
1340 result = PyObject_CallObject(method, args);
1341 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001342 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343 }
1344
1345error:
1346 Py_XDECREF(result);
1347 Py_XDECREF(method);
1348
1349 return cursor;
1350}
1351
Larry Hastings01b08832015-05-08 07:37:49 -07001352PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001353{
1354 PyObject* cursor = 0;
1355 PyObject* result = 0;
1356 PyObject* method = 0;
1357
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001358 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001359 if (!cursor) {
1360 goto error;
1361 }
1362
1363 method = PyObject_GetAttrString(cursor, "executescript");
1364 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001365 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001366 goto error;
1367 }
1368
1369 result = PyObject_CallObject(method, args);
1370 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001371 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001372 }
1373
1374error:
1375 Py_XDECREF(result);
1376 Py_XDECREF(method);
1377
1378 return cursor;
1379}
1380
1381/* ------------------------- COLLATION CODE ------------------------ */
1382
1383static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001384pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001385 void* context,
1386 int text1_length, const void* text1_data,
1387 int text2_length, const void* text2_data)
1388{
1389 PyObject* callback = (PyObject*)context;
1390 PyObject* string1 = 0;
1391 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001392#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001393 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001394#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001395 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001396 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001397 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001398#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001399 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001400#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001401
1402 if (PyErr_Occurred()) {
1403 goto finally;
1404 }
1405
Guido van Rossum98297ee2007-11-06 21:34:58 +00001406 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1407 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001408
1409 if (!string1 || !string2) {
1410 goto finally; /* failed to allocate strings */
1411 }
1412
1413 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1414
1415 if (!retval) {
1416 /* execution failed */
1417 goto finally;
1418 }
1419
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001420 longval = PyLong_AsLongAndOverflow(retval, &result);
1421 if (longval == -1 && PyErr_Occurred()) {
1422 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001423 result = 0;
1424 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001425 else if (!result) {
1426 if (longval > 0)
1427 result = 1;
1428 else if (longval < 0)
1429 result = -1;
1430 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431
1432finally:
1433 Py_XDECREF(string1);
1434 Py_XDECREF(string2);
1435 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001436#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001437 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001438#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001439 return result;
1440}
1441
1442static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001443pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001444{
1445 PyObject* retval = NULL;
1446
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001447 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001448 goto finally;
1449 }
1450
1451 sqlite3_interrupt(self->db);
1452
1453 Py_INCREF(Py_None);
1454 retval = Py_None;
1455
1456finally:
1457 return retval;
1458}
1459
Christian Heimesbbe741d2008-03-28 10:53:29 +00001460/* Function author: Paul Kippes <kippesp@gmail.com>
1461 * Class method of Connection to call the Python function _iterdump
1462 * of the sqlite3 module.
1463 */
1464static PyObject *
1465pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1466{
1467 PyObject* retval = NULL;
1468 PyObject* module = NULL;
1469 PyObject* module_dict;
1470 PyObject* pyfn_iterdump;
1471
1472 if (!pysqlite_check_connection(self)) {
1473 goto finally;
1474 }
1475
1476 module = PyImport_ImportModule(MODULE_NAME ".dump");
1477 if (!module) {
1478 goto finally;
1479 }
1480
1481 module_dict = PyModule_GetDict(module);
1482 if (!module_dict) {
1483 goto finally;
1484 }
1485
1486 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1487 if (!pyfn_iterdump) {
1488 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1489 goto finally;
1490 }
1491
1492 args = PyTuple_New(1);
1493 if (!args) {
1494 goto finally;
1495 }
1496 Py_INCREF(self);
1497 PyTuple_SetItem(args, 0, (PyObject*)self);
1498 retval = PyObject_CallObject(pyfn_iterdump, args);
1499
1500finally:
1501 Py_XDECREF(args);
1502 Py_XDECREF(module);
1503 return retval;
1504}
1505
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001506static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001507pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001508{
1509 PyObject* callable;
1510 PyObject* uppercase_name = 0;
1511 PyObject* name;
1512 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001513 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001514 _Py_IDENTIFIER(upper);
Victor Stinner35466c52010-04-22 11:23:23 +00001515 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001516 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001517 unsigned int kind;
1518 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001519
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001520 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 goto finally;
1522 }
1523
Gerhard Häring6d214562007-08-10 18:15:11 +00001524 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001525 goto finally;
1526 }
1527
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001528 uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001529 if (!uppercase_name) {
1530 goto finally;
1531 }
1532
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001533 if (PyUnicode_READY(uppercase_name))
1534 goto finally;
1535 len = PyUnicode_GET_LENGTH(uppercase_name);
1536 kind = PyUnicode_KIND(uppercase_name);
1537 data = PyUnicode_DATA(uppercase_name);
1538 for (i=0; i<len; i++) {
1539 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1540 if ((ch >= '0' && ch <= '9')
1541 || (ch >= 'A' && ch <= 'Z')
1542 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001543 {
Victor Stinner35466c52010-04-22 11:23:23 +00001544 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001545 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001546 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001547 goto finally;
1548 }
1549 }
1550
Victor Stinner35466c52010-04-22 11:23:23 +00001551 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1552 if (!uppercase_name_str)
1553 goto finally;
1554
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001555 if (callable != Py_None && !PyCallable_Check(callable)) {
1556 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1557 goto finally;
1558 }
1559
1560 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001561 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1562 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001563 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001564 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1565 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001566 }
1567
1568 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001569 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001570 SQLITE_UTF8,
1571 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001572 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001573 if (rc != SQLITE_OK) {
1574 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001575 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001576 goto finally;
1577 }
1578
1579finally:
1580 Py_XDECREF(uppercase_name);
1581
1582 if (PyErr_Occurred()) {
1583 retval = NULL;
1584 } else {
1585 Py_INCREF(Py_None);
1586 retval = Py_None;
1587 }
1588
1589 return retval;
1590}
1591
Christian Heimesbbe741d2008-03-28 10:53:29 +00001592/* Called when the connection is used as a context manager. Returns itself as a
1593 * convenience to the caller. */
1594static PyObject *
1595pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1596{
1597 Py_INCREF(self);
1598 return (PyObject*)self;
1599}
1600
1601/** Called when the connection is used as a context manager. If there was any
1602 * exception, a rollback takes place; otherwise we commit. */
1603static PyObject *
1604pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1605{
1606 PyObject* exc_type, *exc_value, *exc_tb;
1607 char* method_name;
1608 PyObject* result;
1609
1610 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1611 return NULL;
1612 }
1613
1614 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1615 method_name = "commit";
1616 } else {
1617 method_name = "rollback";
1618 }
1619
1620 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1621 if (!result) {
1622 return NULL;
1623 }
1624 Py_DECREF(result);
1625
1626 Py_RETURN_FALSE;
1627}
1628
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001629static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001630PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001631
1632static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001633 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1634 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001635 {NULL}
1636};
1637
1638static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001639 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001640 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001641 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001642 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001643 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001644 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001645 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001646 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001647 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001648 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001649 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001650 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001651 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001652 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001653 #ifdef HAVE_LOAD_EXTENSION
1654 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1655 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1656 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1657 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1658 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001659 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1660 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001661 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1662 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001663 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001664 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001665 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001666 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001667 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001668 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001669 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001670 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001671 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001672 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001673 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001674 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001675 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1676 PyDoc_STR("For context manager. Non-standard.")},
1677 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1678 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001679 {NULL, NULL}
1680};
1681
1682static struct PyMemberDef connection_members[] =
1683{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001684 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1685 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1686 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1687 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1688 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1689 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1690 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1691 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1692 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1693 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001694 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1695 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001696 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001697 {NULL}
1698};
1699
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001700PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001701 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001702 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001703 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001704 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001705 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001706 0, /* tp_print */
1707 0, /* tp_getattr */
1708 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001709 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001710 0, /* tp_repr */
1711 0, /* tp_as_number */
1712 0, /* tp_as_sequence */
1713 0, /* tp_as_mapping */
1714 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001715 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001716 0, /* tp_str */
1717 0, /* tp_getattro */
1718 0, /* tp_setattro */
1719 0, /* tp_as_buffer */
1720 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1721 connection_doc, /* tp_doc */
1722 0, /* tp_traverse */
1723 0, /* tp_clear */
1724 0, /* tp_richcompare */
1725 0, /* tp_weaklistoffset */
1726 0, /* tp_iter */
1727 0, /* tp_iternext */
1728 connection_methods, /* tp_methods */
1729 connection_members, /* tp_members */
1730 connection_getset, /* tp_getset */
1731 0, /* tp_base */
1732 0, /* tp_dict */
1733 0, /* tp_descr_get */
1734 0, /* tp_descr_set */
1735 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001736 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001737 0, /* tp_alloc */
1738 0, /* tp_new */
1739 0 /* tp_free */
1740};
1741
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001742extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001743{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001744 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1745 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001746}