blob: 409fa74f16647e1b71d09940487fbda05abea356 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* connection.c - the connection type
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
5 * This file is part of pysqlite.
Victor Stinner86999502010-05-19 01:27:23 +00006 *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "cache.h"
25#include "module.h"
R. David Murrayd35251d2010-06-01 01:32:12 +000026#include "structmember.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027#include "connection.h"
28#include "statement.h"
29#include "cursor.h"
30#include "prepare_protocol.h"
31#include "util.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032
33#include "pythread.h"
34
Gerhard Häringe7ea7452008-03-29 00:45:29 +000035#define ACTION_FINALIZE 1
36#define ACTION_RESET 2
37
Gerhard Häringf9cee222010-03-05 15:20:03 +000038#if SQLITE_VERSION_NUMBER >= 3003008
39#ifndef SQLITE_OMIT_LOAD_EXTENSION
40#define HAVE_LOAD_EXTENSION
41#endif
42#endif
43
Martin v. Löwise75fc142013-11-07 18:46:53 +010044_Py_IDENTIFIER(cursor);
45
Serhiy Storchaka28914922016-09-01 22:18:03 +030046static const char * const begin_statements[] = {
47 "BEGIN ",
48 "BEGIN DEFERRED",
49 "BEGIN IMMEDIATE",
50 "BEGIN EXCLUSIVE",
51 NULL
52};
53
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000054static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Gerhard Häringf9cee222010-03-05 15:20:03 +000055static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057
Benjamin Petersond7b03282008-09-13 15:58:53 +000058static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059{
60 /* in older SQLite versions, calling sqlite3_result_error in callbacks
61 * triggers a bug in SQLite that leads either to irritating results or
62 * segfaults, depending on the SQLite version */
63#if SQLITE_VERSION_NUMBER >= 3003003
64 sqlite3_result_error(ctx, errmsg, len);
65#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000066 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067#endif
68}
69
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000070int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010072 static char *kwlist[] = {
73 "database", "timeout", "detect_types", "isolation_level",
74 "check_same_thread", "factory", "cached_statements", "uri",
75 NULL
76 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077
78 char* database;
79 int detect_types = 0;
80 PyObject* isolation_level = NULL;
81 PyObject* factory = NULL;
82 int check_same_thread = 1;
83 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010084 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000085 double timeout = 5.0;
86 int rc;
87
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010088 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOip", kwlist,
89 &database, &timeout, &detect_types,
90 &isolation_level, &check_same_thread,
91 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000093 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094 }
95
Gerhard Häringf9cee222010-03-05 15:20:03 +000096 self->initialized = 1;
97
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098 self->begin_statement = NULL;
99
100 self->statement_cache = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000101 self->statements = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000102 self->cursors = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000103
104 Py_INCREF(Py_None);
105 self->row_factory = Py_None;
106
107 Py_INCREF(&PyUnicode_Type);
108 self->text_factory = (PyObject*)&PyUnicode_Type;
109
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100110#ifdef SQLITE_OPEN_URI
111 Py_BEGIN_ALLOW_THREADS
112 rc = sqlite3_open_v2(database, &self->db,
113 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
114 (uri ? SQLITE_OPEN_URI : 0), NULL);
115#else
116 if (uri) {
117 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
118 return -1;
119 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120 Py_BEGIN_ALLOW_THREADS
121 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100122#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 Py_END_ALLOW_THREADS
124
125 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000126 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127 return -1;
128 }
129
130 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000131 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000132 if (!isolation_level) {
133 return -1;
134 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135 } else {
136 Py_INCREF(isolation_level);
137 }
138 self->isolation_level = NULL;
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100139 if (pysqlite_connection_set_isolation_level(self, isolation_level) < 0) {
140 Py_DECREF(isolation_level);
141 return -1;
142 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143 Py_DECREF(isolation_level);
144
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000145 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 if (PyErr_Occurred()) {
147 return -1;
148 }
149
Gerhard Häringf9cee222010-03-05 15:20:03 +0000150 self->created_statements = 0;
151 self->created_cursors = 0;
152
153 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000154 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000155 self->cursors = PyList_New(0);
156 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157 return -1;
158 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000159
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160 /* By default, the Cache class INCREFs the factory in its initializer, and
161 * decrefs it in its deallocator method. Since this would create a circular
162 * reference here, we're breaking it by decrementing self, and telling the
163 * cache class to not decref the factory (self) in its deallocator.
164 */
165 self->statement_cache->decref_factory = 0;
166 Py_DECREF(self);
167
168 self->inTransaction = 0;
169 self->detect_types = detect_types;
170 self->timeout = timeout;
171 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000172#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000174#endif
Berker Peksag7bea2342016-06-12 14:09:51 +0300175 if (!check_same_thread && sqlite3_libversion_number() < 3003001) {
176 PyErr_SetString(pysqlite_NotSupportedError, "shared connections not available");
177 return -1;
178 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179 self->check_same_thread = check_same_thread;
180
181 self->function_pinboard = PyDict_New();
182 if (!self->function_pinboard) {
183 return -1;
184 }
185
186 self->collations = PyDict_New();
187 if (!self->collations) {
188 return -1;
189 }
190
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000191 self->Warning = pysqlite_Warning;
192 self->Error = pysqlite_Error;
193 self->InterfaceError = pysqlite_InterfaceError;
194 self->DatabaseError = pysqlite_DatabaseError;
195 self->DataError = pysqlite_DataError;
196 self->OperationalError = pysqlite_OperationalError;
197 self->IntegrityError = pysqlite_IntegrityError;
198 self->InternalError = pysqlite_InternalError;
199 self->ProgrammingError = pysqlite_ProgrammingError;
200 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201
202 return 0;
203}
204
Thomas Wouters477c8d52006-05-27 19:21:47 +0000205/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000206void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000208 pysqlite_Node* node;
209 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000210
211 node = self->statement_cache->first;
212
213 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000214 statement = (pysqlite_Statement*)(node->data);
215 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000216 node = node->next;
217 }
218
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300219 Py_SETREF(self->statement_cache,
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200220 (pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221 Py_DECREF(self);
222 self->statement_cache->decref_factory = 0;
223}
224
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000225/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000226void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000227{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000228 int i;
229 PyObject* weakref;
230 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000231 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232
Thomas Wouters477c8d52006-05-27 19:21:47 +0000233 for (i = 0; i < PyList_Size(self->statements); i++) {
234 weakref = PyList_GetItem(self->statements, i);
235 statement = PyWeakref_GetObject(weakref);
236 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500237 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000238 if (action == ACTION_RESET) {
239 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
240 } else {
241 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
242 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500243 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000244 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000246
247 if (reset_cursors) {
248 for (i = 0; i < PyList_Size(self->cursors); i++) {
249 weakref = PyList_GetItem(self->cursors, i);
250 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
251 if ((PyObject*)cursor != Py_None) {
252 cursor->reset = 1;
253 }
254 }
255 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256}
257
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000258void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259{
260 Py_XDECREF(self->statement_cache);
261
262 /* Clean up if user has not called .close() explicitly. */
263 if (self->db) {
264 Py_BEGIN_ALLOW_THREADS
265 sqlite3_close(self->db);
266 Py_END_ALLOW_THREADS
267 }
268
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269 Py_XDECREF(self->isolation_level);
270 Py_XDECREF(self->function_pinboard);
271 Py_XDECREF(self->row_factory);
272 Py_XDECREF(self->text_factory);
273 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000274 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000275 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000276
Christian Heimes90aa7642007-12-19 02:45:37 +0000277 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000278}
279
Gerhard Häringf9cee222010-03-05 15:20:03 +0000280/*
281 * Registers a cursor with the connection.
282 *
283 * 0 => error; 1 => ok
284 */
285int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
286{
287 PyObject* weakref;
288
289 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
290 if (!weakref) {
291 goto error;
292 }
293
294 if (PyList_Append(connection->cursors, weakref) != 0) {
295 Py_CLEAR(weakref);
296 goto error;
297 }
298
299 Py_DECREF(weakref);
300
301 return 1;
302error:
303 return 0;
304}
305
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000306PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300308 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000309 PyObject* factory = NULL;
310 PyObject* cursor;
311
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
313 &factory)) {
314 return NULL;
315 }
316
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000317 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 return NULL;
319 }
320
321 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000322 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 }
324
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300325 cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL);
326 if (cursor == NULL)
327 return NULL;
328 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
329 PyErr_Format(PyExc_TypeError,
330 "factory must return a cursor, not %.100s",
331 Py_TYPE(cursor)->tp_name);
332 Py_DECREF(cursor);
333 return NULL;
334 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335
Gerhard Häringf9cee222010-03-05 15:20:03 +0000336 _pysqlite_drop_unused_cursor_references(self);
337
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300340 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341 }
342
343 return cursor;
344}
345
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000346PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347{
348 int rc;
349
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000350 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351 return NULL;
352 }
353
Gerhard Häringf9cee222010-03-05 15:20:03 +0000354 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355
356 if (self->db) {
357 Py_BEGIN_ALLOW_THREADS
358 rc = sqlite3_close(self->db);
359 Py_END_ALLOW_THREADS
360
361 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000362 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 return NULL;
364 } else {
365 self->db = NULL;
366 }
367 }
368
Berker Peksagfe21de92016-04-09 07:34:39 +0300369 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370}
371
372/*
373 * Checks if a connection object is usable (i. e. not closed).
374 *
375 * 0 => error; 1 => ok
376 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000377int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000378{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000379 if (!con->initialized) {
380 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
381 return 0;
382 }
383
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000385 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 return 0;
387 } else {
388 return 1;
389 }
390}
391
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000392PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000393{
394 int rc;
395 const char* tail;
396 sqlite3_stmt* statement;
397
398 Py_BEGIN_ALLOW_THREADS
399 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
400 Py_END_ALLOW_THREADS
401
402 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000403 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404 goto error;
405 }
406
Benjamin Petersond7b03282008-09-13 15:58:53 +0000407 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000408 if (rc == SQLITE_DONE) {
409 self->inTransaction = 1;
410 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000411 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412 }
413
414 Py_BEGIN_ALLOW_THREADS
415 rc = sqlite3_finalize(statement);
416 Py_END_ALLOW_THREADS
417
418 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000419 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420 }
421
422error:
423 if (PyErr_Occurred()) {
424 return NULL;
425 } else {
426 Py_INCREF(Py_None);
427 return Py_None;
428 }
429}
430
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000431PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432{
433 int rc;
434 const char* tail;
435 sqlite3_stmt* statement;
436
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000437 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000438 return NULL;
439 }
440
441 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000442
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443 Py_BEGIN_ALLOW_THREADS
444 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
445 Py_END_ALLOW_THREADS
446 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000447 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 goto error;
449 }
450
Benjamin Petersond7b03282008-09-13 15:58:53 +0000451 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000452 if (rc == SQLITE_DONE) {
453 self->inTransaction = 0;
454 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000455 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000456 }
457
458 Py_BEGIN_ALLOW_THREADS
459 rc = sqlite3_finalize(statement);
460 Py_END_ALLOW_THREADS
461 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000462 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463 }
464
465 }
466
467error:
468 if (PyErr_Occurred()) {
469 return NULL;
470 } else {
471 Py_INCREF(Py_None);
472 return Py_None;
473 }
474}
475
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000476PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000477{
478 int rc;
479 const char* tail;
480 sqlite3_stmt* statement;
481
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000482 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483 return NULL;
484 }
485
486 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000487 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000488
489 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000490 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000491 Py_END_ALLOW_THREADS
492 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000493 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000494 goto error;
495 }
496
Benjamin Petersond7b03282008-09-13 15:58:53 +0000497 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000498 if (rc == SQLITE_DONE) {
499 self->inTransaction = 0;
500 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000501 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502 }
503
504 Py_BEGIN_ALLOW_THREADS
505 rc = sqlite3_finalize(statement);
506 Py_END_ALLOW_THREADS
507 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000508 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509 }
510
511 }
512
513error:
514 if (PyErr_Occurred()) {
515 return NULL;
516 } else {
517 Py_INCREF(Py_None);
518 return Py_None;
519 }
520}
521
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200522static int
523_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000524{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200525 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000527 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200528 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
529 if (value == -1 && PyErr_Occurred())
530 return -1;
531 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000532 } else if (PyFloat_Check(py_val)) {
533 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000534 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200535 const char *str = _PyUnicode_AsString(py_val);
536 if (str == NULL)
537 return -1;
538 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000539 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200540 Py_buffer view;
541 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100542 PyErr_SetString(PyExc_ValueError,
543 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200544 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200546 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100547 PyErr_SetString(PyExc_OverflowError,
548 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200549 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100550 return -1;
551 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200552 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
553 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200555 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200557 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000558}
559
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000560PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000561{
562 PyObject* args;
563 int i;
564 sqlite3_value* cur_value;
565 PyObject* cur_py_value;
566 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000568
569 args = PyTuple_New(argc);
570 if (!args) {
571 return NULL;
572 }
573
574 for (i = 0; i < argc; i++) {
575 cur_value = argv[i];
576 switch (sqlite3_value_type(argv[i])) {
577 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200578 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 break;
580 case SQLITE_FLOAT:
581 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
582 break;
583 case SQLITE_TEXT:
584 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000585 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000586 /* TODO: have a way to show errors here */
587 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000588 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589 Py_INCREF(Py_None);
590 cur_py_value = Py_None;
591 }
592 break;
593 case SQLITE_BLOB:
594 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000595 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000596 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597 break;
598 case SQLITE_NULL:
599 default:
600 Py_INCREF(Py_None);
601 cur_py_value = Py_None;
602 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000603
604 if (!cur_py_value) {
605 Py_DECREF(args);
606 return NULL;
607 }
608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000609 PyTuple_SetItem(args, i, cur_py_value);
610
611 }
612
613 return args;
614}
615
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000616void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000617{
618 PyObject* args;
619 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000620 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200621 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622
Georg Brandldfd73442009-04-05 11:47:34 +0000623#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624 PyGILState_STATE threadstate;
625
626 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000627#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628
629 py_func = (PyObject*)sqlite3_user_data(context);
630
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000631 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000632 if (args) {
633 py_retval = PyObject_CallObject(py_func, args);
634 Py_DECREF(args);
635 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000636
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200637 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000638 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200639 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200641 }
642 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000643 if (_enable_callback_tracebacks) {
644 PyErr_Print();
645 } else {
646 PyErr_Clear();
647 }
648 _sqlite3_result_error(context, "user-defined function raised exception", -1);
649 }
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_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000653#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654}
655
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000656static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000657{
658 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660 PyObject* aggregate_class;
661 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000663
Georg Brandldfd73442009-04-05 11:47:34 +0000664#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000665 PyGILState_STATE threadstate;
666
667 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000668#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000669
670 aggregate_class = (PyObject*)sqlite3_user_data(context);
671
672 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
673
674 if (*aggregate_instance == 0) {
675 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
676
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000679 if (_enable_callback_tracebacks) {
680 PyErr_Print();
681 } else {
682 PyErr_Clear();
683 }
684 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000685 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000686 }
687 }
688
689 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000690 if (!stepmethod) {
691 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692 }
693
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000694 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 if (!args) {
696 goto error;
697 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000698
699 function_result = PyObject_CallObject(stepmethod, args);
700 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000701
Thomas Wouters477c8d52006-05-27 19:21:47 +0000702 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000703 if (_enable_callback_tracebacks) {
704 PyErr_Print();
705 } else {
706 PyErr_Clear();
707 }
708 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 }
710
Thomas Wouters477c8d52006-05-27 19:21:47 +0000711error:
712 Py_XDECREF(stepmethod);
713 Py_XDECREF(function_result);
714
Georg Brandldfd73442009-04-05 11:47:34 +0000715#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000716 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000717#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000718}
719
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000720void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000721{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200722 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000723 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200724 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200725 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200726 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200727 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000728
Georg Brandldfd73442009-04-05 11:47:34 +0000729#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000730 PyGILState_STATE threadstate;
731
732 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000733#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
736 if (!*aggregate_instance) {
737 /* this branch is executed if there was an exception in the aggregate's
738 * __init__ */
739
Thomas Wouters477c8d52006-05-27 19:21:47 +0000740 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 }
742
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200743 /* Keep the exception (if any) of the last call to step() */
744 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200745 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200746
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200747 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200748
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200749 Py_DECREF(*aggregate_instance);
750
751 ok = 0;
752 if (function_result) {
753 ok = _pysqlite_set_result(context, function_result) == 0;
754 Py_DECREF(function_result);
755 }
756 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000757 if (_enable_callback_tracebacks) {
758 PyErr_Print();
759 } else {
760 PyErr_Clear();
761 }
762 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200763#if SQLITE_VERSION_NUMBER < 3003003
764 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
765 exception, so don't restore the previous exception */
766 restore = 0;
767#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768 }
769
Victor Stinnerffff7632013-08-02 01:48:10 +0200770 if (restore) {
771 /* Restore the exception (if any) of the last call to step(),
772 but clear also the current exception if finalize() failed */
773 PyErr_Restore(exception, value, tb);
774 }
Victor Stinner3a857322013-07-22 08:34:32 +0200775
Thomas Wouters477c8d52006-05-27 19:21:47 +0000776error:
Georg Brandldfd73442009-04-05 11:47:34 +0000777#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000778 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000779#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200780 /* explicit return to avoid a compilation error if WITH_THREAD
781 is not defined */
782 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000783}
784
Gerhard Häringf9cee222010-03-05 15:20:03 +0000785static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786{
787 PyObject* new_list;
788 PyObject* weakref;
789 int i;
790
791 /* we only need to do this once in a while */
792 if (self->created_statements++ < 200) {
793 return;
794 }
795
796 self->created_statements = 0;
797
798 new_list = PyList_New(0);
799 if (!new_list) {
800 return;
801 }
802
803 for (i = 0; i < PyList_Size(self->statements); i++) {
804 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000805 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000806 if (PyList_Append(new_list, weakref) != 0) {
807 Py_DECREF(new_list);
808 return;
809 }
810 }
811 }
812
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300813 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815
Gerhard Häringf9cee222010-03-05 15:20:03 +0000816static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
817{
818 PyObject* new_list;
819 PyObject* weakref;
820 int i;
821
822 /* we only need to do this once in a while */
823 if (self->created_cursors++ < 200) {
824 return;
825 }
826
827 self->created_cursors = 0;
828
829 new_list = PyList_New(0);
830 if (!new_list) {
831 return;
832 }
833
834 for (i = 0; i < PyList_Size(self->cursors); i++) {
835 weakref = PyList_GetItem(self->cursors, i);
836 if (PyWeakref_GetObject(weakref) != Py_None) {
837 if (PyList_Append(new_list, weakref) != 0) {
838 Py_DECREF(new_list);
839 return;
840 }
841 }
842 }
843
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300844 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000845}
846
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000847PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848{
849 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
850
851 PyObject* func;
852 char* name;
853 int narg;
854 int rc;
855
Gerhard Häringf9cee222010-03-05 15:20:03 +0000856 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
857 return NULL;
858 }
859
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000860 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
861 &name, &narg, &func))
862 {
863 return NULL;
864 }
865
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000866 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867
Thomas Wouters477c8d52006-05-27 19:21:47 +0000868 if (rc != SQLITE_OK) {
869 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000870 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000871 return NULL;
872 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000873 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
874 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000875
Berker Peksagfe21de92016-04-09 07:34:39 +0300876 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000877 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000878}
879
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000880PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000881{
882 PyObject* aggregate_class;
883
884 int n_arg;
885 char* name;
886 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
887 int rc;
888
Gerhard Häringf9cee222010-03-05 15:20:03 +0000889 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
890 return NULL;
891 }
892
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000893 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
894 kwlist, &name, &n_arg, &aggregate_class)) {
895 return NULL;
896 }
897
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000898 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 +0000899 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000901 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000902 return NULL;
903 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000904 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
905 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000906
Berker Peksagfe21de92016-04-09 07:34:39 +0300907 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000908 }
909}
910
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000911static 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 +0000912{
913 PyObject *ret;
914 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000915#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000916 PyGILState_STATE gilstate;
917
918 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000919#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000920
Victor Stinnerd4095d92013-07-26 22:23:33 +0200921 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000922
Victor Stinnerd4095d92013-07-26 22:23:33 +0200923 if (ret == NULL) {
924 if (_enable_callback_tracebacks)
925 PyErr_Print();
926 else
927 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200928
Victor Stinnerd4095d92013-07-26 22:23:33 +0200929 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200930 }
931 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200932 if (PyLong_Check(ret)) {
933 rc = _PyLong_AsInt(ret);
934 if (rc == -1 && PyErr_Occurred()) {
935 if (_enable_callback_tracebacks)
936 PyErr_Print();
937 else
938 PyErr_Clear();
939 rc = SQLITE_DENY;
940 }
941 }
942 else {
943 rc = SQLITE_DENY;
944 }
945 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000946 }
947
Georg Brandldfd73442009-04-05 11:47:34 +0000948#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000949 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000950#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000951 return rc;
952}
953
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000954static int _progress_handler(void* user_arg)
955{
956 int rc;
957 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000958#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000959 PyGILState_STATE gilstate;
960
961 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000962#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000963 ret = PyObject_CallFunction((PyObject*)user_arg, "");
964
965 if (!ret) {
966 if (_enable_callback_tracebacks) {
967 PyErr_Print();
968 } else {
969 PyErr_Clear();
970 }
971
Mark Dickinson934896d2009-02-21 20:59:32 +0000972 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000973 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000974 } else {
975 rc = (int)PyObject_IsTrue(ret);
976 Py_DECREF(ret);
977 }
978
Georg Brandldfd73442009-04-05 11:47:34 +0000979#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000980 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000981#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000982 return rc;
983}
984
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200985static void _trace_callback(void* user_arg, const char* statement_string)
986{
987 PyObject *py_statement = NULL;
988 PyObject *ret = NULL;
989
990#ifdef WITH_THREAD
991 PyGILState_STATE gilstate;
992
993 gilstate = PyGILState_Ensure();
994#endif
995 py_statement = PyUnicode_DecodeUTF8(statement_string,
996 strlen(statement_string), "replace");
997 if (py_statement) {
998 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
999 Py_DECREF(py_statement);
1000 }
1001
1002 if (ret) {
1003 Py_DECREF(ret);
1004 } else {
1005 if (_enable_callback_tracebacks) {
1006 PyErr_Print();
1007 } else {
1008 PyErr_Clear();
1009 }
1010 }
1011
1012#ifdef WITH_THREAD
1013 PyGILState_Release(gilstate);
1014#endif
1015}
1016
Gerhard Häringf9cee222010-03-05 15:20:03 +00001017static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001018{
1019 PyObject* authorizer_cb;
1020
1021 static char *kwlist[] = { "authorizer_callback", NULL };
1022 int rc;
1023
Gerhard Häringf9cee222010-03-05 15:20:03 +00001024 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1025 return NULL;
1026 }
1027
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001028 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1029 kwlist, &authorizer_cb)) {
1030 return NULL;
1031 }
1032
1033 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1034
1035 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001036 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001037 return NULL;
1038 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001039 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1040 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001041
Berker Peksagfe21de92016-04-09 07:34:39 +03001042 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001043 }
1044}
1045
Gerhard Häringf9cee222010-03-05 15:20:03 +00001046static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001047{
1048 PyObject* progress_handler;
1049 int n;
1050
1051 static char *kwlist[] = { "progress_handler", "n", NULL };
1052
Gerhard Häringf9cee222010-03-05 15:20:03 +00001053 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1054 return NULL;
1055 }
1056
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001057 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1058 kwlist, &progress_handler, &n)) {
1059 return NULL;
1060 }
1061
1062 if (progress_handler == Py_None) {
1063 /* None clears the progress handler previously set */
1064 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1065 } else {
1066 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001067 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1068 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001069 }
1070
Berker Peksagfe21de92016-04-09 07:34:39 +03001071 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001072}
1073
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001074static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1075{
1076 PyObject* trace_callback;
1077
1078 static char *kwlist[] = { "trace_callback", NULL };
1079
1080 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1081 return NULL;
1082 }
1083
1084 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1085 kwlist, &trace_callback)) {
1086 return NULL;
1087 }
1088
1089 if (trace_callback == Py_None) {
1090 /* None clears the trace callback previously set */
1091 sqlite3_trace(self->db, 0, (void*)0);
1092 } else {
1093 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1094 return NULL;
1095 sqlite3_trace(self->db, _trace_callback, trace_callback);
1096 }
1097
Berker Peksagfe21de92016-04-09 07:34:39 +03001098 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001099}
1100
Gerhard Häringf9cee222010-03-05 15:20:03 +00001101#ifdef HAVE_LOAD_EXTENSION
1102static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1103{
1104 int rc;
1105 int onoff;
1106
1107 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1108 return NULL;
1109 }
1110
1111 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1112 return NULL;
1113 }
1114
1115 rc = sqlite3_enable_load_extension(self->db, onoff);
1116
1117 if (rc != SQLITE_OK) {
1118 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1119 return NULL;
1120 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001121 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001122 }
1123}
1124
1125static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1126{
1127 int rc;
1128 char* extension_name;
1129 char* errmsg;
1130
1131 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1132 return NULL;
1133 }
1134
1135 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1136 return NULL;
1137 }
1138
1139 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1140 if (rc != 0) {
1141 PyErr_SetString(pysqlite_OperationalError, errmsg);
1142 return NULL;
1143 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001144 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001145 }
1146}
1147#endif
1148
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001149int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001150{
Georg Brandldfd73442009-04-05 11:47:34 +00001151#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152 if (self->check_same_thread) {
1153 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001154 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155 "SQLite objects created in a thread can only be used in that same thread."
1156 "The object was created in thread id %ld and this is thread id %ld",
1157 self->thread_ident, PyThread_get_thread_ident());
1158 return 0;
1159 }
1160
1161 }
Georg Brandldfd73442009-04-05 11:47:34 +00001162#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163 return 1;
1164}
1165
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001166static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001167{
1168 Py_INCREF(self->isolation_level);
1169 return self->isolation_level;
1170}
1171
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001172static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001174 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001175 return NULL;
1176 } else {
1177 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1178 }
1179}
1180
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001181static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001182{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001183 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001184 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185 if (!res) {
1186 return -1;
1187 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001188 Py_DECREF(res);
1189
Serhiy Storchaka28914922016-09-01 22:18:03 +03001190 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001191 self->inTransaction = 0;
1192 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001193 const char * const *candidate;
1194 PyObject *uppercase_level;
1195 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001196
Serhiy Storchaka28914922016-09-01 22:18:03 +03001197 if (!PyUnicode_Check(isolation_level)) {
1198 PyErr_Format(PyExc_TypeError,
1199 "isolation_level must be a string or None, not %.100s",
1200 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001201 return -1;
1202 }
1203
Serhiy Storchaka28914922016-09-01 22:18:03 +03001204 uppercase_level = _PyObject_CallMethodIdObjArgs(
1205 (PyObject *)&PyUnicode_Type, &PyId_upper,
1206 isolation_level, NULL);
1207 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001208 return -1;
1209 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001210 for (candidate = begin_statements; *candidate; candidate++) {
1211 if (!PyUnicode_CompareWithASCIIString(uppercase_level, *candidate + 6))
1212 break;
1213 }
1214 Py_DECREF(uppercase_level);
1215 if (!*candidate) {
1216 PyErr_SetString(PyExc_ValueError,
1217 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001218 return -1;
1219 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001220 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001221 }
1222
Serhiy Storchaka28914922016-09-01 22:18:03 +03001223 Py_INCREF(isolation_level);
1224 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001225 return 0;
1226}
1227
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001228PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001229{
1230 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001231 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001232 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001233 int rc;
1234
Gerhard Häringf9cee222010-03-05 15:20:03 +00001235 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1236 return NULL;
1237 }
1238
Larry Hastings3b12e952015-05-08 07:45:10 -07001239 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection()", kwargs))
1240 return NULL;
1241
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001242 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001243 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001244
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001245 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001246
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001247 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001248 if (!statement) {
1249 return NULL;
1250 }
1251
Victor Stinner0201f442010-03-13 03:28:34 +00001252 statement->db = NULL;
1253 statement->st = NULL;
1254 statement->sql = NULL;
1255 statement->in_use = 0;
1256 statement->in_weakreflist = NULL;
1257
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001258 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259 if (rc != SQLITE_OK) {
1260 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001261 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001262 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001263 if (PyErr_ExceptionMatches(PyExc_TypeError))
1264 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001265 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001266 (void)pysqlite_statement_reset(statement);
1267 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001269 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270 }
1271
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001272 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1273 if (weakref == NULL)
1274 goto error;
1275 if (PyList_Append(self->statements, weakref) != 0) {
1276 Py_DECREF(weakref);
1277 goto error;
1278 }
1279 Py_DECREF(weakref);
1280
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001281 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001282
1283error:
1284 Py_DECREF(statement);
1285 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001286}
1287
Larry Hastings01b08832015-05-08 07:37:49 -07001288PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001289{
1290 PyObject* cursor = 0;
1291 PyObject* result = 0;
1292 PyObject* method = 0;
1293
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001294 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001295 if (!cursor) {
1296 goto error;
1297 }
1298
1299 method = PyObject_GetAttrString(cursor, "execute");
1300 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001301 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001302 goto error;
1303 }
1304
1305 result = PyObject_CallObject(method, args);
1306 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001307 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001308 }
1309
1310error:
1311 Py_XDECREF(result);
1312 Py_XDECREF(method);
1313
1314 return cursor;
1315}
1316
Larry Hastings01b08832015-05-08 07:37:49 -07001317PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001318{
1319 PyObject* cursor = 0;
1320 PyObject* result = 0;
1321 PyObject* method = 0;
1322
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001323 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001324 if (!cursor) {
1325 goto error;
1326 }
1327
1328 method = PyObject_GetAttrString(cursor, "executemany");
1329 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001330 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001331 goto error;
1332 }
1333
1334 result = PyObject_CallObject(method, args);
1335 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001336 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001337 }
1338
1339error:
1340 Py_XDECREF(result);
1341 Py_XDECREF(method);
1342
1343 return cursor;
1344}
1345
Larry Hastings01b08832015-05-08 07:37:49 -07001346PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001347{
1348 PyObject* cursor = 0;
1349 PyObject* result = 0;
1350 PyObject* method = 0;
1351
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001352 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001353 if (!cursor) {
1354 goto error;
1355 }
1356
1357 method = PyObject_GetAttrString(cursor, "executescript");
1358 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001359 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001360 goto error;
1361 }
1362
1363 result = PyObject_CallObject(method, args);
1364 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001365 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001366 }
1367
1368error:
1369 Py_XDECREF(result);
1370 Py_XDECREF(method);
1371
1372 return cursor;
1373}
1374
1375/* ------------------------- COLLATION CODE ------------------------ */
1376
1377static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001378pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001379 void* context,
1380 int text1_length, const void* text1_data,
1381 int text2_length, const void* text2_data)
1382{
1383 PyObject* callback = (PyObject*)context;
1384 PyObject* string1 = 0;
1385 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001386#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001387 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001388#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001389 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001390 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001391 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001392#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001393 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001394#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001395
1396 if (PyErr_Occurred()) {
1397 goto finally;
1398 }
1399
Guido van Rossum98297ee2007-11-06 21:34:58 +00001400 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1401 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001402
1403 if (!string1 || !string2) {
1404 goto finally; /* failed to allocate strings */
1405 }
1406
1407 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1408
1409 if (!retval) {
1410 /* execution failed */
1411 goto finally;
1412 }
1413
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001414 longval = PyLong_AsLongAndOverflow(retval, &result);
1415 if (longval == -1 && PyErr_Occurred()) {
1416 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001417 result = 0;
1418 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001419 else if (!result) {
1420 if (longval > 0)
1421 result = 1;
1422 else if (longval < 0)
1423 result = -1;
1424 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001425
1426finally:
1427 Py_XDECREF(string1);
1428 Py_XDECREF(string2);
1429 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001430#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001432#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001433 return result;
1434}
1435
1436static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001437pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001438{
1439 PyObject* retval = NULL;
1440
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001441 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001442 goto finally;
1443 }
1444
1445 sqlite3_interrupt(self->db);
1446
1447 Py_INCREF(Py_None);
1448 retval = Py_None;
1449
1450finally:
1451 return retval;
1452}
1453
Christian Heimesbbe741d2008-03-28 10:53:29 +00001454/* Function author: Paul Kippes <kippesp@gmail.com>
1455 * Class method of Connection to call the Python function _iterdump
1456 * of the sqlite3 module.
1457 */
1458static PyObject *
1459pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1460{
1461 PyObject* retval = NULL;
1462 PyObject* module = NULL;
1463 PyObject* module_dict;
1464 PyObject* pyfn_iterdump;
1465
1466 if (!pysqlite_check_connection(self)) {
1467 goto finally;
1468 }
1469
1470 module = PyImport_ImportModule(MODULE_NAME ".dump");
1471 if (!module) {
1472 goto finally;
1473 }
1474
1475 module_dict = PyModule_GetDict(module);
1476 if (!module_dict) {
1477 goto finally;
1478 }
1479
1480 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1481 if (!pyfn_iterdump) {
1482 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1483 goto finally;
1484 }
1485
1486 args = PyTuple_New(1);
1487 if (!args) {
1488 goto finally;
1489 }
1490 Py_INCREF(self);
1491 PyTuple_SetItem(args, 0, (PyObject*)self);
1492 retval = PyObject_CallObject(pyfn_iterdump, args);
1493
1494finally:
1495 Py_XDECREF(args);
1496 Py_XDECREF(module);
1497 return retval;
1498}
1499
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001500static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001501pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001502{
1503 PyObject* callable;
1504 PyObject* uppercase_name = 0;
1505 PyObject* name;
1506 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001507 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001508 _Py_IDENTIFIER(upper);
Victor Stinner35466c52010-04-22 11:23:23 +00001509 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001510 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001511 unsigned int kind;
1512 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001513
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001514 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001515 goto finally;
1516 }
1517
Gerhard Häring6d214562007-08-10 18:15:11 +00001518 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001519 goto finally;
1520 }
1521
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001522 uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523 if (!uppercase_name) {
1524 goto finally;
1525 }
1526
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001527 if (PyUnicode_READY(uppercase_name))
1528 goto finally;
1529 len = PyUnicode_GET_LENGTH(uppercase_name);
1530 kind = PyUnicode_KIND(uppercase_name);
1531 data = PyUnicode_DATA(uppercase_name);
1532 for (i=0; i<len; i++) {
1533 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1534 if ((ch >= '0' && ch <= '9')
1535 || (ch >= 'A' && ch <= 'Z')
1536 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537 {
Victor Stinner35466c52010-04-22 11:23:23 +00001538 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001539 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001540 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001541 goto finally;
1542 }
1543 }
1544
Victor Stinner35466c52010-04-22 11:23:23 +00001545 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1546 if (!uppercase_name_str)
1547 goto finally;
1548
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001549 if (callable != Py_None && !PyCallable_Check(callable)) {
1550 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1551 goto finally;
1552 }
1553
1554 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001555 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1556 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001557 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001558 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1559 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001560 }
1561
1562 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001563 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001564 SQLITE_UTF8,
1565 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001566 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001567 if (rc != SQLITE_OK) {
1568 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001569 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001570 goto finally;
1571 }
1572
1573finally:
1574 Py_XDECREF(uppercase_name);
1575
1576 if (PyErr_Occurred()) {
1577 retval = NULL;
1578 } else {
1579 Py_INCREF(Py_None);
1580 retval = Py_None;
1581 }
1582
1583 return retval;
1584}
1585
Christian Heimesbbe741d2008-03-28 10:53:29 +00001586/* Called when the connection is used as a context manager. Returns itself as a
1587 * convenience to the caller. */
1588static PyObject *
1589pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1590{
1591 Py_INCREF(self);
1592 return (PyObject*)self;
1593}
1594
1595/** Called when the connection is used as a context manager. If there was any
1596 * exception, a rollback takes place; otherwise we commit. */
1597static PyObject *
1598pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1599{
1600 PyObject* exc_type, *exc_value, *exc_tb;
1601 char* method_name;
1602 PyObject* result;
1603
1604 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1605 return NULL;
1606 }
1607
1608 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1609 method_name = "commit";
1610 } else {
1611 method_name = "rollback";
1612 }
1613
1614 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1615 if (!result) {
1616 return NULL;
1617 }
1618 Py_DECREF(result);
1619
1620 Py_RETURN_FALSE;
1621}
1622
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001623static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001625
1626static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001627 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1628 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001629 {NULL}
1630};
1631
1632static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001633 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001634 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001635 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001636 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001637 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001638 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001639 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001640 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001641 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001642 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001643 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001644 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001645 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001646 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001647 #ifdef HAVE_LOAD_EXTENSION
1648 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1649 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1650 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1651 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1652 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001653 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1654 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001655 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1656 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001657 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001658 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001659 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001660 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001661 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001662 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001663 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001664 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001665 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001666 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001667 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001668 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001669 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1670 PyDoc_STR("For context manager. Non-standard.")},
1671 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1672 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001673 {NULL, NULL}
1674};
1675
1676static struct PyMemberDef connection_members[] =
1677{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001678 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1679 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1680 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1681 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1682 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1683 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1684 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1685 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1686 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1687 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001688 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1689 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001690 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001691 {NULL}
1692};
1693
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001694PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001695 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001696 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001697 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001698 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001699 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001700 0, /* tp_print */
1701 0, /* tp_getattr */
1702 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001703 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001704 0, /* tp_repr */
1705 0, /* tp_as_number */
1706 0, /* tp_as_sequence */
1707 0, /* tp_as_mapping */
1708 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001709 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001710 0, /* tp_str */
1711 0, /* tp_getattro */
1712 0, /* tp_setattro */
1713 0, /* tp_as_buffer */
1714 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1715 connection_doc, /* tp_doc */
1716 0, /* tp_traverse */
1717 0, /* tp_clear */
1718 0, /* tp_richcompare */
1719 0, /* tp_weaklistoffset */
1720 0, /* tp_iter */
1721 0, /* tp_iternext */
1722 connection_methods, /* tp_methods */
1723 connection_members, /* tp_members */
1724 connection_getset, /* tp_getset */
1725 0, /* tp_base */
1726 0, /* tp_dict */
1727 0, /* tp_descr_get */
1728 0, /* tp_descr_set */
1729 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001730 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001731 0, /* tp_alloc */
1732 0, /* tp_new */
1733 0 /* tp_free */
1734};
1735
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001736extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001737{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001738 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1739 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001740}