blob: 04bcdf2ce5c074030666aad2fa122c5cb97e2cb2 [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{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010063 static char *kwlist[] = {
64 "database", "timeout", "detect_types", "isolation_level",
65 "check_same_thread", "factory", "cached_statements", "uri",
66 NULL
67 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068
69 char* database;
70 int detect_types = 0;
71 PyObject* isolation_level = NULL;
72 PyObject* factory = NULL;
73 int check_same_thread = 1;
74 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010075 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000076 double timeout = 5.0;
77 int rc;
78
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010079 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOip", kwlist,
80 &database, &timeout, &detect_types,
81 &isolation_level, &check_same_thread,
82 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000083 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000084 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000085 }
86
Gerhard Häringf9cee222010-03-05 15:20:03 +000087 self->initialized = 1;
88
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000089 self->begin_statement = NULL;
90
91 self->statement_cache = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000092 self->statements = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +000093 self->cursors = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094
95 Py_INCREF(Py_None);
96 self->row_factory = Py_None;
97
98 Py_INCREF(&PyUnicode_Type);
99 self->text_factory = (PyObject*)&PyUnicode_Type;
100
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100101#ifdef SQLITE_OPEN_URI
102 Py_BEGIN_ALLOW_THREADS
103 rc = sqlite3_open_v2(database, &self->db,
104 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
105 (uri ? SQLITE_OPEN_URI : 0), NULL);
106#else
107 if (uri) {
108 PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
109 return -1;
110 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111 Py_BEGIN_ALLOW_THREADS
112 rc = sqlite3_open(database, &self->db);
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100113#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114 Py_END_ALLOW_THREADS
115
116 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000117 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 return -1;
119 }
120
121 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000122 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000123 if (!isolation_level) {
124 return -1;
125 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 } else {
127 Py_INCREF(isolation_level);
128 }
129 self->isolation_level = NULL;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000130 pysqlite_connection_set_isolation_level(self, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131 Py_DECREF(isolation_level);
132
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000133 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 if (PyErr_Occurred()) {
135 return -1;
136 }
137
Gerhard Häringf9cee222010-03-05 15:20:03 +0000138 self->created_statements = 0;
139 self->created_cursors = 0;
140
141 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000142 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000143 self->cursors = PyList_New(0);
144 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000145 return -1;
146 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000147
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 /* By default, the Cache class INCREFs the factory in its initializer, and
149 * decrefs it in its deallocator method. Since this would create a circular
150 * reference here, we're breaking it by decrementing self, and telling the
151 * cache class to not decref the factory (self) in its deallocator.
152 */
153 self->statement_cache->decref_factory = 0;
154 Py_DECREF(self);
155
156 self->inTransaction = 0;
157 self->detect_types = detect_types;
158 self->timeout = timeout;
159 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000160#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000162#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000163 self->check_same_thread = check_same_thread;
164
165 self->function_pinboard = PyDict_New();
166 if (!self->function_pinboard) {
167 return -1;
168 }
169
170 self->collations = PyDict_New();
171 if (!self->collations) {
172 return -1;
173 }
174
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000175 self->Warning = pysqlite_Warning;
176 self->Error = pysqlite_Error;
177 self->InterfaceError = pysqlite_InterfaceError;
178 self->DatabaseError = pysqlite_DatabaseError;
179 self->DataError = pysqlite_DataError;
180 self->OperationalError = pysqlite_OperationalError;
181 self->IntegrityError = pysqlite_IntegrityError;
182 self->InternalError = pysqlite_InternalError;
183 self->ProgrammingError = pysqlite_ProgrammingError;
184 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000185
186 return 0;
187}
188
Thomas Wouters477c8d52006-05-27 19:21:47 +0000189/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000190void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000192 pysqlite_Node* node;
193 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194
195 node = self->statement_cache->first;
196
197 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000198 statement = (pysqlite_Statement*)(node->data);
199 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200 node = node->next;
201 }
202
203 Py_DECREF(self->statement_cache);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000204 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205 Py_DECREF(self);
206 self->statement_cache->decref_factory = 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
249 sqlite3_close(self->db);
250 Py_END_ALLOW_THREADS
251 }
252
253 if (self->begin_statement) {
254 PyMem_Free(self->begin_statement);
255 }
256 Py_XDECREF(self->isolation_level);
257 Py_XDECREF(self->function_pinboard);
258 Py_XDECREF(self->row_factory);
259 Py_XDECREF(self->text_factory);
260 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000261 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000262 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263
Christian Heimes90aa7642007-12-19 02:45:37 +0000264 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000265}
266
Gerhard Häringf9cee222010-03-05 15:20:03 +0000267/*
268 * Registers a cursor with the connection.
269 *
270 * 0 => error; 1 => ok
271 */
272int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
273{
274 PyObject* weakref;
275
276 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
277 if (!weakref) {
278 goto error;
279 }
280
281 if (PyList_Append(connection->cursors, weakref) != 0) {
282 Py_CLEAR(weakref);
283 goto error;
284 }
285
286 Py_DECREF(weakref);
287
288 return 1;
289error:
290 return 0;
291}
292
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000293PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294{
295 static char *kwlist[] = {"factory", NULL, NULL};
296 PyObject* factory = NULL;
297 PyObject* cursor;
298
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000299 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
300 &factory)) {
301 return NULL;
302 }
303
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000304 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000305 return NULL;
306 }
307
308 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000309 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000310 }
311
312 cursor = PyObject_CallFunction(factory, "O", self);
313
Gerhard Häringf9cee222010-03-05 15:20:03 +0000314 _pysqlite_drop_unused_cursor_references(self);
315
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316 if (cursor && self->row_factory != Py_None) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000317 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 Py_INCREF(self->row_factory);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000319 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320 }
321
322 return cursor;
323}
324
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000325PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326{
327 int rc;
328
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000329 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 return NULL;
331 }
332
Gerhard Häringf9cee222010-03-05 15:20:03 +0000333 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000334
335 if (self->db) {
336 Py_BEGIN_ALLOW_THREADS
337 rc = sqlite3_close(self->db);
338 Py_END_ALLOW_THREADS
339
340 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000341 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000342 return NULL;
343 } else {
344 self->db = NULL;
345 }
346 }
347
348 Py_INCREF(Py_None);
349 return Py_None;
350}
351
352/*
353 * Checks if a connection object is usable (i. e. not closed).
354 *
355 * 0 => error; 1 => ok
356 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000357int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000359 if (!con->initialized) {
360 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
361 return 0;
362 }
363
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000365 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 return 0;
367 } else {
368 return 1;
369 }
370}
371
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000372PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000373{
374 int rc;
375 const char* tail;
376 sqlite3_stmt* statement;
377
378 Py_BEGIN_ALLOW_THREADS
379 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
380 Py_END_ALLOW_THREADS
381
382 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000383 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384 goto error;
385 }
386
Benjamin Petersond7b03282008-09-13 15:58:53 +0000387 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388 if (rc == SQLITE_DONE) {
389 self->inTransaction = 1;
390 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000391 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 }
393
394 Py_BEGIN_ALLOW_THREADS
395 rc = sqlite3_finalize(statement);
396 Py_END_ALLOW_THREADS
397
398 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000399 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 }
401
402error:
403 if (PyErr_Occurred()) {
404 return NULL;
405 } else {
406 Py_INCREF(Py_None);
407 return Py_None;
408 }
409}
410
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000411PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412{
413 int rc;
414 const char* tail;
415 sqlite3_stmt* statement;
416
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000417 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000418 return NULL;
419 }
420
421 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000422 pysqlite_do_all_statements(self, ACTION_RESET, 0);
423
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 Py_BEGIN_ALLOW_THREADS
425 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
426 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);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000433 if (rc == SQLITE_DONE) {
434 self->inTransaction = 0;
435 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000436 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 }
438
439 Py_BEGIN_ALLOW_THREADS
440 rc = sqlite3_finalize(statement);
441 Py_END_ALLOW_THREADS
442 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000443 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000444 }
445
446 }
447
448error:
449 if (PyErr_Occurred()) {
450 return NULL;
451 } else {
452 Py_INCREF(Py_None);
453 return Py_None;
454 }
455}
456
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000457PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000458{
459 int rc;
460 const char* tail;
461 sqlite3_stmt* statement;
462
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000463 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 return NULL;
465 }
466
467 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000468 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469
470 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000471 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000472 Py_END_ALLOW_THREADS
473 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000474 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 goto error;
476 }
477
Benjamin Petersond7b03282008-09-13 15:58:53 +0000478 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000479 if (rc == SQLITE_DONE) {
480 self->inTransaction = 0;
481 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000482 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483 }
484
485 Py_BEGIN_ALLOW_THREADS
486 rc = sqlite3_finalize(statement);
487 Py_END_ALLOW_THREADS
488 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000489 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490 }
491
492 }
493
494error:
495 if (PyErr_Occurred()) {
496 return NULL;
497 } else {
498 Py_INCREF(Py_None);
499 return Py_None;
500 }
501}
502
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200503static int
504_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200506 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000508 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200509 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
510 if (value == -1 && PyErr_Occurred())
511 return -1;
512 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513 } else if (PyFloat_Check(py_val)) {
514 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000515 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200516 const char *str = _PyUnicode_AsString(py_val);
517 if (str == NULL)
518 return -1;
519 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000520 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200521 const char* buffer;
522 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000523 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
524 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200525 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200527 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000528 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200529 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000530 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200531 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000532}
533
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000534PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535{
536 PyObject* args;
537 int i;
538 sqlite3_value* cur_value;
539 PyObject* cur_py_value;
540 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542
543 args = PyTuple_New(argc);
544 if (!args) {
545 return NULL;
546 }
547
548 for (i = 0; i < argc; i++) {
549 cur_value = argv[i];
550 switch (sqlite3_value_type(argv[i])) {
551 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200552 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000553 break;
554 case SQLITE_FLOAT:
555 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
556 break;
557 case SQLITE_TEXT:
558 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000559 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000560 /* TODO: have a way to show errors here */
561 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000562 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000563 Py_INCREF(Py_None);
564 cur_py_value = Py_None;
565 }
566 break;
567 case SQLITE_BLOB:
568 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000569 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000570 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000571 break;
572 case SQLITE_NULL:
573 default:
574 Py_INCREF(Py_None);
575 cur_py_value = Py_None;
576 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000577
578 if (!cur_py_value) {
579 Py_DECREF(args);
580 return NULL;
581 }
582
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000583 PyTuple_SetItem(args, i, cur_py_value);
584
585 }
586
587 return args;
588}
589
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000590void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000591{
592 PyObject* args;
593 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000594 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200595 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596
Georg Brandldfd73442009-04-05 11:47:34 +0000597#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598 PyGILState_STATE threadstate;
599
600 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000601#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602
603 py_func = (PyObject*)sqlite3_user_data(context);
604
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000605 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000606 if (args) {
607 py_retval = PyObject_CallObject(py_func, args);
608 Py_DECREF(args);
609 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200611 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200613 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000614 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200615 }
616 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617 if (_enable_callback_tracebacks) {
618 PyErr_Print();
619 } else {
620 PyErr_Clear();
621 }
622 _sqlite3_result_error(context, "user-defined function raised exception", -1);
623 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624
Georg Brandldfd73442009-04-05 11:47:34 +0000625#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000626 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000627#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628}
629
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000630static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000631{
632 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000633 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634 PyObject* aggregate_class;
635 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000636 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637
Georg Brandldfd73442009-04-05 11:47:34 +0000638#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000639 PyGILState_STATE threadstate;
640
641 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000642#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643
644 aggregate_class = (PyObject*)sqlite3_user_data(context);
645
646 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
647
648 if (*aggregate_instance == 0) {
649 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
650
Thomas Wouters477c8d52006-05-27 19:21:47 +0000651 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000653 if (_enable_callback_tracebacks) {
654 PyErr_Print();
655 } else {
656 PyErr_Clear();
657 }
658 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660 }
661 }
662
663 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664 if (!stepmethod) {
665 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666 }
667
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000668 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000669 if (!args) {
670 goto error;
671 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672
673 function_result = PyObject_CallObject(stepmethod, args);
674 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675
Thomas Wouters477c8d52006-05-27 19:21:47 +0000676 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000677 if (_enable_callback_tracebacks) {
678 PyErr_Print();
679 } else {
680 PyErr_Clear();
681 }
682 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683 }
684
Thomas Wouters477c8d52006-05-27 19:21:47 +0000685error:
686 Py_XDECREF(stepmethod);
687 Py_XDECREF(function_result);
688
Georg Brandldfd73442009-04-05 11:47:34 +0000689#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000691#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692}
693
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000694void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000695{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200696 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000697 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200698 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200699 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000700
Georg Brandldfd73442009-04-05 11:47:34 +0000701#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000702 PyGILState_STATE threadstate;
703
704 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000705#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
708 if (!*aggregate_instance) {
709 /* this branch is executed if there was an exception in the aggregate's
710 * __init__ */
711
Thomas Wouters477c8d52006-05-27 19:21:47 +0000712 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713 }
714
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200715 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200716 Py_DECREF(*aggregate_instance);
717
718 ok = 0;
719 if (function_result) {
720 ok = _pysqlite_set_result(context, function_result) == 0;
721 Py_DECREF(function_result);
722 }
723 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000724 if (_enable_callback_tracebacks) {
725 PyErr_Print();
726 } else {
727 PyErr_Clear();
728 }
729 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000730 }
731
Thomas Wouters477c8d52006-05-27 19:21:47 +0000732error:
Georg Brandldfd73442009-04-05 11:47:34 +0000733#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000735#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000736}
737
Gerhard Häringf9cee222010-03-05 15:20:03 +0000738static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000739{
740 PyObject* new_list;
741 PyObject* weakref;
742 int i;
743
744 /* we only need to do this once in a while */
745 if (self->created_statements++ < 200) {
746 return;
747 }
748
749 self->created_statements = 0;
750
751 new_list = PyList_New(0);
752 if (!new_list) {
753 return;
754 }
755
756 for (i = 0; i < PyList_Size(self->statements); i++) {
757 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000758 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000759 if (PyList_Append(new_list, weakref) != 0) {
760 Py_DECREF(new_list);
761 return;
762 }
763 }
764 }
765
766 Py_DECREF(self->statements);
767 self->statements = new_list;
768}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000769
Gerhard Häringf9cee222010-03-05 15:20:03 +0000770static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
771{
772 PyObject* new_list;
773 PyObject* weakref;
774 int i;
775
776 /* we only need to do this once in a while */
777 if (self->created_cursors++ < 200) {
778 return;
779 }
780
781 self->created_cursors = 0;
782
783 new_list = PyList_New(0);
784 if (!new_list) {
785 return;
786 }
787
788 for (i = 0; i < PyList_Size(self->cursors); i++) {
789 weakref = PyList_GetItem(self->cursors, i);
790 if (PyWeakref_GetObject(weakref) != Py_None) {
791 if (PyList_Append(new_list, weakref) != 0) {
792 Py_DECREF(new_list);
793 return;
794 }
795 }
796 }
797
798 Py_DECREF(self->cursors);
799 self->cursors = new_list;
800}
801
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000802PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000803{
804 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
805
806 PyObject* func;
807 char* name;
808 int narg;
809 int rc;
810
Gerhard Häringf9cee222010-03-05 15:20:03 +0000811 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
812 return NULL;
813 }
814
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
816 &name, &narg, &func))
817 {
818 return NULL;
819 }
820
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000821 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000822
Thomas Wouters477c8d52006-05-27 19:21:47 +0000823 if (rc != SQLITE_OK) {
824 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000825 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000826 return NULL;
827 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000828 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
829 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000830
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831 Py_INCREF(Py_None);
832 return Py_None;
833 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834}
835
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000836PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837{
838 PyObject* aggregate_class;
839
840 int n_arg;
841 char* name;
842 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
843 int rc;
844
Gerhard Häringf9cee222010-03-05 15:20:03 +0000845 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
846 return NULL;
847 }
848
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000849 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
850 kwlist, &name, &n_arg, &aggregate_class)) {
851 return NULL;
852 }
853
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000854 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 +0000855 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000856 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000857 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858 return NULL;
859 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000860 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
861 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862
863 Py_INCREF(Py_None);
864 return Py_None;
865 }
866}
867
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000868static 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 +0000869{
870 PyObject *ret;
871 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000872#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000873 PyGILState_STATE gilstate;
874
875 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000876#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000877 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
878
879 if (!ret) {
880 if (_enable_callback_tracebacks) {
881 PyErr_Print();
882 } else {
883 PyErr_Clear();
884 }
885
886 rc = SQLITE_DENY;
887 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000888 if (PyLong_Check(ret)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200889 rc = _PyLong_AsInt(ret);
890 if (rc == -1 && PyErr_Occurred())
891 rc = SQLITE_DENY;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000892 } else {
893 rc = SQLITE_DENY;
894 }
895 Py_DECREF(ret);
896 }
897
Georg Brandldfd73442009-04-05 11:47:34 +0000898#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000899 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000900#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000901 return rc;
902}
903
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000904static int _progress_handler(void* user_arg)
905{
906 int rc;
907 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000908#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000909 PyGILState_STATE gilstate;
910
911 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000912#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000913 ret = PyObject_CallFunction((PyObject*)user_arg, "");
914
915 if (!ret) {
916 if (_enable_callback_tracebacks) {
917 PyErr_Print();
918 } else {
919 PyErr_Clear();
920 }
921
Mark Dickinson934896d2009-02-21 20:59:32 +0000922 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000923 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000924 } else {
925 rc = (int)PyObject_IsTrue(ret);
926 Py_DECREF(ret);
927 }
928
Georg Brandldfd73442009-04-05 11:47:34 +0000929#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000930 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000931#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000932 return rc;
933}
934
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200935static void _trace_callback(void* user_arg, const char* statement_string)
936{
937 PyObject *py_statement = NULL;
938 PyObject *ret = NULL;
939
940#ifdef WITH_THREAD
941 PyGILState_STATE gilstate;
942
943 gilstate = PyGILState_Ensure();
944#endif
945 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
962#ifdef WITH_THREAD
963 PyGILState_Release(gilstate);
964#endif
965}
966
Gerhard Häringf9cee222010-03-05 15:20:03 +0000967static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000968{
969 PyObject* authorizer_cb;
970
971 static char *kwlist[] = { "authorizer_callback", NULL };
972 int rc;
973
Gerhard Häringf9cee222010-03-05 15:20:03 +0000974 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
975 return NULL;
976 }
977
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000978 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
979 kwlist, &authorizer_cb)) {
980 return NULL;
981 }
982
983 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
984
985 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000986 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000987 return NULL;
988 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000989 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
990 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000991
992 Py_INCREF(Py_None);
993 return Py_None;
994 }
995}
996
Gerhard Häringf9cee222010-03-05 15:20:03 +0000997static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000998{
999 PyObject* progress_handler;
1000 int n;
1001
1002 static char *kwlist[] = { "progress_handler", "n", NULL };
1003
Gerhard Häringf9cee222010-03-05 15:20:03 +00001004 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1005 return NULL;
1006 }
1007
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001008 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1009 kwlist, &progress_handler, &n)) {
1010 return NULL;
1011 }
1012
1013 if (progress_handler == Py_None) {
1014 /* None clears the progress handler previously set */
1015 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1016 } else {
1017 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001018 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1019 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001020 }
1021
1022 Py_INCREF(Py_None);
1023 return Py_None;
1024}
1025
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001026static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1027{
1028 PyObject* trace_callback;
1029
1030 static char *kwlist[] = { "trace_callback", NULL };
1031
1032 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1033 return NULL;
1034 }
1035
1036 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1037 kwlist, &trace_callback)) {
1038 return NULL;
1039 }
1040
1041 if (trace_callback == Py_None) {
1042 /* None clears the trace callback previously set */
1043 sqlite3_trace(self->db, 0, (void*)0);
1044 } else {
1045 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1046 return NULL;
1047 sqlite3_trace(self->db, _trace_callback, trace_callback);
1048 }
1049
1050 Py_INCREF(Py_None);
1051 return Py_None;
1052}
1053
Gerhard Häringf9cee222010-03-05 15:20:03 +00001054#ifdef HAVE_LOAD_EXTENSION
1055static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1056{
1057 int rc;
1058 int onoff;
1059
1060 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1061 return NULL;
1062 }
1063
1064 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1065 return NULL;
1066 }
1067
1068 rc = sqlite3_enable_load_extension(self->db, onoff);
1069
1070 if (rc != SQLITE_OK) {
1071 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1072 return NULL;
1073 } else {
1074 Py_INCREF(Py_None);
1075 return Py_None;
1076 }
1077}
1078
1079static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1080{
1081 int rc;
1082 char* extension_name;
1083 char* errmsg;
1084
1085 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1086 return NULL;
1087 }
1088
1089 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1090 return NULL;
1091 }
1092
1093 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1094 if (rc != 0) {
1095 PyErr_SetString(pysqlite_OperationalError, errmsg);
1096 return NULL;
1097 } else {
1098 Py_INCREF(Py_None);
1099 return Py_None;
1100 }
1101}
1102#endif
1103
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001104int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001105{
Georg Brandldfd73442009-04-05 11:47:34 +00001106#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001107 if (self->check_same_thread) {
1108 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001109 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001110 "SQLite objects created in a thread can only be used in that same thread."
1111 "The object was created in thread id %ld and this is thread id %ld",
1112 self->thread_ident, PyThread_get_thread_ident());
1113 return 0;
1114 }
1115
1116 }
Georg Brandldfd73442009-04-05 11:47:34 +00001117#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001118 return 1;
1119}
1120
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001121static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001122{
1123 Py_INCREF(self->isolation_level);
1124 return self->isolation_level;
1125}
1126
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001127static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001128{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001129 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001130 return NULL;
1131 } else {
1132 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1133 }
1134}
1135
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001136static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001137{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001138 PyObject* res;
1139 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001140 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001141
1142 Py_XDECREF(self->isolation_level);
1143
1144 if (self->begin_statement) {
1145 PyMem_Free(self->begin_statement);
1146 self->begin_statement = NULL;
1147 }
1148
1149 if (isolation_level == Py_None) {
1150 Py_INCREF(Py_None);
1151 self->isolation_level = Py_None;
1152
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001153 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001154 if (!res) {
1155 return -1;
1156 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001157 Py_DECREF(res);
1158
1159 self->inTransaction = 0;
1160 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001161 const char *statement;
1162 Py_ssize_t size;
1163
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001164 Py_INCREF(isolation_level);
1165 self->isolation_level = isolation_level;
1166
Georg Brandlceab6102007-11-25 00:45:05 +00001167 if (!begin_word) {
1168 begin_word = PyUnicode_FromString("BEGIN ");
1169 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170 }
Georg Brandlceab6102007-11-25 00:45:05 +00001171 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001172 if (!begin_statement) {
1173 return -1;
1174 }
1175
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001176 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001177 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001178 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001179 return -1;
1180 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001181 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001182 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001183 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001184 return -1;
1185 }
1186
Neal Norwitzefee9f52007-10-27 02:50:52 +00001187 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001188 Py_DECREF(begin_statement);
1189 }
1190
1191 return 0;
1192}
1193
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001194PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001195{
1196 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001197 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001198 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001199 int rc;
1200
Gerhard Häringf9cee222010-03-05 15:20:03 +00001201 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1202 return NULL;
1203 }
1204
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001205 if (!PyArg_ParseTuple(args, "O", &sql)) {
1206 return NULL;
1207 }
1208
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001209 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001210
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001211 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001212 if (!statement) {
1213 return NULL;
1214 }
1215
Victor Stinner0201f442010-03-13 03:28:34 +00001216 statement->db = NULL;
1217 statement->st = NULL;
1218 statement->sql = NULL;
1219 statement->in_use = 0;
1220 statement->in_weakreflist = NULL;
1221
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001222 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001223
1224 if (rc != SQLITE_OK) {
1225 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001226 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001228 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001229 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001230 (void)pysqlite_statement_reset(statement);
1231 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001232 }
1233
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001234 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001235 } else {
1236 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1237 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001238 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001239 goto error;
1240 }
1241
1242 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001243 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001244 goto error;
1245 }
1246
1247 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001248 }
1249
Thomas Wouters477c8d52006-05-27 19:21:47 +00001250error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001251 return (PyObject*)statement;
1252}
1253
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001254PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001255{
1256 PyObject* cursor = 0;
1257 PyObject* result = 0;
1258 PyObject* method = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001259 _Py_IDENTIFIER(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001260
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001261 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001262 if (!cursor) {
1263 goto error;
1264 }
1265
1266 method = PyObject_GetAttrString(cursor, "execute");
1267 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001268 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001269 goto error;
1270 }
1271
1272 result = PyObject_CallObject(method, args);
1273 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001274 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275 }
1276
1277error:
1278 Py_XDECREF(result);
1279 Py_XDECREF(method);
1280
1281 return cursor;
1282}
1283
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001284PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001285{
1286 PyObject* cursor = 0;
1287 PyObject* result = 0;
1288 PyObject* method = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001289 _Py_IDENTIFIER(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001290
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001291 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001292 if (!cursor) {
1293 goto error;
1294 }
1295
1296 method = PyObject_GetAttrString(cursor, "executemany");
1297 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001298 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001299 goto error;
1300 }
1301
1302 result = PyObject_CallObject(method, args);
1303 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001304 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001305 }
1306
1307error:
1308 Py_XDECREF(result);
1309 Py_XDECREF(method);
1310
1311 return cursor;
1312}
1313
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001314PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001315{
1316 PyObject* cursor = 0;
1317 PyObject* result = 0;
1318 PyObject* method = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001319 _Py_IDENTIFIER(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001320
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001321 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001322 if (!cursor) {
1323 goto error;
1324 }
1325
1326 method = PyObject_GetAttrString(cursor, "executescript");
1327 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001328 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001329 goto error;
1330 }
1331
1332 result = PyObject_CallObject(method, args);
1333 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001334 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001335 }
1336
1337error:
1338 Py_XDECREF(result);
1339 Py_XDECREF(method);
1340
1341 return cursor;
1342}
1343
1344/* ------------------------- COLLATION CODE ------------------------ */
1345
1346static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001347pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001348 void* context,
1349 int text1_length, const void* text1_data,
1350 int text2_length, const void* text2_data)
1351{
1352 PyObject* callback = (PyObject*)context;
1353 PyObject* string1 = 0;
1354 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001355#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001356 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001357#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001358 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001359 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001360 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001361#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001362 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001363#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001364
1365 if (PyErr_Occurred()) {
1366 goto finally;
1367 }
1368
Guido van Rossum98297ee2007-11-06 21:34:58 +00001369 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1370 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001371
1372 if (!string1 || !string2) {
1373 goto finally; /* failed to allocate strings */
1374 }
1375
1376 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1377
1378 if (!retval) {
1379 /* execution failed */
1380 goto finally;
1381 }
1382
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001383 longval = PyLong_AsLongAndOverflow(retval, &result);
1384 if (longval == -1 && PyErr_Occurred()) {
1385 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001386 result = 0;
1387 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001388 else if (!result) {
1389 if (longval > 0)
1390 result = 1;
1391 else if (longval < 0)
1392 result = -1;
1393 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001394
1395finally:
1396 Py_XDECREF(string1);
1397 Py_XDECREF(string2);
1398 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001399#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001400 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001401#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001402 return result;
1403}
1404
1405static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001406pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001407{
1408 PyObject* retval = NULL;
1409
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001410 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001411 goto finally;
1412 }
1413
1414 sqlite3_interrupt(self->db);
1415
1416 Py_INCREF(Py_None);
1417 retval = Py_None;
1418
1419finally:
1420 return retval;
1421}
1422
Christian Heimesbbe741d2008-03-28 10:53:29 +00001423/* Function author: Paul Kippes <kippesp@gmail.com>
1424 * Class method of Connection to call the Python function _iterdump
1425 * of the sqlite3 module.
1426 */
1427static PyObject *
1428pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1429{
1430 PyObject* retval = NULL;
1431 PyObject* module = NULL;
1432 PyObject* module_dict;
1433 PyObject* pyfn_iterdump;
1434
1435 if (!pysqlite_check_connection(self)) {
1436 goto finally;
1437 }
1438
1439 module = PyImport_ImportModule(MODULE_NAME ".dump");
1440 if (!module) {
1441 goto finally;
1442 }
1443
1444 module_dict = PyModule_GetDict(module);
1445 if (!module_dict) {
1446 goto finally;
1447 }
1448
1449 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1450 if (!pyfn_iterdump) {
1451 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1452 goto finally;
1453 }
1454
1455 args = PyTuple_New(1);
1456 if (!args) {
1457 goto finally;
1458 }
1459 Py_INCREF(self);
1460 PyTuple_SetItem(args, 0, (PyObject*)self);
1461 retval = PyObject_CallObject(pyfn_iterdump, args);
1462
1463finally:
1464 Py_XDECREF(args);
1465 Py_XDECREF(module);
1466 return retval;
1467}
1468
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001469static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001470pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001471{
1472 PyObject* callable;
1473 PyObject* uppercase_name = 0;
1474 PyObject* name;
1475 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001476 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001477 _Py_IDENTIFIER(upper);
Victor Stinner35466c52010-04-22 11:23:23 +00001478 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001479 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001480 unsigned int kind;
1481 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001482
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001483 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001484 goto finally;
1485 }
1486
Gerhard Häring6d214562007-08-10 18:15:11 +00001487 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001488 goto finally;
1489 }
1490
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001491 uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001492 if (!uppercase_name) {
1493 goto finally;
1494 }
1495
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001496 if (PyUnicode_READY(uppercase_name))
1497 goto finally;
1498 len = PyUnicode_GET_LENGTH(uppercase_name);
1499 kind = PyUnicode_KIND(uppercase_name);
1500 data = PyUnicode_DATA(uppercase_name);
1501 for (i=0; i<len; i++) {
1502 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1503 if ((ch >= '0' && ch <= '9')
1504 || (ch >= 'A' && ch <= 'Z')
1505 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001506 {
Victor Stinner35466c52010-04-22 11:23:23 +00001507 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001508 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001509 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001510 goto finally;
1511 }
1512 }
1513
Victor Stinner35466c52010-04-22 11:23:23 +00001514 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1515 if (!uppercase_name_str)
1516 goto finally;
1517
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001518 if (callable != Py_None && !PyCallable_Check(callable)) {
1519 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1520 goto finally;
1521 }
1522
1523 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001524 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1525 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001526 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001527 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1528 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001529 }
1530
1531 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001532 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001533 SQLITE_UTF8,
1534 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001535 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001536 if (rc != SQLITE_OK) {
1537 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001538 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001539 goto finally;
1540 }
1541
1542finally:
1543 Py_XDECREF(uppercase_name);
1544
1545 if (PyErr_Occurred()) {
1546 retval = NULL;
1547 } else {
1548 Py_INCREF(Py_None);
1549 retval = Py_None;
1550 }
1551
1552 return retval;
1553}
1554
Christian Heimesbbe741d2008-03-28 10:53:29 +00001555/* Called when the connection is used as a context manager. Returns itself as a
1556 * convenience to the caller. */
1557static PyObject *
1558pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1559{
1560 Py_INCREF(self);
1561 return (PyObject*)self;
1562}
1563
1564/** Called when the connection is used as a context manager. If there was any
1565 * exception, a rollback takes place; otherwise we commit. */
1566static PyObject *
1567pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1568{
1569 PyObject* exc_type, *exc_value, *exc_tb;
1570 char* method_name;
1571 PyObject* result;
1572
1573 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1574 return NULL;
1575 }
1576
1577 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1578 method_name = "commit";
1579 } else {
1580 method_name = "rollback";
1581 }
1582
1583 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1584 if (!result) {
1585 return NULL;
1586 }
1587 Py_DECREF(result);
1588
1589 Py_RETURN_FALSE;
1590}
1591
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001592static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001593PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001594
1595static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001596 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1597 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001598 {NULL}
1599};
1600
1601static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001602 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001603 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001604 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001605 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001606 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001607 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001608 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001609 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001610 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001611 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001612 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001613 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001614 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001615 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001616 #ifdef HAVE_LOAD_EXTENSION
1617 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1618 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1619 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1620 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1621 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001622 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1623 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001624 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1625 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001626 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001627 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001628 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001629 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001630 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001631 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001632 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001633 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001634 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001635 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001636 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001637 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001638 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1639 PyDoc_STR("For context manager. Non-standard.")},
1640 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1641 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001642 {NULL, NULL}
1643};
1644
1645static struct PyMemberDef connection_members[] =
1646{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001647 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1648 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1649 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1650 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1651 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1652 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1653 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1654 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1655 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1656 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001657 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1658 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001659 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001660 {NULL}
1661};
1662
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001663PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001664 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001665 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001666 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001667 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001668 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001669 0, /* tp_print */
1670 0, /* tp_getattr */
1671 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001672 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001673 0, /* tp_repr */
1674 0, /* tp_as_number */
1675 0, /* tp_as_sequence */
1676 0, /* tp_as_mapping */
1677 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001678 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001679 0, /* tp_str */
1680 0, /* tp_getattro */
1681 0, /* tp_setattro */
1682 0, /* tp_as_buffer */
1683 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1684 connection_doc, /* tp_doc */
1685 0, /* tp_traverse */
1686 0, /* tp_clear */
1687 0, /* tp_richcompare */
1688 0, /* tp_weaklistoffset */
1689 0, /* tp_iter */
1690 0, /* tp_iternext */
1691 connection_methods, /* tp_methods */
1692 connection_members, /* tp_members */
1693 connection_getset, /* tp_getset */
1694 0, /* tp_base */
1695 0, /* tp_dict */
1696 0, /* tp_descr_get */
1697 0, /* tp_descr_set */
1698 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001699 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001700 0, /* tp_alloc */
1701 0, /* tp_new */
1702 0 /* tp_free */
1703};
1704
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001705extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001706{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001707 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1708 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001709}