blob: 4beed7896dea7e306b992fd78a634d154a24ef59 [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167 self->check_same_thread = check_same_thread;
168
169 self->function_pinboard = PyDict_New();
170 if (!self->function_pinboard) {
171 return -1;
172 }
173
174 self->collations = PyDict_New();
175 if (!self->collations) {
176 return -1;
177 }
178
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000179 self->Warning = pysqlite_Warning;
180 self->Error = pysqlite_Error;
181 self->InterfaceError = pysqlite_InterfaceError;
182 self->DatabaseError = pysqlite_DatabaseError;
183 self->DataError = pysqlite_DataError;
184 self->OperationalError = pysqlite_OperationalError;
185 self->IntegrityError = pysqlite_IntegrityError;
186 self->InternalError = pysqlite_InternalError;
187 self->ProgrammingError = pysqlite_ProgrammingError;
188 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189
190 return 0;
191}
192
Thomas Wouters477c8d52006-05-27 19:21:47 +0000193/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000194void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000196 pysqlite_Node* node;
197 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198
199 node = self->statement_cache->first;
200
201 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000202 statement = (pysqlite_Statement*)(node->data);
203 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000204 node = node->next;
205 }
206
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300207 Py_SETREF(self->statement_cache,
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200208 (pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209 Py_DECREF(self);
210 self->statement_cache->decref_factory = 0;
211}
212
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000213/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000214void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000216 int i;
217 PyObject* weakref;
218 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000219 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220
Thomas Wouters477c8d52006-05-27 19:21:47 +0000221 for (i = 0; i < PyList_Size(self->statements); i++) {
222 weakref = PyList_GetItem(self->statements, i);
223 statement = PyWeakref_GetObject(weakref);
224 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500225 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000226 if (action == ACTION_RESET) {
227 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
228 } else {
229 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
230 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500231 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000232 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000233 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000234
235 if (reset_cursors) {
236 for (i = 0; i < PyList_Size(self->cursors); i++) {
237 weakref = PyList_GetItem(self->cursors, i);
238 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
239 if ((PyObject*)cursor != Py_None) {
240 cursor->reset = 1;
241 }
242 }
243 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244}
245
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000246void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247{
248 Py_XDECREF(self->statement_cache);
249
250 /* Clean up if user has not called .close() explicitly. */
251 if (self->db) {
252 Py_BEGIN_ALLOW_THREADS
253 sqlite3_close(self->db);
254 Py_END_ALLOW_THREADS
255 }
256
257 if (self->begin_statement) {
258 PyMem_Free(self->begin_statement);
259 }
260 Py_XDECREF(self->isolation_level);
261 Py_XDECREF(self->function_pinboard);
262 Py_XDECREF(self->row_factory);
263 Py_XDECREF(self->text_factory);
264 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000265 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000266 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267
Christian Heimes90aa7642007-12-19 02:45:37 +0000268 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269}
270
Gerhard Häringf9cee222010-03-05 15:20:03 +0000271/*
272 * Registers a cursor with the connection.
273 *
274 * 0 => error; 1 => ok
275 */
276int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
277{
278 PyObject* weakref;
279
280 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
281 if (!weakref) {
282 goto error;
283 }
284
285 if (PyList_Append(connection->cursors, weakref) != 0) {
286 Py_CLEAR(weakref);
287 goto error;
288 }
289
290 Py_DECREF(weakref);
291
292 return 1;
293error:
294 return 0;
295}
296
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000297PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298{
299 static char *kwlist[] = {"factory", NULL, NULL};
300 PyObject* factory = NULL;
301 PyObject* cursor;
302
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
304 &factory)) {
305 return NULL;
306 }
307
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000308 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000309 return NULL;
310 }
311
312 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000313 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000314 }
315
316 cursor = PyObject_CallFunction(factory, "O", self);
317
Gerhard Häringf9cee222010-03-05 15:20:03 +0000318 _pysqlite_drop_unused_cursor_references(self);
319
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000321 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300322 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 }
324
325 return cursor;
326}
327
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000328PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000329{
330 int rc;
331
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000332 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333 return NULL;
334 }
335
Gerhard Häringf9cee222010-03-05 15:20:03 +0000336 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337
338 if (self->db) {
339 Py_BEGIN_ALLOW_THREADS
340 rc = sqlite3_close(self->db);
341 Py_END_ALLOW_THREADS
342
343 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000344 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000345 return NULL;
346 } else {
347 self->db = NULL;
348 }
349 }
350
Berker Peksagfe21de92016-04-09 07:34:39 +0300351 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352}
353
354/*
355 * Checks if a connection object is usable (i. e. not closed).
356 *
357 * 0 => error; 1 => ok
358 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000359int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000361 if (!con->initialized) {
362 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
363 return 0;
364 }
365
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000367 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 return 0;
369 } else {
370 return 1;
371 }
372}
373
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000374PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000375{
376 int rc;
377 const char* tail;
378 sqlite3_stmt* statement;
379
380 Py_BEGIN_ALLOW_THREADS
381 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
382 Py_END_ALLOW_THREADS
383
384 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000385 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 goto error;
387 }
388
Benjamin Petersond7b03282008-09-13 15:58:53 +0000389 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 if (rc == SQLITE_DONE) {
391 self->inTransaction = 1;
392 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000393 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394 }
395
396 Py_BEGIN_ALLOW_THREADS
397 rc = sqlite3_finalize(statement);
398 Py_END_ALLOW_THREADS
399
400 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000401 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402 }
403
404error:
405 if (PyErr_Occurred()) {
406 return NULL;
407 } else {
408 Py_INCREF(Py_None);
409 return Py_None;
410 }
411}
412
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000413PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000414{
415 int rc;
416 const char* tail;
417 sqlite3_stmt* statement;
418
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000419 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420 return NULL;
421 }
422
423 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000424 pysqlite_do_all_statements(self, ACTION_RESET, 0);
425
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426 Py_BEGIN_ALLOW_THREADS
427 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
428 Py_END_ALLOW_THREADS
429 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000430 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431 goto error;
432 }
433
Benjamin Petersond7b03282008-09-13 15:58:53 +0000434 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000435 if (rc == SQLITE_DONE) {
436 self->inTransaction = 0;
437 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000438 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439 }
440
441 Py_BEGIN_ALLOW_THREADS
442 rc = sqlite3_finalize(statement);
443 Py_END_ALLOW_THREADS
444 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000445 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446 }
447
448 }
449
450error:
451 if (PyErr_Occurred()) {
452 return NULL;
453 } else {
454 Py_INCREF(Py_None);
455 return Py_None;
456 }
457}
458
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000459PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000460{
461 int rc;
462 const char* tail;
463 sqlite3_stmt* statement;
464
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000465 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000466 return NULL;
467 }
468
469 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000470 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000471
472 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000473 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474 Py_END_ALLOW_THREADS
475 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000476 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000477 goto error;
478 }
479
Benjamin Petersond7b03282008-09-13 15:58:53 +0000480 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000481 if (rc == SQLITE_DONE) {
482 self->inTransaction = 0;
483 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000484 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000485 }
486
487 Py_BEGIN_ALLOW_THREADS
488 rc = sqlite3_finalize(statement);
489 Py_END_ALLOW_THREADS
490 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000491 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492 }
493
494 }
495
496error:
497 if (PyErr_Occurred()) {
498 return NULL;
499 } else {
500 Py_INCREF(Py_None);
501 return Py_None;
502 }
503}
504
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200505static int
506_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200508 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000510 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200511 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
512 if (value == -1 && PyErr_Occurred())
513 return -1;
514 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515 } else if (PyFloat_Check(py_val)) {
516 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000517 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200518 const char *str = _PyUnicode_AsString(py_val);
519 if (str == NULL)
520 return -1;
521 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000522 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200523 Py_buffer view;
524 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100525 PyErr_SetString(PyExc_ValueError,
526 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200527 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000528 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200529 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100530 PyErr_SetString(PyExc_OverflowError,
531 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200532 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100533 return -1;
534 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200535 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
536 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200538 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200540 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541}
542
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000543PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544{
545 PyObject* args;
546 int i;
547 sqlite3_value* cur_value;
548 PyObject* cur_py_value;
549 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551
552 args = PyTuple_New(argc);
553 if (!args) {
554 return NULL;
555 }
556
557 for (i = 0; i < argc; i++) {
558 cur_value = argv[i];
559 switch (sqlite3_value_type(argv[i])) {
560 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200561 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562 break;
563 case SQLITE_FLOAT:
564 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
565 break;
566 case SQLITE_TEXT:
567 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000568 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569 /* TODO: have a way to show errors here */
570 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000571 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000572 Py_INCREF(Py_None);
573 cur_py_value = Py_None;
574 }
575 break;
576 case SQLITE_BLOB:
577 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000578 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000579 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000580 break;
581 case SQLITE_NULL:
582 default:
583 Py_INCREF(Py_None);
584 cur_py_value = Py_None;
585 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000586
587 if (!cur_py_value) {
588 Py_DECREF(args);
589 return NULL;
590 }
591
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592 PyTuple_SetItem(args, i, cur_py_value);
593
594 }
595
596 return args;
597}
598
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000599void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000600{
601 PyObject* args;
602 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000603 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200604 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000605
Georg Brandldfd73442009-04-05 11:47:34 +0000606#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000607 PyGILState_STATE threadstate;
608
609 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000610#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611
612 py_func = (PyObject*)sqlite3_user_data(context);
613
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000614 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000615 if (args) {
616 py_retval = PyObject_CallObject(py_func, args);
617 Py_DECREF(args);
618 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000619
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200620 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000621 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200622 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000623 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200624 }
625 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626 if (_enable_callback_tracebacks) {
627 PyErr_Print();
628 } else {
629 PyErr_Clear();
630 }
631 _sqlite3_result_error(context, "user-defined function raised exception", -1);
632 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000633
Georg Brandldfd73442009-04-05 11:47:34 +0000634#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000635 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000636#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637}
638
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000639static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000640{
641 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000642 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643 PyObject* aggregate_class;
644 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000645 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646
Georg Brandldfd73442009-04-05 11:47:34 +0000647#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648 PyGILState_STATE threadstate;
649
650 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000651#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652
653 aggregate_class = (PyObject*)sqlite3_user_data(context);
654
655 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
656
657 if (*aggregate_instance == 0) {
658 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
659
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000662 if (_enable_callback_tracebacks) {
663 PyErr_Print();
664 } else {
665 PyErr_Clear();
666 }
667 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000668 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000669 }
670 }
671
672 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000673 if (!stepmethod) {
674 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 }
676
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000677 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678 if (!args) {
679 goto error;
680 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000681
682 function_result = PyObject_CallObject(stepmethod, args);
683 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684
Thomas Wouters477c8d52006-05-27 19:21:47 +0000685 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000686 if (_enable_callback_tracebacks) {
687 PyErr_Print();
688 } else {
689 PyErr_Clear();
690 }
691 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692 }
693
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694error:
695 Py_XDECREF(stepmethod);
696 Py_XDECREF(function_result);
697
Georg Brandldfd73442009-04-05 11:47:34 +0000698#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000699 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000700#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000701}
702
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000703void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200705 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200707 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200708 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200709 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200710 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711
Georg Brandldfd73442009-04-05 11:47:34 +0000712#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713 PyGILState_STATE threadstate;
714
715 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000716#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000717
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000718 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
719 if (!*aggregate_instance) {
720 /* this branch is executed if there was an exception in the aggregate's
721 * __init__ */
722
Thomas Wouters477c8d52006-05-27 19:21:47 +0000723 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000724 }
725
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200726 /* Keep the exception (if any) of the last call to step() */
727 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200728 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200729
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200730 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200731
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200732 Py_DECREF(*aggregate_instance);
733
734 ok = 0;
735 if (function_result) {
736 ok = _pysqlite_set_result(context, function_result) == 0;
737 Py_DECREF(function_result);
738 }
739 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000740 if (_enable_callback_tracebacks) {
741 PyErr_Print();
742 } else {
743 PyErr_Clear();
744 }
745 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200746#if SQLITE_VERSION_NUMBER < 3003003
747 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
748 exception, so don't restore the previous exception */
749 restore = 0;
750#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751 }
752
Victor Stinnerffff7632013-08-02 01:48:10 +0200753 if (restore) {
754 /* Restore the exception (if any) of the last call to step(),
755 but clear also the current exception if finalize() failed */
756 PyErr_Restore(exception, value, tb);
757 }
Victor Stinner3a857322013-07-22 08:34:32 +0200758
Thomas Wouters477c8d52006-05-27 19:21:47 +0000759error:
Georg Brandldfd73442009-04-05 11:47:34 +0000760#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000762#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200763 /* explicit return to avoid a compilation error if WITH_THREAD
764 is not defined */
765 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766}
767
Gerhard Häringf9cee222010-03-05 15:20:03 +0000768static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000769{
770 PyObject* new_list;
771 PyObject* weakref;
772 int i;
773
774 /* we only need to do this once in a while */
775 if (self->created_statements++ < 200) {
776 return;
777 }
778
779 self->created_statements = 0;
780
781 new_list = PyList_New(0);
782 if (!new_list) {
783 return;
784 }
785
786 for (i = 0; i < PyList_Size(self->statements); i++) {
787 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000788 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000789 if (PyList_Append(new_list, weakref) != 0) {
790 Py_DECREF(new_list);
791 return;
792 }
793 }
794 }
795
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300796 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000797}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798
Gerhard Häringf9cee222010-03-05 15:20:03 +0000799static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
800{
801 PyObject* new_list;
802 PyObject* weakref;
803 int i;
804
805 /* we only need to do this once in a while */
806 if (self->created_cursors++ < 200) {
807 return;
808 }
809
810 self->created_cursors = 0;
811
812 new_list = PyList_New(0);
813 if (!new_list) {
814 return;
815 }
816
817 for (i = 0; i < PyList_Size(self->cursors); i++) {
818 weakref = PyList_GetItem(self->cursors, i);
819 if (PyWeakref_GetObject(weakref) != Py_None) {
820 if (PyList_Append(new_list, weakref) != 0) {
821 Py_DECREF(new_list);
822 return;
823 }
824 }
825 }
826
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300827 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000828}
829
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000830PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831{
832 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
833
834 PyObject* func;
835 char* name;
836 int narg;
837 int rc;
838
Gerhard Häringf9cee222010-03-05 15:20:03 +0000839 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
840 return NULL;
841 }
842
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000843 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
844 &name, &narg, &func))
845 {
846 return NULL;
847 }
848
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000849 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000850
Thomas Wouters477c8d52006-05-27 19:21:47 +0000851 if (rc != SQLITE_OK) {
852 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000853 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000854 return NULL;
855 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000856 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
857 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858
Berker Peksagfe21de92016-04-09 07:34:39 +0300859 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000860 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861}
862
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000863PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864{
865 PyObject* aggregate_class;
866
867 int n_arg;
868 char* name;
869 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
870 int rc;
871
Gerhard Häringf9cee222010-03-05 15:20:03 +0000872 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
873 return NULL;
874 }
875
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000876 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
877 kwlist, &name, &n_arg, &aggregate_class)) {
878 return NULL;
879 }
880
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000881 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 +0000882 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000883 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000884 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000885 return NULL;
886 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000887 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
888 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000889
Berker Peksagfe21de92016-04-09 07:34:39 +0300890 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000891 }
892}
893
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000894static 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 +0000895{
896 PyObject *ret;
897 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000898#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000899 PyGILState_STATE gilstate;
900
901 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000902#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000903
Victor Stinnerd4095d92013-07-26 22:23:33 +0200904 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000905
Victor Stinnerd4095d92013-07-26 22:23:33 +0200906 if (ret == NULL) {
907 if (_enable_callback_tracebacks)
908 PyErr_Print();
909 else
910 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200911
Victor Stinnerd4095d92013-07-26 22:23:33 +0200912 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200913 }
914 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200915 if (PyLong_Check(ret)) {
916 rc = _PyLong_AsInt(ret);
917 if (rc == -1 && PyErr_Occurred()) {
918 if (_enable_callback_tracebacks)
919 PyErr_Print();
920 else
921 PyErr_Clear();
922 rc = SQLITE_DENY;
923 }
924 }
925 else {
926 rc = SQLITE_DENY;
927 }
928 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000929 }
930
Georg Brandldfd73442009-04-05 11:47:34 +0000931#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000932 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000933#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000934 return rc;
935}
936
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000937static int _progress_handler(void* user_arg)
938{
939 int rc;
940 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000941#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000942 PyGILState_STATE gilstate;
943
944 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000945#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000946 ret = PyObject_CallFunction((PyObject*)user_arg, "");
947
948 if (!ret) {
949 if (_enable_callback_tracebacks) {
950 PyErr_Print();
951 } else {
952 PyErr_Clear();
953 }
954
Mark Dickinson934896d2009-02-21 20:59:32 +0000955 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000956 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000957 } else {
958 rc = (int)PyObject_IsTrue(ret);
959 Py_DECREF(ret);
960 }
961
Georg Brandldfd73442009-04-05 11:47:34 +0000962#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000963 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000964#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000965 return rc;
966}
967
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200968static void _trace_callback(void* user_arg, const char* statement_string)
969{
970 PyObject *py_statement = NULL;
971 PyObject *ret = NULL;
972
973#ifdef WITH_THREAD
974 PyGILState_STATE gilstate;
975
976 gilstate = PyGILState_Ensure();
977#endif
978 py_statement = PyUnicode_DecodeUTF8(statement_string,
979 strlen(statement_string), "replace");
980 if (py_statement) {
981 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
982 Py_DECREF(py_statement);
983 }
984
985 if (ret) {
986 Py_DECREF(ret);
987 } else {
988 if (_enable_callback_tracebacks) {
989 PyErr_Print();
990 } else {
991 PyErr_Clear();
992 }
993 }
994
995#ifdef WITH_THREAD
996 PyGILState_Release(gilstate);
997#endif
998}
999
Gerhard Häringf9cee222010-03-05 15:20:03 +00001000static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001001{
1002 PyObject* authorizer_cb;
1003
1004 static char *kwlist[] = { "authorizer_callback", NULL };
1005 int rc;
1006
Gerhard Häringf9cee222010-03-05 15:20:03 +00001007 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1008 return NULL;
1009 }
1010
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001011 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1012 kwlist, &authorizer_cb)) {
1013 return NULL;
1014 }
1015
1016 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1017
1018 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001019 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001020 return NULL;
1021 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001022 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1023 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001024
Berker Peksagfe21de92016-04-09 07:34:39 +03001025 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001026 }
1027}
1028
Gerhard Häringf9cee222010-03-05 15:20:03 +00001029static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001030{
1031 PyObject* progress_handler;
1032 int n;
1033
1034 static char *kwlist[] = { "progress_handler", "n", NULL };
1035
Gerhard Häringf9cee222010-03-05 15:20:03 +00001036 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1037 return NULL;
1038 }
1039
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001040 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1041 kwlist, &progress_handler, &n)) {
1042 return NULL;
1043 }
1044
1045 if (progress_handler == Py_None) {
1046 /* None clears the progress handler previously set */
1047 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1048 } else {
1049 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001050 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1051 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001052 }
1053
Berker Peksagfe21de92016-04-09 07:34:39 +03001054 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001055}
1056
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001057static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1058{
1059 PyObject* trace_callback;
1060
1061 static char *kwlist[] = { "trace_callback", NULL };
1062
1063 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1064 return NULL;
1065 }
1066
1067 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1068 kwlist, &trace_callback)) {
1069 return NULL;
1070 }
1071
1072 if (trace_callback == Py_None) {
1073 /* None clears the trace callback previously set */
1074 sqlite3_trace(self->db, 0, (void*)0);
1075 } else {
1076 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1077 return NULL;
1078 sqlite3_trace(self->db, _trace_callback, trace_callback);
1079 }
1080
Berker Peksagfe21de92016-04-09 07:34:39 +03001081 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001082}
1083
Gerhard Häringf9cee222010-03-05 15:20:03 +00001084#ifdef HAVE_LOAD_EXTENSION
1085static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1086{
1087 int rc;
1088 int onoff;
1089
1090 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1091 return NULL;
1092 }
1093
1094 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1095 return NULL;
1096 }
1097
1098 rc = sqlite3_enable_load_extension(self->db, onoff);
1099
1100 if (rc != SQLITE_OK) {
1101 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1102 return NULL;
1103 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001104 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001105 }
1106}
1107
1108static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1109{
1110 int rc;
1111 char* extension_name;
1112 char* errmsg;
1113
1114 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1115 return NULL;
1116 }
1117
1118 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1119 return NULL;
1120 }
1121
1122 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1123 if (rc != 0) {
1124 PyErr_SetString(pysqlite_OperationalError, errmsg);
1125 return NULL;
1126 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001127 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001128 }
1129}
1130#endif
1131
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001132int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001133{
Georg Brandldfd73442009-04-05 11:47:34 +00001134#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135 if (self->check_same_thread) {
1136 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001137 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001138 "SQLite objects created in a thread can only be used in that same thread."
1139 "The object was created in thread id %ld and this is thread id %ld",
1140 self->thread_ident, PyThread_get_thread_ident());
1141 return 0;
1142 }
1143
1144 }
Georg Brandldfd73442009-04-05 11:47:34 +00001145#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 return 1;
1147}
1148
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001149static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001150{
1151 Py_INCREF(self->isolation_level);
1152 return self->isolation_level;
1153}
1154
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001155static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001156{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001157 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001158 return NULL;
1159 } else {
1160 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1161 }
1162}
1163
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001164static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001165{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166 PyObject* res;
1167 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001168 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001169
1170 Py_XDECREF(self->isolation_level);
1171
1172 if (self->begin_statement) {
1173 PyMem_Free(self->begin_statement);
1174 self->begin_statement = NULL;
1175 }
1176
1177 if (isolation_level == Py_None) {
1178 Py_INCREF(Py_None);
1179 self->isolation_level = Py_None;
1180
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001181 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001182 if (!res) {
1183 return -1;
1184 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185 Py_DECREF(res);
1186
1187 self->inTransaction = 0;
1188 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001189 const char *statement;
1190 Py_ssize_t size;
1191
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001192 Py_INCREF(isolation_level);
1193 self->isolation_level = isolation_level;
1194
Georg Brandlceab6102007-11-25 00:45:05 +00001195 if (!begin_word) {
1196 begin_word = PyUnicode_FromString("BEGIN ");
1197 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001198 }
Georg Brandlceab6102007-11-25 00:45:05 +00001199 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001200 if (!begin_statement) {
1201 return -1;
1202 }
1203
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001204 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001205 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001206 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001207 return -1;
1208 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001209 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001210 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001211 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001212 return -1;
1213 }
1214
Neal Norwitzefee9f52007-10-27 02:50:52 +00001215 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001216 Py_DECREF(begin_statement);
1217 }
1218
1219 return 0;
1220}
1221
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001222PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001223{
1224 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001225 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001226 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227 int rc;
1228
Gerhard Häringf9cee222010-03-05 15:20:03 +00001229 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1230 return NULL;
1231 }
1232
Larry Hastings3b12e952015-05-08 07:45:10 -07001233 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection()", kwargs))
1234 return NULL;
1235
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001236 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001237 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001238
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001239 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001240
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001241 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242 if (!statement) {
1243 return NULL;
1244 }
1245
Victor Stinner0201f442010-03-13 03:28:34 +00001246 statement->db = NULL;
1247 statement->st = NULL;
1248 statement->sql = NULL;
1249 statement->in_use = 0;
1250 statement->in_weakreflist = NULL;
1251
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001252 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001253 if (rc != SQLITE_OK) {
1254 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001255 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001256 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001257 if (PyErr_ExceptionMatches(PyExc_TypeError))
1258 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001260 (void)pysqlite_statement_reset(statement);
1261 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001262 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001263 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264 }
1265
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001266 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1267 if (weakref == NULL)
1268 goto error;
1269 if (PyList_Append(self->statements, weakref) != 0) {
1270 Py_DECREF(weakref);
1271 goto error;
1272 }
1273 Py_DECREF(weakref);
1274
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001276
1277error:
1278 Py_DECREF(statement);
1279 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280}
1281
Larry Hastings01b08832015-05-08 07:37:49 -07001282PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001283{
1284 PyObject* cursor = 0;
1285 PyObject* result = 0;
1286 PyObject* method = 0;
1287
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001288 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001289 if (!cursor) {
1290 goto error;
1291 }
1292
1293 method = PyObject_GetAttrString(cursor, "execute");
1294 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001295 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001296 goto error;
1297 }
1298
1299 result = PyObject_CallObject(method, args);
1300 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001301 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001302 }
1303
1304error:
1305 Py_XDECREF(result);
1306 Py_XDECREF(method);
1307
1308 return cursor;
1309}
1310
Larry Hastings01b08832015-05-08 07:37:49 -07001311PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001312{
1313 PyObject* cursor = 0;
1314 PyObject* result = 0;
1315 PyObject* method = 0;
1316
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001317 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001318 if (!cursor) {
1319 goto error;
1320 }
1321
1322 method = PyObject_GetAttrString(cursor, "executemany");
1323 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001324 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001325 goto error;
1326 }
1327
1328 result = PyObject_CallObject(method, args);
1329 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001330 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001331 }
1332
1333error:
1334 Py_XDECREF(result);
1335 Py_XDECREF(method);
1336
1337 return cursor;
1338}
1339
Larry Hastings01b08832015-05-08 07:37:49 -07001340PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001341{
1342 PyObject* cursor = 0;
1343 PyObject* result = 0;
1344 PyObject* method = 0;
1345
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001346 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001347 if (!cursor) {
1348 goto error;
1349 }
1350
1351 method = PyObject_GetAttrString(cursor, "executescript");
1352 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001353 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001354 goto error;
1355 }
1356
1357 result = PyObject_CallObject(method, args);
1358 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001359 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001360 }
1361
1362error:
1363 Py_XDECREF(result);
1364 Py_XDECREF(method);
1365
1366 return cursor;
1367}
1368
1369/* ------------------------- COLLATION CODE ------------------------ */
1370
1371static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001372pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373 void* context,
1374 int text1_length, const void* text1_data,
1375 int text2_length, const void* text2_data)
1376{
1377 PyObject* callback = (PyObject*)context;
1378 PyObject* string1 = 0;
1379 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001380#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001381 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001382#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001383 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001384 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001385 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001386#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001387 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001388#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001389
1390 if (PyErr_Occurred()) {
1391 goto finally;
1392 }
1393
Guido van Rossum98297ee2007-11-06 21:34:58 +00001394 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1395 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001396
1397 if (!string1 || !string2) {
1398 goto finally; /* failed to allocate strings */
1399 }
1400
1401 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1402
1403 if (!retval) {
1404 /* execution failed */
1405 goto finally;
1406 }
1407
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001408 longval = PyLong_AsLongAndOverflow(retval, &result);
1409 if (longval == -1 && PyErr_Occurred()) {
1410 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411 result = 0;
1412 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001413 else if (!result) {
1414 if (longval > 0)
1415 result = 1;
1416 else if (longval < 0)
1417 result = -1;
1418 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001419
1420finally:
1421 Py_XDECREF(string1);
1422 Py_XDECREF(string2);
1423 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001424#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001425 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001426#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001427 return result;
1428}
1429
1430static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001431pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001432{
1433 PyObject* retval = NULL;
1434
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001435 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001436 goto finally;
1437 }
1438
1439 sqlite3_interrupt(self->db);
1440
1441 Py_INCREF(Py_None);
1442 retval = Py_None;
1443
1444finally:
1445 return retval;
1446}
1447
Christian Heimesbbe741d2008-03-28 10:53:29 +00001448/* Function author: Paul Kippes <kippesp@gmail.com>
1449 * Class method of Connection to call the Python function _iterdump
1450 * of the sqlite3 module.
1451 */
1452static PyObject *
1453pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1454{
1455 PyObject* retval = NULL;
1456 PyObject* module = NULL;
1457 PyObject* module_dict;
1458 PyObject* pyfn_iterdump;
1459
1460 if (!pysqlite_check_connection(self)) {
1461 goto finally;
1462 }
1463
1464 module = PyImport_ImportModule(MODULE_NAME ".dump");
1465 if (!module) {
1466 goto finally;
1467 }
1468
1469 module_dict = PyModule_GetDict(module);
1470 if (!module_dict) {
1471 goto finally;
1472 }
1473
1474 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1475 if (!pyfn_iterdump) {
1476 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1477 goto finally;
1478 }
1479
1480 args = PyTuple_New(1);
1481 if (!args) {
1482 goto finally;
1483 }
1484 Py_INCREF(self);
1485 PyTuple_SetItem(args, 0, (PyObject*)self);
1486 retval = PyObject_CallObject(pyfn_iterdump, args);
1487
1488finally:
1489 Py_XDECREF(args);
1490 Py_XDECREF(module);
1491 return retval;
1492}
1493
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001494static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001495pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001496{
1497 PyObject* callable;
1498 PyObject* uppercase_name = 0;
1499 PyObject* name;
1500 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001501 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001502 _Py_IDENTIFIER(upper);
Victor Stinner35466c52010-04-22 11:23:23 +00001503 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001504 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001505 unsigned int kind;
1506 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001507
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001508 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001509 goto finally;
1510 }
1511
Gerhard Häring6d214562007-08-10 18:15:11 +00001512 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001513 goto finally;
1514 }
1515
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001516 uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001517 if (!uppercase_name) {
1518 goto finally;
1519 }
1520
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001521 if (PyUnicode_READY(uppercase_name))
1522 goto finally;
1523 len = PyUnicode_GET_LENGTH(uppercase_name);
1524 kind = PyUnicode_KIND(uppercase_name);
1525 data = PyUnicode_DATA(uppercase_name);
1526 for (i=0; i<len; i++) {
1527 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1528 if ((ch >= '0' && ch <= '9')
1529 || (ch >= 'A' && ch <= 'Z')
1530 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001531 {
Victor Stinner35466c52010-04-22 11:23:23 +00001532 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001533 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001534 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001535 goto finally;
1536 }
1537 }
1538
Victor Stinner35466c52010-04-22 11:23:23 +00001539 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1540 if (!uppercase_name_str)
1541 goto finally;
1542
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001543 if (callable != Py_None && !PyCallable_Check(callable)) {
1544 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1545 goto finally;
1546 }
1547
1548 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001549 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1550 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001551 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001552 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1553 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001554 }
1555
1556 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001557 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001558 SQLITE_UTF8,
1559 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001560 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001561 if (rc != SQLITE_OK) {
1562 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001563 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001564 goto finally;
1565 }
1566
1567finally:
1568 Py_XDECREF(uppercase_name);
1569
1570 if (PyErr_Occurred()) {
1571 retval = NULL;
1572 } else {
1573 Py_INCREF(Py_None);
1574 retval = Py_None;
1575 }
1576
1577 return retval;
1578}
1579
Christian Heimesbbe741d2008-03-28 10:53:29 +00001580/* Called when the connection is used as a context manager. Returns itself as a
1581 * convenience to the caller. */
1582static PyObject *
1583pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1584{
1585 Py_INCREF(self);
1586 return (PyObject*)self;
1587}
1588
1589/** Called when the connection is used as a context manager. If there was any
1590 * exception, a rollback takes place; otherwise we commit. */
1591static PyObject *
1592pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1593{
1594 PyObject* exc_type, *exc_value, *exc_tb;
1595 char* method_name;
1596 PyObject* result;
1597
1598 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1599 return NULL;
1600 }
1601
1602 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1603 method_name = "commit";
1604 } else {
1605 method_name = "rollback";
1606 }
1607
1608 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1609 if (!result) {
1610 return NULL;
1611 }
1612 Py_DECREF(result);
1613
1614 Py_RETURN_FALSE;
1615}
1616
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001617static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001619
1620static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001621 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1622 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001623 {NULL}
1624};
1625
1626static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001627 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001628 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001629 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001630 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001631 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001632 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001633 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001634 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001635 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001636 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001637 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001638 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001639 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001640 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001641 #ifdef HAVE_LOAD_EXTENSION
1642 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1643 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1644 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1645 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1646 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001647 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1648 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001649 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1650 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001651 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001652 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001653 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001654 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001655 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001656 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001657 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001658 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001659 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001660 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001661 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001662 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001663 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1664 PyDoc_STR("For context manager. Non-standard.")},
1665 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1666 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001667 {NULL, NULL}
1668};
1669
1670static struct PyMemberDef connection_members[] =
1671{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001672 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1673 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1674 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1675 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1676 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1677 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1678 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1679 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1680 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1681 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001682 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1683 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001684 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001685 {NULL}
1686};
1687
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001688PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001689 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001690 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001691 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001692 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001693 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001694 0, /* tp_print */
1695 0, /* tp_getattr */
1696 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001697 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001698 0, /* tp_repr */
1699 0, /* tp_as_number */
1700 0, /* tp_as_sequence */
1701 0, /* tp_as_mapping */
1702 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001703 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001704 0, /* tp_str */
1705 0, /* tp_getattro */
1706 0, /* tp_setattro */
1707 0, /* tp_as_buffer */
1708 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1709 connection_doc, /* tp_doc */
1710 0, /* tp_traverse */
1711 0, /* tp_clear */
1712 0, /* tp_richcompare */
1713 0, /* tp_weaklistoffset */
1714 0, /* tp_iter */
1715 0, /* tp_iternext */
1716 connection_methods, /* tp_methods */
1717 connection_members, /* tp_members */
1718 connection_getset, /* tp_getset */
1719 0, /* tp_base */
1720 0, /* tp_dict */
1721 0, /* tp_descr_get */
1722 0, /* tp_descr_set */
1723 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001724 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001725 0, /* tp_alloc */
1726 0, /* tp_new */
1727 0 /* tp_free */
1728};
1729
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001730extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001731{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001732 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1733 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001734}