blob: 8bfc9ba65681ecd1c32b0d266a4a25635d2e8840 [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"
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) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000203 if (action == ACTION_RESET) {
204 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
205 } else {
206 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
207 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000208 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000210
211 if (reset_cursors) {
212 for (i = 0; i < PyList_Size(self->cursors); i++) {
213 weakref = PyList_GetItem(self->cursors, i);
214 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
215 if ((PyObject*)cursor != Py_None) {
216 cursor->reset = 1;
217 }
218 }
219 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220}
221
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000222void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000223{
224 Py_XDECREF(self->statement_cache);
225
226 /* Clean up if user has not called .close() explicitly. */
227 if (self->db) {
228 Py_BEGIN_ALLOW_THREADS
229 sqlite3_close(self->db);
230 Py_END_ALLOW_THREADS
231 }
232
233 if (self->begin_statement) {
234 PyMem_Free(self->begin_statement);
235 }
236 Py_XDECREF(self->isolation_level);
237 Py_XDECREF(self->function_pinboard);
238 Py_XDECREF(self->row_factory);
239 Py_XDECREF(self->text_factory);
240 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000241 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000242 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243
Christian Heimes90aa7642007-12-19 02:45:37 +0000244 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245}
246
Gerhard Häringf9cee222010-03-05 15:20:03 +0000247/*
248 * Registers a cursor with the connection.
249 *
250 * 0 => error; 1 => ok
251 */
252int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
253{
254 PyObject* weakref;
255
256 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
257 if (!weakref) {
258 goto error;
259 }
260
261 if (PyList_Append(connection->cursors, weakref) != 0) {
262 Py_CLEAR(weakref);
263 goto error;
264 }
265
266 Py_DECREF(weakref);
267
268 return 1;
269error:
270 return 0;
271}
272
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000273PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274{
275 static char *kwlist[] = {"factory", NULL, NULL};
276 PyObject* factory = NULL;
277 PyObject* cursor;
278
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000279 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
280 &factory)) {
281 return NULL;
282 }
283
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000284 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285 return NULL;
286 }
287
288 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000289 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000290 }
291
292 cursor = PyObject_CallFunction(factory, "O", self);
293
Gerhard Häringf9cee222010-03-05 15:20:03 +0000294 _pysqlite_drop_unused_cursor_references(self);
295
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296 if (cursor && self->row_factory != Py_None) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000297 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298 Py_INCREF(self->row_factory);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000299 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000300 }
301
302 return cursor;
303}
304
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000305PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000306{
307 int rc;
308
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000309 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000310 return NULL;
311 }
312
Gerhard Häringf9cee222010-03-05 15:20:03 +0000313 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000314
315 if (self->db) {
316 Py_BEGIN_ALLOW_THREADS
317 rc = sqlite3_close(self->db);
318 Py_END_ALLOW_THREADS
319
320 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000321 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000322 return NULL;
323 } else {
324 self->db = NULL;
325 }
326 }
327
328 Py_INCREF(Py_None);
329 return Py_None;
330}
331
332/*
333 * Checks if a connection object is usable (i. e. not closed).
334 *
335 * 0 => error; 1 => ok
336 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000337int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000339 if (!con->initialized) {
340 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
341 return 0;
342 }
343
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000345 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346 return 0;
347 } else {
348 return 1;
349 }
350}
351
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000352PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000353{
354 int rc;
355 const char* tail;
356 sqlite3_stmt* statement;
357
358 Py_BEGIN_ALLOW_THREADS
359 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
360 Py_END_ALLOW_THREADS
361
362 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000363 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364 goto error;
365 }
366
Benjamin Petersond7b03282008-09-13 15:58:53 +0000367 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 if (rc == SQLITE_DONE) {
369 self->inTransaction = 1;
370 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000371 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372 }
373
374 Py_BEGIN_ALLOW_THREADS
375 rc = sqlite3_finalize(statement);
376 Py_END_ALLOW_THREADS
377
378 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000379 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000380 }
381
382error:
383 if (PyErr_Occurred()) {
384 return NULL;
385 } else {
386 Py_INCREF(Py_None);
387 return Py_None;
388 }
389}
390
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000391PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392{
393 int rc;
394 const char* tail;
395 sqlite3_stmt* statement;
396
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000397 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000398 return NULL;
399 }
400
401 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000402 pysqlite_do_all_statements(self, ACTION_RESET, 0);
403
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404 Py_BEGIN_ALLOW_THREADS
405 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
406 Py_END_ALLOW_THREADS
407 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000408 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 goto error;
410 }
411
Benjamin Petersond7b03282008-09-13 15:58:53 +0000412 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413 if (rc == SQLITE_DONE) {
414 self->inTransaction = 0;
415 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000416 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000417 }
418
419 Py_BEGIN_ALLOW_THREADS
420 rc = sqlite3_finalize(statement);
421 Py_END_ALLOW_THREADS
422 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000423 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 }
425
426 }
427
428error:
429 if (PyErr_Occurred()) {
430 return NULL;
431 } else {
432 Py_INCREF(Py_None);
433 return Py_None;
434 }
435}
436
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000437PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000438{
439 int rc;
440 const char* tail;
441 sqlite3_stmt* statement;
442
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000443 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000444 return NULL;
445 }
446
447 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000448 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000449
450 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000451 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000452 Py_END_ALLOW_THREADS
453 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000454 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455 goto error;
456 }
457
Benjamin Petersond7b03282008-09-13 15:58:53 +0000458 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000459 if (rc == SQLITE_DONE) {
460 self->inTransaction = 0;
461 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000462 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463 }
464
465 Py_BEGIN_ALLOW_THREADS
466 rc = sqlite3_finalize(statement);
467 Py_END_ALLOW_THREADS
468 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000469 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000470 }
471
472 }
473
474error:
475 if (PyErr_Occurred()) {
476 return NULL;
477 } else {
478 Py_INCREF(Py_None);
479 return Py_None;
480 }
481}
482
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000483void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000484{
485 long longval;
486 const char* buffer;
487 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000488
Thomas Wouters477c8d52006-05-27 19:21:47 +0000489 if ((!py_val) || PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490 sqlite3_result_null(context);
491 } else if (py_val == Py_None) {
492 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000493 } else if (PyLong_Check(py_val)) {
494 longval = PyLong_AsLong(py_val);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000495 sqlite3_result_int64(context, (PY_LONG_LONG)longval);
496 } else if (PyFloat_Check(py_val)) {
497 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000498 } else if (PyUnicode_Check(py_val)) {
Victor Stinner86999502010-05-19 01:27:23 +0000499 char *str = _PyUnicode_AsString(py_val);
500 if (str != NULL)
501 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000502 } else if (PyObject_CheckBuffer(py_val)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
504 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000505 } else {
506 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 } else {
509 /* TODO: raise error */
510 }
511}
512
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000513PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000514{
515 PyObject* args;
516 int i;
517 sqlite3_value* cur_value;
518 PyObject* cur_py_value;
519 const char* val_str;
520 PY_LONG_LONG val_int;
521 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000522
523 args = PyTuple_New(argc);
524 if (!args) {
525 return NULL;
526 }
527
528 for (i = 0; i < argc; i++) {
529 cur_value = argv[i];
530 switch (sqlite3_value_type(argv[i])) {
531 case SQLITE_INTEGER:
532 val_int = sqlite3_value_int64(cur_value);
Christian Heimes217cfd12007-12-02 14:31:20 +0000533 cur_py_value = PyLong_FromLong((long)val_int);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000534 break;
535 case SQLITE_FLOAT:
536 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
537 break;
538 case SQLITE_TEXT:
539 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000540 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541 /* TODO: have a way to show errors here */
542 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000543 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544 Py_INCREF(Py_None);
545 cur_py_value = Py_None;
546 }
547 break;
548 case SQLITE_BLOB:
549 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000550 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000551 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000552 break;
553 case SQLITE_NULL:
554 default:
555 Py_INCREF(Py_None);
556 cur_py_value = Py_None;
557 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000558
559 if (!cur_py_value) {
560 Py_DECREF(args);
561 return NULL;
562 }
563
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000564 PyTuple_SetItem(args, i, cur_py_value);
565
566 }
567
568 return args;
569}
570
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000571void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000572{
573 PyObject* args;
574 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000575 PyObject* py_retval = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000576
Georg Brandldfd73442009-04-05 11:47:34 +0000577#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000578 PyGILState_STATE threadstate;
579
580 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000581#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000582
583 py_func = (PyObject*)sqlite3_user_data(context);
584
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000585 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000586 if (args) {
587 py_retval = PyObject_CallObject(py_func, args);
588 Py_DECREF(args);
589 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000591 if (py_retval) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000592 _pysqlite_set_result(context, py_retval);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593 Py_DECREF(py_retval);
594 } else {
595 if (_enable_callback_tracebacks) {
596 PyErr_Print();
597 } else {
598 PyErr_Clear();
599 }
600 _sqlite3_result_error(context, "user-defined function raised exception", -1);
601 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602
Georg Brandldfd73442009-04-05 11:47:34 +0000603#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000605#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606}
607
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000608static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000609{
610 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000612 PyObject* aggregate_class;
613 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000615
Georg Brandldfd73442009-04-05 11:47:34 +0000616#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000617 PyGILState_STATE threadstate;
618
619 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000620#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621
622 aggregate_class = (PyObject*)sqlite3_user_data(context);
623
624 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
625
626 if (*aggregate_instance == 0) {
627 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
628
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000630 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631 if (_enable_callback_tracebacks) {
632 PyErr_Print();
633 } else {
634 PyErr_Clear();
635 }
636 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000637 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000638 }
639 }
640
641 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000642 if (!stepmethod) {
643 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644 }
645
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000646 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000647 if (!args) {
648 goto error;
649 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650
651 function_result = PyObject_CallObject(stepmethod, args);
652 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000653
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000655 if (_enable_callback_tracebacks) {
656 PyErr_Print();
657 } else {
658 PyErr_Clear();
659 }
660 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661 }
662
Thomas Wouters477c8d52006-05-27 19:21:47 +0000663error:
664 Py_XDECREF(stepmethod);
665 Py_XDECREF(function_result);
666
Georg Brandldfd73442009-04-05 11:47:34 +0000667#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000669#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670}
671
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000672void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000673{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000674 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 PyObject** aggregate_instance;
676 PyObject* aggregate_class;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677
Georg Brandldfd73442009-04-05 11:47:34 +0000678#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000679 PyGILState_STATE threadstate;
680
681 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000682#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683
684 aggregate_class = (PyObject*)sqlite3_user_data(context);
685
686 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
687 if (!*aggregate_instance) {
688 /* this branch is executed if there was an exception in the aggregate's
689 * __init__ */
690
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692 }
693
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
695 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000696 if (_enable_callback_tracebacks) {
697 PyErr_Print();
698 } else {
699 PyErr_Clear();
700 }
701 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
702 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000703 _pysqlite_set_result(context, function_result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 }
705
Thomas Wouters477c8d52006-05-27 19:21:47 +0000706error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707 Py_XDECREF(*aggregate_instance);
708 Py_XDECREF(function_result);
709
Georg Brandldfd73442009-04-05 11:47:34 +0000710#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000712#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713}
714
Gerhard Häringf9cee222010-03-05 15:20:03 +0000715static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000716{
717 PyObject* new_list;
718 PyObject* weakref;
719 int i;
720
721 /* we only need to do this once in a while */
722 if (self->created_statements++ < 200) {
723 return;
724 }
725
726 self->created_statements = 0;
727
728 new_list = PyList_New(0);
729 if (!new_list) {
730 return;
731 }
732
733 for (i = 0; i < PyList_Size(self->statements); i++) {
734 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000735 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000736 if (PyList_Append(new_list, weakref) != 0) {
737 Py_DECREF(new_list);
738 return;
739 }
740 }
741 }
742
743 Py_DECREF(self->statements);
744 self->statements = new_list;
745}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000746
Gerhard Häringf9cee222010-03-05 15:20:03 +0000747static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
748{
749 PyObject* new_list;
750 PyObject* weakref;
751 int i;
752
753 /* we only need to do this once in a while */
754 if (self->created_cursors++ < 200) {
755 return;
756 }
757
758 self->created_cursors = 0;
759
760 new_list = PyList_New(0);
761 if (!new_list) {
762 return;
763 }
764
765 for (i = 0; i < PyList_Size(self->cursors); i++) {
766 weakref = PyList_GetItem(self->cursors, i);
767 if (PyWeakref_GetObject(weakref) != Py_None) {
768 if (PyList_Append(new_list, weakref) != 0) {
769 Py_DECREF(new_list);
770 return;
771 }
772 }
773 }
774
775 Py_DECREF(self->cursors);
776 self->cursors = new_list;
777}
778
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000779PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780{
781 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
782
783 PyObject* func;
784 char* name;
785 int narg;
786 int rc;
787
Gerhard Häringf9cee222010-03-05 15:20:03 +0000788 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
789 return NULL;
790 }
791
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000792 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
793 &name, &narg, &func))
794 {
795 return NULL;
796 }
797
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000798 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000799
Thomas Wouters477c8d52006-05-27 19:21:47 +0000800 if (rc != SQLITE_OK) {
801 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000802 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000803 return NULL;
804 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000805 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
806 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000807
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808 Py_INCREF(Py_None);
809 return Py_None;
810 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000811}
812
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000813PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000814{
815 PyObject* aggregate_class;
816
817 int n_arg;
818 char* name;
819 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
820 int rc;
821
Gerhard Häringf9cee222010-03-05 15:20:03 +0000822 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
823 return NULL;
824 }
825
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
827 kwlist, &name, &n_arg, &aggregate_class)) {
828 return NULL;
829 }
830
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000831 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 +0000832 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000833 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000834 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835 return NULL;
836 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000837 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
838 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839
840 Py_INCREF(Py_None);
841 return Py_None;
842 }
843}
844
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000845static 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 +0000846{
847 PyObject *ret;
848 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000849#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000850 PyGILState_STATE gilstate;
851
852 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000853#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000854 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
855
856 if (!ret) {
857 if (_enable_callback_tracebacks) {
858 PyErr_Print();
859 } else {
860 PyErr_Clear();
861 }
862
863 rc = SQLITE_DENY;
864 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000865 if (PyLong_Check(ret)) {
866 rc = (int)PyLong_AsLong(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000867 } else {
868 rc = SQLITE_DENY;
869 }
870 Py_DECREF(ret);
871 }
872
Georg Brandldfd73442009-04-05 11:47:34 +0000873#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000874 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000875#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000876 return rc;
877}
878
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000879static int _progress_handler(void* user_arg)
880{
881 int rc;
882 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000883#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000884 PyGILState_STATE gilstate;
885
886 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000887#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000888 ret = PyObject_CallFunction((PyObject*)user_arg, "");
889
890 if (!ret) {
891 if (_enable_callback_tracebacks) {
892 PyErr_Print();
893 } else {
894 PyErr_Clear();
895 }
896
Mark Dickinson934896d2009-02-21 20:59:32 +0000897 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000898 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000899 } else {
900 rc = (int)PyObject_IsTrue(ret);
901 Py_DECREF(ret);
902 }
903
Georg Brandldfd73442009-04-05 11:47:34 +0000904#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000905 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000906#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000907 return rc;
908}
909
Gerhard Häringf9cee222010-03-05 15:20:03 +0000910static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000911{
912 PyObject* authorizer_cb;
913
914 static char *kwlist[] = { "authorizer_callback", NULL };
915 int rc;
916
Gerhard Häringf9cee222010-03-05 15:20:03 +0000917 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
918 return NULL;
919 }
920
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000921 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
922 kwlist, &authorizer_cb)) {
923 return NULL;
924 }
925
926 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
927
928 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000929 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000930 return NULL;
931 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000932 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
933 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000934
935 Py_INCREF(Py_None);
936 return Py_None;
937 }
938}
939
Gerhard Häringf9cee222010-03-05 15:20:03 +0000940static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000941{
942 PyObject* progress_handler;
943 int n;
944
945 static char *kwlist[] = { "progress_handler", "n", NULL };
946
Gerhard Häringf9cee222010-03-05 15:20:03 +0000947 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
948 return NULL;
949 }
950
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000951 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
952 kwlist, &progress_handler, &n)) {
953 return NULL;
954 }
955
956 if (progress_handler == Py_None) {
957 /* None clears the progress handler previously set */
958 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
959 } else {
960 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000961 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
962 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000963 }
964
965 Py_INCREF(Py_None);
966 return Py_None;
967}
968
Gerhard Häringf9cee222010-03-05 15:20:03 +0000969#ifdef HAVE_LOAD_EXTENSION
970static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
971{
972 int rc;
973 int onoff;
974
975 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
976 return NULL;
977 }
978
979 if (!PyArg_ParseTuple(args, "i", &onoff)) {
980 return NULL;
981 }
982
983 rc = sqlite3_enable_load_extension(self->db, onoff);
984
985 if (rc != SQLITE_OK) {
986 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
987 return NULL;
988 } else {
989 Py_INCREF(Py_None);
990 return Py_None;
991 }
992}
993
994static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
995{
996 int rc;
997 char* extension_name;
998 char* errmsg;
999
1000 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1001 return NULL;
1002 }
1003
1004 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1005 return NULL;
1006 }
1007
1008 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1009 if (rc != 0) {
1010 PyErr_SetString(pysqlite_OperationalError, errmsg);
1011 return NULL;
1012 } else {
1013 Py_INCREF(Py_None);
1014 return Py_None;
1015 }
1016}
1017#endif
1018
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001019int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020{
Georg Brandldfd73442009-04-05 11:47:34 +00001021#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001022 if (self->check_same_thread) {
1023 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001024 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001025 "SQLite objects created in a thread can only be used in that same thread."
1026 "The object was created in thread id %ld and this is thread id %ld",
1027 self->thread_ident, PyThread_get_thread_ident());
1028 return 0;
1029 }
1030
1031 }
Georg Brandldfd73442009-04-05 11:47:34 +00001032#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001033 return 1;
1034}
1035
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001036static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001037{
1038 Py_INCREF(self->isolation_level);
1039 return self->isolation_level;
1040}
1041
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001042static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001043{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001044 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001045 return NULL;
1046 } else {
1047 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1048 }
1049}
1050
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001051static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001052{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001053 PyObject* res;
1054 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001055 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001056
1057 Py_XDECREF(self->isolation_level);
1058
1059 if (self->begin_statement) {
1060 PyMem_Free(self->begin_statement);
1061 self->begin_statement = NULL;
1062 }
1063
1064 if (isolation_level == Py_None) {
1065 Py_INCREF(Py_None);
1066 self->isolation_level = Py_None;
1067
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001068 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001069 if (!res) {
1070 return -1;
1071 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001072 Py_DECREF(res);
1073
1074 self->inTransaction = 0;
1075 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001076 const char *statement;
1077 Py_ssize_t size;
1078
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001079 Py_INCREF(isolation_level);
1080 self->isolation_level = isolation_level;
1081
Georg Brandlceab6102007-11-25 00:45:05 +00001082 if (!begin_word) {
1083 begin_word = PyUnicode_FromString("BEGIN ");
1084 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085 }
Georg Brandlceab6102007-11-25 00:45:05 +00001086 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001087 if (!begin_statement) {
1088 return -1;
1089 }
1090
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001091 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001092 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001093 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001094 return -1;
1095 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001096 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001097 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001098 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001099 return -1;
1100 }
1101
Neal Norwitzefee9f52007-10-27 02:50:52 +00001102 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001103 Py_DECREF(begin_statement);
1104 }
1105
1106 return 0;
1107}
1108
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001109PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001110{
1111 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001112 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001113 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001114 int rc;
1115
Gerhard Häringf9cee222010-03-05 15:20:03 +00001116 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1117 return NULL;
1118 }
1119
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001120 if (!PyArg_ParseTuple(args, "O", &sql)) {
1121 return NULL;
1122 }
1123
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001124 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001125
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001126 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127 if (!statement) {
1128 return NULL;
1129 }
1130
Victor Stinner0201f442010-03-13 03:28:34 +00001131 statement->db = NULL;
1132 statement->st = NULL;
1133 statement->sql = NULL;
1134 statement->in_use = 0;
1135 statement->in_weakreflist = NULL;
1136
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001137 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001138
1139 if (rc != SQLITE_OK) {
1140 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001141 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001142 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001143 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001145 (void)pysqlite_statement_reset(statement);
1146 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147 }
1148
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001149 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001150 } else {
1151 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1152 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001153 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001154 goto error;
1155 }
1156
1157 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001158 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001159 goto error;
1160 }
1161
1162 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163 }
1164
Thomas Wouters477c8d52006-05-27 19:21:47 +00001165error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166 return (PyObject*)statement;
1167}
1168
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001169PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170{
1171 PyObject* cursor = 0;
1172 PyObject* result = 0;
1173 PyObject* method = 0;
1174
1175 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1176 if (!cursor) {
1177 goto error;
1178 }
1179
1180 method = PyObject_GetAttrString(cursor, "execute");
1181 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001182 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001183 goto error;
1184 }
1185
1186 result = PyObject_CallObject(method, args);
1187 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001188 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001189 }
1190
1191error:
1192 Py_XDECREF(result);
1193 Py_XDECREF(method);
1194
1195 return cursor;
1196}
1197
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001198PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001199{
1200 PyObject* cursor = 0;
1201 PyObject* result = 0;
1202 PyObject* method = 0;
1203
1204 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1205 if (!cursor) {
1206 goto error;
1207 }
1208
1209 method = PyObject_GetAttrString(cursor, "executemany");
1210 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001211 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001212 goto error;
1213 }
1214
1215 result = PyObject_CallObject(method, args);
1216 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001217 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001218 }
1219
1220error:
1221 Py_XDECREF(result);
1222 Py_XDECREF(method);
1223
1224 return cursor;
1225}
1226
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001227PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228{
1229 PyObject* cursor = 0;
1230 PyObject* result = 0;
1231 PyObject* method = 0;
1232
1233 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1234 if (!cursor) {
1235 goto error;
1236 }
1237
1238 method = PyObject_GetAttrString(cursor, "executescript");
1239 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001240 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001241 goto error;
1242 }
1243
1244 result = PyObject_CallObject(method, args);
1245 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001246 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001247 }
1248
1249error:
1250 Py_XDECREF(result);
1251 Py_XDECREF(method);
1252
1253 return cursor;
1254}
1255
1256/* ------------------------- COLLATION CODE ------------------------ */
1257
1258static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001259pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001260 void* context,
1261 int text1_length, const void* text1_data,
1262 int text2_length, const void* text2_data)
1263{
1264 PyObject* callback = (PyObject*)context;
1265 PyObject* string1 = 0;
1266 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001267#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001269#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270 PyObject* retval = NULL;
1271 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001272#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001273 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001274#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275
1276 if (PyErr_Occurred()) {
1277 goto finally;
1278 }
1279
Guido van Rossum98297ee2007-11-06 21:34:58 +00001280 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1281 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001282
1283 if (!string1 || !string2) {
1284 goto finally; /* failed to allocate strings */
1285 }
1286
1287 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1288
1289 if (!retval) {
1290 /* execution failed */
1291 goto finally;
1292 }
1293
Christian Heimes217cfd12007-12-02 14:31:20 +00001294 result = PyLong_AsLong(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001295 if (PyErr_Occurred()) {
1296 result = 0;
1297 }
1298
1299finally:
1300 Py_XDECREF(string1);
1301 Py_XDECREF(string2);
1302 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001303#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001304 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001305#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001306 return result;
1307}
1308
1309static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001310pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001311{
1312 PyObject* retval = NULL;
1313
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001314 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001315 goto finally;
1316 }
1317
1318 sqlite3_interrupt(self->db);
1319
1320 Py_INCREF(Py_None);
1321 retval = Py_None;
1322
1323finally:
1324 return retval;
1325}
1326
Christian Heimesbbe741d2008-03-28 10:53:29 +00001327/* Function author: Paul Kippes <kippesp@gmail.com>
1328 * Class method of Connection to call the Python function _iterdump
1329 * of the sqlite3 module.
1330 */
1331static PyObject *
1332pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1333{
1334 PyObject* retval = NULL;
1335 PyObject* module = NULL;
1336 PyObject* module_dict;
1337 PyObject* pyfn_iterdump;
1338
1339 if (!pysqlite_check_connection(self)) {
1340 goto finally;
1341 }
1342
1343 module = PyImport_ImportModule(MODULE_NAME ".dump");
1344 if (!module) {
1345 goto finally;
1346 }
1347
1348 module_dict = PyModule_GetDict(module);
1349 if (!module_dict) {
1350 goto finally;
1351 }
1352
1353 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1354 if (!pyfn_iterdump) {
1355 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1356 goto finally;
1357 }
1358
1359 args = PyTuple_New(1);
1360 if (!args) {
1361 goto finally;
1362 }
1363 Py_INCREF(self);
1364 PyTuple_SetItem(args, 0, (PyObject*)self);
1365 retval = PyObject_CallObject(pyfn_iterdump, args);
1366
1367finally:
1368 Py_XDECREF(args);
1369 Py_XDECREF(module);
1370 return retval;
1371}
1372
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001373static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001374pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001375{
1376 PyObject* callable;
1377 PyObject* uppercase_name = 0;
1378 PyObject* name;
1379 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001380 Py_UNICODE* chk;
1381 Py_ssize_t i, len;
1382 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001383 int rc;
1384
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001385 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001386 goto finally;
1387 }
1388
Gerhard Häring6d214562007-08-10 18:15:11 +00001389 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001390 goto finally;
1391 }
1392
1393 uppercase_name = PyObject_CallMethod(name, "upper", "");
1394 if (!uppercase_name) {
1395 goto finally;
1396 }
1397
Victor Stinner35466c52010-04-22 11:23:23 +00001398 len = PyUnicode_GET_SIZE(uppercase_name);
1399 chk = PyUnicode_AS_UNICODE(uppercase_name);
1400 for (i=0; i<len; i++, chk++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001401 if ((*chk >= '0' && *chk <= '9')
1402 || (*chk >= 'A' && *chk <= 'Z')
1403 || (*chk == '_'))
1404 {
Victor Stinner35466c52010-04-22 11:23:23 +00001405 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001406 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001407 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001408 goto finally;
1409 }
1410 }
1411
Victor Stinner35466c52010-04-22 11:23:23 +00001412 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1413 if (!uppercase_name_str)
1414 goto finally;
1415
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001416 if (callable != Py_None && !PyCallable_Check(callable)) {
1417 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1418 goto finally;
1419 }
1420
1421 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001422 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1423 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001424 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001425 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1426 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001427 }
1428
1429 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001430 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431 SQLITE_UTF8,
1432 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001433 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001434 if (rc != SQLITE_OK) {
1435 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001436 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001437 goto finally;
1438 }
1439
1440finally:
1441 Py_XDECREF(uppercase_name);
1442
1443 if (PyErr_Occurred()) {
1444 retval = NULL;
1445 } else {
1446 Py_INCREF(Py_None);
1447 retval = Py_None;
1448 }
1449
1450 return retval;
1451}
1452
Christian Heimesbbe741d2008-03-28 10:53:29 +00001453/* Called when the connection is used as a context manager. Returns itself as a
1454 * convenience to the caller. */
1455static PyObject *
1456pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1457{
1458 Py_INCREF(self);
1459 return (PyObject*)self;
1460}
1461
1462/** Called when the connection is used as a context manager. If there was any
1463 * exception, a rollback takes place; otherwise we commit. */
1464static PyObject *
1465pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1466{
1467 PyObject* exc_type, *exc_value, *exc_tb;
1468 char* method_name;
1469 PyObject* result;
1470
1471 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1472 return NULL;
1473 }
1474
1475 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1476 method_name = "commit";
1477 } else {
1478 method_name = "rollback";
1479 }
1480
1481 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1482 if (!result) {
1483 return NULL;
1484 }
1485 Py_DECREF(result);
1486
1487 Py_RETURN_FALSE;
1488}
1489
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001491PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001492
1493static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001494 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1495 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001496 {NULL}
1497};
1498
1499static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001500 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001501 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001502 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001503 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001504 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001505 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001506 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001507 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001508 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001509 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001510 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001511 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001512 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001513 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001514 #ifdef HAVE_LOAD_EXTENSION
1515 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1516 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1517 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1518 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1519 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001520 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1521 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001522 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001524 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001525 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001526 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001527 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001528 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001529 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001530 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001531 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001532 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001533 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001534 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1535 PyDoc_STR("For context manager. Non-standard.")},
1536 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1537 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001538 {NULL, NULL}
1539};
1540
1541static struct PyMemberDef connection_members[] =
1542{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001543 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1544 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1545 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1546 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1547 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1548 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1549 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1550 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1551 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1552 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001553 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1554 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001555 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001556 {NULL}
1557};
1558
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001559PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001560 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001561 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001562 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001563 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001564 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001565 0, /* tp_print */
1566 0, /* tp_getattr */
1567 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001568 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001569 0, /* tp_repr */
1570 0, /* tp_as_number */
1571 0, /* tp_as_sequence */
1572 0, /* tp_as_mapping */
1573 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001574 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001575 0, /* tp_str */
1576 0, /* tp_getattro */
1577 0, /* tp_setattro */
1578 0, /* tp_as_buffer */
1579 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1580 connection_doc, /* tp_doc */
1581 0, /* tp_traverse */
1582 0, /* tp_clear */
1583 0, /* tp_richcompare */
1584 0, /* tp_weaklistoffset */
1585 0, /* tp_iter */
1586 0, /* tp_iternext */
1587 connection_methods, /* tp_methods */
1588 connection_members, /* tp_members */
1589 connection_getset, /* tp_getset */
1590 0, /* tp_base */
1591 0, /* tp_dict */
1592 0, /* tp_descr_get */
1593 0, /* tp_descr_set */
1594 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001595 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001596 0, /* tp_alloc */
1597 0, /* tp_new */
1598 0 /* tp_free */
1599};
1600
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001601extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001602{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001603 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1604 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001605}