blob: 3e83fb662bba782ab33dc11da9391ff2691d4e2d [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;
Anders Lorentsena22a1272017-11-07 01:47:43 +010079 PyObject* database_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080 int detect_types = 0;
81 PyObject* isolation_level = NULL;
82 PyObject* factory = NULL;
83 int check_same_thread = 1;
84 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010085 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 double timeout = 5.0;
87 int rc;
88
Anders Lorentsena22a1272017-11-07 01:47:43 +010089 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
90 PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010091 &isolation_level, &check_same_thread,
92 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000094 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000095 }
96
Anders Lorentsena22a1272017-11-07 01:47:43 +010097 database = PyBytes_AsString(database_obj);
98
Gerhard Häringf9cee222010-03-05 15:20:03 +000099 self->initialized = 1;
100
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101 self->begin_statement = NULL;
102
Oren Milman93c5a5d2017-10-10 22:27:46 +0300103 Py_CLEAR(self->statement_cache);
104 Py_CLEAR(self->statements);
105 Py_CLEAR(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000106
107 Py_INCREF(Py_None);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300108 Py_XSETREF(self->row_factory, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000109
110 Py_INCREF(&PyUnicode_Type);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300111 Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100113#ifdef SQLITE_OPEN_URI
114 Py_BEGIN_ALLOW_THREADS
115 rc = sqlite3_open_v2(database, &self->db,
116 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
117 (uri ? SQLITE_OPEN_URI : 0), NULL);
118#else
119 if (uri) {
120 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
121 return -1;
122 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200124 /* No need to use sqlite3_open_v2 as sqlite3_open(filename, db) is the
125 same as sqlite3_open_v2(filename, db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL). */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100127#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128 Py_END_ALLOW_THREADS
129
Anders Lorentsena22a1272017-11-07 01:47:43 +0100130 Py_DECREF(database_obj);
131
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000133 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 return -1;
135 }
136
137 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000138 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000139 if (!isolation_level) {
140 return -1;
141 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142 } else {
143 Py_INCREF(isolation_level);
144 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300145 Py_CLEAR(self->isolation_level);
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100146 if (pysqlite_connection_set_isolation_level(self, isolation_level) < 0) {
147 Py_DECREF(isolation_level);
148 return -1;
149 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150 Py_DECREF(isolation_level);
151
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000152 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000153 if (PyErr_Occurred()) {
154 return -1;
155 }
156
Gerhard Häringf9cee222010-03-05 15:20:03 +0000157 self->created_statements = 0;
158 self->created_cursors = 0;
159
160 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000161 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000162 self->cursors = PyList_New(0);
163 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000164 return -1;
165 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000166
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167 /* By default, the Cache class INCREFs the factory in its initializer, and
168 * decrefs it in its deallocator method. Since this would create a circular
169 * reference here, we're breaking it by decrementing self, and telling the
170 * cache class to not decref the factory (self) in its deallocator.
171 */
172 self->statement_cache->decref_factory = 0;
173 Py_DECREF(self);
174
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175 self->detect_types = detect_types;
176 self->timeout = timeout;
177 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000178 self->thread_ident = PyThread_get_thread_ident();
Berker Peksag7bea2342016-06-12 14:09:51 +0300179 if (!check_same_thread && sqlite3_libversion_number() < 3003001) {
180 PyErr_SetString(pysqlite_NotSupportedError, "shared connections not available");
181 return -1;
182 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183 self->check_same_thread = check_same_thread;
184
Oren Milman93c5a5d2017-10-10 22:27:46 +0300185 Py_XSETREF(self->function_pinboard, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000186 if (!self->function_pinboard) {
187 return -1;
188 }
189
Oren Milman93c5a5d2017-10-10 22:27:46 +0300190 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191 if (!self->collations) {
192 return -1;
193 }
194
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000195 self->Warning = pysqlite_Warning;
196 self->Error = pysqlite_Error;
197 self->InterfaceError = pysqlite_InterfaceError;
198 self->DatabaseError = pysqlite_DatabaseError;
199 self->DataError = pysqlite_DataError;
200 self->OperationalError = pysqlite_OperationalError;
201 self->IntegrityError = pysqlite_IntegrityError;
202 self->InternalError = pysqlite_InternalError;
203 self->ProgrammingError = pysqlite_ProgrammingError;
204 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205
206 return 0;
207}
208
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000209/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000210void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000212 int i;
213 PyObject* weakref;
214 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000215 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000216
Thomas Wouters477c8d52006-05-27 19:21:47 +0000217 for (i = 0; i < PyList_Size(self->statements); i++) {
218 weakref = PyList_GetItem(self->statements, i);
219 statement = PyWeakref_GetObject(weakref);
220 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500221 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000222 if (action == ACTION_RESET) {
223 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
224 } else {
225 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
226 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500227 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000228 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000230
231 if (reset_cursors) {
232 for (i = 0; i < PyList_Size(self->cursors); i++) {
233 weakref = PyList_GetItem(self->cursors, i);
234 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
235 if ((PyObject*)cursor != Py_None) {
236 cursor->reset = 1;
237 }
238 }
239 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240}
241
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000242void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243{
244 Py_XDECREF(self->statement_cache);
245
246 /* Clean up if user has not called .close() explicitly. */
247 if (self->db) {
248 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200249 SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250 Py_END_ALLOW_THREADS
251 }
252
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000253 Py_XDECREF(self->isolation_level);
254 Py_XDECREF(self->function_pinboard);
255 Py_XDECREF(self->row_factory);
256 Py_XDECREF(self->text_factory);
257 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000258 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000259 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260
Christian Heimes90aa7642007-12-19 02:45:37 +0000261 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000262}
263
Gerhard Häringf9cee222010-03-05 15:20:03 +0000264/*
265 * Registers a cursor with the connection.
266 *
267 * 0 => error; 1 => ok
268 */
269int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
270{
271 PyObject* weakref;
272
273 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
274 if (!weakref) {
275 goto error;
276 }
277
278 if (PyList_Append(connection->cursors, weakref) != 0) {
279 Py_CLEAR(weakref);
280 goto error;
281 }
282
283 Py_DECREF(weakref);
284
285 return 1;
286error:
287 return 0;
288}
289
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000290PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291{
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300292 static char *kwlist[] = {"factory", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293 PyObject* factory = NULL;
294 PyObject* cursor;
295
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
297 &factory)) {
298 return NULL;
299 }
300
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000301 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302 return NULL;
303 }
304
305 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000306 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307 }
308
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300309 cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL);
310 if (cursor == NULL)
311 return NULL;
312 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
313 PyErr_Format(PyExc_TypeError,
314 "factory must return a cursor, not %.100s",
315 Py_TYPE(cursor)->tp_name);
316 Py_DECREF(cursor);
317 return NULL;
318 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319
Gerhard Häringf9cee222010-03-05 15:20:03 +0000320 _pysqlite_drop_unused_cursor_references(self);
321
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000322 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300324 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 }
326
327 return cursor;
328}
329
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000330PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331{
332 int rc;
333
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000334 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335 return NULL;
336 }
337
Gerhard Häringf9cee222010-03-05 15:20:03 +0000338 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339
340 if (self->db) {
341 Py_BEGIN_ALLOW_THREADS
Aviv Palivoda86a67052017-03-03 12:58:17 +0200342 rc = SQLITE3_CLOSE(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343 Py_END_ALLOW_THREADS
344
345 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000346 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347 return NULL;
348 } else {
349 self->db = NULL;
350 }
351 }
352
Berker Peksagfe21de92016-04-09 07:34:39 +0300353 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000354}
355
356/*
357 * Checks if a connection object is usable (i. e. not closed).
358 *
359 * 0 => error; 1 => ok
360 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000361int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000362{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000363 if (!con->initialized) {
364 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
365 return 0;
366 }
367
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000369 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 return 0;
371 } else {
372 return 1;
373 }
374}
375
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000376PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377{
378 int rc;
379 const char* tail;
380 sqlite3_stmt* statement;
381
382 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700383 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384 Py_END_ALLOW_THREADS
385
386 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000387 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388 goto error;
389 }
390
Benjamin Petersond7b03282008-09-13 15:58:53 +0000391 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300392 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000393 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394 }
395
396 Py_BEGIN_ALLOW_THREADS
397 rc = sqlite3_finalize(statement);
398 Py_END_ALLOW_THREADS
399
400 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000401 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402 }
403
404error:
405 if (PyErr_Occurred()) {
406 return NULL;
407 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200408 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 }
410}
411
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000412PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413{
414 int rc;
415 const char* tail;
416 sqlite3_stmt* statement;
417
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000418 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419 return NULL;
420 }
421
Berker Peksag59da4b32016-09-12 07:16:43 +0300422 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000423
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700425 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426 Py_END_ALLOW_THREADS
427 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000428 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000429 goto error;
430 }
431
Benjamin Petersond7b03282008-09-13 15:58:53 +0000432 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300433 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000434 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000435 }
436
437 Py_BEGIN_ALLOW_THREADS
438 rc = sqlite3_finalize(statement);
439 Py_END_ALLOW_THREADS
440 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000441 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442 }
443
444 }
445
446error:
447 if (PyErr_Occurred()) {
448 return NULL;
449 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200450 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451 }
452}
453
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000454PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455{
456 int rc;
457 const char* tail;
458 sqlite3_stmt* statement;
459
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000460 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461 return NULL;
462 }
463
Berker Peksag59da4b32016-09-12 07:16:43 +0300464 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000465 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000466
467 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson52526942017-09-20 07:36:18 -0700468 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469 Py_END_ALLOW_THREADS
470 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000471 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000472 goto error;
473 }
474
Benjamin Petersond7b03282008-09-13 15:58:53 +0000475 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300476 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000477 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000478 }
479
480 Py_BEGIN_ALLOW_THREADS
481 rc = sqlite3_finalize(statement);
482 Py_END_ALLOW_THREADS
483 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000484 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000485 }
486
487 }
488
489error:
490 if (PyErr_Occurred()) {
491 return NULL;
492 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200493 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000494 }
495}
496
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200497static int
498_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200500 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000501 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000502 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200503 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
504 if (value == -1 && PyErr_Occurred())
505 return -1;
506 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507 } else if (PyFloat_Check(py_val)) {
508 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000509 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200510 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200511 if (str == NULL)
512 return -1;
513 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000514 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200515 Py_buffer view;
516 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100517 PyErr_SetString(PyExc_ValueError,
518 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200519 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200521 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100522 PyErr_SetString(PyExc_OverflowError,
523 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200524 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100525 return -1;
526 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200527 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
528 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000529 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200530 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000531 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200532 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533}
534
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000535PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536{
537 PyObject* args;
538 int i;
539 sqlite3_value* cur_value;
540 PyObject* cur_py_value;
541 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543
544 args = PyTuple_New(argc);
545 if (!args) {
546 return NULL;
547 }
548
549 for (i = 0; i < argc; i++) {
550 cur_value = argv[i];
551 switch (sqlite3_value_type(argv[i])) {
552 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200553 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554 break;
555 case SQLITE_FLOAT:
556 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
557 break;
558 case SQLITE_TEXT:
559 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000560 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000561 /* TODO: have a way to show errors here */
562 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000563 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000564 Py_INCREF(Py_None);
565 cur_py_value = Py_None;
566 }
567 break;
568 case SQLITE_BLOB:
569 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000570 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000571 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000572 break;
573 case SQLITE_NULL:
574 default:
575 Py_INCREF(Py_None);
576 cur_py_value = Py_None;
577 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000578
579 if (!cur_py_value) {
580 Py_DECREF(args);
581 return NULL;
582 }
583
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000584 PyTuple_SetItem(args, i, cur_py_value);
585
586 }
587
588 return args;
589}
590
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000591void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592{
593 PyObject* args;
594 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000595 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200596 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597
598 PyGILState_STATE threadstate;
599
600 threadstate = PyGILState_Ensure();
601
602 py_func = (PyObject*)sqlite3_user_data(context);
603
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000604 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000605 if (args) {
606 py_retval = PyObject_CallObject(py_func, args);
607 Py_DECREF(args);
608 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000609
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200610 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000611 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200612 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000613 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200614 }
615 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000616 if (_enable_callback_tracebacks) {
617 PyErr_Print();
618 } else {
619 PyErr_Clear();
620 }
621 _sqlite3_result_error(context, "user-defined function raised exception", -1);
622 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623
624 PyGILState_Release(threadstate);
625}
626
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000627static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628{
629 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000630 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000631 PyObject* aggregate_class;
632 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000633 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634
635 PyGILState_STATE threadstate;
636
637 threadstate = PyGILState_Ensure();
638
639 aggregate_class = (PyObject*)sqlite3_user_data(context);
640
641 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
642
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200643 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100644 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000645
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000647 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000648 if (_enable_callback_tracebacks) {
649 PyErr_Print();
650 } else {
651 PyErr_Clear();
652 }
653 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655 }
656 }
657
658 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659 if (!stepmethod) {
660 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661 }
662
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000663 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664 if (!args) {
665 goto error;
666 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000667
668 function_result = PyObject_CallObject(stepmethod, args);
669 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670
Thomas Wouters477c8d52006-05-27 19:21:47 +0000671 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000672 if (_enable_callback_tracebacks) {
673 PyErr_Print();
674 } else {
675 PyErr_Clear();
676 }
677 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678 }
679
Thomas Wouters477c8d52006-05-27 19:21:47 +0000680error:
681 Py_XDECREF(stepmethod);
682 Py_XDECREF(function_result);
683
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 PyGILState_Release(threadstate);
685}
686
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000687void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200689 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200691 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200692 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200693 PyObject *exception, *value, *tb;
Victor Stinnerffff7632013-08-02 01:48:10 +0200694 int restore;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000695
696 PyGILState_STATE threadstate;
697
698 threadstate = PyGILState_Ensure();
699
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000700 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
701 if (!*aggregate_instance) {
702 /* this branch is executed if there was an exception in the aggregate's
703 * __init__ */
704
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706 }
707
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200708 /* Keep the exception (if any) of the last call to step() */
709 PyErr_Fetch(&exception, &value, &tb);
Victor Stinnerffff7632013-08-02 01:48:10 +0200710 restore = 1;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200711
Victor Stinner3466bde2016-09-05 18:16:01 -0700712 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200713
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200714 Py_DECREF(*aggregate_instance);
715
716 ok = 0;
717 if (function_result) {
718 ok = _pysqlite_set_result(context, function_result) == 0;
719 Py_DECREF(function_result);
720 }
721 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000722 if (_enable_callback_tracebacks) {
723 PyErr_Print();
724 } else {
725 PyErr_Clear();
726 }
727 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Victor Stinnerffff7632013-08-02 01:48:10 +0200728#if SQLITE_VERSION_NUMBER < 3003003
729 /* with old SQLite versions, _sqlite3_result_error() sets a new Python
730 exception, so don't restore the previous exception */
731 restore = 0;
732#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000733 }
734
Victor Stinnerffff7632013-08-02 01:48:10 +0200735 if (restore) {
736 /* Restore the exception (if any) of the last call to step(),
737 but clear also the current exception if finalize() failed */
738 PyErr_Restore(exception, value, tb);
739 }
Victor Stinner3a857322013-07-22 08:34:32 +0200740
Thomas Wouters477c8d52006-05-27 19:21:47 +0000741error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000742 PyGILState_Release(threadstate);
743}
744
Gerhard Häringf9cee222010-03-05 15:20:03 +0000745static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000746{
747 PyObject* new_list;
748 PyObject* weakref;
749 int i;
750
751 /* we only need to do this once in a while */
752 if (self->created_statements++ < 200) {
753 return;
754 }
755
756 self->created_statements = 0;
757
758 new_list = PyList_New(0);
759 if (!new_list) {
760 return;
761 }
762
763 for (i = 0; i < PyList_Size(self->statements); i++) {
764 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000765 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766 if (PyList_Append(new_list, weakref) != 0) {
767 Py_DECREF(new_list);
768 return;
769 }
770 }
771 }
772
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300773 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000774}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775
Gerhard Häringf9cee222010-03-05 15:20:03 +0000776static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
777{
778 PyObject* new_list;
779 PyObject* weakref;
780 int i;
781
782 /* we only need to do this once in a while */
783 if (self->created_cursors++ < 200) {
784 return;
785 }
786
787 self->created_cursors = 0;
788
789 new_list = PyList_New(0);
790 if (!new_list) {
791 return;
792 }
793
794 for (i = 0; i < PyList_Size(self->cursors); i++) {
795 weakref = PyList_GetItem(self->cursors, i);
796 if (PyWeakref_GetObject(weakref) != Py_None) {
797 if (PyList_Append(new_list, weakref) != 0) {
798 Py_DECREF(new_list);
799 return;
800 }
801 }
802 }
803
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300804 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000805}
806
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000807PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000808{
809 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
810
811 PyObject* func;
812 char* name;
813 int narg;
814 int rc;
815
Gerhard Häringf9cee222010-03-05 15:20:03 +0000816 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
817 return NULL;
818 }
819
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
821 &name, &narg, &func))
822 {
823 return NULL;
824 }
825
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000826 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827
Thomas Wouters477c8d52006-05-27 19:21:47 +0000828 if (rc != SQLITE_OK) {
829 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000830 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831 return NULL;
832 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000833 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
834 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835
Berker Peksagfe21de92016-04-09 07:34:39 +0300836 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000837 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838}
839
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000840PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000841{
842 PyObject* aggregate_class;
843
844 int n_arg;
845 char* name;
846 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
847 int rc;
848
Gerhard Häringf9cee222010-03-05 15:20:03 +0000849 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
850 return NULL;
851 }
852
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000853 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
854 kwlist, &name, &n_arg, &aggregate_class)) {
855 return NULL;
856 }
857
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000858 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 +0000859 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000860 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000861 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862 return NULL;
863 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000864 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
865 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866
Berker Peksagfe21de92016-04-09 07:34:39 +0300867 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868 }
869}
870
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000871static 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 +0000872{
873 PyObject *ret;
874 int rc;
875 PyGILState_STATE gilstate;
876
877 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000878
Victor Stinnerd4095d92013-07-26 22:23:33 +0200879 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000880
Victor Stinnerd4095d92013-07-26 22:23:33 +0200881 if (ret == NULL) {
882 if (_enable_callback_tracebacks)
883 PyErr_Print();
884 else
885 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200886
Victor Stinnerd4095d92013-07-26 22:23:33 +0200887 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200888 }
889 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200890 if (PyLong_Check(ret)) {
891 rc = _PyLong_AsInt(ret);
892 if (rc == -1 && PyErr_Occurred()) {
893 if (_enable_callback_tracebacks)
894 PyErr_Print();
895 else
896 PyErr_Clear();
897 rc = SQLITE_DENY;
898 }
899 }
900 else {
901 rc = SQLITE_DENY;
902 }
903 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000904 }
905
906 PyGILState_Release(gilstate);
907 return rc;
908}
909
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000910static int _progress_handler(void* user_arg)
911{
912 int rc;
913 PyObject *ret;
914 PyGILState_STATE gilstate;
915
916 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100917 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000918
919 if (!ret) {
920 if (_enable_callback_tracebacks) {
921 PyErr_Print();
922 } else {
923 PyErr_Clear();
924 }
925
Mark Dickinson934896d2009-02-21 20:59:32 +0000926 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000927 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000928 } else {
929 rc = (int)PyObject_IsTrue(ret);
930 Py_DECREF(ret);
931 }
932
933 PyGILState_Release(gilstate);
934 return rc;
935}
936
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200937static void _trace_callback(void* user_arg, const char* statement_string)
938{
939 PyObject *py_statement = NULL;
940 PyObject *ret = NULL;
941
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200942 PyGILState_STATE gilstate;
943
944 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200945 py_statement = PyUnicode_DecodeUTF8(statement_string,
946 strlen(statement_string), "replace");
947 if (py_statement) {
948 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
949 Py_DECREF(py_statement);
950 }
951
952 if (ret) {
953 Py_DECREF(ret);
954 } else {
955 if (_enable_callback_tracebacks) {
956 PyErr_Print();
957 } else {
958 PyErr_Clear();
959 }
960 }
961
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200962 PyGILState_Release(gilstate);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200963}
964
Gerhard Häringf9cee222010-03-05 15:20:03 +0000965static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000966{
967 PyObject* authorizer_cb;
968
969 static char *kwlist[] = { "authorizer_callback", NULL };
970 int rc;
971
Gerhard Häringf9cee222010-03-05 15:20:03 +0000972 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
973 return NULL;
974 }
975
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000976 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
977 kwlist, &authorizer_cb)) {
978 return NULL;
979 }
980
981 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
982
983 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000984 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000985 return NULL;
986 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000987 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
988 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000989
Berker Peksagfe21de92016-04-09 07:34:39 +0300990 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000991 }
992}
993
Gerhard Häringf9cee222010-03-05 15:20:03 +0000994static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000995{
996 PyObject* progress_handler;
997 int n;
998
999 static char *kwlist[] = { "progress_handler", "n", NULL };
1000
Gerhard Häringf9cee222010-03-05 15:20:03 +00001001 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1002 return NULL;
1003 }
1004
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001005 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1006 kwlist, &progress_handler, &n)) {
1007 return NULL;
1008 }
1009
1010 if (progress_handler == Py_None) {
1011 /* None clears the progress handler previously set */
1012 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1013 } else {
1014 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001015 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1016 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001017 }
1018
Berker Peksagfe21de92016-04-09 07:34:39 +03001019 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001020}
1021
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001022static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1023{
1024 PyObject* trace_callback;
1025
1026 static char *kwlist[] = { "trace_callback", NULL };
1027
1028 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1029 return NULL;
1030 }
1031
1032 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1033 kwlist, &trace_callback)) {
1034 return NULL;
1035 }
1036
1037 if (trace_callback == Py_None) {
1038 /* None clears the trace callback previously set */
1039 sqlite3_trace(self->db, 0, (void*)0);
1040 } else {
1041 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1042 return NULL;
1043 sqlite3_trace(self->db, _trace_callback, trace_callback);
1044 }
1045
Berker Peksagfe21de92016-04-09 07:34:39 +03001046 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001047}
1048
Gerhard Häringf9cee222010-03-05 15:20:03 +00001049#ifdef HAVE_LOAD_EXTENSION
1050static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1051{
1052 int rc;
1053 int onoff;
1054
1055 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1056 return NULL;
1057 }
1058
1059 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1060 return NULL;
1061 }
1062
1063 rc = sqlite3_enable_load_extension(self->db, onoff);
1064
1065 if (rc != SQLITE_OK) {
1066 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1067 return NULL;
1068 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001069 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001070 }
1071}
1072
1073static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1074{
1075 int rc;
1076 char* extension_name;
1077 char* errmsg;
1078
1079 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1080 return NULL;
1081 }
1082
1083 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1084 return NULL;
1085 }
1086
1087 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1088 if (rc != 0) {
1089 PyErr_SetString(pysqlite_OperationalError, errmsg);
1090 return NULL;
1091 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001092 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001093 }
1094}
1095#endif
1096
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001097int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098{
1099 if (self->check_same_thread) {
1100 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001101 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001102 "SQLite objects created in a thread can only be used in that same thread."
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001103 "The object was created in thread id %lu and this is thread id %lu",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001104 self->thread_ident, PyThread_get_thread_ident());
1105 return 0;
1106 }
1107
1108 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001109 return 1;
1110}
1111
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001112static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001113{
1114 Py_INCREF(self->isolation_level);
1115 return self->isolation_level;
1116}
1117
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001118static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001119{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001120 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001121 return NULL;
1122 } else {
1123 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1124 }
1125}
1126
Berker Peksag59da4b32016-09-12 07:16:43 +03001127static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1128{
1129 if (!pysqlite_check_connection(self)) {
1130 return NULL;
1131 }
1132 if (!sqlite3_get_autocommit(self->db)) {
1133 Py_RETURN_TRUE;
1134 }
1135 Py_RETURN_FALSE;
1136}
1137
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001138static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001139{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001140 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001141 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001142 if (!res) {
1143 return -1;
1144 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001145 Py_DECREF(res);
1146
Serhiy Storchaka28914922016-09-01 22:18:03 +03001147 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001148 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001149 const char * const *candidate;
1150 PyObject *uppercase_level;
1151 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001152
Serhiy Storchaka28914922016-09-01 22:18:03 +03001153 if (!PyUnicode_Check(isolation_level)) {
1154 PyErr_Format(PyExc_TypeError,
1155 "isolation_level must be a string or None, not %.100s",
1156 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001157 return -1;
1158 }
1159
Serhiy Storchaka28914922016-09-01 22:18:03 +03001160 uppercase_level = _PyObject_CallMethodIdObjArgs(
1161 (PyObject *)&PyUnicode_Type, &PyId_upper,
1162 isolation_level, NULL);
1163 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001164 return -1;
1165 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001166 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001167 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001168 break;
1169 }
1170 Py_DECREF(uppercase_level);
1171 if (!*candidate) {
1172 PyErr_SetString(PyExc_ValueError,
1173 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001174 return -1;
1175 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001176 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001177 }
1178
Serhiy Storchaka28914922016-09-01 22:18:03 +03001179 Py_INCREF(isolation_level);
1180 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181 return 0;
1182}
1183
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001184PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185{
1186 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001187 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001188 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001189 int rc;
1190
Gerhard Häringf9cee222010-03-05 15:20:03 +00001191 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1192 return NULL;
1193 }
1194
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001195 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001196 return NULL;
1197
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001198 if (!PyArg_ParseTuple(args, "O", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001199 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001200
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001201 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001202
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001203 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001204 if (!statement) {
1205 return NULL;
1206 }
1207
Victor Stinner0201f442010-03-13 03:28:34 +00001208 statement->db = NULL;
1209 statement->st = NULL;
1210 statement->sql = NULL;
1211 statement->in_use = 0;
1212 statement->in_weakreflist = NULL;
1213
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001214 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001215 if (rc != SQLITE_OK) {
1216 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001217 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001218 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001219 if (PyErr_ExceptionMatches(PyExc_TypeError))
1220 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001221 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001222 (void)pysqlite_statement_reset(statement);
1223 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001225 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001226 }
1227
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001228 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1229 if (weakref == NULL)
1230 goto error;
1231 if (PyList_Append(self->statements, weakref) != 0) {
1232 Py_DECREF(weakref);
1233 goto error;
1234 }
1235 Py_DECREF(weakref);
1236
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001237 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001238
1239error:
1240 Py_DECREF(statement);
1241 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242}
1243
Larry Hastings01b08832015-05-08 07:37:49 -07001244PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001245{
1246 PyObject* cursor = 0;
1247 PyObject* result = 0;
1248 PyObject* method = 0;
1249
Victor Stinner3466bde2016-09-05 18:16:01 -07001250 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001251 if (!cursor) {
1252 goto error;
1253 }
1254
1255 method = PyObject_GetAttrString(cursor, "execute");
1256 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001257 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001258 goto error;
1259 }
1260
1261 result = PyObject_CallObject(method, args);
1262 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001263 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264 }
1265
1266error:
1267 Py_XDECREF(result);
1268 Py_XDECREF(method);
1269
1270 return cursor;
1271}
1272
Larry Hastings01b08832015-05-08 07:37:49 -07001273PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001274{
1275 PyObject* cursor = 0;
1276 PyObject* result = 0;
1277 PyObject* method = 0;
1278
Victor Stinner3466bde2016-09-05 18:16:01 -07001279 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280 if (!cursor) {
1281 goto error;
1282 }
1283
1284 method = PyObject_GetAttrString(cursor, "executemany");
1285 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001286 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287 goto error;
1288 }
1289
1290 result = PyObject_CallObject(method, args);
1291 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001292 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001293 }
1294
1295error:
1296 Py_XDECREF(result);
1297 Py_XDECREF(method);
1298
1299 return cursor;
1300}
1301
Larry Hastings01b08832015-05-08 07:37:49 -07001302PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001303{
1304 PyObject* cursor = 0;
1305 PyObject* result = 0;
1306 PyObject* method = 0;
1307
Victor Stinner3466bde2016-09-05 18:16:01 -07001308 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001309 if (!cursor) {
1310 goto error;
1311 }
1312
1313 method = PyObject_GetAttrString(cursor, "executescript");
1314 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001315 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001316 goto error;
1317 }
1318
1319 result = PyObject_CallObject(method, args);
1320 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001321 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001322 }
1323
1324error:
1325 Py_XDECREF(result);
1326 Py_XDECREF(method);
1327
1328 return cursor;
1329}
1330
1331/* ------------------------- COLLATION CODE ------------------------ */
1332
1333static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001334pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001335 void* context,
1336 int text1_length, const void* text1_data,
1337 int text2_length, const void* text2_data)
1338{
1339 PyObject* callback = (PyObject*)context;
1340 PyObject* string1 = 0;
1341 PyObject* string2 = 0;
1342 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001344 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001345 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001346 gilstate = PyGILState_Ensure();
1347
1348 if (PyErr_Occurred()) {
1349 goto finally;
1350 }
1351
Guido van Rossum98297ee2007-11-06 21:34:58 +00001352 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1353 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001354
1355 if (!string1 || !string2) {
1356 goto finally; /* failed to allocate strings */
1357 }
1358
1359 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1360
1361 if (!retval) {
1362 /* execution failed */
1363 goto finally;
1364 }
1365
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001366 longval = PyLong_AsLongAndOverflow(retval, &result);
1367 if (longval == -1 && PyErr_Occurred()) {
1368 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369 result = 0;
1370 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001371 else if (!result) {
1372 if (longval > 0)
1373 result = 1;
1374 else if (longval < 0)
1375 result = -1;
1376 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001377
1378finally:
1379 Py_XDECREF(string1);
1380 Py_XDECREF(string2);
1381 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001382 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001383 return result;
1384}
1385
1386static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001387pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001388{
1389 PyObject* retval = NULL;
1390
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001391 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001392 goto finally;
1393 }
1394
1395 sqlite3_interrupt(self->db);
1396
1397 Py_INCREF(Py_None);
1398 retval = Py_None;
1399
1400finally:
1401 return retval;
1402}
1403
Christian Heimesbbe741d2008-03-28 10:53:29 +00001404/* Function author: Paul Kippes <kippesp@gmail.com>
1405 * Class method of Connection to call the Python function _iterdump
1406 * of the sqlite3 module.
1407 */
1408static PyObject *
1409pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1410{
1411 PyObject* retval = NULL;
1412 PyObject* module = NULL;
1413 PyObject* module_dict;
1414 PyObject* pyfn_iterdump;
1415
1416 if (!pysqlite_check_connection(self)) {
1417 goto finally;
1418 }
1419
1420 module = PyImport_ImportModule(MODULE_NAME ".dump");
1421 if (!module) {
1422 goto finally;
1423 }
1424
1425 module_dict = PyModule_GetDict(module);
1426 if (!module_dict) {
1427 goto finally;
1428 }
1429
1430 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1431 if (!pyfn_iterdump) {
1432 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1433 goto finally;
1434 }
1435
1436 args = PyTuple_New(1);
1437 if (!args) {
1438 goto finally;
1439 }
1440 Py_INCREF(self);
1441 PyTuple_SetItem(args, 0, (PyObject*)self);
1442 retval = PyObject_CallObject(pyfn_iterdump, args);
1443
1444finally:
1445 Py_XDECREF(args);
1446 Py_XDECREF(module);
1447 return retval;
1448}
1449
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001450static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001451pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001452{
1453 PyObject* callable;
1454 PyObject* uppercase_name = 0;
1455 PyObject* name;
1456 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001457 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001458 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001459 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001460 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001461 unsigned int kind;
1462 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001463
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001464 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001465 goto finally;
1466 }
1467
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001468 if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1469 &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001470 goto finally;
1471 }
1472
Serhiy Storchaka407ac472016-09-27 00:10:03 +03001473 uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type,
1474 &PyId_upper, name, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001475 if (!uppercase_name) {
1476 goto finally;
1477 }
1478
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001479 if (PyUnicode_READY(uppercase_name))
1480 goto finally;
1481 len = PyUnicode_GET_LENGTH(uppercase_name);
1482 kind = PyUnicode_KIND(uppercase_name);
1483 data = PyUnicode_DATA(uppercase_name);
1484 for (i=0; i<len; i++) {
1485 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1486 if ((ch >= '0' && ch <= '9')
1487 || (ch >= 'A' && ch <= 'Z')
1488 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001489 {
Victor Stinner35466c52010-04-22 11:23:23 +00001490 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001491 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001492 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001493 goto finally;
1494 }
1495 }
1496
Serhiy Storchaka06515832016-11-20 09:13:07 +02001497 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001498 if (!uppercase_name_str)
1499 goto finally;
1500
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001501 if (callable != Py_None && !PyCallable_Check(callable)) {
1502 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1503 goto finally;
1504 }
1505
1506 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001507 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1508 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001509 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001510 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1511 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001512 }
1513
1514 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001515 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001516 SQLITE_UTF8,
1517 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001518 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001519 if (rc != SQLITE_OK) {
1520 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001521 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001522 goto finally;
1523 }
1524
1525finally:
1526 Py_XDECREF(uppercase_name);
1527
1528 if (PyErr_Occurred()) {
1529 retval = NULL;
1530 } else {
1531 Py_INCREF(Py_None);
1532 retval = Py_None;
1533 }
1534
1535 return retval;
1536}
1537
Christian Heimesbbe741d2008-03-28 10:53:29 +00001538/* Called when the connection is used as a context manager. Returns itself as a
1539 * convenience to the caller. */
1540static PyObject *
1541pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1542{
1543 Py_INCREF(self);
1544 return (PyObject*)self;
1545}
1546
1547/** Called when the connection is used as a context manager. If there was any
1548 * exception, a rollback takes place; otherwise we commit. */
1549static PyObject *
1550pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1551{
1552 PyObject* exc_type, *exc_value, *exc_tb;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001553 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001554 PyObject* result;
1555
1556 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1557 return NULL;
1558 }
1559
1560 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1561 method_name = "commit";
1562 } else {
1563 method_name = "rollback";
1564 }
1565
Victor Stinner3466bde2016-09-05 18:16:01 -07001566 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001567 if (!result) {
1568 return NULL;
1569 }
1570 Py_DECREF(result);
1571
1572 Py_RETURN_FALSE;
1573}
1574
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001575static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001576PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001577
1578static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001579 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1580 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001581 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001582 {NULL}
1583};
1584
1585static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001586 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001587 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001588 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001589 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001590 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001591 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001592 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001593 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001594 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001595 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001596 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001597 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001598 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001599 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001600 #ifdef HAVE_LOAD_EXTENSION
1601 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1602 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1603 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1604 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1605 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001606 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1607 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001608 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1609 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001610 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001611 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001612 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001613 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001614 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001615 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001616 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001617 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001618 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001619 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001620 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001621 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001622 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1623 PyDoc_STR("For context manager. Non-standard.")},
1624 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1625 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001626 {NULL, NULL}
1627};
1628
1629static struct PyMemberDef connection_members[] =
1630{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001631 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1632 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1633 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1634 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1635 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1636 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1637 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1638 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1639 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1640 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001641 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1642 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001643 {NULL}
1644};
1645
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001646PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001647 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001648 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001649 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001650 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001651 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001652 0, /* tp_print */
1653 0, /* tp_getattr */
1654 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001655 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001656 0, /* tp_repr */
1657 0, /* tp_as_number */
1658 0, /* tp_as_sequence */
1659 0, /* tp_as_mapping */
1660 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001661 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001662 0, /* tp_str */
1663 0, /* tp_getattro */
1664 0, /* tp_setattro */
1665 0, /* tp_as_buffer */
1666 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1667 connection_doc, /* tp_doc */
1668 0, /* tp_traverse */
1669 0, /* tp_clear */
1670 0, /* tp_richcompare */
1671 0, /* tp_weaklistoffset */
1672 0, /* tp_iter */
1673 0, /* tp_iternext */
1674 connection_methods, /* tp_methods */
1675 connection_members, /* tp_members */
1676 connection_getset, /* tp_getset */
1677 0, /* tp_base */
1678 0, /* tp_dict */
1679 0, /* tp_descr_get */
1680 0, /* tp_descr_set */
1681 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001682 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001683 0, /* tp_alloc */
1684 0, /* tp_new */
1685 0 /* tp_free */
1686};
1687
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001688extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001689{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001690 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1691 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001692}