blob: 53492997659bf5f67dfe32db5a7981c54dde34b7 [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
Berker Peksagfe21de92016-04-09 07:34:39 +0300355 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000356}
357
358/*
359 * Checks if a connection object is usable (i. e. not closed).
360 *
361 * 0 => error; 1 => ok
362 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000363int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000365 if (!con->initialized) {
366 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
367 return 0;
368 }
369
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000371 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372 return 0;
373 } else {
374 return 1;
375 }
376}
377
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000378PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379{
380 int rc;
381 const char* tail;
382 sqlite3_stmt* statement;
383
384 Py_BEGIN_ALLOW_THREADS
385 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
386 Py_END_ALLOW_THREADS
387
388 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000389 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 goto error;
391 }
392
Benjamin Petersond7b03282008-09-13 15:58:53 +0000393 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394 if (rc == SQLITE_DONE) {
395 self->inTransaction = 1;
396 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000397 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000398 }
399
400 Py_BEGIN_ALLOW_THREADS
401 rc = sqlite3_finalize(statement);
402 Py_END_ALLOW_THREADS
403
404 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000405 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406 }
407
408error:
409 if (PyErr_Occurred()) {
410 return NULL;
411 } else {
412 Py_INCREF(Py_None);
413 return Py_None;
414 }
415}
416
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000417PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000418{
419 int rc;
420 const char* tail;
421 sqlite3_stmt* statement;
422
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000423 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 return NULL;
425 }
426
427 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000428 pysqlite_do_all_statements(self, ACTION_RESET, 0);
429
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430 Py_BEGIN_ALLOW_THREADS
431 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
432 Py_END_ALLOW_THREADS
433 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000434 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000435 goto error;
436 }
437
Benjamin Petersond7b03282008-09-13 15:58:53 +0000438 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439 if (rc == SQLITE_DONE) {
440 self->inTransaction = 0;
441 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000442 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443 }
444
445 Py_BEGIN_ALLOW_THREADS
446 rc = sqlite3_finalize(statement);
447 Py_END_ALLOW_THREADS
448 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000449 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000450 }
451
452 }
453
454error:
455 if (PyErr_Occurred()) {
456 return NULL;
457 } else {
458 Py_INCREF(Py_None);
459 return Py_None;
460 }
461}
462
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000463PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464{
465 int rc;
466 const char* tail;
467 sqlite3_stmt* statement;
468
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000469 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000470 return NULL;
471 }
472
473 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000474 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475
476 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000477 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000478 Py_END_ALLOW_THREADS
479 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000480 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000481 goto error;
482 }
483
Benjamin Petersond7b03282008-09-13 15:58:53 +0000484 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000485 if (rc == SQLITE_DONE) {
486 self->inTransaction = 0;
487 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000488 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000489 }
490
491 Py_BEGIN_ALLOW_THREADS
492 rc = sqlite3_finalize(statement);
493 Py_END_ALLOW_THREADS
494 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000495 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000496 }
497
498 }
499
500error:
501 if (PyErr_Occurred()) {
502 return NULL;
503 } else {
504 Py_INCREF(Py_None);
505 return Py_None;
506 }
507}
508
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200509static int
510_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200512 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000514 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200515 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
516 if (value == -1 && PyErr_Occurred())
517 return -1;
518 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000519 } else if (PyFloat_Check(py_val)) {
520 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000521 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200522 const char *str = _PyUnicode_AsString(py_val);
523 if (str == NULL)
524 return -1;
525 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000526 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200527 Py_buffer view;
528 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100529 PyErr_SetString(PyExc_ValueError,
530 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200531 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000532 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200533 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100534 PyErr_SetString(PyExc_OverflowError,
535 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200536 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100537 return -1;
538 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200539 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
540 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200542 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200544 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545}
546
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000547PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548{
549 PyObject* args;
550 int i;
551 sqlite3_value* cur_value;
552 PyObject* cur_py_value;
553 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000555
556 args = PyTuple_New(argc);
557 if (!args) {
558 return NULL;
559 }
560
561 for (i = 0; i < argc; i++) {
562 cur_value = argv[i];
563 switch (sqlite3_value_type(argv[i])) {
564 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200565 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000566 break;
567 case SQLITE_FLOAT:
568 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
569 break;
570 case SQLITE_TEXT:
571 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000572 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000573 /* TODO: have a way to show errors here */
574 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000575 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000576 Py_INCREF(Py_None);
577 cur_py_value = Py_None;
578 }
579 break;
580 case SQLITE_BLOB:
581 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000582 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000583 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000584 break;
585 case SQLITE_NULL:
586 default:
587 Py_INCREF(Py_None);
588 cur_py_value = Py_None;
589 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000590
591 if (!cur_py_value) {
592 Py_DECREF(args);
593 return NULL;
594 }
595
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596 PyTuple_SetItem(args, i, cur_py_value);
597
598 }
599
600 return args;
601}
602
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000603void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604{
605 PyObject* args;
606 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000607 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200608 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000609
Georg Brandldfd73442009-04-05 11:47:34 +0000610#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611 PyGILState_STATE threadstate;
612
613 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000614#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000615
616 py_func = (PyObject*)sqlite3_user_data(context);
617
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000618 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000619 if (args) {
620 py_retval = PyObject_CallObject(py_func, args);
621 Py_DECREF(args);
622 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200624 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000625 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200626 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000627 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200628 }
629 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630 if (_enable_callback_tracebacks) {
631 PyErr_Print();
632 } else {
633 PyErr_Clear();
634 }
635 _sqlite3_result_error(context, "user-defined function raised exception", -1);
636 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637
Georg Brandldfd73442009-04-05 11:47:34 +0000638#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000639 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000640#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000641}
642
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000643static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644{
645 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647 PyObject* aggregate_class;
648 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650
Georg Brandldfd73442009-04-05 11:47:34 +0000651#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652 PyGILState_STATE threadstate;
653
654 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000655#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000656
657 aggregate_class = (PyObject*)sqlite3_user_data(context);
658
659 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
660
661 if (*aggregate_instance == 0) {
662 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
663
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000665 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000666 if (_enable_callback_tracebacks) {
667 PyErr_Print();
668 } else {
669 PyErr_Clear();
670 }
671 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000673 }
674 }
675
676 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 if (!stepmethod) {
678 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000679 }
680
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000681 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 if (!args) {
683 goto error;
684 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000685
686 function_result = PyObject_CallObject(stepmethod, args);
687 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688
Thomas Wouters477c8d52006-05-27 19:21:47 +0000689 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000690 if (_enable_callback_tracebacks) {
691 PyErr_Print();
692 } else {
693 PyErr_Clear();
694 }
695 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000696 }
697
Thomas Wouters477c8d52006-05-27 19:21:47 +0000698error:
699 Py_XDECREF(stepmethod);
700 Py_XDECREF(function_result);
701
Georg Brandldfd73442009-04-05 11:47:34 +0000702#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000703 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000704#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705}
706
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000707void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000708{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200709 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200711 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200712 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200713 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200714 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715
Georg Brandldfd73442009-04-05 11:47:34 +0000716#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000717 PyGILState_STATE threadstate;
718
719 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000720#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000721
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
723 if (!*aggregate_instance) {
724 /* this branch is executed if there was an exception in the aggregate's
725 * __init__ */
726
Thomas Wouters477c8d52006-05-27 19:21:47 +0000727 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000728 }
729
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200730 /* Keep the exception (if any) of the last call to step() */
731 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200732 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200733
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200734 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200735
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200736 Py_DECREF(*aggregate_instance);
737
738 ok = 0;
739 if (function_result) {
740 ok = _pysqlite_set_result(context, function_result) == 0;
741 Py_DECREF(function_result);
742 }
743 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000744 if (_enable_callback_tracebacks) {
745 PyErr_Print();
746 } else {
747 PyErr_Clear();
748 }
749 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200750#if SQLITE_VERSION_NUMBER < 3003003
751 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
752 exception, so don't restore the previous exception */
753 restore = 0;
754#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000755 }
756
Victor Stinnerffff7632013-08-02 01:48:10 +0200757 if (restore) {
758 /* Restore the exception (if any) of the last call to step(),
759 but clear also the current exception if finalize() failed */
760 PyErr_Restore(exception, value, tb);
761 }
Victor Stinner3a857322013-07-22 08:34:32 +0200762
Thomas Wouters477c8d52006-05-27 19:21:47 +0000763error:
Georg Brandldfd73442009-04-05 11:47:34 +0000764#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000765 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000766#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200767 /* explicit return to avoid a compilation error if WITH_THREAD
768 is not defined */
769 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000770}
771
Gerhard Häringf9cee222010-03-05 15:20:03 +0000772static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000773{
774 PyObject* new_list;
775 PyObject* weakref;
776 int i;
777
778 /* we only need to do this once in a while */
779 if (self->created_statements++ < 200) {
780 return;
781 }
782
783 self->created_statements = 0;
784
785 new_list = PyList_New(0);
786 if (!new_list) {
787 return;
788 }
789
790 for (i = 0; i < PyList_Size(self->statements); i++) {
791 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000792 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000793 if (PyList_Append(new_list, weakref) != 0) {
794 Py_DECREF(new_list);
795 return;
796 }
797 }
798 }
799
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300800 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000801}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000802
Gerhard Häringf9cee222010-03-05 15:20:03 +0000803static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
804{
805 PyObject* new_list;
806 PyObject* weakref;
807 int i;
808
809 /* we only need to do this once in a while */
810 if (self->created_cursors++ < 200) {
811 return;
812 }
813
814 self->created_cursors = 0;
815
816 new_list = PyList_New(0);
817 if (!new_list) {
818 return;
819 }
820
821 for (i = 0; i < PyList_Size(self->cursors); i++) {
822 weakref = PyList_GetItem(self->cursors, i);
823 if (PyWeakref_GetObject(weakref) != Py_None) {
824 if (PyList_Append(new_list, weakref) != 0) {
825 Py_DECREF(new_list);
826 return;
827 }
828 }
829 }
830
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300831 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000832}
833
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000834PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835{
836 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
837
838 PyObject* func;
839 char* name;
840 int narg;
841 int rc;
842
Gerhard Häringf9cee222010-03-05 15:20:03 +0000843 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
844 return NULL;
845 }
846
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000847 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
848 &name, &narg, &func))
849 {
850 return NULL;
851 }
852
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000853 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000854
Thomas Wouters477c8d52006-05-27 19:21:47 +0000855 if (rc != SQLITE_OK) {
856 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000857 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000858 return NULL;
859 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000860 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
861 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862
Berker Peksagfe21de92016-04-09 07:34:39 +0300863 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000864 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865}
866
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000867PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868{
869 PyObject* aggregate_class;
870
871 int n_arg;
872 char* name;
873 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
874 int rc;
875
Gerhard Häringf9cee222010-03-05 15:20:03 +0000876 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
877 return NULL;
878 }
879
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
881 kwlist, &name, &n_arg, &aggregate_class)) {
882 return NULL;
883 }
884
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000885 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 +0000886 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000887 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000888 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000889 return NULL;
890 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000891 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
892 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000893
Berker Peksagfe21de92016-04-09 07:34:39 +0300894 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000895 }
896}
897
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000898static 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 +0000899{
900 PyObject *ret;
901 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000902#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000903 PyGILState_STATE gilstate;
904
905 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000906#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000907
Victor Stinnerd4095d92013-07-26 22:23:33 +0200908 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000909
Victor Stinnerd4095d92013-07-26 22:23:33 +0200910 if (ret == NULL) {
911 if (_enable_callback_tracebacks)
912 PyErr_Print();
913 else
914 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200915
Victor Stinnerd4095d92013-07-26 22:23:33 +0200916 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200917 }
918 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200919 if (PyLong_Check(ret)) {
920 rc = _PyLong_AsInt(ret);
921 if (rc == -1 && PyErr_Occurred()) {
922 if (_enable_callback_tracebacks)
923 PyErr_Print();
924 else
925 PyErr_Clear();
926 rc = SQLITE_DENY;
927 }
928 }
929 else {
930 rc = SQLITE_DENY;
931 }
932 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000933 }
934
Georg Brandldfd73442009-04-05 11:47:34 +0000935#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000936 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000937#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000938 return rc;
939}
940
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000941static int _progress_handler(void* user_arg)
942{
943 int rc;
944 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000945#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000946 PyGILState_STATE gilstate;
947
948 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000949#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000950 ret = PyObject_CallFunction((PyObject*)user_arg, "");
951
952 if (!ret) {
953 if (_enable_callback_tracebacks) {
954 PyErr_Print();
955 } else {
956 PyErr_Clear();
957 }
958
Mark Dickinson934896d2009-02-21 20:59:32 +0000959 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000960 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000961 } else {
962 rc = (int)PyObject_IsTrue(ret);
963 Py_DECREF(ret);
964 }
965
Georg Brandldfd73442009-04-05 11:47:34 +0000966#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000967 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000968#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000969 return rc;
970}
971
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200972static void _trace_callback(void* user_arg, const char* statement_string)
973{
974 PyObject *py_statement = NULL;
975 PyObject *ret = NULL;
976
977#ifdef WITH_THREAD
978 PyGILState_STATE gilstate;
979
980 gilstate = PyGILState_Ensure();
981#endif
982 py_statement = PyUnicode_DecodeUTF8(statement_string,
983 strlen(statement_string), "replace");
984 if (py_statement) {
985 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
986 Py_DECREF(py_statement);
987 }
988
989 if (ret) {
990 Py_DECREF(ret);
991 } else {
992 if (_enable_callback_tracebacks) {
993 PyErr_Print();
994 } else {
995 PyErr_Clear();
996 }
997 }
998
999#ifdef WITH_THREAD
1000 PyGILState_Release(gilstate);
1001#endif
1002}
1003
Gerhard Häringf9cee222010-03-05 15:20:03 +00001004static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001005{
1006 PyObject* authorizer_cb;
1007
1008 static char *kwlist[] = { "authorizer_callback", NULL };
1009 int rc;
1010
Gerhard Häringf9cee222010-03-05 15:20:03 +00001011 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1012 return NULL;
1013 }
1014
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001015 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1016 kwlist, &authorizer_cb)) {
1017 return NULL;
1018 }
1019
1020 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1021
1022 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001023 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001024 return NULL;
1025 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001026 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1027 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001028
Berker Peksagfe21de92016-04-09 07:34:39 +03001029 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001030 }
1031}
1032
Gerhard Häringf9cee222010-03-05 15:20:03 +00001033static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001034{
1035 PyObject* progress_handler;
1036 int n;
1037
1038 static char *kwlist[] = { "progress_handler", "n", NULL };
1039
Gerhard Häringf9cee222010-03-05 15:20:03 +00001040 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1041 return NULL;
1042 }
1043
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001044 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1045 kwlist, &progress_handler, &n)) {
1046 return NULL;
1047 }
1048
1049 if (progress_handler == Py_None) {
1050 /* None clears the progress handler previously set */
1051 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1052 } else {
1053 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001054 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1055 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001056 }
1057
Berker Peksagfe21de92016-04-09 07:34:39 +03001058 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001059}
1060
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001061static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1062{
1063 PyObject* trace_callback;
1064
1065 static char *kwlist[] = { "trace_callback", NULL };
1066
1067 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1068 return NULL;
1069 }
1070
1071 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1072 kwlist, &trace_callback)) {
1073 return NULL;
1074 }
1075
1076 if (trace_callback == Py_None) {
1077 /* None clears the trace callback previously set */
1078 sqlite3_trace(self->db, 0, (void*)0);
1079 } else {
1080 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1081 return NULL;
1082 sqlite3_trace(self->db, _trace_callback, trace_callback);
1083 }
1084
Berker Peksagfe21de92016-04-09 07:34:39 +03001085 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001086}
1087
Gerhard Häringf9cee222010-03-05 15:20:03 +00001088#ifdef HAVE_LOAD_EXTENSION
1089static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1090{
1091 int rc;
1092 int onoff;
1093
1094 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1095 return NULL;
1096 }
1097
1098 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1099 return NULL;
1100 }
1101
1102 rc = sqlite3_enable_load_extension(self->db, onoff);
1103
1104 if (rc != SQLITE_OK) {
1105 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1106 return NULL;
1107 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001108 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001109 }
1110}
1111
1112static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1113{
1114 int rc;
1115 char* extension_name;
1116 char* errmsg;
1117
1118 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1119 return NULL;
1120 }
1121
1122 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1123 return NULL;
1124 }
1125
1126 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1127 if (rc != 0) {
1128 PyErr_SetString(pysqlite_OperationalError, errmsg);
1129 return NULL;
1130 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001131 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001132 }
1133}
1134#endif
1135
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001136int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001137{
Georg Brandldfd73442009-04-05 11:47:34 +00001138#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001139 if (self->check_same_thread) {
1140 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001141 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001142 "SQLite objects created in a thread can only be used in that same thread."
1143 "The object was created in thread id %ld and this is thread id %ld",
1144 self->thread_ident, PyThread_get_thread_ident());
1145 return 0;
1146 }
1147
1148 }
Georg Brandldfd73442009-04-05 11:47:34 +00001149#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001150 return 1;
1151}
1152
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001153static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001154{
1155 Py_INCREF(self->isolation_level);
1156 return self->isolation_level;
1157}
1158
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001159static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001160{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001161 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162 return NULL;
1163 } else {
1164 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1165 }
1166}
1167
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001168static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001169{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170 PyObject* res;
1171 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001172 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173
1174 Py_XDECREF(self->isolation_level);
1175
1176 if (self->begin_statement) {
1177 PyMem_Free(self->begin_statement);
1178 self->begin_statement = NULL;
1179 }
1180
1181 if (isolation_level == Py_None) {
1182 Py_INCREF(Py_None);
1183 self->isolation_level = Py_None;
1184
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001185 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001186 if (!res) {
1187 return -1;
1188 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001189 Py_DECREF(res);
1190
1191 self->inTransaction = 0;
1192 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001193 const char *statement;
1194 Py_ssize_t size;
1195
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001196 Py_INCREF(isolation_level);
1197 self->isolation_level = isolation_level;
1198
Georg Brandlceab6102007-11-25 00:45:05 +00001199 if (!begin_word) {
1200 begin_word = PyUnicode_FromString("BEGIN ");
1201 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001202 }
Georg Brandlceab6102007-11-25 00:45:05 +00001203 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001204 if (!begin_statement) {
1205 return -1;
1206 }
1207
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001208 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001209 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001210 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001211 return -1;
1212 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001213 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001214 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001215 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001216 return -1;
1217 }
1218
Neal Norwitzefee9f52007-10-27 02:50:52 +00001219 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001220 Py_DECREF(begin_statement);
1221 }
1222
1223 return 0;
1224}
1225
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001226PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227{
1228 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001229 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001230 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001231 int rc;
1232
Gerhard Häringf9cee222010-03-05 15:20:03 +00001233 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1234 return NULL;
1235 }
1236
Larry Hastings3b12e952015-05-08 07:45:10 -07001237 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection()", kwargs))
1238 return NULL;
1239
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001240 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001241 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001243 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001244
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001245 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001246 if (!statement) {
1247 return NULL;
1248 }
1249
Victor Stinner0201f442010-03-13 03:28:34 +00001250 statement->db = NULL;
1251 statement->st = NULL;
1252 statement->sql = NULL;
1253 statement->in_use = 0;
1254 statement->in_weakreflist = NULL;
1255
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001256 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001257 if (rc != SQLITE_OK) {
1258 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001259 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001260 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001261 if (PyErr_ExceptionMatches(PyExc_TypeError))
1262 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001263 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001264 (void)pysqlite_statement_reset(statement);
1265 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001267 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268 }
1269
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001270 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1271 if (weakref == NULL)
1272 goto error;
1273 if (PyList_Append(self->statements, weakref) != 0) {
1274 Py_DECREF(weakref);
1275 goto error;
1276 }
1277 Py_DECREF(weakref);
1278
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001279 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001280
1281error:
1282 Py_DECREF(statement);
1283 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284}
1285
Larry Hastings01b08832015-05-08 07:37:49 -07001286PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287{
1288 PyObject* cursor = 0;
1289 PyObject* result = 0;
1290 PyObject* method = 0;
1291
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001292 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001293 if (!cursor) {
1294 goto error;
1295 }
1296
1297 method = PyObject_GetAttrString(cursor, "execute");
1298 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001299 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001300 goto error;
1301 }
1302
1303 result = PyObject_CallObject(method, args);
1304 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001305 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001306 }
1307
1308error:
1309 Py_XDECREF(result);
1310 Py_XDECREF(method);
1311
1312 return cursor;
1313}
1314
Larry Hastings01b08832015-05-08 07:37:49 -07001315PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001316{
1317 PyObject* cursor = 0;
1318 PyObject* result = 0;
1319 PyObject* method = 0;
1320
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001321 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001322 if (!cursor) {
1323 goto error;
1324 }
1325
1326 method = PyObject_GetAttrString(cursor, "executemany");
1327 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001328 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001329 goto error;
1330 }
1331
1332 result = PyObject_CallObject(method, args);
1333 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001334 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001335 }
1336
1337error:
1338 Py_XDECREF(result);
1339 Py_XDECREF(method);
1340
1341 return cursor;
1342}
1343
Larry Hastings01b08832015-05-08 07:37:49 -07001344PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001345{
1346 PyObject* cursor = 0;
1347 PyObject* result = 0;
1348 PyObject* method = 0;
1349
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001350 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001351 if (!cursor) {
1352 goto error;
1353 }
1354
1355 method = PyObject_GetAttrString(cursor, "executescript");
1356 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001357 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001358 goto error;
1359 }
1360
1361 result = PyObject_CallObject(method, args);
1362 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001363 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001364 }
1365
1366error:
1367 Py_XDECREF(result);
1368 Py_XDECREF(method);
1369
1370 return cursor;
1371}
1372
1373/* ------------------------- COLLATION CODE ------------------------ */
1374
1375static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001376pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001377 void* context,
1378 int text1_length, const void* text1_data,
1379 int text2_length, const void* text2_data)
1380{
1381 PyObject* callback = (PyObject*)context;
1382 PyObject* string1 = 0;
1383 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001384#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001385 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001386#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001387 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001388 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001389 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001390#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001391 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001392#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001393
1394 if (PyErr_Occurred()) {
1395 goto finally;
1396 }
1397
Guido van Rossum98297ee2007-11-06 21:34:58 +00001398 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1399 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001400
1401 if (!string1 || !string2) {
1402 goto finally; /* failed to allocate strings */
1403 }
1404
1405 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1406
1407 if (!retval) {
1408 /* execution failed */
1409 goto finally;
1410 }
1411
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001412 longval = PyLong_AsLongAndOverflow(retval, &result);
1413 if (longval == -1 && PyErr_Occurred()) {
1414 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001415 result = 0;
1416 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001417 else if (!result) {
1418 if (longval > 0)
1419 result = 1;
1420 else if (longval < 0)
1421 result = -1;
1422 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001423
1424finally:
1425 Py_XDECREF(string1);
1426 Py_XDECREF(string2);
1427 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001428#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001429 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001430#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431 return result;
1432}
1433
1434static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001435pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001436{
1437 PyObject* retval = NULL;
1438
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001439 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001440 goto finally;
1441 }
1442
1443 sqlite3_interrupt(self->db);
1444
1445 Py_INCREF(Py_None);
1446 retval = Py_None;
1447
1448finally:
1449 return retval;
1450}
1451
Christian Heimesbbe741d2008-03-28 10:53:29 +00001452/* Function author: Paul Kippes <kippesp@gmail.com>
1453 * Class method of Connection to call the Python function _iterdump
1454 * of the sqlite3 module.
1455 */
1456static PyObject *
1457pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1458{
1459 PyObject* retval = NULL;
1460 PyObject* module = NULL;
1461 PyObject* module_dict;
1462 PyObject* pyfn_iterdump;
1463
1464 if (!pysqlite_check_connection(self)) {
1465 goto finally;
1466 }
1467
1468 module = PyImport_ImportModule(MODULE_NAME ".dump");
1469 if (!module) {
1470 goto finally;
1471 }
1472
1473 module_dict = PyModule_GetDict(module);
1474 if (!module_dict) {
1475 goto finally;
1476 }
1477
1478 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1479 if (!pyfn_iterdump) {
1480 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1481 goto finally;
1482 }
1483
1484 args = PyTuple_New(1);
1485 if (!args) {
1486 goto finally;
1487 }
1488 Py_INCREF(self);
1489 PyTuple_SetItem(args, 0, (PyObject*)self);
1490 retval = PyObject_CallObject(pyfn_iterdump, args);
1491
1492finally:
1493 Py_XDECREF(args);
1494 Py_XDECREF(module);
1495 return retval;
1496}
1497
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001498static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001499pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001500{
1501 PyObject* callable;
1502 PyObject* uppercase_name = 0;
1503 PyObject* name;
1504 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001505 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001506 _Py_IDENTIFIER(upper);
Victor Stinner35466c52010-04-22 11:23:23 +00001507 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001508 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001509 unsigned int kind;
1510 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001511
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001512 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001513 goto finally;
1514 }
1515
Gerhard Häring6d214562007-08-10 18:15:11 +00001516 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001517 goto finally;
1518 }
1519
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001520 uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 if (!uppercase_name) {
1522 goto finally;
1523 }
1524
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001525 if (PyUnicode_READY(uppercase_name))
1526 goto finally;
1527 len = PyUnicode_GET_LENGTH(uppercase_name);
1528 kind = PyUnicode_KIND(uppercase_name);
1529 data = PyUnicode_DATA(uppercase_name);
1530 for (i=0; i<len; i++) {
1531 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1532 if ((ch >= '0' && ch <= '9')
1533 || (ch >= 'A' && ch <= 'Z')
1534 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001535 {
Victor Stinner35466c52010-04-22 11:23:23 +00001536 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001538 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001539 goto finally;
1540 }
1541 }
1542
Victor Stinner35466c52010-04-22 11:23:23 +00001543 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1544 if (!uppercase_name_str)
1545 goto finally;
1546
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001547 if (callable != Py_None && !PyCallable_Check(callable)) {
1548 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1549 goto finally;
1550 }
1551
1552 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001553 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1554 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001555 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001556 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1557 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001558 }
1559
1560 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001561 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001562 SQLITE_UTF8,
1563 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001564 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001565 if (rc != SQLITE_OK) {
1566 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001567 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001568 goto finally;
1569 }
1570
1571finally:
1572 Py_XDECREF(uppercase_name);
1573
1574 if (PyErr_Occurred()) {
1575 retval = NULL;
1576 } else {
1577 Py_INCREF(Py_None);
1578 retval = Py_None;
1579 }
1580
1581 return retval;
1582}
1583
Christian Heimesbbe741d2008-03-28 10:53:29 +00001584/* Called when the connection is used as a context manager. Returns itself as a
1585 * convenience to the caller. */
1586static PyObject *
1587pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1588{
1589 Py_INCREF(self);
1590 return (PyObject*)self;
1591}
1592
1593/** Called when the connection is used as a context manager. If there was any
1594 * exception, a rollback takes place; otherwise we commit. */
1595static PyObject *
1596pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1597{
1598 PyObject* exc_type, *exc_value, *exc_tb;
1599 char* method_name;
1600 PyObject* result;
1601
1602 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1603 return NULL;
1604 }
1605
1606 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1607 method_name = "commit";
1608 } else {
1609 method_name = "rollback";
1610 }
1611
1612 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1613 if (!result) {
1614 return NULL;
1615 }
1616 Py_DECREF(result);
1617
1618 Py_RETURN_FALSE;
1619}
1620
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001621static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001623
1624static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001625 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1626 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001627 {NULL}
1628};
1629
1630static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001631 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001632 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001633 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001634 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001635 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001636 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001637 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001638 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001639 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001640 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001641 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001642 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001643 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001644 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001645 #ifdef HAVE_LOAD_EXTENSION
1646 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1647 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1648 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1649 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1650 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001651 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1652 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001653 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1654 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001655 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001656 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001657 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001658 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001659 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001660 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001661 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001662 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001663 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001664 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001665 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001666 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001667 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1668 PyDoc_STR("For context manager. Non-standard.")},
1669 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1670 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001671 {NULL, NULL}
1672};
1673
1674static struct PyMemberDef connection_members[] =
1675{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001676 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1677 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1678 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1679 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1680 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1681 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1682 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1683 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1684 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1685 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001686 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1687 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001688 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001689 {NULL}
1690};
1691
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001692PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001693 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001694 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001695 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001696 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001697 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001698 0, /* tp_print */
1699 0, /* tp_getattr */
1700 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001701 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001702 0, /* tp_repr */
1703 0, /* tp_as_number */
1704 0, /* tp_as_sequence */
1705 0, /* tp_as_mapping */
1706 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001707 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001708 0, /* tp_str */
1709 0, /* tp_getattro */
1710 0, /* tp_setattro */
1711 0, /* tp_as_buffer */
1712 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1713 connection_doc, /* tp_doc */
1714 0, /* tp_traverse */
1715 0, /* tp_clear */
1716 0, /* tp_richcompare */
1717 0, /* tp_weaklistoffset */
1718 0, /* tp_iter */
1719 0, /* tp_iternext */
1720 connection_methods, /* tp_methods */
1721 connection_members, /* tp_members */
1722 connection_getset, /* tp_getset */
1723 0, /* tp_base */
1724 0, /* tp_dict */
1725 0, /* tp_descr_get */
1726 0, /* tp_descr_set */
1727 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001728 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001729 0, /* tp_alloc */
1730 0, /* tp_new */
1731 0 /* tp_free */
1732};
1733
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001734extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001735{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001736 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1737 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001738}