blob: 37b45f330b349381f4d29e086fbaa34563221ba5 [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000168 self->detect_types = detect_types;
169 self->timeout = timeout;
170 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000171#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000172 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000173#endif
Berker Peksag7bea2342016-06-12 14:09:51 +0300174 if (!check_same_thread && sqlite3_libversion_number() < 3003001) {
175 PyErr_SetString(pysqlite_NotSupportedError, "shared connections not available");
176 return -1;
177 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000178 self->check_same_thread = check_same_thread;
179
180 self->function_pinboard = PyDict_New();
181 if (!self->function_pinboard) {
182 return -1;
183 }
184
185 self->collations = PyDict_New();
186 if (!self->collations) {
187 return -1;
188 }
189
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000190 self->Warning = pysqlite_Warning;
191 self->Error = pysqlite_Error;
192 self->InterfaceError = pysqlite_InterfaceError;
193 self->DatabaseError = pysqlite_DatabaseError;
194 self->DataError = pysqlite_DataError;
195 self->OperationalError = pysqlite_OperationalError;
196 self->IntegrityError = pysqlite_IntegrityError;
197 self->InternalError = pysqlite_InternalError;
198 self->ProgrammingError = pysqlite_ProgrammingError;
199 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200
201 return 0;
202}
203
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000204/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000205void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000207 int i;
208 PyObject* weakref;
209 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000210 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211
Thomas Wouters477c8d52006-05-27 19:21:47 +0000212 for (i = 0; i < PyList_Size(self->statements); i++) {
213 weakref = PyList_GetItem(self->statements, i);
214 statement = PyWeakref_GetObject(weakref);
215 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500216 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000217 if (action == ACTION_RESET) {
218 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
219 } else {
220 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
221 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500222 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000223 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000224 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000225
226 if (reset_cursors) {
227 for (i = 0; i < PyList_Size(self->cursors); i++) {
228 weakref = PyList_GetItem(self->cursors, i);
229 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
230 if ((PyObject*)cursor != Py_None) {
231 cursor->reset = 1;
232 }
233 }
234 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000235}
236
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000237void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000238{
239 Py_XDECREF(self->statement_cache);
240
241 /* Clean up if user has not called .close() explicitly. */
242 if (self->db) {
243 Py_BEGIN_ALLOW_THREADS
244 sqlite3_close(self->db);
245 Py_END_ALLOW_THREADS
246 }
247
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248 Py_XDECREF(self->isolation_level);
249 Py_XDECREF(self->function_pinboard);
250 Py_XDECREF(self->row_factory);
251 Py_XDECREF(self->text_factory);
252 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000253 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000254 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255
Christian Heimes90aa7642007-12-19 02:45:37 +0000256 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257}
258
Gerhard Häringf9cee222010-03-05 15:20:03 +0000259/*
260 * Registers a cursor with the connection.
261 *
262 * 0 => error; 1 => ok
263 */
264int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
265{
266 PyObject* weakref;
267
268 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
269 if (!weakref) {
270 goto error;
271 }
272
273 if (PyList_Append(connection->cursors, weakref) != 0) {
274 Py_CLEAR(weakref);
275 goto error;
276 }
277
278 Py_DECREF(weakref);
279
280 return 1;
281error:
282 return 0;
283}
284
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000285PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000286{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300287 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000288 PyObject* factory = NULL;
289 PyObject* cursor;
290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
292 &factory)) {
293 return NULL;
294 }
295
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000296 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000297 return NULL;
298 }
299
300 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000301 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302 }
303
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300304 cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL);
305 if (cursor == NULL)
306 return NULL;
307 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
308 PyErr_Format(PyExc_TypeError,
309 "factory must return a cursor, not %.100s",
310 Py_TYPE(cursor)->tp_name);
311 Py_DECREF(cursor);
312 return NULL;
313 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000314
Gerhard Häringf9cee222010-03-05 15:20:03 +0000315 _pysqlite_drop_unused_cursor_references(self);
316
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300319 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320 }
321
322 return cursor;
323}
324
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000325PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326{
327 int rc;
328
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000329 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 return NULL;
331 }
332
Gerhard Häringf9cee222010-03-05 15:20:03 +0000333 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000334
335 if (self->db) {
336 Py_BEGIN_ALLOW_THREADS
337 rc = sqlite3_close(self->db);
338 Py_END_ALLOW_THREADS
339
340 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000341 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000342 return NULL;
343 } else {
344 self->db = NULL;
345 }
346 }
347
Berker Peksagfe21de92016-04-09 07:34:39 +0300348 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000349}
350
351/*
352 * Checks if a connection object is usable (i. e. not closed).
353 *
354 * 0 => error; 1 => ok
355 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000356int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000357{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000358 if (!con->initialized) {
359 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
360 return 0;
361 }
362
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000364 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000365 return 0;
366 } else {
367 return 1;
368 }
369}
370
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000371PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372{
373 int rc;
374 const char* tail;
375 sqlite3_stmt* statement;
376
377 Py_BEGIN_ALLOW_THREADS
378 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
379 Py_END_ALLOW_THREADS
380
381 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000382 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000383 goto error;
384 }
385
Benjamin Petersond7b03282008-09-13 15:58:53 +0000386 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300387 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000388 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000389 }
390
391 Py_BEGIN_ALLOW_THREADS
392 rc = sqlite3_finalize(statement);
393 Py_END_ALLOW_THREADS
394
395 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000396 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000397 }
398
399error:
400 if (PyErr_Occurred()) {
401 return NULL;
402 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200403 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404 }
405}
406
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000407PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000408{
409 int rc;
410 const char* tail;
411 sqlite3_stmt* statement;
412
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000413 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000414 return NULL;
415 }
416
Berker Peksag59da4b32016-09-12 07:16:43 +0300417 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000418
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419 Py_BEGIN_ALLOW_THREADS
420 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
421 Py_END_ALLOW_THREADS
422 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000423 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 goto error;
425 }
426
Benjamin Petersond7b03282008-09-13 15:58:53 +0000427 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300428 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000429 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430 }
431
432 Py_BEGIN_ALLOW_THREADS
433 rc = sqlite3_finalize(statement);
434 Py_END_ALLOW_THREADS
435 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000436 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 }
438
439 }
440
441error:
442 if (PyErr_Occurred()) {
443 return NULL;
444 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200445 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446 }
447}
448
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000449PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000450{
451 int rc;
452 const char* tail;
453 sqlite3_stmt* statement;
454
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000455 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000456 return NULL;
457 }
458
Berker Peksag59da4b32016-09-12 07:16:43 +0300459 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000460 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461
462 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000463 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 Py_END_ALLOW_THREADS
465 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000466 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000467 goto error;
468 }
469
Benjamin Petersond7b03282008-09-13 15:58:53 +0000470 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300471 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000472 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000473 }
474
475 Py_BEGIN_ALLOW_THREADS
476 rc = sqlite3_finalize(statement);
477 Py_END_ALLOW_THREADS
478 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000479 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000480 }
481
482 }
483
484error:
485 if (PyErr_Occurred()) {
486 return NULL;
487 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200488 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000489 }
490}
491
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200492static int
493_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000494{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200495 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000496 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000497 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200498 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
499 if (value == -1 && PyErr_Occurred())
500 return -1;
501 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502 } else if (PyFloat_Check(py_val)) {
503 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000504 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200505 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200506 if (str == NULL)
507 return -1;
508 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000509 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200510 Py_buffer view;
511 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100512 PyErr_SetString(PyExc_ValueError,
513 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200514 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200516 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100517 PyErr_SetString(PyExc_OverflowError,
518 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200519 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100520 return -1;
521 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200522 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
523 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000524 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200525 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200527 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000528}
529
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000530PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000531{
532 PyObject* args;
533 int i;
534 sqlite3_value* cur_value;
535 PyObject* cur_py_value;
536 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538
539 args = PyTuple_New(argc);
540 if (!args) {
541 return NULL;
542 }
543
544 for (i = 0; i < argc; i++) {
545 cur_value = argv[i];
546 switch (sqlite3_value_type(argv[i])) {
547 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200548 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549 break;
550 case SQLITE_FLOAT:
551 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
552 break;
553 case SQLITE_TEXT:
554 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000555 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556 /* TODO: have a way to show errors here */
557 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000558 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000559 Py_INCREF(Py_None);
560 cur_py_value = Py_None;
561 }
562 break;
563 case SQLITE_BLOB:
564 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000565 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000566 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567 break;
568 case SQLITE_NULL:
569 default:
570 Py_INCREF(Py_None);
571 cur_py_value = Py_None;
572 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000573
574 if (!cur_py_value) {
575 Py_DECREF(args);
576 return NULL;
577 }
578
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 PyTuple_SetItem(args, i, cur_py_value);
580
581 }
582
583 return args;
584}
585
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000586void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587{
588 PyObject* args;
589 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000590 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200591 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592
Georg Brandldfd73442009-04-05 11:47:34 +0000593#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000594 PyGILState_STATE threadstate;
595
596 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000597#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598
599 py_func = (PyObject*)sqlite3_user_data(context);
600
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000601 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000602 if (args) {
603 py_retval = PyObject_CallObject(py_func, args);
604 Py_DECREF(args);
605 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200607 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000608 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200609 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000610 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200611 }
612 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000613 if (_enable_callback_tracebacks) {
614 PyErr_Print();
615 } else {
616 PyErr_Clear();
617 }
618 _sqlite3_result_error(context, "user-defined function raised exception", -1);
619 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620
Georg Brandldfd73442009-04-05 11:47:34 +0000621#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000623#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624}
625
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000626static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000627{
628 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000630 PyObject* aggregate_class;
631 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000632 PyObject* stepmethod = NULL;
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_STATE threadstate;
636
637 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000638#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000639
640 aggregate_class = (PyObject*)sqlite3_user_data(context);
641
642 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
643
644 if (*aggregate_instance == 0) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100645 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646
Thomas Wouters477c8d52006-05-27 19:21:47 +0000647 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000649 if (_enable_callback_tracebacks) {
650 PyErr_Print();
651 } else {
652 PyErr_Clear();
653 }
654 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000655 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000656 }
657 }
658
659 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660 if (!stepmethod) {
661 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000662 }
663
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000664 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000665 if (!args) {
666 goto error;
667 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668
669 function_result = PyObject_CallObject(stepmethod, args);
670 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000673 if (_enable_callback_tracebacks) {
674 PyErr_Print();
675 } else {
676 PyErr_Clear();
677 }
678 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000679 }
680
Thomas Wouters477c8d52006-05-27 19:21:47 +0000681error:
682 Py_XDECREF(stepmethod);
683 Py_XDECREF(function_result);
684
Georg Brandldfd73442009-04-05 11:47:34 +0000685#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000686 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000687#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688}
689
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000690void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000691{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200692 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000693 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200694 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200695 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200696 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200697 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000698
Georg Brandldfd73442009-04-05 11:47:34 +0000699#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000700 PyGILState_STATE threadstate;
701
702 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000703#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
706 if (!*aggregate_instance) {
707 /* this branch is executed if there was an exception in the aggregate's
708 * __init__ */
709
Thomas Wouters477c8d52006-05-27 19:21:47 +0000710 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 }
712
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200713 /* Keep the exception (if any) of the last call to step() */
714 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200715 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200716
Victor Stinner3466bde2016-09-05 18:16:01 -0700717 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200718
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200719 Py_DECREF(*aggregate_instance);
720
721 ok = 0;
722 if (function_result) {
723 ok = _pysqlite_set_result(context, function_result) == 0;
724 Py_DECREF(function_result);
725 }
726 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000727 if (_enable_callback_tracebacks) {
728 PyErr_Print();
729 } else {
730 PyErr_Clear();
731 }
732 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200733#if SQLITE_VERSION_NUMBER < 3003003
734 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
735 exception, so don't restore the previous exception */
736 restore = 0;
737#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000738 }
739
Victor Stinnerffff7632013-08-02 01:48:10 +0200740 if (restore) {
741 /* Restore the exception (if any) of the last call to step(),
742 but clear also the current exception if finalize() failed */
743 PyErr_Restore(exception, value, tb);
744 }
Victor Stinner3a857322013-07-22 08:34:32 +0200745
Thomas Wouters477c8d52006-05-27 19:21:47 +0000746error:
Georg Brandldfd73442009-04-05 11:47:34 +0000747#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000748 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000749#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200750 /* explicit return to avoid a compilation error if WITH_THREAD
751 is not defined */
752 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000753}
754
Gerhard Häringf9cee222010-03-05 15:20:03 +0000755static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756{
757 PyObject* new_list;
758 PyObject* weakref;
759 int i;
760
761 /* we only need to do this once in a while */
762 if (self->created_statements++ < 200) {
763 return;
764 }
765
766 self->created_statements = 0;
767
768 new_list = PyList_New(0);
769 if (!new_list) {
770 return;
771 }
772
773 for (i = 0; i < PyList_Size(self->statements); i++) {
774 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000775 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000776 if (PyList_Append(new_list, weakref) != 0) {
777 Py_DECREF(new_list);
778 return;
779 }
780 }
781 }
782
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300783 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000784}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000785
Gerhard Häringf9cee222010-03-05 15:20:03 +0000786static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
787{
788 PyObject* new_list;
789 PyObject* weakref;
790 int i;
791
792 /* we only need to do this once in a while */
793 if (self->created_cursors++ < 200) {
794 return;
795 }
796
797 self->created_cursors = 0;
798
799 new_list = PyList_New(0);
800 if (!new_list) {
801 return;
802 }
803
804 for (i = 0; i < PyList_Size(self->cursors); i++) {
805 weakref = PyList_GetItem(self->cursors, i);
806 if (PyWeakref_GetObject(weakref) != Py_None) {
807 if (PyList_Append(new_list, weakref) != 0) {
808 Py_DECREF(new_list);
809 return;
810 }
811 }
812 }
813
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300814 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000815}
816
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000817PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818{
819 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
820
821 PyObject* func;
822 char* name;
823 int narg;
824 int rc;
825
Gerhard Häringf9cee222010-03-05 15:20:03 +0000826 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
827 return NULL;
828 }
829
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000830 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
831 &name, &narg, &func))
832 {
833 return NULL;
834 }
835
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000836 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837
Thomas Wouters477c8d52006-05-27 19:21:47 +0000838 if (rc != SQLITE_OK) {
839 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000840 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841 return NULL;
842 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000843 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
844 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000845
Berker Peksagfe21de92016-04-09 07:34:39 +0300846 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848}
849
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000850PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000851{
852 PyObject* aggregate_class;
853
854 int n_arg;
855 char* name;
856 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
857 int rc;
858
Gerhard Häringf9cee222010-03-05 15:20:03 +0000859 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
860 return NULL;
861 }
862
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000863 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
864 kwlist, &name, &n_arg, &aggregate_class)) {
865 return NULL;
866 }
867
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000868 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 +0000869 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000870 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000871 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000872 return NULL;
873 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000874 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
875 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000876
Berker Peksagfe21de92016-04-09 07:34:39 +0300877 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000878 }
879}
880
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000881static 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 +0000882{
883 PyObject *ret;
884 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000885#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000886 PyGILState_STATE gilstate;
887
888 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000889#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000890
Victor Stinnerd4095d92013-07-26 22:23:33 +0200891 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000892
Victor Stinnerd4095d92013-07-26 22:23:33 +0200893 if (ret == NULL) {
894 if (_enable_callback_tracebacks)
895 PyErr_Print();
896 else
897 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200898
Victor Stinnerd4095d92013-07-26 22:23:33 +0200899 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200900 }
901 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200902 if (PyLong_Check(ret)) {
903 rc = _PyLong_AsInt(ret);
904 if (rc == -1 && PyErr_Occurred()) {
905 if (_enable_callback_tracebacks)
906 PyErr_Print();
907 else
908 PyErr_Clear();
909 rc = SQLITE_DENY;
910 }
911 }
912 else {
913 rc = SQLITE_DENY;
914 }
915 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000916 }
917
Georg Brandldfd73442009-04-05 11:47:34 +0000918#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000919 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000920#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000921 return rc;
922}
923
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000924static int _progress_handler(void* user_arg)
925{
926 int rc;
927 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000928#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000929 PyGILState_STATE gilstate;
930
931 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000932#endif
Victor Stinner070c4d72016-12-09 12:29:18 +0100933 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000934
935 if (!ret) {
936 if (_enable_callback_tracebacks) {
937 PyErr_Print();
938 } else {
939 PyErr_Clear();
940 }
941
Mark Dickinson934896d2009-02-21 20:59:32 +0000942 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000943 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000944 } else {
945 rc = (int)PyObject_IsTrue(ret);
946 Py_DECREF(ret);
947 }
948
Georg Brandldfd73442009-04-05 11:47:34 +0000949#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000950 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000951#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000952 return rc;
953}
954
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200955static void _trace_callback(void* user_arg, const char* statement_string)
956{
957 PyObject *py_statement = NULL;
958 PyObject *ret = NULL;
959
960#ifdef WITH_THREAD
961 PyGILState_STATE gilstate;
962
963 gilstate = PyGILState_Ensure();
964#endif
965 py_statement = PyUnicode_DecodeUTF8(statement_string,
966 strlen(statement_string), "replace");
967 if (py_statement) {
968 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
969 Py_DECREF(py_statement);
970 }
971
972 if (ret) {
973 Py_DECREF(ret);
974 } else {
975 if (_enable_callback_tracebacks) {
976 PyErr_Print();
977 } else {
978 PyErr_Clear();
979 }
980 }
981
982#ifdef WITH_THREAD
983 PyGILState_Release(gilstate);
984#endif
985}
986
Gerhard Häringf9cee222010-03-05 15:20:03 +0000987static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000988{
989 PyObject* authorizer_cb;
990
991 static char *kwlist[] = { "authorizer_callback", NULL };
992 int rc;
993
Gerhard Häringf9cee222010-03-05 15:20:03 +0000994 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
995 return NULL;
996 }
997
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000998 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
999 kwlist, &authorizer_cb)) {
1000 return NULL;
1001 }
1002
1003 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1004
1005 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001006 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001007 return NULL;
1008 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001009 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1010 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001011
Berker Peksagfe21de92016-04-09 07:34:39 +03001012 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001013 }
1014}
1015
Gerhard Häringf9cee222010-03-05 15:20:03 +00001016static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001017{
1018 PyObject* progress_handler;
1019 int n;
1020
1021 static char *kwlist[] = { "progress_handler", "n", NULL };
1022
Gerhard Häringf9cee222010-03-05 15:20:03 +00001023 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1024 return NULL;
1025 }
1026
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001027 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1028 kwlist, &progress_handler, &n)) {
1029 return NULL;
1030 }
1031
1032 if (progress_handler == Py_None) {
1033 /* None clears the progress handler previously set */
1034 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1035 } else {
1036 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001037 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1038 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001039 }
1040
Berker Peksagfe21de92016-04-09 07:34:39 +03001041 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001042}
1043
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001044static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1045{
1046 PyObject* trace_callback;
1047
1048 static char *kwlist[] = { "trace_callback", NULL };
1049
1050 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1051 return NULL;
1052 }
1053
1054 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1055 kwlist, &trace_callback)) {
1056 return NULL;
1057 }
1058
1059 if (trace_callback == Py_None) {
1060 /* None clears the trace callback previously set */
1061 sqlite3_trace(self->db, 0, (void*)0);
1062 } else {
1063 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1064 return NULL;
1065 sqlite3_trace(self->db, _trace_callback, trace_callback);
1066 }
1067
Berker Peksagfe21de92016-04-09 07:34:39 +03001068 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001069}
1070
Gerhard Häringf9cee222010-03-05 15:20:03 +00001071#ifdef HAVE_LOAD_EXTENSION
1072static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1073{
1074 int rc;
1075 int onoff;
1076
1077 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1078 return NULL;
1079 }
1080
1081 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1082 return NULL;
1083 }
1084
1085 rc = sqlite3_enable_load_extension(self->db, onoff);
1086
1087 if (rc != SQLITE_OK) {
1088 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1089 return NULL;
1090 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001091 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001092 }
1093}
1094
1095static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1096{
1097 int rc;
1098 char* extension_name;
1099 char* errmsg;
1100
1101 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1102 return NULL;
1103 }
1104
1105 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1106 return NULL;
1107 }
1108
1109 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1110 if (rc != 0) {
1111 PyErr_SetString(pysqlite_OperationalError, errmsg);
1112 return NULL;
1113 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001114 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001115 }
1116}
1117#endif
1118
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001119int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001120{
Georg Brandldfd73442009-04-05 11:47:34 +00001121#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001122 if (self->check_same_thread) {
1123 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001124 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 "SQLite objects created in a thread can only be used in that same thread."
1126 "The object was created in thread id %ld and this is thread id %ld",
1127 self->thread_ident, PyThread_get_thread_ident());
1128 return 0;
1129 }
1130
1131 }
Georg Brandldfd73442009-04-05 11:47:34 +00001132#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001133 return 1;
1134}
1135
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001136static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001137{
1138 Py_INCREF(self->isolation_level);
1139 return self->isolation_level;
1140}
1141
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001142static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001144 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001145 return NULL;
1146 } else {
1147 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1148 }
1149}
1150
Berker Peksag59da4b32016-09-12 07:16:43 +03001151static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1152{
1153 if (!pysqlite_check_connection(self)) {
1154 return NULL;
1155 }
1156 if (!sqlite3_get_autocommit(self->db)) {
1157 Py_RETURN_TRUE;
1158 }
1159 Py_RETURN_FALSE;
1160}
1161
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001162static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001164 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001165 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166 if (!res) {
1167 return -1;
1168 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001169 Py_DECREF(res);
1170
Serhiy Storchaka28914922016-09-01 22:18:03 +03001171 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001172 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001173 const char * const *candidate;
1174 PyObject *uppercase_level;
1175 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001176
Serhiy Storchaka28914922016-09-01 22:18:03 +03001177 if (!PyUnicode_Check(isolation_level)) {
1178 PyErr_Format(PyExc_TypeError,
1179 "isolation_level must be a string or None, not %.100s",
1180 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181 return -1;
1182 }
1183
Serhiy Storchaka28914922016-09-01 22:18:03 +03001184 uppercase_level = _PyObject_CallMethodIdObjArgs(
1185 (PyObject *)&PyUnicode_Type, &PyId_upper,
1186 isolation_level, NULL);
1187 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001188 return -1;
1189 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001190 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001191 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001192 break;
1193 }
1194 Py_DECREF(uppercase_level);
1195 if (!*candidate) {
1196 PyErr_SetString(PyExc_ValueError,
1197 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001198 return -1;
1199 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001200 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001201 }
1202
Serhiy Storchaka28914922016-09-01 22:18:03 +03001203 Py_INCREF(isolation_level);
1204 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001205 return 0;
1206}
1207
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001208PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001209{
1210 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001211 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001212 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001213 int rc;
1214
Gerhard Häringf9cee222010-03-05 15:20:03 +00001215 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1216 return NULL;
1217 }
1218
Larry Hastings3b12e952015-05-08 07:45:10 -07001219 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection()", kwargs))
1220 return NULL;
1221
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001222 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001223 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001225 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001226
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001227 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228 if (!statement) {
1229 return NULL;
1230 }
1231
Victor Stinner0201f442010-03-13 03:28:34 +00001232 statement->db = NULL;
1233 statement->st = NULL;
1234 statement->sql = NULL;
1235 statement->in_use = 0;
1236 statement->in_weakreflist = NULL;
1237
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001238 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001239 if (rc != SQLITE_OK) {
1240 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001241 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001243 if (PyErr_ExceptionMatches(PyExc_TypeError))
1244 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001245 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001246 (void)pysqlite_statement_reset(statement);
1247 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001248 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001249 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 }
1251
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001252 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1253 if (weakref == NULL)
1254 goto error;
1255 if (PyList_Append(self->statements, weakref) != 0) {
1256 Py_DECREF(weakref);
1257 goto error;
1258 }
1259 Py_DECREF(weakref);
1260
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001261 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001262
1263error:
1264 Py_DECREF(statement);
1265 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266}
1267
Larry Hastings01b08832015-05-08 07:37:49 -07001268PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001269{
1270 PyObject* cursor = 0;
1271 PyObject* result = 0;
1272 PyObject* method = 0;
1273
Victor Stinner3466bde2016-09-05 18:16:01 -07001274 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275 if (!cursor) {
1276 goto error;
1277 }
1278
1279 method = PyObject_GetAttrString(cursor, "execute");
1280 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001281 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001282 goto error;
1283 }
1284
1285 result = PyObject_CallObject(method, args);
1286 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001287 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001288 }
1289
1290error:
1291 Py_XDECREF(result);
1292 Py_XDECREF(method);
1293
1294 return cursor;
1295}
1296
Larry Hastings01b08832015-05-08 07:37:49 -07001297PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001298{
1299 PyObject* cursor = 0;
1300 PyObject* result = 0;
1301 PyObject* method = 0;
1302
Victor Stinner3466bde2016-09-05 18:16:01 -07001303 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001304 if (!cursor) {
1305 goto error;
1306 }
1307
1308 method = PyObject_GetAttrString(cursor, "executemany");
1309 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001310 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001311 goto error;
1312 }
1313
1314 result = PyObject_CallObject(method, args);
1315 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001316 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001317 }
1318
1319error:
1320 Py_XDECREF(result);
1321 Py_XDECREF(method);
1322
1323 return cursor;
1324}
1325
Larry Hastings01b08832015-05-08 07:37:49 -07001326PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327{
1328 PyObject* cursor = 0;
1329 PyObject* result = 0;
1330 PyObject* method = 0;
1331
Victor Stinner3466bde2016-09-05 18:16:01 -07001332 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333 if (!cursor) {
1334 goto error;
1335 }
1336
1337 method = PyObject_GetAttrString(cursor, "executescript");
1338 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001339 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001340 goto error;
1341 }
1342
1343 result = PyObject_CallObject(method, args);
1344 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001345 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001346 }
1347
1348error:
1349 Py_XDECREF(result);
1350 Py_XDECREF(method);
1351
1352 return cursor;
1353}
1354
1355/* ------------------------- COLLATION CODE ------------------------ */
1356
1357static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001358pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001359 void* context,
1360 int text1_length, const void* text1_data,
1361 int text2_length, const void* text2_data)
1362{
1363 PyObject* callback = (PyObject*)context;
1364 PyObject* string1 = 0;
1365 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001366#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001367 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001368#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001370 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001371 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001372#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001374#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001375
1376 if (PyErr_Occurred()) {
1377 goto finally;
1378 }
1379
Guido van Rossum98297ee2007-11-06 21:34:58 +00001380 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1381 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001382
1383 if (!string1 || !string2) {
1384 goto finally; /* failed to allocate strings */
1385 }
1386
1387 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1388
1389 if (!retval) {
1390 /* execution failed */
1391 goto finally;
1392 }
1393
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001394 longval = PyLong_AsLongAndOverflow(retval, &result);
1395 if (longval == -1 && PyErr_Occurred()) {
1396 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001397 result = 0;
1398 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001399 else if (!result) {
1400 if (longval > 0)
1401 result = 1;
1402 else if (longval < 0)
1403 result = -1;
1404 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001405
1406finally:
1407 Py_XDECREF(string1);
1408 Py_XDECREF(string2);
1409 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001410#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001412#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001413 return result;
1414}
1415
1416static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001417pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001418{
1419 PyObject* retval = NULL;
1420
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001421 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001422 goto finally;
1423 }
1424
1425 sqlite3_interrupt(self->db);
1426
1427 Py_INCREF(Py_None);
1428 retval = Py_None;
1429
1430finally:
1431 return retval;
1432}
1433
Christian Heimesbbe741d2008-03-28 10:53:29 +00001434/* Function author: Paul Kippes <kippesp@gmail.com>
1435 * Class method of Connection to call the Python function _iterdump
1436 * of the sqlite3 module.
1437 */
1438static PyObject *
1439pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1440{
1441 PyObject* retval = NULL;
1442 PyObject* module = NULL;
1443 PyObject* module_dict;
1444 PyObject* pyfn_iterdump;
1445
1446 if (!pysqlite_check_connection(self)) {
1447 goto finally;
1448 }
1449
1450 module = PyImport_ImportModule(MODULE_NAME ".dump");
1451 if (!module) {
1452 goto finally;
1453 }
1454
1455 module_dict = PyModule_GetDict(module);
1456 if (!module_dict) {
1457 goto finally;
1458 }
1459
1460 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1461 if (!pyfn_iterdump) {
1462 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1463 goto finally;
1464 }
1465
1466 args = PyTuple_New(1);
1467 if (!args) {
1468 goto finally;
1469 }
1470 Py_INCREF(self);
1471 PyTuple_SetItem(args, 0, (PyObject*)self);
1472 retval = PyObject_CallObject(pyfn_iterdump, args);
1473
1474finally:
1475 Py_XDECREF(args);
1476 Py_XDECREF(module);
1477 return retval;
1478}
1479
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001480static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001481pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001482{
1483 PyObject* callable;
1484 PyObject* uppercase_name = 0;
1485 PyObject* name;
1486 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001487 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001488 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001489 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001491 unsigned int kind;
1492 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001493
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001494 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001495 goto finally;
1496 }
1497
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001498 if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1499 &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001500 goto finally;
1501 }
1502
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001503 uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type,
1504 &PyId_upper, name, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001505 if (!uppercase_name) {
1506 goto finally;
1507 }
1508
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001509 if (PyUnicode_READY(uppercase_name))
1510 goto finally;
1511 len = PyUnicode_GET_LENGTH(uppercase_name);
1512 kind = PyUnicode_KIND(uppercase_name);
1513 data = PyUnicode_DATA(uppercase_name);
1514 for (i=0; i<len; i++) {
1515 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1516 if ((ch >= '0' && ch <= '9')
1517 || (ch >= 'A' && ch <= 'Z')
1518 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001519 {
Victor Stinner35466c52010-04-22 11:23:23 +00001520 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001522 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523 goto finally;
1524 }
1525 }
1526
Serhiy Storchaka06515832016-11-20 09:13:07 +02001527 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001528 if (!uppercase_name_str)
1529 goto finally;
1530
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001531 if (callable != Py_None && !PyCallable_Check(callable)) {
1532 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1533 goto finally;
1534 }
1535
1536 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001537 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1538 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001539 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001540 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1541 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001542 }
1543
1544 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001545 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001546 SQLITE_UTF8,
1547 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001548 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001549 if (rc != SQLITE_OK) {
1550 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001551 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001552 goto finally;
1553 }
1554
1555finally:
1556 Py_XDECREF(uppercase_name);
1557
1558 if (PyErr_Occurred()) {
1559 retval = NULL;
1560 } else {
1561 Py_INCREF(Py_None);
1562 retval = Py_None;
1563 }
1564
1565 return retval;
1566}
1567
Christian Heimesbbe741d2008-03-28 10:53:29 +00001568/* Called when the connection is used as a context manager. Returns itself as a
1569 * convenience to the caller. */
1570static PyObject *
1571pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1572{
1573 Py_INCREF(self);
1574 return (PyObject*)self;
1575}
1576
1577/** Called when the connection is used as a context manager. If there was any
1578 * exception, a rollback takes place; otherwise we commit. */
1579static PyObject *
1580pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1581{
1582 PyObject* exc_type, *exc_value, *exc_tb;
1583 char* method_name;
1584 PyObject* result;
1585
1586 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1587 return NULL;
1588 }
1589
1590 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1591 method_name = "commit";
1592 } else {
1593 method_name = "rollback";
1594 }
1595
Victor Stinner3466bde2016-09-05 18:16:01 -07001596 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001597 if (!result) {
1598 return NULL;
1599 }
1600 Py_DECREF(result);
1601
1602 Py_RETURN_FALSE;
1603}
1604
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001605static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001606PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001607
1608static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001609 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1610 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001611 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001612 {NULL}
1613};
1614
1615static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001616 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001617 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001618 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001619 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001620 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001621 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001622 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001623 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001624 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001625 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001626 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001627 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001628 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001629 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001630 #ifdef HAVE_LOAD_EXTENSION
1631 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1632 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1633 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1634 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1635 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001636 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1637 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001638 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1639 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001640 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001641 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001642 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001643 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001644 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001645 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001646 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001647 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001648 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001649 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001650 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001651 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001652 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1653 PyDoc_STR("For context manager. Non-standard.")},
1654 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1655 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001656 {NULL, NULL}
1657};
1658
1659static struct PyMemberDef connection_members[] =
1660{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001661 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1662 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1663 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1664 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1665 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1666 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1667 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1668 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1669 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1670 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001671 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1672 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001673 {NULL}
1674};
1675
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001676PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001677 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001678 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001679 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001680 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001681 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001682 0, /* tp_print */
1683 0, /* tp_getattr */
1684 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001685 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001686 0, /* tp_repr */
1687 0, /* tp_as_number */
1688 0, /* tp_as_sequence */
1689 0, /* tp_as_mapping */
1690 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001691 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001692 0, /* tp_str */
1693 0, /* tp_getattro */
1694 0, /* tp_setattro */
1695 0, /* tp_as_buffer */
1696 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1697 connection_doc, /* tp_doc */
1698 0, /* tp_traverse */
1699 0, /* tp_clear */
1700 0, /* tp_richcompare */
1701 0, /* tp_weaklistoffset */
1702 0, /* tp_iter */
1703 0, /* tp_iternext */
1704 connection_methods, /* tp_methods */
1705 connection_members, /* tp_members */
1706 connection_getset, /* tp_getset */
1707 0, /* tp_base */
1708 0, /* tp_dict */
1709 0, /* tp_descr_get */
1710 0, /* tp_descr_set */
1711 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001712 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001713 0, /* tp_alloc */
1714 0, /* tp_new */
1715 0 /* tp_free */
1716};
1717
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001718extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001719{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001720 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1721 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001722}