blob: 451ea241bd3f317060c93202eeec411bc2be6435 [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"
32#include "sqlitecompat.h"
33
34#include "pythread.h"
35
Gerhard Häringe7ea7452008-03-29 00:45:29 +000036#define ACTION_FINALIZE 1
37#define ACTION_RESET 2
38
Gerhard Häringf9cee222010-03-05 15:20:03 +000039#if SQLITE_VERSION_NUMBER >= 3003008
40#ifndef SQLITE_OMIT_LOAD_EXTENSION
41#define HAVE_LOAD_EXTENSION
42#endif
43#endif
44
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000045static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Gerhard Häringf9cee222010-03-05 15:20:03 +000046static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047
Thomas Wouters0e3f5912006-08-11 14:57:12 +000048
Benjamin Petersond7b03282008-09-13 15:58:53 +000049static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050{
51 /* in older SQLite versions, calling sqlite3_result_error in callbacks
52 * triggers a bug in SQLite that leads either to irritating results or
53 * segfaults, depending on the SQLite version */
54#if SQLITE_VERSION_NUMBER >= 3003003
55 sqlite3_result_error(ctx, errmsg, len);
56#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000057 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058#endif
59}
60
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000061int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062{
63 static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
64
65 char* database;
66 int detect_types = 0;
67 PyObject* isolation_level = NULL;
68 PyObject* factory = NULL;
69 int check_same_thread = 1;
70 int cached_statements = 100;
71 double timeout = 5.0;
72 int rc;
73
74 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist,
75 &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
76 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000077 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 }
79
Gerhard Häringf9cee222010-03-05 15:20:03 +000080 self->initialized = 1;
81
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 self->begin_statement = NULL;
83
84 self->statement_cache = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000085 self->statements = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +000086 self->cursors = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087
88 Py_INCREF(Py_None);
89 self->row_factory = Py_None;
90
91 Py_INCREF(&PyUnicode_Type);
92 self->text_factory = (PyObject*)&PyUnicode_Type;
93
94 Py_BEGIN_ALLOW_THREADS
95 rc = sqlite3_open(database, &self->db);
96 Py_END_ALLOW_THREADS
97
98 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000099 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000100 return -1;
101 }
102
103 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000104 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000105 if (!isolation_level) {
106 return -1;
107 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108 } else {
109 Py_INCREF(isolation_level);
110 }
111 self->isolation_level = NULL;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000112 pysqlite_connection_set_isolation_level(self, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 Py_DECREF(isolation_level);
114
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000115 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116 if (PyErr_Occurred()) {
117 return -1;
118 }
119
Gerhard Häringf9cee222010-03-05 15:20:03 +0000120 self->created_statements = 0;
121 self->created_cursors = 0;
122
123 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000125 self->cursors = PyList_New(0);
126 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000127 return -1;
128 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000129
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 /* By default, the Cache class INCREFs the factory in its initializer, and
131 * decrefs it in its deallocator method. Since this would create a circular
132 * reference here, we're breaking it by decrementing self, and telling the
133 * cache class to not decref the factory (self) in its deallocator.
134 */
135 self->statement_cache->decref_factory = 0;
136 Py_DECREF(self);
137
138 self->inTransaction = 0;
139 self->detect_types = detect_types;
140 self->timeout = timeout;
141 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000142#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000144#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000145 self->check_same_thread = check_same_thread;
146
147 self->function_pinboard = PyDict_New();
148 if (!self->function_pinboard) {
149 return -1;
150 }
151
152 self->collations = PyDict_New();
153 if (!self->collations) {
154 return -1;
155 }
156
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000157 self->Warning = pysqlite_Warning;
158 self->Error = pysqlite_Error;
159 self->InterfaceError = pysqlite_InterfaceError;
160 self->DatabaseError = pysqlite_DatabaseError;
161 self->DataError = pysqlite_DataError;
162 self->OperationalError = pysqlite_OperationalError;
163 self->IntegrityError = pysqlite_IntegrityError;
164 self->InternalError = pysqlite_InternalError;
165 self->ProgrammingError = pysqlite_ProgrammingError;
166 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167
168 return 0;
169}
170
Thomas Wouters477c8d52006-05-27 19:21:47 +0000171/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000172void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000174 pysqlite_Node* node;
175 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000176
177 node = self->statement_cache->first;
178
179 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000180 statement = (pysqlite_Statement*)(node->data);
181 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182 node = node->next;
183 }
184
185 Py_DECREF(self->statement_cache);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000186 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000187 Py_DECREF(self);
188 self->statement_cache->decref_factory = 0;
189}
190
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000191/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000192void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000194 int i;
195 PyObject* weakref;
196 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000197 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198
Thomas Wouters477c8d52006-05-27 19:21:47 +0000199 for (i = 0; i < PyList_Size(self->statements); i++) {
200 weakref = PyList_GetItem(self->statements, i);
201 statement = PyWeakref_GetObject(weakref);
202 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500203 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000204 if (action == ACTION_RESET) {
205 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
206 } else {
207 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
208 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500209 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000210 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000212
213 if (reset_cursors) {
214 for (i = 0; i < PyList_Size(self->cursors); i++) {
215 weakref = PyList_GetItem(self->cursors, i);
216 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
217 if ((PyObject*)cursor != Py_None) {
218 cursor->reset = 1;
219 }
220 }
221 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222}
223
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000224void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225{
226 Py_XDECREF(self->statement_cache);
227
228 /* Clean up if user has not called .close() explicitly. */
229 if (self->db) {
230 Py_BEGIN_ALLOW_THREADS
231 sqlite3_close(self->db);
232 Py_END_ALLOW_THREADS
233 }
234
235 if (self->begin_statement) {
236 PyMem_Free(self->begin_statement);
237 }
238 Py_XDECREF(self->isolation_level);
239 Py_XDECREF(self->function_pinboard);
240 Py_XDECREF(self->row_factory);
241 Py_XDECREF(self->text_factory);
242 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000243 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000244 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245
Christian Heimes90aa7642007-12-19 02:45:37 +0000246 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247}
248
Gerhard Häringf9cee222010-03-05 15:20:03 +0000249/*
250 * Registers a cursor with the connection.
251 *
252 * 0 => error; 1 => ok
253 */
254int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
255{
256 PyObject* weakref;
257
258 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
259 if (!weakref) {
260 goto error;
261 }
262
263 if (PyList_Append(connection->cursors, weakref) != 0) {
264 Py_CLEAR(weakref);
265 goto error;
266 }
267
268 Py_DECREF(weakref);
269
270 return 1;
271error:
272 return 0;
273}
274
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000275PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000276{
277 static char *kwlist[] = {"factory", NULL, NULL};
278 PyObject* factory = NULL;
279 PyObject* cursor;
280
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000281 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
282 &factory)) {
283 return NULL;
284 }
285
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000286 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 return NULL;
288 }
289
290 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000291 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292 }
293
294 cursor = PyObject_CallFunction(factory, "O", self);
295
Gerhard Häringf9cee222010-03-05 15:20:03 +0000296 _pysqlite_drop_unused_cursor_references(self);
297
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298 if (cursor && self->row_factory != Py_None) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000299 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000300 Py_INCREF(self->row_factory);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000301 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302 }
303
304 return cursor;
305}
306
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000307PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308{
309 int rc;
310
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000311 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312 return NULL;
313 }
314
Gerhard Häringf9cee222010-03-05 15:20:03 +0000315 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316
317 if (self->db) {
318 Py_BEGIN_ALLOW_THREADS
319 rc = sqlite3_close(self->db);
320 Py_END_ALLOW_THREADS
321
322 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000323 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324 return NULL;
325 } else {
326 self->db = NULL;
327 }
328 }
329
330 Py_INCREF(Py_None);
331 return Py_None;
332}
333
334/*
335 * Checks if a connection object is usable (i. e. not closed).
336 *
337 * 0 => error; 1 => ok
338 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000339int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000340{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000341 if (!con->initialized) {
342 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
343 return 0;
344 }
345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000347 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000348 return 0;
349 } else {
350 return 1;
351 }
352}
353
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000354PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355{
356 int rc;
357 const char* tail;
358 sqlite3_stmt* statement;
359
360 Py_BEGIN_ALLOW_THREADS
361 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
362 Py_END_ALLOW_THREADS
363
364 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000365 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 goto error;
367 }
368
Benjamin Petersond7b03282008-09-13 15:58:53 +0000369 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 if (rc == SQLITE_DONE) {
371 self->inTransaction = 1;
372 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000373 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374 }
375
376 Py_BEGIN_ALLOW_THREADS
377 rc = sqlite3_finalize(statement);
378 Py_END_ALLOW_THREADS
379
380 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000381 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 }
383
384error:
385 if (PyErr_Occurred()) {
386 return NULL;
387 } else {
388 Py_INCREF(Py_None);
389 return Py_None;
390 }
391}
392
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000393PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394{
395 int rc;
396 const char* tail;
397 sqlite3_stmt* statement;
398
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000399 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 return NULL;
401 }
402
403 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000404 pysqlite_do_all_statements(self, ACTION_RESET, 0);
405
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406 Py_BEGIN_ALLOW_THREADS
407 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
408 Py_END_ALLOW_THREADS
409 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000410 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000411 goto error;
412 }
413
Benjamin Petersond7b03282008-09-13 15:58:53 +0000414 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000415 if (rc == SQLITE_DONE) {
416 self->inTransaction = 0;
417 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000418 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419 }
420
421 Py_BEGIN_ALLOW_THREADS
422 rc = sqlite3_finalize(statement);
423 Py_END_ALLOW_THREADS
424 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000425 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426 }
427
428 }
429
430error:
431 if (PyErr_Occurred()) {
432 return NULL;
433 } else {
434 Py_INCREF(Py_None);
435 return Py_None;
436 }
437}
438
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000439PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000440{
441 int rc;
442 const char* tail;
443 sqlite3_stmt* statement;
444
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000445 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446 return NULL;
447 }
448
449 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000450 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451
452 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000453 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000454 Py_END_ALLOW_THREADS
455 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000456 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457 goto error;
458 }
459
Benjamin Petersond7b03282008-09-13 15:58:53 +0000460 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461 if (rc == SQLITE_DONE) {
462 self->inTransaction = 0;
463 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000464 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000465 }
466
467 Py_BEGIN_ALLOW_THREADS
468 rc = sqlite3_finalize(statement);
469 Py_END_ALLOW_THREADS
470 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000471 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000472 }
473
474 }
475
476error:
477 if (PyErr_Occurred()) {
478 return NULL;
479 } else {
480 Py_INCREF(Py_None);
481 return Py_None;
482 }
483}
484
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200485static int
486_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000487{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200488 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000489 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000490 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200491 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
492 if (value == -1 && PyErr_Occurred())
493 return -1;
494 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000495 } else if (PyFloat_Check(py_val)) {
496 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000497 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200498 const char *str = _PyUnicode_AsString(py_val);
499 if (str == NULL)
500 return -1;
501 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000502 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200503 const char* buffer;
504 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
506 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200507 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200509 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000510 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200511 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200513 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000514}
515
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000516PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517{
518 PyObject* args;
519 int i;
520 sqlite3_value* cur_value;
521 PyObject* cur_py_value;
522 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000523 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000524
525 args = PyTuple_New(argc);
526 if (!args) {
527 return NULL;
528 }
529
530 for (i = 0; i < argc; i++) {
531 cur_value = argv[i];
532 switch (sqlite3_value_type(argv[i])) {
533 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200534 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535 break;
536 case SQLITE_FLOAT:
537 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
538 break;
539 case SQLITE_TEXT:
540 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000541 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542 /* TODO: have a way to show errors here */
543 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000544 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545 Py_INCREF(Py_None);
546 cur_py_value = Py_None;
547 }
548 break;
549 case SQLITE_BLOB:
550 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000551 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000552 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000553 break;
554 case SQLITE_NULL:
555 default:
556 Py_INCREF(Py_None);
557 cur_py_value = Py_None;
558 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000559
560 if (!cur_py_value) {
561 Py_DECREF(args);
562 return NULL;
563 }
564
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000565 PyTuple_SetItem(args, i, cur_py_value);
566
567 }
568
569 return args;
570}
571
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000572void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000573{
574 PyObject* args;
575 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000576 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200577 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000578
Georg Brandldfd73442009-04-05 11:47:34 +0000579#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000580 PyGILState_STATE threadstate;
581
582 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000583#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000584
585 py_func = (PyObject*)sqlite3_user_data(context);
586
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000587 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000588 if (args) {
589 py_retval = PyObject_CallObject(py_func, args);
590 Py_DECREF(args);
591 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200593 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000594 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200595 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000596 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200597 }
598 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000599 if (_enable_callback_tracebacks) {
600 PyErr_Print();
601 } else {
602 PyErr_Clear();
603 }
604 _sqlite3_result_error(context, "user-defined function raised exception", -1);
605 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606
Georg Brandldfd73442009-04-05 11:47:34 +0000607#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000608 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000609#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610}
611
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000612static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000613{
614 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000615 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616 PyObject* aggregate_class;
617 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000618 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000619
Georg Brandldfd73442009-04-05 11:47:34 +0000620#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621 PyGILState_STATE threadstate;
622
623 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000624#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000625
626 aggregate_class = (PyObject*)sqlite3_user_data(context);
627
628 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
629
630 if (*aggregate_instance == 0) {
631 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
632
Thomas Wouters477c8d52006-05-27 19:21:47 +0000633 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000635 if (_enable_callback_tracebacks) {
636 PyErr_Print();
637 } else {
638 PyErr_Clear();
639 }
640 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000641 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000642 }
643 }
644
645 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646 if (!stepmethod) {
647 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648 }
649
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000650 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000651 if (!args) {
652 goto error;
653 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654
655 function_result = PyObject_CallObject(stepmethod, args);
656 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000657
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000659 if (_enable_callback_tracebacks) {
660 PyErr_Print();
661 } else {
662 PyErr_Clear();
663 }
664 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000665 }
666
Thomas Wouters477c8d52006-05-27 19:21:47 +0000667error:
668 Py_XDECREF(stepmethod);
669 Py_XDECREF(function_result);
670
Georg Brandldfd73442009-04-05 11:47:34 +0000671#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000673#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674}
675
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000676void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200678 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000679 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200680 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200681 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000682
Georg Brandldfd73442009-04-05 11:47:34 +0000683#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 PyGILState_STATE threadstate;
685
686 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000687#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000689 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
690 if (!*aggregate_instance) {
691 /* this branch is executed if there was an exception in the aggregate's
692 * __init__ */
693
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000695 }
696
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200697 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200698 Py_DECREF(*aggregate_instance);
699
700 ok = 0;
701 if (function_result) {
702 ok = _pysqlite_set_result(context, function_result) == 0;
703 Py_DECREF(function_result);
704 }
705 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000706 if (_enable_callback_tracebacks) {
707 PyErr_Print();
708 } else {
709 PyErr_Clear();
710 }
711 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000712 }
713
Thomas Wouters477c8d52006-05-27 19:21:47 +0000714error:
Georg Brandldfd73442009-04-05 11:47:34 +0000715#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000716 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000717#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200718 /* explicit return to avoid a compilation error if WITH_THREAD
719 is not defined */
720 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000721}
722
Gerhard Häringf9cee222010-03-05 15:20:03 +0000723static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000724{
725 PyObject* new_list;
726 PyObject* weakref;
727 int i;
728
729 /* we only need to do this once in a while */
730 if (self->created_statements++ < 200) {
731 return;
732 }
733
734 self->created_statements = 0;
735
736 new_list = PyList_New(0);
737 if (!new_list) {
738 return;
739 }
740
741 for (i = 0; i < PyList_Size(self->statements); i++) {
742 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000743 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000744 if (PyList_Append(new_list, weakref) != 0) {
745 Py_DECREF(new_list);
746 return;
747 }
748 }
749 }
750
751 Py_DECREF(self->statements);
752 self->statements = new_list;
753}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000754
Gerhard Häringf9cee222010-03-05 15:20:03 +0000755static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
756{
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_cursors++ < 200) {
763 return;
764 }
765
766 self->created_cursors = 0;
767
768 new_list = PyList_New(0);
769 if (!new_list) {
770 return;
771 }
772
773 for (i = 0; i < PyList_Size(self->cursors); i++) {
774 weakref = PyList_GetItem(self->cursors, i);
775 if (PyWeakref_GetObject(weakref) != Py_None) {
776 if (PyList_Append(new_list, weakref) != 0) {
777 Py_DECREF(new_list);
778 return;
779 }
780 }
781 }
782
783 Py_DECREF(self->cursors);
784 self->cursors = new_list;
785}
786
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000787PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000788{
789 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
790
791 PyObject* func;
792 char* name;
793 int narg;
794 int rc;
795
Gerhard Häringf9cee222010-03-05 15:20:03 +0000796 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
797 return NULL;
798 }
799
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000800 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
801 &name, &narg, &func))
802 {
803 return NULL;
804 }
805
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000806 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000807
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808 if (rc != SQLITE_OK) {
809 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000810 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000811 return NULL;
812 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000813 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
814 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815
Thomas Wouters477c8d52006-05-27 19:21:47 +0000816 Py_INCREF(Py_None);
817 return Py_None;
818 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819}
820
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000821PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000822{
823 PyObject* aggregate_class;
824
825 int n_arg;
826 char* name;
827 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
828 int rc;
829
Gerhard Häringf9cee222010-03-05 15:20:03 +0000830 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
831 return NULL;
832 }
833
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
835 kwlist, &name, &n_arg, &aggregate_class)) {
836 return NULL;
837 }
838
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000839 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 +0000840 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000842 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000843 return NULL;
844 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000845 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
846 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000847
848 Py_INCREF(Py_None);
849 return Py_None;
850 }
851}
852
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000853static 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 +0000854{
855 PyObject *ret;
856 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000857#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000858 PyGILState_STATE gilstate;
859
860 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000861#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000862 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
863
864 if (!ret) {
865 if (_enable_callback_tracebacks) {
866 PyErr_Print();
867 } else {
868 PyErr_Clear();
869 }
870
871 rc = SQLITE_DENY;
872 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000873 if (PyLong_Check(ret)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200874 rc = _PyLong_AsInt(ret);
875 if (rc == -1 && PyErr_Occurred())
876 rc = SQLITE_DENY;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000877 } else {
878 rc = SQLITE_DENY;
879 }
880 Py_DECREF(ret);
881 }
882
Georg Brandldfd73442009-04-05 11:47:34 +0000883#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000884 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000885#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000886 return rc;
887}
888
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000889static int _progress_handler(void* user_arg)
890{
891 int rc;
892 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000893#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000894 PyGILState_STATE gilstate;
895
896 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000897#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000898 ret = PyObject_CallFunction((PyObject*)user_arg, "");
899
900 if (!ret) {
901 if (_enable_callback_tracebacks) {
902 PyErr_Print();
903 } else {
904 PyErr_Clear();
905 }
906
Mark Dickinson934896d2009-02-21 20:59:32 +0000907 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000908 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000909 } else {
910 rc = (int)PyObject_IsTrue(ret);
911 Py_DECREF(ret);
912 }
913
Georg Brandldfd73442009-04-05 11:47:34 +0000914#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000915 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000916#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000917 return rc;
918}
919
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200920static void _trace_callback(void* user_arg, const char* statement_string)
921{
922 PyObject *py_statement = NULL;
923 PyObject *ret = NULL;
924
925#ifdef WITH_THREAD
926 PyGILState_STATE gilstate;
927
928 gilstate = PyGILState_Ensure();
929#endif
930 py_statement = PyUnicode_DecodeUTF8(statement_string,
931 strlen(statement_string), "replace");
932 if (py_statement) {
933 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
934 Py_DECREF(py_statement);
935 }
936
937 if (ret) {
938 Py_DECREF(ret);
939 } else {
940 if (_enable_callback_tracebacks) {
941 PyErr_Print();
942 } else {
943 PyErr_Clear();
944 }
945 }
946
947#ifdef WITH_THREAD
948 PyGILState_Release(gilstate);
949#endif
950}
951
Gerhard Häringf9cee222010-03-05 15:20:03 +0000952static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000953{
954 PyObject* authorizer_cb;
955
956 static char *kwlist[] = { "authorizer_callback", NULL };
957 int rc;
958
Gerhard Häringf9cee222010-03-05 15:20:03 +0000959 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
960 return NULL;
961 }
962
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000963 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
964 kwlist, &authorizer_cb)) {
965 return NULL;
966 }
967
968 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
969
970 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000971 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000972 return NULL;
973 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000974 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
975 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000976
977 Py_INCREF(Py_None);
978 return Py_None;
979 }
980}
981
Gerhard Häringf9cee222010-03-05 15:20:03 +0000982static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000983{
984 PyObject* progress_handler;
985 int n;
986
987 static char *kwlist[] = { "progress_handler", "n", NULL };
988
Gerhard Häringf9cee222010-03-05 15:20:03 +0000989 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
990 return NULL;
991 }
992
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000993 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
994 kwlist, &progress_handler, &n)) {
995 return NULL;
996 }
997
998 if (progress_handler == Py_None) {
999 /* None clears the progress handler previously set */
1000 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1001 } else {
1002 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001003 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1004 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001005 }
1006
1007 Py_INCREF(Py_None);
1008 return Py_None;
1009}
1010
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001011static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1012{
1013 PyObject* trace_callback;
1014
1015 static char *kwlist[] = { "trace_callback", NULL };
1016
1017 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1018 return NULL;
1019 }
1020
1021 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1022 kwlist, &trace_callback)) {
1023 return NULL;
1024 }
1025
1026 if (trace_callback == Py_None) {
1027 /* None clears the trace callback previously set */
1028 sqlite3_trace(self->db, 0, (void*)0);
1029 } else {
1030 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1031 return NULL;
1032 sqlite3_trace(self->db, _trace_callback, trace_callback);
1033 }
1034
1035 Py_INCREF(Py_None);
1036 return Py_None;
1037}
1038
Gerhard Häringf9cee222010-03-05 15:20:03 +00001039#ifdef HAVE_LOAD_EXTENSION
1040static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1041{
1042 int rc;
1043 int onoff;
1044
1045 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1046 return NULL;
1047 }
1048
1049 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1050 return NULL;
1051 }
1052
1053 rc = sqlite3_enable_load_extension(self->db, onoff);
1054
1055 if (rc != SQLITE_OK) {
1056 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1057 return NULL;
1058 } else {
1059 Py_INCREF(Py_None);
1060 return Py_None;
1061 }
1062}
1063
1064static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1065{
1066 int rc;
1067 char* extension_name;
1068 char* errmsg;
1069
1070 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1071 return NULL;
1072 }
1073
1074 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1075 return NULL;
1076 }
1077
1078 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1079 if (rc != 0) {
1080 PyErr_SetString(pysqlite_OperationalError, errmsg);
1081 return NULL;
1082 } else {
1083 Py_INCREF(Py_None);
1084 return Py_None;
1085 }
1086}
1087#endif
1088
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001089int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001090{
Georg Brandldfd73442009-04-05 11:47:34 +00001091#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092 if (self->check_same_thread) {
1093 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001094 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001095 "SQLite objects created in a thread can only be used in that same thread."
1096 "The object was created in thread id %ld and this is thread id %ld",
1097 self->thread_ident, PyThread_get_thread_ident());
1098 return 0;
1099 }
1100
1101 }
Georg Brandldfd73442009-04-05 11:47:34 +00001102#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001103 return 1;
1104}
1105
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001106static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001107{
1108 Py_INCREF(self->isolation_level);
1109 return self->isolation_level;
1110}
1111
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001112static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001113{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001114 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001115 return NULL;
1116 } else {
1117 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1118 }
1119}
1120
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001121static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001122{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001123 PyObject* res;
1124 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001125 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001126
1127 Py_XDECREF(self->isolation_level);
1128
1129 if (self->begin_statement) {
1130 PyMem_Free(self->begin_statement);
1131 self->begin_statement = NULL;
1132 }
1133
1134 if (isolation_level == Py_None) {
1135 Py_INCREF(Py_None);
1136 self->isolation_level = Py_None;
1137
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001138 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001139 if (!res) {
1140 return -1;
1141 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001142 Py_DECREF(res);
1143
1144 self->inTransaction = 0;
1145 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001146 const char *statement;
1147 Py_ssize_t size;
1148
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149 Py_INCREF(isolation_level);
1150 self->isolation_level = isolation_level;
1151
Georg Brandlceab6102007-11-25 00:45:05 +00001152 if (!begin_word) {
1153 begin_word = PyUnicode_FromString("BEGIN ");
1154 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155 }
Georg Brandlceab6102007-11-25 00:45:05 +00001156 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001157 if (!begin_statement) {
1158 return -1;
1159 }
1160
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001161 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001162 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001163 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001164 return -1;
1165 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001166 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001167 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001168 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001169 return -1;
1170 }
1171
Neal Norwitzefee9f52007-10-27 02:50:52 +00001172 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173 Py_DECREF(begin_statement);
1174 }
1175
1176 return 0;
1177}
1178
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001179PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001180{
1181 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001182 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001183 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001184 int rc;
1185
Gerhard Häringf9cee222010-03-05 15:20:03 +00001186 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1187 return NULL;
1188 }
1189
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001190 if (!PyArg_ParseTuple(args, "O", &sql)) {
1191 return NULL;
1192 }
1193
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001194 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001195
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001196 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197 if (!statement) {
1198 return NULL;
1199 }
1200
Victor Stinner0201f442010-03-13 03:28:34 +00001201 statement->db = NULL;
1202 statement->st = NULL;
1203 statement->sql = NULL;
1204 statement->in_use = 0;
1205 statement->in_weakreflist = NULL;
1206
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001207 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001208
1209 if (rc != SQLITE_OK) {
1210 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001211 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001212 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001213 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001214 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001215 (void)pysqlite_statement_reset(statement);
1216 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001217 }
1218
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001219 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001220 } else {
1221 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1222 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001223 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001224 goto error;
1225 }
1226
1227 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001228 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001229 goto error;
1230 }
1231
1232 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001233 }
1234
Thomas Wouters477c8d52006-05-27 19:21:47 +00001235error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001236 return (PyObject*)statement;
1237}
1238
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001239PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001240{
1241 PyObject* cursor = 0;
1242 PyObject* result = 0;
1243 PyObject* method = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001244 _Py_IDENTIFIER(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001245
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001246 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001247 if (!cursor) {
1248 goto error;
1249 }
1250
1251 method = PyObject_GetAttrString(cursor, "execute");
1252 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001253 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001254 goto error;
1255 }
1256
1257 result = PyObject_CallObject(method, args);
1258 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001259 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001260 }
1261
1262error:
1263 Py_XDECREF(result);
1264 Py_XDECREF(method);
1265
1266 return cursor;
1267}
1268
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001269PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270{
1271 PyObject* cursor = 0;
1272 PyObject* result = 0;
1273 PyObject* method = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001274 _Py_IDENTIFIER(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001276 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001277 if (!cursor) {
1278 goto error;
1279 }
1280
1281 method = PyObject_GetAttrString(cursor, "executemany");
1282 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001283 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 goto error;
1285 }
1286
1287 result = PyObject_CallObject(method, args);
1288 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001289 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001290 }
1291
1292error:
1293 Py_XDECREF(result);
1294 Py_XDECREF(method);
1295
1296 return cursor;
1297}
1298
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001299PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001300{
1301 PyObject* cursor = 0;
1302 PyObject* result = 0;
1303 PyObject* method = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001304 _Py_IDENTIFIER(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001305
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001306 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001307 if (!cursor) {
1308 goto error;
1309 }
1310
1311 method = PyObject_GetAttrString(cursor, "executescript");
1312 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001313 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001314 goto error;
1315 }
1316
1317 result = PyObject_CallObject(method, args);
1318 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001319 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001320 }
1321
1322error:
1323 Py_XDECREF(result);
1324 Py_XDECREF(method);
1325
1326 return cursor;
1327}
1328
1329/* ------------------------- COLLATION CODE ------------------------ */
1330
1331static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001332pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333 void* context,
1334 int text1_length, const void* text1_data,
1335 int text2_length, const void* text2_data)
1336{
1337 PyObject* callback = (PyObject*)context;
1338 PyObject* string1 = 0;
1339 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001340#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001341 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001342#endif
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;
Georg Brandldfd73442009-04-05 11:47:34 +00001346#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001347 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001348#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001349
1350 if (PyErr_Occurred()) {
1351 goto finally;
1352 }
1353
Guido van Rossum98297ee2007-11-06 21:34:58 +00001354 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1355 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001356
1357 if (!string1 || !string2) {
1358 goto finally; /* failed to allocate strings */
1359 }
1360
1361 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1362
1363 if (!retval) {
1364 /* execution failed */
1365 goto finally;
1366 }
1367
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001368 longval = PyLong_AsLongAndOverflow(retval, &result);
1369 if (longval == -1 && PyErr_Occurred()) {
1370 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001371 result = 0;
1372 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001373 else if (!result) {
1374 if (longval > 0)
1375 result = 1;
1376 else if (longval < 0)
1377 result = -1;
1378 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001379
1380finally:
1381 Py_XDECREF(string1);
1382 Py_XDECREF(string2);
1383 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001384#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001385 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001386#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001387 return result;
1388}
1389
1390static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001391pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001392{
1393 PyObject* retval = NULL;
1394
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001395 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001396 goto finally;
1397 }
1398
1399 sqlite3_interrupt(self->db);
1400
1401 Py_INCREF(Py_None);
1402 retval = Py_None;
1403
1404finally:
1405 return retval;
1406}
1407
Christian Heimesbbe741d2008-03-28 10:53:29 +00001408/* Function author: Paul Kippes <kippesp@gmail.com>
1409 * Class method of Connection to call the Python function _iterdump
1410 * of the sqlite3 module.
1411 */
1412static PyObject *
1413pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1414{
1415 PyObject* retval = NULL;
1416 PyObject* module = NULL;
1417 PyObject* module_dict;
1418 PyObject* pyfn_iterdump;
1419
1420 if (!pysqlite_check_connection(self)) {
1421 goto finally;
1422 }
1423
1424 module = PyImport_ImportModule(MODULE_NAME ".dump");
1425 if (!module) {
1426 goto finally;
1427 }
1428
1429 module_dict = PyModule_GetDict(module);
1430 if (!module_dict) {
1431 goto finally;
1432 }
1433
1434 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1435 if (!pyfn_iterdump) {
1436 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1437 goto finally;
1438 }
1439
1440 args = PyTuple_New(1);
1441 if (!args) {
1442 goto finally;
1443 }
1444 Py_INCREF(self);
1445 PyTuple_SetItem(args, 0, (PyObject*)self);
1446 retval = PyObject_CallObject(pyfn_iterdump, args);
1447
1448finally:
1449 Py_XDECREF(args);
1450 Py_XDECREF(module);
1451 return retval;
1452}
1453
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001454static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001455pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001456{
1457 PyObject* callable;
1458 PyObject* uppercase_name = 0;
1459 PyObject* name;
1460 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001461 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001462 _Py_IDENTIFIER(upper);
Victor Stinner35466c52010-04-22 11:23:23 +00001463 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001464 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001465 unsigned int kind;
1466 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001467
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001468 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001469 goto finally;
1470 }
1471
Gerhard Häring6d214562007-08-10 18:15:11 +00001472 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001473 goto finally;
1474 }
1475
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001476 uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477 if (!uppercase_name) {
1478 goto finally;
1479 }
1480
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001481 if (PyUnicode_READY(uppercase_name))
1482 goto finally;
1483 len = PyUnicode_GET_LENGTH(uppercase_name);
1484 kind = PyUnicode_KIND(uppercase_name);
1485 data = PyUnicode_DATA(uppercase_name);
1486 for (i=0; i<len; i++) {
1487 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1488 if ((ch >= '0' && ch <= '9')
1489 || (ch >= 'A' && ch <= 'Z')
1490 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001491 {
Victor Stinner35466c52010-04-22 11:23:23 +00001492 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001493 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001494 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001495 goto finally;
1496 }
1497 }
1498
Victor Stinner35466c52010-04-22 11:23:23 +00001499 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1500 if (!uppercase_name_str)
1501 goto finally;
1502
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001503 if (callable != Py_None && !PyCallable_Check(callable)) {
1504 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1505 goto finally;
1506 }
1507
1508 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001509 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1510 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001511 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001512 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1513 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001514 }
1515
1516 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001517 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001518 SQLITE_UTF8,
1519 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001520 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 if (rc != SQLITE_OK) {
1522 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001523 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001524 goto finally;
1525 }
1526
1527finally:
1528 Py_XDECREF(uppercase_name);
1529
1530 if (PyErr_Occurred()) {
1531 retval = NULL;
1532 } else {
1533 Py_INCREF(Py_None);
1534 retval = Py_None;
1535 }
1536
1537 return retval;
1538}
1539
Christian Heimesbbe741d2008-03-28 10:53:29 +00001540/* Called when the connection is used as a context manager. Returns itself as a
1541 * convenience to the caller. */
1542static PyObject *
1543pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1544{
1545 Py_INCREF(self);
1546 return (PyObject*)self;
1547}
1548
1549/** Called when the connection is used as a context manager. If there was any
1550 * exception, a rollback takes place; otherwise we commit. */
1551static PyObject *
1552pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1553{
1554 PyObject* exc_type, *exc_value, *exc_tb;
1555 char* method_name;
1556 PyObject* result;
1557
1558 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1559 return NULL;
1560 }
1561
1562 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1563 method_name = "commit";
1564 } else {
1565 method_name = "rollback";
1566 }
1567
1568 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1569 if (!result) {
1570 return NULL;
1571 }
1572 Py_DECREF(result);
1573
1574 Py_RETURN_FALSE;
1575}
1576
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001577static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001578PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001579
1580static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001581 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1582 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001583 {NULL}
1584};
1585
1586static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001587 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001588 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001589 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001590 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001591 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001592 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001593 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001594 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001595 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001596 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001597 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001598 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001599 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001600 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001601 #ifdef HAVE_LOAD_EXTENSION
1602 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1603 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1604 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1605 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1606 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001607 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1608 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001609 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1610 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001611 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001612 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001613 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001614 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001615 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001616 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001617 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001619 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001620 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001621 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001622 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001623 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1624 PyDoc_STR("For context manager. Non-standard.")},
1625 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1626 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001627 {NULL, NULL}
1628};
1629
1630static struct PyMemberDef connection_members[] =
1631{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001632 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1633 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1634 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1635 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1636 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1637 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1638 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1639 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1640 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1641 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001642 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1643 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001644 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001645 {NULL}
1646};
1647
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001648PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001649 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001650 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001651 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001652 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001653 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001654 0, /* tp_print */
1655 0, /* tp_getattr */
1656 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001657 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001658 0, /* tp_repr */
1659 0, /* tp_as_number */
1660 0, /* tp_as_sequence */
1661 0, /* tp_as_mapping */
1662 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001663 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001664 0, /* tp_str */
1665 0, /* tp_getattro */
1666 0, /* tp_setattro */
1667 0, /* tp_as_buffer */
1668 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1669 connection_doc, /* tp_doc */
1670 0, /* tp_traverse */
1671 0, /* tp_clear */
1672 0, /* tp_richcompare */
1673 0, /* tp_weaklistoffset */
1674 0, /* tp_iter */
1675 0, /* tp_iternext */
1676 connection_methods, /* tp_methods */
1677 connection_members, /* tp_members */
1678 connection_getset, /* tp_getset */
1679 0, /* tp_base */
1680 0, /* tp_dict */
1681 0, /* tp_descr_get */
1682 0, /* tp_descr_set */
1683 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001684 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001685 0, /* tp_alloc */
1686 0, /* tp_new */
1687 0 /* tp_free */
1688};
1689
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001690extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001691{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001692 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1693 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001694}