blob: 310a27c42a2a3ac8d2dc2eb069a2697214920d9b [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
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000485void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000486{
487 long longval;
488 const char* buffer;
489 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490
Thomas Wouters477c8d52006-05-27 19:21:47 +0000491 if ((!py_val) || PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492 sqlite3_result_null(context);
493 } else if (py_val == Py_None) {
494 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000495 } else if (PyLong_Check(py_val)) {
496 longval = PyLong_AsLong(py_val);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000497 sqlite3_result_int64(context, (PY_LONG_LONG)longval);
498 } else if (PyFloat_Check(py_val)) {
499 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000500 } else if (PyUnicode_Check(py_val)) {
Victor Stinner86999502010-05-19 01:27:23 +0000501 char *str = _PyUnicode_AsString(py_val);
502 if (str != NULL)
503 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000504 } else if (PyObject_CheckBuffer(py_val)) {
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");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000507 } else {
508 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000510 } else {
511 /* TODO: raise error */
512 }
513}
514
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000515PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000516{
517 PyObject* args;
518 int i;
519 sqlite3_value* cur_value;
520 PyObject* cur_py_value;
521 const char* val_str;
522 PY_LONG_LONG val_int;
523 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:
534 val_int = sqlite3_value_int64(cur_value);
Christian Heimes217cfd12007-12-02 14:31:20 +0000535 cur_py_value = PyLong_FromLong((long)val_int);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536 break;
537 case SQLITE_FLOAT:
538 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
539 break;
540 case SQLITE_TEXT:
541 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000542 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 /* TODO: have a way to show errors here */
544 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000545 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546 Py_INCREF(Py_None);
547 cur_py_value = Py_None;
548 }
549 break;
550 case SQLITE_BLOB:
551 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000552 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000553 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554 break;
555 case SQLITE_NULL:
556 default:
557 Py_INCREF(Py_None);
558 cur_py_value = Py_None;
559 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000560
561 if (!cur_py_value) {
562 Py_DECREF(args);
563 return NULL;
564 }
565
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000566 PyTuple_SetItem(args, i, cur_py_value);
567
568 }
569
570 return args;
571}
572
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000573void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000574{
575 PyObject* args;
576 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000577 PyObject* py_retval = NULL;
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
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593 if (py_retval) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000594 _pysqlite_set_result(context, py_retval);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000595 Py_DECREF(py_retval);
596 } else {
597 if (_enable_callback_tracebacks) {
598 PyErr_Print();
599 } else {
600 PyErr_Clear();
601 }
602 _sqlite3_result_error(context, "user-defined function raised exception", -1);
603 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604
Georg Brandldfd73442009-04-05 11:47:34 +0000605#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000607#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000608}
609
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000610static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611{
612 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000613 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000614 PyObject* aggregate_class;
615 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000616 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000617
Georg Brandldfd73442009-04-05 11:47:34 +0000618#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000619 PyGILState_STATE threadstate;
620
621 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000622#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623
624 aggregate_class = (PyObject*)sqlite3_user_data(context);
625
626 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
627
628 if (*aggregate_instance == 0) {
629 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
630
Thomas Wouters477c8d52006-05-27 19:21:47 +0000631 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000632 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000633 if (_enable_callback_tracebacks) {
634 PyErr_Print();
635 } else {
636 PyErr_Clear();
637 }
638 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000639 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000640 }
641 }
642
643 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644 if (!stepmethod) {
645 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646 }
647
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000648 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649 if (!args) {
650 goto error;
651 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652
653 function_result = PyObject_CallObject(stepmethod, args);
654 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000657 if (_enable_callback_tracebacks) {
658 PyErr_Print();
659 } else {
660 PyErr_Clear();
661 }
662 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000663 }
664
Thomas Wouters477c8d52006-05-27 19:21:47 +0000665error:
666 Py_XDECREF(stepmethod);
667 Py_XDECREF(function_result);
668
Georg Brandldfd73442009-04-05 11:47:34 +0000669#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000671#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672}
673
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000674void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000676 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677 PyObject** aggregate_instance;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678
Georg Brandldfd73442009-04-05 11:47:34 +0000679#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 PyGILState_STATE threadstate;
681
682 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000683#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000685 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
686 if (!*aggregate_instance) {
687 /* this branch is executed if there was an exception in the aggregate's
688 * __init__ */
689
Thomas Wouters477c8d52006-05-27 19:21:47 +0000690 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000691 }
692
Thomas Wouters477c8d52006-05-27 19:21:47 +0000693 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
694 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000695 if (_enable_callback_tracebacks) {
696 PyErr_Print();
697 } else {
698 PyErr_Clear();
699 }
700 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
701 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000702 _pysqlite_set_result(context, function_result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000703 }
704
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706 Py_XDECREF(*aggregate_instance);
707 Py_XDECREF(function_result);
708
Georg Brandldfd73442009-04-05 11:47:34 +0000709#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000711#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000712}
713
Gerhard Häringf9cee222010-03-05 15:20:03 +0000714static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000715{
716 PyObject* new_list;
717 PyObject* weakref;
718 int i;
719
720 /* we only need to do this once in a while */
721 if (self->created_statements++ < 200) {
722 return;
723 }
724
725 self->created_statements = 0;
726
727 new_list = PyList_New(0);
728 if (!new_list) {
729 return;
730 }
731
732 for (i = 0; i < PyList_Size(self->statements); i++) {
733 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000734 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000735 if (PyList_Append(new_list, weakref) != 0) {
736 Py_DECREF(new_list);
737 return;
738 }
739 }
740 }
741
742 Py_DECREF(self->statements);
743 self->statements = new_list;
744}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000745
Gerhard Häringf9cee222010-03-05 15:20:03 +0000746static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
747{
748 PyObject* new_list;
749 PyObject* weakref;
750 int i;
751
752 /* we only need to do this once in a while */
753 if (self->created_cursors++ < 200) {
754 return;
755 }
756
757 self->created_cursors = 0;
758
759 new_list = PyList_New(0);
760 if (!new_list) {
761 return;
762 }
763
764 for (i = 0; i < PyList_Size(self->cursors); i++) {
765 weakref = PyList_GetItem(self->cursors, i);
766 if (PyWeakref_GetObject(weakref) != Py_None) {
767 if (PyList_Append(new_list, weakref) != 0) {
768 Py_DECREF(new_list);
769 return;
770 }
771 }
772 }
773
774 Py_DECREF(self->cursors);
775 self->cursors = new_list;
776}
777
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000778PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000779{
780 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
781
782 PyObject* func;
783 char* name;
784 int narg;
785 int rc;
786
Gerhard Häringf9cee222010-03-05 15:20:03 +0000787 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
788 return NULL;
789 }
790
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
792 &name, &narg, &func))
793 {
794 return NULL;
795 }
796
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000797 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799 if (rc != SQLITE_OK) {
800 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000801 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802 return NULL;
803 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000804 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
805 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000806
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807 Py_INCREF(Py_None);
808 return Py_None;
809 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000810}
811
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000812PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813{
814 PyObject* aggregate_class;
815
816 int n_arg;
817 char* name;
818 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
819 int rc;
820
Gerhard Häringf9cee222010-03-05 15:20:03 +0000821 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
822 return NULL;
823 }
824
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
826 kwlist, &name, &n_arg, &aggregate_class)) {
827 return NULL;
828 }
829
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000830 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 +0000831 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000833 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834 return NULL;
835 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000836 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
837 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838
839 Py_INCREF(Py_None);
840 return Py_None;
841 }
842}
843
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000844static 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 +0000845{
846 PyObject *ret;
847 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000848#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000849 PyGILState_STATE gilstate;
850
851 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000852#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000853 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
854
855 if (!ret) {
856 if (_enable_callback_tracebacks) {
857 PyErr_Print();
858 } else {
859 PyErr_Clear();
860 }
861
862 rc = SQLITE_DENY;
863 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000864 if (PyLong_Check(ret)) {
865 rc = (int)PyLong_AsLong(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000866 } else {
867 rc = SQLITE_DENY;
868 }
869 Py_DECREF(ret);
870 }
871
Georg Brandldfd73442009-04-05 11:47:34 +0000872#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000873 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000874#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000875 return rc;
876}
877
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000878static int _progress_handler(void* user_arg)
879{
880 int rc;
881 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000882#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000883 PyGILState_STATE gilstate;
884
885 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000886#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000887 ret = PyObject_CallFunction((PyObject*)user_arg, "");
888
889 if (!ret) {
890 if (_enable_callback_tracebacks) {
891 PyErr_Print();
892 } else {
893 PyErr_Clear();
894 }
895
Mark Dickinson934896d2009-02-21 20:59:32 +0000896 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000897 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000898 } else {
899 rc = (int)PyObject_IsTrue(ret);
900 Py_DECREF(ret);
901 }
902
Georg Brandldfd73442009-04-05 11:47:34 +0000903#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000904 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000905#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000906 return rc;
907}
908
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200909static void _trace_callback(void* user_arg, const char* statement_string)
910{
911 PyObject *py_statement = NULL;
912 PyObject *ret = NULL;
913
914#ifdef WITH_THREAD
915 PyGILState_STATE gilstate;
916
917 gilstate = PyGILState_Ensure();
918#endif
919 py_statement = PyUnicode_DecodeUTF8(statement_string,
920 strlen(statement_string), "replace");
921 if (py_statement) {
922 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
923 Py_DECREF(py_statement);
924 }
925
926 if (ret) {
927 Py_DECREF(ret);
928 } else {
929 if (_enable_callback_tracebacks) {
930 PyErr_Print();
931 } else {
932 PyErr_Clear();
933 }
934 }
935
936#ifdef WITH_THREAD
937 PyGILState_Release(gilstate);
938#endif
939}
940
Gerhard Häringf9cee222010-03-05 15:20:03 +0000941static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000942{
943 PyObject* authorizer_cb;
944
945 static char *kwlist[] = { "authorizer_callback", NULL };
946 int rc;
947
Gerhard Häringf9cee222010-03-05 15:20:03 +0000948 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
949 return NULL;
950 }
951
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000952 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
953 kwlist, &authorizer_cb)) {
954 return NULL;
955 }
956
957 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
958
959 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000960 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000961 return NULL;
962 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000963 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
964 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000965
966 Py_INCREF(Py_None);
967 return Py_None;
968 }
969}
970
Gerhard Häringf9cee222010-03-05 15:20:03 +0000971static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000972{
973 PyObject* progress_handler;
974 int n;
975
976 static char *kwlist[] = { "progress_handler", "n", NULL };
977
Gerhard Häringf9cee222010-03-05 15:20:03 +0000978 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
979 return NULL;
980 }
981
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000982 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
983 kwlist, &progress_handler, &n)) {
984 return NULL;
985 }
986
987 if (progress_handler == Py_None) {
988 /* None clears the progress handler previously set */
989 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
990 } else {
991 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000992 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
993 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000994 }
995
996 Py_INCREF(Py_None);
997 return Py_None;
998}
999
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001000static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1001{
1002 PyObject* trace_callback;
1003
1004 static char *kwlist[] = { "trace_callback", NULL };
1005
1006 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1007 return NULL;
1008 }
1009
1010 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1011 kwlist, &trace_callback)) {
1012 return NULL;
1013 }
1014
1015 if (trace_callback == Py_None) {
1016 /* None clears the trace callback previously set */
1017 sqlite3_trace(self->db, 0, (void*)0);
1018 } else {
1019 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1020 return NULL;
1021 sqlite3_trace(self->db, _trace_callback, trace_callback);
1022 }
1023
1024 Py_INCREF(Py_None);
1025 return Py_None;
1026}
1027
Gerhard Häringf9cee222010-03-05 15:20:03 +00001028#ifdef HAVE_LOAD_EXTENSION
1029static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1030{
1031 int rc;
1032 int onoff;
1033
1034 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1035 return NULL;
1036 }
1037
1038 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1039 return NULL;
1040 }
1041
1042 rc = sqlite3_enable_load_extension(self->db, onoff);
1043
1044 if (rc != SQLITE_OK) {
1045 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1046 return NULL;
1047 } else {
1048 Py_INCREF(Py_None);
1049 return Py_None;
1050 }
1051}
1052
1053static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1054{
1055 int rc;
1056 char* extension_name;
1057 char* errmsg;
1058
1059 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1060 return NULL;
1061 }
1062
1063 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1064 return NULL;
1065 }
1066
1067 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1068 if (rc != 0) {
1069 PyErr_SetString(pysqlite_OperationalError, errmsg);
1070 return NULL;
1071 } else {
1072 Py_INCREF(Py_None);
1073 return Py_None;
1074 }
1075}
1076#endif
1077
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001078int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001079{
Georg Brandldfd73442009-04-05 11:47:34 +00001080#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001081 if (self->check_same_thread) {
1082 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001083 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001084 "SQLite objects created in a thread can only be used in that same thread."
1085 "The object was created in thread id %ld and this is thread id %ld",
1086 self->thread_ident, PyThread_get_thread_ident());
1087 return 0;
1088 }
1089
1090 }
Georg Brandldfd73442009-04-05 11:47:34 +00001091#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092 return 1;
1093}
1094
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001095static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001096{
1097 Py_INCREF(self->isolation_level);
1098 return self->isolation_level;
1099}
1100
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001101static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001102{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001103 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001104 return NULL;
1105 } else {
1106 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1107 }
1108}
1109
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001110static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001111{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001112 PyObject* res;
1113 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001114 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001115
1116 Py_XDECREF(self->isolation_level);
1117
1118 if (self->begin_statement) {
1119 PyMem_Free(self->begin_statement);
1120 self->begin_statement = NULL;
1121 }
1122
1123 if (isolation_level == Py_None) {
1124 Py_INCREF(Py_None);
1125 self->isolation_level = Py_None;
1126
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001127 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001128 if (!res) {
1129 return -1;
1130 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001131 Py_DECREF(res);
1132
1133 self->inTransaction = 0;
1134 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001135 const char *statement;
1136 Py_ssize_t size;
1137
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001138 Py_INCREF(isolation_level);
1139 self->isolation_level = isolation_level;
1140
Georg Brandlceab6102007-11-25 00:45:05 +00001141 if (!begin_word) {
1142 begin_word = PyUnicode_FromString("BEGIN ");
1143 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 }
Georg Brandlceab6102007-11-25 00:45:05 +00001145 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 if (!begin_statement) {
1147 return -1;
1148 }
1149
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001150 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001151 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001152 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001153 return -1;
1154 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001155 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001156 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001157 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001158 return -1;
1159 }
1160
Neal Norwitzefee9f52007-10-27 02:50:52 +00001161 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162 Py_DECREF(begin_statement);
1163 }
1164
1165 return 0;
1166}
1167
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001168PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001169{
1170 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001171 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001172 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173 int rc;
1174
Gerhard Häringf9cee222010-03-05 15:20:03 +00001175 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1176 return NULL;
1177 }
1178
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001179 if (!PyArg_ParseTuple(args, "O", &sql)) {
1180 return NULL;
1181 }
1182
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001183 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001184
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001185 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001186 if (!statement) {
1187 return NULL;
1188 }
1189
Victor Stinner0201f442010-03-13 03:28:34 +00001190 statement->db = NULL;
1191 statement->st = NULL;
1192 statement->sql = NULL;
1193 statement->in_use = 0;
1194 statement->in_weakreflist = NULL;
1195
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001196 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197
1198 if (rc != SQLITE_OK) {
1199 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001200 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001201 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001202 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001203 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001204 (void)pysqlite_statement_reset(statement);
1205 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001206 }
1207
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001208 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001209 } else {
1210 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1211 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001212 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001213 goto error;
1214 }
1215
1216 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001217 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001218 goto error;
1219 }
1220
1221 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001222 }
1223
Thomas Wouters477c8d52006-05-27 19:21:47 +00001224error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001225 return (PyObject*)statement;
1226}
1227
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001228PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001229{
1230 PyObject* cursor = 0;
1231 PyObject* result = 0;
1232 PyObject* method = 0;
1233
1234 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1235 if (!cursor) {
1236 goto error;
1237 }
1238
1239 method = PyObject_GetAttrString(cursor, "execute");
1240 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001241 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242 goto error;
1243 }
1244
1245 result = PyObject_CallObject(method, args);
1246 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001247 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001248 }
1249
1250error:
1251 Py_XDECREF(result);
1252 Py_XDECREF(method);
1253
1254 return cursor;
1255}
1256
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001257PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001258{
1259 PyObject* cursor = 0;
1260 PyObject* result = 0;
1261 PyObject* method = 0;
1262
1263 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1264 if (!cursor) {
1265 goto error;
1266 }
1267
1268 method = PyObject_GetAttrString(cursor, "executemany");
1269 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001270 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271 goto error;
1272 }
1273
1274 result = PyObject_CallObject(method, args);
1275 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001276 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001277 }
1278
1279error:
1280 Py_XDECREF(result);
1281 Py_XDECREF(method);
1282
1283 return cursor;
1284}
1285
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001286PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287{
1288 PyObject* cursor = 0;
1289 PyObject* result = 0;
1290 PyObject* method = 0;
1291
1292 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1293 if (!cursor) {
1294 goto error;
1295 }
1296
1297 method = PyObject_GetAttrString(cursor, "executescript");
1298 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001299 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001300 goto error;
1301 }
1302
1303 result = PyObject_CallObject(method, args);
1304 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001305 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001306 }
1307
1308error:
1309 Py_XDECREF(result);
1310 Py_XDECREF(method);
1311
1312 return cursor;
1313}
1314
1315/* ------------------------- COLLATION CODE ------------------------ */
1316
1317static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001318pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001319 void* context,
1320 int text1_length, const void* text1_data,
1321 int text2_length, const void* text2_data)
1322{
1323 PyObject* callback = (PyObject*)context;
1324 PyObject* string1 = 0;
1325 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001326#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001328#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001329 PyObject* retval = NULL;
1330 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001331#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001332 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001333#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001334
1335 if (PyErr_Occurred()) {
1336 goto finally;
1337 }
1338
Guido van Rossum98297ee2007-11-06 21:34:58 +00001339 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1340 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001341
1342 if (!string1 || !string2) {
1343 goto finally; /* failed to allocate strings */
1344 }
1345
1346 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1347
1348 if (!retval) {
1349 /* execution failed */
1350 goto finally;
1351 }
1352
Christian Heimes217cfd12007-12-02 14:31:20 +00001353 result = PyLong_AsLong(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001354 if (PyErr_Occurred()) {
1355 result = 0;
1356 }
1357
1358finally:
1359 Py_XDECREF(string1);
1360 Py_XDECREF(string2);
1361 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001362#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001363 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001364#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001365 return result;
1366}
1367
1368static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001369pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001370{
1371 PyObject* retval = NULL;
1372
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001373 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001374 goto finally;
1375 }
1376
1377 sqlite3_interrupt(self->db);
1378
1379 Py_INCREF(Py_None);
1380 retval = Py_None;
1381
1382finally:
1383 return retval;
1384}
1385
Christian Heimesbbe741d2008-03-28 10:53:29 +00001386/* Function author: Paul Kippes <kippesp@gmail.com>
1387 * Class method of Connection to call the Python function _iterdump
1388 * of the sqlite3 module.
1389 */
1390static PyObject *
1391pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1392{
1393 PyObject* retval = NULL;
1394 PyObject* module = NULL;
1395 PyObject* module_dict;
1396 PyObject* pyfn_iterdump;
1397
1398 if (!pysqlite_check_connection(self)) {
1399 goto finally;
1400 }
1401
1402 module = PyImport_ImportModule(MODULE_NAME ".dump");
1403 if (!module) {
1404 goto finally;
1405 }
1406
1407 module_dict = PyModule_GetDict(module);
1408 if (!module_dict) {
1409 goto finally;
1410 }
1411
1412 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1413 if (!pyfn_iterdump) {
1414 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1415 goto finally;
1416 }
1417
1418 args = PyTuple_New(1);
1419 if (!args) {
1420 goto finally;
1421 }
1422 Py_INCREF(self);
1423 PyTuple_SetItem(args, 0, (PyObject*)self);
1424 retval = PyObject_CallObject(pyfn_iterdump, args);
1425
1426finally:
1427 Py_XDECREF(args);
1428 Py_XDECREF(module);
1429 return retval;
1430}
1431
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001432static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001433pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001434{
1435 PyObject* callable;
1436 PyObject* uppercase_name = 0;
1437 PyObject* name;
1438 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001439 Py_ssize_t i, len;
1440 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001441 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001442 unsigned int kind;
1443 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001444
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001445 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001446 goto finally;
1447 }
1448
Gerhard Häring6d214562007-08-10 18:15:11 +00001449 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001450 goto finally;
1451 }
1452
1453 uppercase_name = PyObject_CallMethod(name, "upper", "");
1454 if (!uppercase_name) {
1455 goto finally;
1456 }
1457
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001458 if (PyUnicode_READY(uppercase_name))
1459 goto finally;
1460 len = PyUnicode_GET_LENGTH(uppercase_name);
1461 kind = PyUnicode_KIND(uppercase_name);
1462 data = PyUnicode_DATA(uppercase_name);
1463 for (i=0; i<len; i++) {
1464 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1465 if ((ch >= '0' && ch <= '9')
1466 || (ch >= 'A' && ch <= 'Z')
1467 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001468 {
Victor Stinner35466c52010-04-22 11:23:23 +00001469 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001470 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001471 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001472 goto finally;
1473 }
1474 }
1475
Victor Stinner35466c52010-04-22 11:23:23 +00001476 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1477 if (!uppercase_name_str)
1478 goto finally;
1479
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001480 if (callable != Py_None && !PyCallable_Check(callable)) {
1481 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1482 goto finally;
1483 }
1484
1485 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001486 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1487 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001488 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001489 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1490 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001491 }
1492
1493 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001494 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001495 SQLITE_UTF8,
1496 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001497 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001498 if (rc != SQLITE_OK) {
1499 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001500 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001501 goto finally;
1502 }
1503
1504finally:
1505 Py_XDECREF(uppercase_name);
1506
1507 if (PyErr_Occurred()) {
1508 retval = NULL;
1509 } else {
1510 Py_INCREF(Py_None);
1511 retval = Py_None;
1512 }
1513
1514 return retval;
1515}
1516
Christian Heimesbbe741d2008-03-28 10:53:29 +00001517/* Called when the connection is used as a context manager. Returns itself as a
1518 * convenience to the caller. */
1519static PyObject *
1520pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1521{
1522 Py_INCREF(self);
1523 return (PyObject*)self;
1524}
1525
1526/** Called when the connection is used as a context manager. If there was any
1527 * exception, a rollback takes place; otherwise we commit. */
1528static PyObject *
1529pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1530{
1531 PyObject* exc_type, *exc_value, *exc_tb;
1532 char* method_name;
1533 PyObject* result;
1534
1535 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1536 return NULL;
1537 }
1538
1539 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1540 method_name = "commit";
1541 } else {
1542 method_name = "rollback";
1543 }
1544
1545 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1546 if (!result) {
1547 return NULL;
1548 }
1549 Py_DECREF(result);
1550
1551 Py_RETURN_FALSE;
1552}
1553
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001554static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001555PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001556
1557static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001558 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1559 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001560 {NULL}
1561};
1562
1563static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001564 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001565 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001566 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001567 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001568 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001569 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001570 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001571 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001572 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001573 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001574 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001575 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001576 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001577 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001578 #ifdef HAVE_LOAD_EXTENSION
1579 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1580 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1581 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1582 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1583 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001584 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1585 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001586 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1587 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001588 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001589 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001590 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001591 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001592 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001593 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001594 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001595 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001596 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001597 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001598 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001599 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001600 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1601 PyDoc_STR("For context manager. Non-standard.")},
1602 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1603 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001604 {NULL, NULL}
1605};
1606
1607static struct PyMemberDef connection_members[] =
1608{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001609 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1610 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1611 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1612 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1613 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1614 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1615 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1616 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1617 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1618 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001619 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1620 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001621 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001622 {NULL}
1623};
1624
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001625PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001626 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001627 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001628 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001629 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001630 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001631 0, /* tp_print */
1632 0, /* tp_getattr */
1633 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001634 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001635 0, /* tp_repr */
1636 0, /* tp_as_number */
1637 0, /* tp_as_sequence */
1638 0, /* tp_as_mapping */
1639 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001640 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001641 0, /* tp_str */
1642 0, /* tp_getattro */
1643 0, /* tp_setattro */
1644 0, /* tp_as_buffer */
1645 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1646 connection_doc, /* tp_doc */
1647 0, /* tp_traverse */
1648 0, /* tp_clear */
1649 0, /* tp_richcompare */
1650 0, /* tp_weaklistoffset */
1651 0, /* tp_iter */
1652 0, /* tp_iternext */
1653 connection_methods, /* tp_methods */
1654 connection_members, /* tp_members */
1655 connection_getset, /* tp_getset */
1656 0, /* tp_base */
1657 0, /* tp_dict */
1658 0, /* tp_descr_get */
1659 0, /* tp_descr_set */
1660 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001661 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001662 0, /* tp_alloc */
1663 0, /* tp_new */
1664 0 /* tp_free */
1665};
1666
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001667extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001668{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001669 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1670 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001671}