blob: 184bdeef93aac68e9be99eae2d37f8b8b209b422 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* connection.c - the connection type
2 *
Gerhard Häringf9cee222010-03-05 15:20:03 +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"
26#include "connection.h"
27#include "statement.h"
28#include "cursor.h"
29#include "prepare_protocol.h"
30#include "util.h"
31#include "sqlitecompat.h"
32
33#include "pythread.h"
34
Gerhard Häringe7ea7452008-03-29 00:45:29 +000035#define ACTION_FINALIZE 1
36#define ACTION_RESET 2
37
Gerhard Häringf9cee222010-03-05 15:20:03 +000038#if SQLITE_VERSION_NUMBER >= 3003008
39#ifndef SQLITE_OMIT_LOAD_EXTENSION
40#define HAVE_LOAD_EXTENSION
41#endif
42#endif
43
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000044static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Gerhard Häringf9cee222010-03-05 15:20:03 +000045static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000046
Thomas Wouters0e3f5912006-08-11 14:57:12 +000047
Benjamin Petersond7b03282008-09-13 15:58:53 +000048static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000049{
50 /* in older SQLite versions, calling sqlite3_result_error in callbacks
51 * triggers a bug in SQLite that leads either to irritating results or
52 * segfaults, depending on the SQLite version */
53#if SQLITE_VERSION_NUMBER >= 3003003
54 sqlite3_result_error(ctx, errmsg, len);
55#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000056 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057#endif
58}
59
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000060int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000061{
62 static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
63
64 char* database;
65 int detect_types = 0;
66 PyObject* isolation_level = NULL;
67 PyObject* factory = NULL;
68 int check_same_thread = 1;
69 int cached_statements = 100;
70 double timeout = 5.0;
71 int rc;
72
73 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist,
74 &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
75 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000076 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077 }
78
Gerhard Häringf9cee222010-03-05 15:20:03 +000079 self->initialized = 1;
80
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081 self->begin_statement = NULL;
82
83 self->statement_cache = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000084 self->statements = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +000085 self->cursors = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086
87 Py_INCREF(Py_None);
88 self->row_factory = Py_None;
89
90 Py_INCREF(&PyUnicode_Type);
91 self->text_factory = (PyObject*)&PyUnicode_Type;
92
93 Py_BEGIN_ALLOW_THREADS
94 rc = sqlite3_open(database, &self->db);
95 Py_END_ALLOW_THREADS
96
97 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000098 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000099 return -1;
100 }
101
102 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000103 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104 if (!isolation_level) {
105 return -1;
106 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107 } else {
108 Py_INCREF(isolation_level);
109 }
110 self->isolation_level = NULL;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000111 pysqlite_connection_set_isolation_level(self, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112 Py_DECREF(isolation_level);
113
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000114 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 if (PyErr_Occurred()) {
116 return -1;
117 }
118
Gerhard Häringf9cee222010-03-05 15:20:03 +0000119 self->created_statements = 0;
120 self->created_cursors = 0;
121
122 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000123 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000124 self->cursors = PyList_New(0);
125 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126 return -1;
127 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000128
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000129 /* By default, the Cache class INCREFs the factory in its initializer, and
130 * decrefs it in its deallocator method. Since this would create a circular
131 * reference here, we're breaking it by decrementing self, and telling the
132 * cache class to not decref the factory (self) in its deallocator.
133 */
134 self->statement_cache->decref_factory = 0;
135 Py_DECREF(self);
136
137 self->inTransaction = 0;
138 self->detect_types = detect_types;
139 self->timeout = timeout;
140 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000141#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000143#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000144 self->check_same_thread = check_same_thread;
145
146 self->function_pinboard = PyDict_New();
147 if (!self->function_pinboard) {
148 return -1;
149 }
150
151 self->collations = PyDict_New();
152 if (!self->collations) {
153 return -1;
154 }
155
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000156 self->Warning = pysqlite_Warning;
157 self->Error = pysqlite_Error;
158 self->InterfaceError = pysqlite_InterfaceError;
159 self->DatabaseError = pysqlite_DatabaseError;
160 self->DataError = pysqlite_DataError;
161 self->OperationalError = pysqlite_OperationalError;
162 self->IntegrityError = pysqlite_IntegrityError;
163 self->InternalError = pysqlite_InternalError;
164 self->ProgrammingError = pysqlite_ProgrammingError;
165 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000166
167 return 0;
168}
169
Thomas Wouters477c8d52006-05-27 19:21:47 +0000170/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000171void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000172{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000173 pysqlite_Node* node;
174 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175
176 node = self->statement_cache->first;
177
178 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000179 statement = (pysqlite_Statement*)(node->data);
180 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000181 node = node->next;
182 }
183
184 Py_DECREF(self->statement_cache);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000185 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000186 Py_DECREF(self);
187 self->statement_cache->decref_factory = 0;
188}
189
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000190/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000191void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000192{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000193 int i;
194 PyObject* weakref;
195 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000196 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197
Thomas Wouters477c8d52006-05-27 19:21:47 +0000198 for (i = 0; i < PyList_Size(self->statements); i++) {
199 weakref = PyList_GetItem(self->statements, i);
200 statement = PyWeakref_GetObject(weakref);
201 if (statement != Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000202 if (action == ACTION_RESET) {
203 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
204 } else {
205 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
206 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000207 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000209
210 if (reset_cursors) {
211 for (i = 0; i < PyList_Size(self->cursors); i++) {
212 weakref = PyList_GetItem(self->cursors, i);
213 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
214 if ((PyObject*)cursor != Py_None) {
215 cursor->reset = 1;
216 }
217 }
218 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000219}
220
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000221void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222{
223 Py_XDECREF(self->statement_cache);
224
225 /* Clean up if user has not called .close() explicitly. */
226 if (self->db) {
227 Py_BEGIN_ALLOW_THREADS
228 sqlite3_close(self->db);
229 Py_END_ALLOW_THREADS
230 }
231
232 if (self->begin_statement) {
233 PyMem_Free(self->begin_statement);
234 }
235 Py_XDECREF(self->isolation_level);
236 Py_XDECREF(self->function_pinboard);
237 Py_XDECREF(self->row_factory);
238 Py_XDECREF(self->text_factory);
239 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000240 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000241 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242
Christian Heimes90aa7642007-12-19 02:45:37 +0000243 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244}
245
Gerhard Häringf9cee222010-03-05 15:20:03 +0000246/*
247 * Registers a cursor with the connection.
248 *
249 * 0 => error; 1 => ok
250 */
251int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
252{
253 PyObject* weakref;
254
255 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
256 if (!weakref) {
257 goto error;
258 }
259
260 if (PyList_Append(connection->cursors, weakref) != 0) {
261 Py_CLEAR(weakref);
262 goto error;
263 }
264
265 Py_DECREF(weakref);
266
267 return 1;
268error:
269 return 0;
270}
271
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000272PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000273{
274 static char *kwlist[] = {"factory", NULL, NULL};
275 PyObject* factory = NULL;
276 PyObject* cursor;
277
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000278 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
279 &factory)) {
280 return NULL;
281 }
282
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000283 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000284 return NULL;
285 }
286
287 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000288 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000289 }
290
291 cursor = PyObject_CallFunction(factory, "O", self);
292
Gerhard Häringf9cee222010-03-05 15:20:03 +0000293 _pysqlite_drop_unused_cursor_references(self);
294
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000295 if (cursor && self->row_factory != Py_None) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000296 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000297 Py_INCREF(self->row_factory);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000298 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000299 }
300
301 return cursor;
302}
303
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000304PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000305{
306 int rc;
307
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000308 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000309 return NULL;
310 }
311
Gerhard Häringf9cee222010-03-05 15:20:03 +0000312 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313
314 if (self->db) {
315 Py_BEGIN_ALLOW_THREADS
316 rc = sqlite3_close(self->db);
317 Py_END_ALLOW_THREADS
318
319 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000320 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000321 return NULL;
322 } else {
323 self->db = NULL;
324 }
325 }
326
327 Py_INCREF(Py_None);
328 return Py_None;
329}
330
331/*
332 * Checks if a connection object is usable (i. e. not closed).
333 *
334 * 0 => error; 1 => ok
335 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000336int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000338 if (!con->initialized) {
339 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
340 return 0;
341 }
342
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000344 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000345 return 0;
346 } else {
347 return 1;
348 }
349}
350
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000351PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352{
353 int rc;
354 const char* tail;
355 sqlite3_stmt* statement;
356
357 Py_BEGIN_ALLOW_THREADS
358 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
359 Py_END_ALLOW_THREADS
360
361 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000362 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 goto error;
364 }
365
Benjamin Petersond7b03282008-09-13 15:58:53 +0000366 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367 if (rc == SQLITE_DONE) {
368 self->inTransaction = 1;
369 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000370 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000371 }
372
373 Py_BEGIN_ALLOW_THREADS
374 rc = sqlite3_finalize(statement);
375 Py_END_ALLOW_THREADS
376
377 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000378 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379 }
380
381error:
382 if (PyErr_Occurred()) {
383 return NULL;
384 } else {
385 Py_INCREF(Py_None);
386 return Py_None;
387 }
388}
389
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000390PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000391{
392 int rc;
393 const char* tail;
394 sqlite3_stmt* statement;
395
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000396 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000397 return NULL;
398 }
399
400 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000401 pysqlite_do_all_statements(self, ACTION_RESET, 0);
402
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000403 Py_BEGIN_ALLOW_THREADS
404 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
405 Py_END_ALLOW_THREADS
406 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000407 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000408 goto error;
409 }
410
Benjamin Petersond7b03282008-09-13 15:58:53 +0000411 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412 if (rc == SQLITE_DONE) {
413 self->inTransaction = 0;
414 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000415 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000416 }
417
418 Py_BEGIN_ALLOW_THREADS
419 rc = sqlite3_finalize(statement);
420 Py_END_ALLOW_THREADS
421 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000422 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423 }
424
425 }
426
427error:
428 if (PyErr_Occurred()) {
429 return NULL;
430 } else {
431 Py_INCREF(Py_None);
432 return Py_None;
433 }
434}
435
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000436PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437{
438 int rc;
439 const char* tail;
440 sqlite3_stmt* statement;
441
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000442 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443 return NULL;
444 }
445
446 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000447 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448
449 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000450 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451 Py_END_ALLOW_THREADS
452 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000453 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000454 goto error;
455 }
456
Benjamin Petersond7b03282008-09-13 15:58:53 +0000457 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000458 if (rc == SQLITE_DONE) {
459 self->inTransaction = 0;
460 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000461 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000462 }
463
464 Py_BEGIN_ALLOW_THREADS
465 rc = sqlite3_finalize(statement);
466 Py_END_ALLOW_THREADS
467 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000468 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469 }
470
471 }
472
473error:
474 if (PyErr_Occurred()) {
475 return NULL;
476 } else {
477 Py_INCREF(Py_None);
478 return Py_None;
479 }
480}
481
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000482void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483{
484 long longval;
485 const char* buffer;
486 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000487
Thomas Wouters477c8d52006-05-27 19:21:47 +0000488 if ((!py_val) || PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000489 sqlite3_result_null(context);
490 } else if (py_val == Py_None) {
491 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000492 } else if (PyLong_Check(py_val)) {
493 longval = PyLong_AsLong(py_val);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000494 sqlite3_result_int64(context, (PY_LONG_LONG)longval);
495 } else if (PyFloat_Check(py_val)) {
496 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000497 } else if (PyUnicode_Check(py_val)) {
Victor Stinner86999502010-05-19 01:27:23 +0000498 char *str = _PyUnicode_AsString(py_val);
499 if (str != NULL)
500 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000501 } else if (PyObject_CheckBuffer(py_val)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
503 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000504 } else {
505 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000506 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507 } else {
508 /* TODO: raise error */
509 }
510}
511
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000512PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513{
514 PyObject* args;
515 int i;
516 sqlite3_value* cur_value;
517 PyObject* cur_py_value;
518 const char* val_str;
519 PY_LONG_LONG val_int;
520 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000521
522 args = PyTuple_New(argc);
523 if (!args) {
524 return NULL;
525 }
526
527 for (i = 0; i < argc; i++) {
528 cur_value = argv[i];
529 switch (sqlite3_value_type(argv[i])) {
530 case SQLITE_INTEGER:
531 val_int = sqlite3_value_int64(cur_value);
Christian Heimes217cfd12007-12-02 14:31:20 +0000532 cur_py_value = PyLong_FromLong((long)val_int);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533 break;
534 case SQLITE_FLOAT:
535 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
536 break;
537 case SQLITE_TEXT:
538 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000539 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000540 /* TODO: have a way to show errors here */
541 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000542 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 Py_INCREF(Py_None);
544 cur_py_value = Py_None;
545 }
546 break;
547 case SQLITE_BLOB:
548 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000549 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000550 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551 break;
552 case SQLITE_NULL:
553 default:
554 Py_INCREF(Py_None);
555 cur_py_value = Py_None;
556 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000557
558 if (!cur_py_value) {
559 Py_DECREF(args);
560 return NULL;
561 }
562
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000563 PyTuple_SetItem(args, i, cur_py_value);
564
565 }
566
567 return args;
568}
569
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000570void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000571{
572 PyObject* args;
573 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000574 PyObject* py_retval = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000575
Georg Brandldfd73442009-04-05 11:47:34 +0000576#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000577 PyGILState_STATE threadstate;
578
579 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000580#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000581
582 py_func = (PyObject*)sqlite3_user_data(context);
583
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000584 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000585 if (args) {
586 py_retval = PyObject_CallObject(py_func, args);
587 Py_DECREF(args);
588 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000590 if (py_retval) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000591 _pysqlite_set_result(context, py_retval);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000592 Py_DECREF(py_retval);
593 } else {
594 if (_enable_callback_tracebacks) {
595 PyErr_Print();
596 } else {
597 PyErr_Clear();
598 }
599 _sqlite3_result_error(context, "user-defined function raised exception", -1);
600 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000601
Georg Brandldfd73442009-04-05 11:47:34 +0000602#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000603 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000604#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000605}
606
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000607static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000608{
609 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000610 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611 PyObject* aggregate_class;
612 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000613 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000614
Georg Brandldfd73442009-04-05 11:47:34 +0000615#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616 PyGILState_STATE threadstate;
617
618 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000619#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620
621 aggregate_class = (PyObject*)sqlite3_user_data(context);
622
623 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
624
625 if (*aggregate_instance == 0) {
626 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
627
Thomas Wouters477c8d52006-05-27 19:21:47 +0000628 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630 if (_enable_callback_tracebacks) {
631 PyErr_Print();
632 } else {
633 PyErr_Clear();
634 }
635 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000636 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637 }
638 }
639
640 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000641 if (!stepmethod) {
642 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643 }
644
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000645 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646 if (!args) {
647 goto error;
648 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000649
650 function_result = PyObject_CallObject(stepmethod, args);
651 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000654 if (_enable_callback_tracebacks) {
655 PyErr_Print();
656 } else {
657 PyErr_Clear();
658 }
659 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660 }
661
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662error:
663 Py_XDECREF(stepmethod);
664 Py_XDECREF(function_result);
665
Georg Brandldfd73442009-04-05 11:47:34 +0000666#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000667 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000668#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000669}
670
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000671void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000673 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674 PyObject** aggregate_instance;
675 PyObject* aggregate_class;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000676
Georg Brandldfd73442009-04-05 11:47:34 +0000677#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678 PyGILState_STATE threadstate;
679
680 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000681#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000682
683 aggregate_class = (PyObject*)sqlite3_user_data(context);
684
685 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
Gerhard Häringf9cee222010-03-05 15:20:03 +0000909static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000910{
911 PyObject* authorizer_cb;
912
913 static char *kwlist[] = { "authorizer_callback", NULL };
914 int rc;
915
Gerhard Häringf9cee222010-03-05 15:20:03 +0000916 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
917 return NULL;
918 }
919
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000920 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
921 kwlist, &authorizer_cb)) {
922 return NULL;
923 }
924
925 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
926
927 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000928 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000929 return NULL;
930 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000931 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
932 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000933
934 Py_INCREF(Py_None);
935 return Py_None;
936 }
937}
938
Gerhard Häringf9cee222010-03-05 15:20:03 +0000939static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000940{
941 PyObject* progress_handler;
942 int n;
943
944 static char *kwlist[] = { "progress_handler", "n", NULL };
945
Gerhard Häringf9cee222010-03-05 15:20:03 +0000946 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
947 return NULL;
948 }
949
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000950 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
951 kwlist, &progress_handler, &n)) {
952 return NULL;
953 }
954
955 if (progress_handler == Py_None) {
956 /* None clears the progress handler previously set */
957 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
958 } else {
959 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000960 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
961 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000962 }
963
964 Py_INCREF(Py_None);
965 return Py_None;
966}
967
Gerhard Häringf9cee222010-03-05 15:20:03 +0000968#ifdef HAVE_LOAD_EXTENSION
969static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
970{
971 int rc;
972 int onoff;
973
974 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
975 return NULL;
976 }
977
978 if (!PyArg_ParseTuple(args, "i", &onoff)) {
979 return NULL;
980 }
981
982 rc = sqlite3_enable_load_extension(self->db, onoff);
983
984 if (rc != SQLITE_OK) {
985 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
986 return NULL;
987 } else {
988 Py_INCREF(Py_None);
989 return Py_None;
990 }
991}
992
993static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
994{
995 int rc;
996 char* extension_name;
997 char* errmsg;
998
999 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1000 return NULL;
1001 }
1002
1003 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1004 return NULL;
1005 }
1006
1007 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1008 if (rc != 0) {
1009 PyErr_SetString(pysqlite_OperationalError, errmsg);
1010 return NULL;
1011 } else {
1012 Py_INCREF(Py_None);
1013 return Py_None;
1014 }
1015}
1016#endif
1017
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001018int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001019{
Georg Brandldfd73442009-04-05 11:47:34 +00001020#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001021 if (self->check_same_thread) {
1022 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001023 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024 "SQLite objects created in a thread can only be used in that same thread."
1025 "The object was created in thread id %ld and this is thread id %ld",
1026 self->thread_ident, PyThread_get_thread_ident());
1027 return 0;
1028 }
1029
1030 }
Georg Brandldfd73442009-04-05 11:47:34 +00001031#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001032 return 1;
1033}
1034
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001035static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001036{
1037 Py_INCREF(self->isolation_level);
1038 return self->isolation_level;
1039}
1040
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001041static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001042{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001043 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001044 return NULL;
1045 } else {
1046 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1047 }
1048}
1049
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001050static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001051{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001052 PyObject* res;
1053 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001054 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055
1056 Py_XDECREF(self->isolation_level);
1057
1058 if (self->begin_statement) {
1059 PyMem_Free(self->begin_statement);
1060 self->begin_statement = NULL;
1061 }
1062
1063 if (isolation_level == Py_None) {
1064 Py_INCREF(Py_None);
1065 self->isolation_level = Py_None;
1066
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001067 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001068 if (!res) {
1069 return -1;
1070 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001071 Py_DECREF(res);
1072
1073 self->inTransaction = 0;
1074 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001075 const char *statement;
1076 Py_ssize_t size;
1077
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001078 Py_INCREF(isolation_level);
1079 self->isolation_level = isolation_level;
1080
Georg Brandlceab6102007-11-25 00:45:05 +00001081 if (!begin_word) {
1082 begin_word = PyUnicode_FromString("BEGIN ");
1083 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001084 }
Georg Brandlceab6102007-11-25 00:45:05 +00001085 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001086 if (!begin_statement) {
1087 return -1;
1088 }
1089
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001090 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001091 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001092 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001093 return -1;
1094 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001095 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001096 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001097 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098 return -1;
1099 }
1100
Neal Norwitzefee9f52007-10-27 02:50:52 +00001101 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001102 Py_DECREF(begin_statement);
1103 }
1104
1105 return 0;
1106}
1107
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001108PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001109{
1110 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001111 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001112 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001113 int rc;
1114
Gerhard Häringf9cee222010-03-05 15:20:03 +00001115 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1116 return NULL;
1117 }
1118
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001119 if (!PyArg_ParseTuple(args, "O", &sql)) {
1120 return NULL;
1121 }
1122
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001123 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001124
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001125 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001126 if (!statement) {
1127 return NULL;
1128 }
1129
Victor Stinner0201f442010-03-13 03:28:34 +00001130 statement->db = NULL;
1131 statement->st = NULL;
1132 statement->sql = NULL;
1133 statement->in_use = 0;
1134 statement->in_weakreflist = NULL;
1135
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001136 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001137
1138 if (rc != SQLITE_OK) {
1139 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001140 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001141 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001142 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001144 (void)pysqlite_statement_reset(statement);
1145 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 }
1147
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001148 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001149 } else {
1150 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1151 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001152 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001153 goto error;
1154 }
1155
1156 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001157 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001158 goto error;
1159 }
1160
1161 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162 }
1163
Thomas Wouters477c8d52006-05-27 19:21:47 +00001164error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001165 return (PyObject*)statement;
1166}
1167
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001168PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001169{
1170 PyObject* cursor = 0;
1171 PyObject* result = 0;
1172 PyObject* method = 0;
1173
1174 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1175 if (!cursor) {
1176 goto error;
1177 }
1178
1179 method = PyObject_GetAttrString(cursor, "execute");
1180 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001181 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001182 goto error;
1183 }
1184
1185 result = PyObject_CallObject(method, args);
1186 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001187 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001188 }
1189
1190error:
1191 Py_XDECREF(result);
1192 Py_XDECREF(method);
1193
1194 return cursor;
1195}
1196
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001197PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001198{
1199 PyObject* cursor = 0;
1200 PyObject* result = 0;
1201 PyObject* method = 0;
1202
1203 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1204 if (!cursor) {
1205 goto error;
1206 }
1207
1208 method = PyObject_GetAttrString(cursor, "executemany");
1209 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001210 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001211 goto error;
1212 }
1213
1214 result = PyObject_CallObject(method, args);
1215 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001216 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001217 }
1218
1219error:
1220 Py_XDECREF(result);
1221 Py_XDECREF(method);
1222
1223 return cursor;
1224}
1225
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001226PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227{
1228 PyObject* cursor = 0;
1229 PyObject* result = 0;
1230 PyObject* method = 0;
1231
1232 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1233 if (!cursor) {
1234 goto error;
1235 }
1236
1237 method = PyObject_GetAttrString(cursor, "executescript");
1238 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001239 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001240 goto error;
1241 }
1242
1243 result = PyObject_CallObject(method, args);
1244 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001245 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001246 }
1247
1248error:
1249 Py_XDECREF(result);
1250 Py_XDECREF(method);
1251
1252 return cursor;
1253}
1254
1255/* ------------------------- COLLATION CODE ------------------------ */
1256
1257static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001258pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259 void* context,
1260 int text1_length, const void* text1_data,
1261 int text2_length, const void* text2_data)
1262{
1263 PyObject* callback = (PyObject*)context;
1264 PyObject* string1 = 0;
1265 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001266#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001267 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001268#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001269 PyObject* retval = NULL;
1270 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001271#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001272 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001273#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001274
1275 if (PyErr_Occurred()) {
1276 goto finally;
1277 }
1278
Guido van Rossum98297ee2007-11-06 21:34:58 +00001279 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1280 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001281
1282 if (!string1 || !string2) {
1283 goto finally; /* failed to allocate strings */
1284 }
1285
1286 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1287
1288 if (!retval) {
1289 /* execution failed */
1290 goto finally;
1291 }
1292
Christian Heimes217cfd12007-12-02 14:31:20 +00001293 result = PyLong_AsLong(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001294 if (PyErr_Occurred()) {
1295 result = 0;
1296 }
1297
1298finally:
1299 Py_XDECREF(string1);
1300 Py_XDECREF(string2);
1301 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001302#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001303 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001304#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001305 return result;
1306}
1307
1308static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001309pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001310{
1311 PyObject* retval = NULL;
1312
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001313 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001314 goto finally;
1315 }
1316
1317 sqlite3_interrupt(self->db);
1318
1319 Py_INCREF(Py_None);
1320 retval = Py_None;
1321
1322finally:
1323 return retval;
1324}
1325
Christian Heimesbbe741d2008-03-28 10:53:29 +00001326/* Function author: Paul Kippes <kippesp@gmail.com>
1327 * Class method of Connection to call the Python function _iterdump
1328 * of the sqlite3 module.
1329 */
1330static PyObject *
1331pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1332{
1333 PyObject* retval = NULL;
1334 PyObject* module = NULL;
1335 PyObject* module_dict;
1336 PyObject* pyfn_iterdump;
1337
1338 if (!pysqlite_check_connection(self)) {
1339 goto finally;
1340 }
1341
1342 module = PyImport_ImportModule(MODULE_NAME ".dump");
1343 if (!module) {
1344 goto finally;
1345 }
1346
1347 module_dict = PyModule_GetDict(module);
1348 if (!module_dict) {
1349 goto finally;
1350 }
1351
1352 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1353 if (!pyfn_iterdump) {
1354 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1355 goto finally;
1356 }
1357
1358 args = PyTuple_New(1);
1359 if (!args) {
1360 goto finally;
1361 }
1362 Py_INCREF(self);
1363 PyTuple_SetItem(args, 0, (PyObject*)self);
1364 retval = PyObject_CallObject(pyfn_iterdump, args);
1365
1366finally:
1367 Py_XDECREF(args);
1368 Py_XDECREF(module);
1369 return retval;
1370}
1371
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001372static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001373pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001374{
1375 PyObject* callable;
1376 PyObject* uppercase_name = 0;
1377 PyObject* name;
1378 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001379 Py_UNICODE* chk;
1380 Py_ssize_t i, len;
1381 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001382 int rc;
1383
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001384 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001385 goto finally;
1386 }
1387
Gerhard Häring6d214562007-08-10 18:15:11 +00001388 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001389 goto finally;
1390 }
1391
1392 uppercase_name = PyObject_CallMethod(name, "upper", "");
1393 if (!uppercase_name) {
1394 goto finally;
1395 }
1396
Victor Stinner35466c52010-04-22 11:23:23 +00001397 len = PyUnicode_GET_SIZE(uppercase_name);
1398 chk = PyUnicode_AS_UNICODE(uppercase_name);
1399 for (i=0; i<len; i++, chk++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001400 if ((*chk >= '0' && *chk <= '9')
1401 || (*chk >= 'A' && *chk <= 'Z')
1402 || (*chk == '_'))
1403 {
Victor Stinner35466c52010-04-22 11:23:23 +00001404 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001405 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001406 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001407 goto finally;
1408 }
1409 }
1410
Victor Stinner35466c52010-04-22 11:23:23 +00001411 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1412 if (!uppercase_name_str)
1413 goto finally;
1414
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001415 if (callable != Py_None && !PyCallable_Check(callable)) {
1416 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1417 goto finally;
1418 }
1419
1420 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001421 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1422 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001423 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001424 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1425 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001426 }
1427
1428 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001429 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001430 SQLITE_UTF8,
1431 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001432 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001433 if (rc != SQLITE_OK) {
1434 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001435 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001436 goto finally;
1437 }
1438
1439finally:
1440 Py_XDECREF(uppercase_name);
1441
1442 if (PyErr_Occurred()) {
1443 retval = NULL;
1444 } else {
1445 Py_INCREF(Py_None);
1446 retval = Py_None;
1447 }
1448
1449 return retval;
1450}
1451
Christian Heimesbbe741d2008-03-28 10:53:29 +00001452/* Called when the connection is used as a context manager. Returns itself as a
1453 * convenience to the caller. */
1454static PyObject *
1455pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1456{
1457 Py_INCREF(self);
1458 return (PyObject*)self;
1459}
1460
1461/** Called when the connection is used as a context manager. If there was any
1462 * exception, a rollback takes place; otherwise we commit. */
1463static PyObject *
1464pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1465{
1466 PyObject* exc_type, *exc_value, *exc_tb;
1467 char* method_name;
1468 PyObject* result;
1469
1470 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1471 return NULL;
1472 }
1473
1474 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1475 method_name = "commit";
1476 } else {
1477 method_name = "rollback";
1478 }
1479
1480 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1481 if (!result) {
1482 return NULL;
1483 }
1484 Py_DECREF(result);
1485
1486 Py_RETURN_FALSE;
1487}
1488
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001489static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001490PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001491
1492static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001493 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1494 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001495 {NULL}
1496};
1497
1498static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001499 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001500 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001501 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001502 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001503 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001504 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001505 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001506 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001507 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001508 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001509 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001510 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001511 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001512 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001513 #ifdef HAVE_LOAD_EXTENSION
1514 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1515 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1516 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1517 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1518 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001519 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1520 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001521 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001522 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001523 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001524 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001525 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001526 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001527 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001528 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001529 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001530 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001531 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001532 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001533 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1534 PyDoc_STR("For context manager. Non-standard.")},
1535 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1536 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001537 {NULL, NULL}
1538};
1539
1540static struct PyMemberDef connection_members[] =
1541{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001542 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1543 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1544 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1545 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1546 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1547 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1548 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1549 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1550 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1551 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001552 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1553 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001554 {NULL}
1555};
1556
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001557PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001558 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001559 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001560 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001561 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001562 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001563 0, /* tp_print */
1564 0, /* tp_getattr */
1565 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001566 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001567 0, /* tp_repr */
1568 0, /* tp_as_number */
1569 0, /* tp_as_sequence */
1570 0, /* tp_as_mapping */
1571 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001572 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001573 0, /* tp_str */
1574 0, /* tp_getattro */
1575 0, /* tp_setattro */
1576 0, /* tp_as_buffer */
1577 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1578 connection_doc, /* tp_doc */
1579 0, /* tp_traverse */
1580 0, /* tp_clear */
1581 0, /* tp_richcompare */
1582 0, /* tp_weaklistoffset */
1583 0, /* tp_iter */
1584 0, /* tp_iternext */
1585 connection_methods, /* tp_methods */
1586 connection_members, /* tp_members */
1587 connection_getset, /* tp_getset */
1588 0, /* tp_base */
1589 0, /* tp_dict */
1590 0, /* tp_descr_get */
1591 0, /* tp_descr_set */
1592 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001593 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001594 0, /* tp_alloc */
1595 0, /* tp_new */
1596 0 /* tp_free */
1597};
1598
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001599extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001600{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001601 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1602 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001603}