blob: 9d4c72f695bad80ffc87618be297c769a6ac2ca9 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* connection.c - the connection type
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
5 * This file is part of pysqlite.
Victor Stinner86999502010-05-19 01:27:23 +00006 *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "cache.h"
25#include "module.h"
R. David Murrayd35251d2010-06-01 01:32:12 +000026#include "structmember.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027#include "connection.h"
28#include "statement.h"
29#include "cursor.h"
30#include "prepare_protocol.h"
31#include "util.h"
32#include "sqlitecompat.h"
33
34#include "pythread.h"
35
Gerhard Häringe7ea7452008-03-29 00:45:29 +000036#define ACTION_FINALIZE 1
37#define ACTION_RESET 2
38
Gerhard Häringf9cee222010-03-05 15:20:03 +000039#if SQLITE_VERSION_NUMBER >= 3003008
40#ifndef SQLITE_OMIT_LOAD_EXTENSION
41#define HAVE_LOAD_EXTENSION
42#endif
43#endif
44
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000045static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Gerhard Häringf9cee222010-03-05 15:20:03 +000046static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047
Thomas Wouters0e3f5912006-08-11 14:57:12 +000048
Benjamin Petersond7b03282008-09-13 15:58:53 +000049static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050{
51 /* in older SQLite versions, calling sqlite3_result_error in callbacks
52 * triggers a bug in SQLite that leads either to irritating results or
53 * segfaults, depending on the SQLite version */
54#if SQLITE_VERSION_NUMBER >= 3003003
55 sqlite3_result_error(ctx, errmsg, len);
56#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000057 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058#endif
59}
60
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000061int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062{
63 static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
64
65 char* database;
66 int detect_types = 0;
67 PyObject* isolation_level = NULL;
68 PyObject* factory = NULL;
69 int check_same_thread = 1;
70 int cached_statements = 100;
71 double timeout = 5.0;
72 int rc;
73
74 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist,
75 &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
76 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000077 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 }
79
Gerhard Häringf9cee222010-03-05 15:20:03 +000080 self->initialized = 1;
81
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 self->begin_statement = NULL;
83
84 self->statement_cache = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000085 self->statements = NULL;
Gerhard Häringf9cee222010-03-05 15:20:03 +000086 self->cursors = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087
88 Py_INCREF(Py_None);
89 self->row_factory = Py_None;
90
91 Py_INCREF(&PyUnicode_Type);
92 self->text_factory = (PyObject*)&PyUnicode_Type;
93
94 Py_BEGIN_ALLOW_THREADS
95 rc = sqlite3_open(database, &self->db);
96 Py_END_ALLOW_THREADS
97
98 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000099 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000100 return -1;
101 }
102
103 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000104 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000105 if (!isolation_level) {
106 return -1;
107 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108 } else {
109 Py_INCREF(isolation_level);
110 }
111 self->isolation_level = NULL;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000112 pysqlite_connection_set_isolation_level(self, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 Py_DECREF(isolation_level);
114
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000115 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116 if (PyErr_Occurred()) {
117 return -1;
118 }
119
Gerhard Häringf9cee222010-03-05 15:20:03 +0000120 self->created_statements = 0;
121 self->created_cursors = 0;
122
123 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000125 self->cursors = PyList_New(0);
126 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000127 return -1;
128 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000129
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 /* By default, the Cache class INCREFs the factory in its initializer, and
131 * decrefs it in its deallocator method. Since this would create a circular
132 * reference here, we're breaking it by decrementing self, and telling the
133 * cache class to not decref the factory (self) in its deallocator.
134 */
135 self->statement_cache->decref_factory = 0;
136 Py_DECREF(self);
137
138 self->inTransaction = 0;
139 self->detect_types = detect_types;
140 self->timeout = timeout;
141 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000142#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000144#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000145 self->check_same_thread = check_same_thread;
146
147 self->function_pinboard = PyDict_New();
148 if (!self->function_pinboard) {
149 return -1;
150 }
151
152 self->collations = PyDict_New();
153 if (!self->collations) {
154 return -1;
155 }
156
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000157 self->Warning = pysqlite_Warning;
158 self->Error = pysqlite_Error;
159 self->InterfaceError = pysqlite_InterfaceError;
160 self->DatabaseError = pysqlite_DatabaseError;
161 self->DataError = pysqlite_DataError;
162 self->OperationalError = pysqlite_OperationalError;
163 self->IntegrityError = pysqlite_IntegrityError;
164 self->InternalError = pysqlite_InternalError;
165 self->ProgrammingError = pysqlite_ProgrammingError;
166 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167
168 return 0;
169}
170
Thomas Wouters477c8d52006-05-27 19:21:47 +0000171/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000172void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000174 pysqlite_Node* node;
175 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000176
177 node = self->statement_cache->first;
178
179 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000180 statement = (pysqlite_Statement*)(node->data);
181 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182 node = node->next;
183 }
184
185 Py_DECREF(self->statement_cache);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000186 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000187 Py_DECREF(self);
188 self->statement_cache->decref_factory = 0;
189}
190
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000191/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000192void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000194 int i;
195 PyObject* weakref;
196 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000197 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198
Thomas Wouters477c8d52006-05-27 19:21:47 +0000199 for (i = 0; i < PyList_Size(self->statements); i++) {
200 weakref = PyList_GetItem(self->statements, i);
201 statement = PyWeakref_GetObject(weakref);
202 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500203 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000204 if (action == ACTION_RESET) {
205 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
206 } else {
207 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
208 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500209 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000210 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000212
213 if (reset_cursors) {
214 for (i = 0; i < PyList_Size(self->cursors); i++) {
215 weakref = PyList_GetItem(self->cursors, i);
216 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
217 if ((PyObject*)cursor != Py_None) {
218 cursor->reset = 1;
219 }
220 }
221 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222}
223
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000224void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225{
226 Py_XDECREF(self->statement_cache);
227
228 /* Clean up if user has not called .close() explicitly. */
229 if (self->db) {
230 Py_BEGIN_ALLOW_THREADS
231 sqlite3_close(self->db);
232 Py_END_ALLOW_THREADS
233 }
234
235 if (self->begin_statement) {
236 PyMem_Free(self->begin_statement);
237 }
238 Py_XDECREF(self->isolation_level);
239 Py_XDECREF(self->function_pinboard);
240 Py_XDECREF(self->row_factory);
241 Py_XDECREF(self->text_factory);
242 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000243 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000244 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245
Christian Heimes90aa7642007-12-19 02:45:37 +0000246 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247}
248
Gerhard Häringf9cee222010-03-05 15:20:03 +0000249/*
250 * Registers a cursor with the connection.
251 *
252 * 0 => error; 1 => ok
253 */
254int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
255{
256 PyObject* weakref;
257
258 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
259 if (!weakref) {
260 goto error;
261 }
262
263 if (PyList_Append(connection->cursors, weakref) != 0) {
264 Py_CLEAR(weakref);
265 goto error;
266 }
267
268 Py_DECREF(weakref);
269
270 return 1;
271error:
272 return 0;
273}
274
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000275PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000276{
277 static char *kwlist[] = {"factory", NULL, NULL};
278 PyObject* factory = NULL;
279 PyObject* cursor;
280
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000281 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
282 &factory)) {
283 return NULL;
284 }
285
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000286 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 return NULL;
288 }
289
290 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000291 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292 }
293
294 cursor = PyObject_CallFunction(factory, "O", self);
295
Gerhard Häringf9cee222010-03-05 15:20:03 +0000296 _pysqlite_drop_unused_cursor_references(self);
297
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298 if (cursor && self->row_factory != Py_None) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000299 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000300 Py_INCREF(self->row_factory);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000301 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302 }
303
304 return cursor;
305}
306
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000307PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308{
309 int rc;
310
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000311 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312 return NULL;
313 }
314
Gerhard Häringf9cee222010-03-05 15:20:03 +0000315 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316
317 if (self->db) {
318 Py_BEGIN_ALLOW_THREADS
319 rc = sqlite3_close(self->db);
320 Py_END_ALLOW_THREADS
321
322 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000323 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324 return NULL;
325 } else {
326 self->db = NULL;
327 }
328 }
329
330 Py_INCREF(Py_None);
331 return Py_None;
332}
333
334/*
335 * Checks if a connection object is usable (i. e. not closed).
336 *
337 * 0 => error; 1 => ok
338 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000339int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000340{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000341 if (!con->initialized) {
342 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
343 return 0;
344 }
345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000347 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000348 return 0;
349 } else {
350 return 1;
351 }
352}
353
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000354PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355{
356 int rc;
357 const char* tail;
358 sqlite3_stmt* statement;
359
360 Py_BEGIN_ALLOW_THREADS
361 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
362 Py_END_ALLOW_THREADS
363
364 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000365 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 goto error;
367 }
368
Benjamin Petersond7b03282008-09-13 15:58:53 +0000369 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 if (rc == SQLITE_DONE) {
371 self->inTransaction = 1;
372 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000373 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374 }
375
376 Py_BEGIN_ALLOW_THREADS
377 rc = sqlite3_finalize(statement);
378 Py_END_ALLOW_THREADS
379
380 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000381 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 }
383
384error:
385 if (PyErr_Occurred()) {
386 return NULL;
387 } else {
388 Py_INCREF(Py_None);
389 return Py_None;
390 }
391}
392
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000393PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394{
395 int rc;
396 const char* tail;
397 sqlite3_stmt* statement;
398
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000399 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 return NULL;
401 }
402
403 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000404 pysqlite_do_all_statements(self, ACTION_RESET, 0);
405
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406 Py_BEGIN_ALLOW_THREADS
407 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
408 Py_END_ALLOW_THREADS
409 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000410 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000411 goto error;
412 }
413
Benjamin Petersond7b03282008-09-13 15:58:53 +0000414 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000415 if (rc == SQLITE_DONE) {
416 self->inTransaction = 0;
417 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000418 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419 }
420
421 Py_BEGIN_ALLOW_THREADS
422 rc = sqlite3_finalize(statement);
423 Py_END_ALLOW_THREADS
424 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000425 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426 }
427
428 }
429
430error:
431 if (PyErr_Occurred()) {
432 return NULL;
433 } else {
434 Py_INCREF(Py_None);
435 return Py_None;
436 }
437}
438
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000439PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000440{
441 int rc;
442 const char* tail;
443 sqlite3_stmt* statement;
444
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000445 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446 return NULL;
447 }
448
449 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000450 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451
452 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000453 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000454 Py_END_ALLOW_THREADS
455 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000456 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457 goto error;
458 }
459
Benjamin Petersond7b03282008-09-13 15:58:53 +0000460 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461 if (rc == SQLITE_DONE) {
462 self->inTransaction = 0;
463 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000464 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000465 }
466
467 Py_BEGIN_ALLOW_THREADS
468 rc = sqlite3_finalize(statement);
469 Py_END_ALLOW_THREADS
470 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000471 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000472 }
473
474 }
475
476error:
477 if (PyErr_Occurred()) {
478 return NULL;
479 } else {
480 Py_INCREF(Py_None);
481 return Py_None;
482 }
483}
484
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000485void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000486{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000487 const char* buffer;
488 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000489
Thomas Wouters477c8d52006-05-27 19:21:47 +0000490 if ((!py_val) || PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000491 sqlite3_result_null(context);
492 } else if (py_val == Py_None) {
493 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000494 } else if (PyLong_Check(py_val)) {
Petri Lehtinen4fe85ab2012-02-19 21:38:00 +0200495 sqlite3_result_int64(context, PyLong_AsLongLong(py_val));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000496 } 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;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520 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:
Petri Lehtinen4fe85ab2012-02-19 21:38:00 +0200531 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000532 break;
533 case SQLITE_FLOAT:
534 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
535 break;
536 case SQLITE_TEXT:
537 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000538 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539 /* TODO: have a way to show errors here */
540 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000541 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542 Py_INCREF(Py_None);
543 cur_py_value = Py_None;
544 }
545 break;
546 case SQLITE_BLOB:
547 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000548 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000549 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550 break;
551 case SQLITE_NULL:
552 default:
553 Py_INCREF(Py_None);
554 cur_py_value = Py_None;
555 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000556
557 if (!cur_py_value) {
558 Py_DECREF(args);
559 return NULL;
560 }
561
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562 PyTuple_SetItem(args, i, cur_py_value);
563
564 }
565
566 return args;
567}
568
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000569void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000570{
571 PyObject* args;
572 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000573 PyObject* py_retval = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000574
Georg Brandldfd73442009-04-05 11:47:34 +0000575#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000576 PyGILState_STATE threadstate;
577
578 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000579#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000580
581 py_func = (PyObject*)sqlite3_user_data(context);
582
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000583 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000584 if (args) {
585 py_retval = PyObject_CallObject(py_func, args);
586 Py_DECREF(args);
587 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000588
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000589 if (py_retval) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000590 _pysqlite_set_result(context, py_retval);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000591 Py_DECREF(py_retval);
592 } else {
593 if (_enable_callback_tracebacks) {
594 PyErr_Print();
595 } else {
596 PyErr_Clear();
597 }
598 _sqlite3_result_error(context, "user-defined function raised exception", -1);
599 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000600
Georg Brandldfd73442009-04-05 11:47:34 +0000601#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000603#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604}
605
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000606static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000607{
608 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000609 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610 PyObject* aggregate_class;
611 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000612 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000613
Georg Brandldfd73442009-04-05 11:47:34 +0000614#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000615 PyGILState_STATE threadstate;
616
617 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000618#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000619
620 aggregate_class = (PyObject*)sqlite3_user_data(context);
621
622 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
623
624 if (*aggregate_instance == 0) {
625 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
626
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000629 if (_enable_callback_tracebacks) {
630 PyErr_Print();
631 } else {
632 PyErr_Clear();
633 }
634 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000635 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000636 }
637 }
638
639 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000640 if (!stepmethod) {
641 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000642 }
643
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000644 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000645 if (!args) {
646 goto error;
647 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648
649 function_result = PyObject_CallObject(stepmethod, args);
650 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000653 if (_enable_callback_tracebacks) {
654 PyErr_Print();
655 } else {
656 PyErr_Clear();
657 }
658 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000659 }
660
Thomas Wouters477c8d52006-05-27 19:21:47 +0000661error:
662 Py_XDECREF(stepmethod);
663 Py_XDECREF(function_result);
664
Georg Brandldfd73442009-04-05 11:47:34 +0000665#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000667#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668}
669
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000670void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000673 PyObject** aggregate_instance;
674 PyObject* aggregate_class;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675
Georg Brandldfd73442009-04-05 11:47:34 +0000676#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677 PyGILState_STATE threadstate;
678
679 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000680#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000681
682 aggregate_class = (PyObject*)sqlite3_user_data(context);
683
684 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
685 if (!*aggregate_instance) {
686 /* this branch is executed if there was an exception in the aggregate's
687 * __init__ */
688
Thomas Wouters477c8d52006-05-27 19:21:47 +0000689 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690 }
691
Thomas Wouters477c8d52006-05-27 19:21:47 +0000692 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
693 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000694 if (_enable_callback_tracebacks) {
695 PyErr_Print();
696 } else {
697 PyErr_Clear();
698 }
699 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
700 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000701 _pysqlite_set_result(context, function_result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000702 }
703
Thomas Wouters477c8d52006-05-27 19:21:47 +0000704error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705 Py_XDECREF(*aggregate_instance);
706 Py_XDECREF(function_result);
707
Georg Brandldfd73442009-04-05 11:47:34 +0000708#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000710#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711}
712
Gerhard Häringf9cee222010-03-05 15:20:03 +0000713static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000714{
715 PyObject* new_list;
716 PyObject* weakref;
717 int i;
718
719 /* we only need to do this once in a while */
720 if (self->created_statements++ < 200) {
721 return;
722 }
723
724 self->created_statements = 0;
725
726 new_list = PyList_New(0);
727 if (!new_list) {
728 return;
729 }
730
731 for (i = 0; i < PyList_Size(self->statements); i++) {
732 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000733 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000734 if (PyList_Append(new_list, weakref) != 0) {
735 Py_DECREF(new_list);
736 return;
737 }
738 }
739 }
740
741 Py_DECREF(self->statements);
742 self->statements = new_list;
743}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744
Gerhard Häringf9cee222010-03-05 15:20:03 +0000745static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
746{
747 PyObject* new_list;
748 PyObject* weakref;
749 int i;
750
751 /* we only need to do this once in a while */
752 if (self->created_cursors++ < 200) {
753 return;
754 }
755
756 self->created_cursors = 0;
757
758 new_list = PyList_New(0);
759 if (!new_list) {
760 return;
761 }
762
763 for (i = 0; i < PyList_Size(self->cursors); i++) {
764 weakref = PyList_GetItem(self->cursors, i);
765 if (PyWeakref_GetObject(weakref) != Py_None) {
766 if (PyList_Append(new_list, weakref) != 0) {
767 Py_DECREF(new_list);
768 return;
769 }
770 }
771 }
772
773 Py_DECREF(self->cursors);
774 self->cursors = new_list;
775}
776
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000777PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000778{
779 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
780
781 PyObject* func;
782 char* name;
783 int narg;
784 int rc;
785
Gerhard Häringf9cee222010-03-05 15:20:03 +0000786 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
787 return NULL;
788 }
789
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
791 &name, &narg, &func))
792 {
793 return NULL;
794 }
795
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000796 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000797
Thomas Wouters477c8d52006-05-27 19:21:47 +0000798 if (rc != SQLITE_OK) {
799 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000800 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000801 return NULL;
802 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000803 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
804 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805
Thomas Wouters477c8d52006-05-27 19:21:47 +0000806 Py_INCREF(Py_None);
807 return Py_None;
808 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000809}
810
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000811PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000812{
813 PyObject* aggregate_class;
814
815 int n_arg;
816 char* name;
817 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
818 int rc;
819
Gerhard Häringf9cee222010-03-05 15:20:03 +0000820 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
821 return NULL;
822 }
823
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
825 kwlist, &name, &n_arg, &aggregate_class)) {
826 return NULL;
827 }
828
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000829 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 +0000830 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000832 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833 return NULL;
834 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000835 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
836 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837
838 Py_INCREF(Py_None);
839 return Py_None;
840 }
841}
842
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000843static 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 +0000844{
845 PyObject *ret;
846 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000847#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000848 PyGILState_STATE gilstate;
849
850 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000851#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000852 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
853
854 if (!ret) {
855 if (_enable_callback_tracebacks) {
856 PyErr_Print();
857 } else {
858 PyErr_Clear();
859 }
860
861 rc = SQLITE_DENY;
862 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000863 if (PyLong_Check(ret)) {
864 rc = (int)PyLong_AsLong(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000865 } else {
866 rc = SQLITE_DENY;
867 }
868 Py_DECREF(ret);
869 }
870
Georg Brandldfd73442009-04-05 11:47:34 +0000871#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000872 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000873#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000874 return rc;
875}
876
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000877static int _progress_handler(void* user_arg)
878{
879 int rc;
880 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000881#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000882 PyGILState_STATE gilstate;
883
884 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000885#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000886 ret = PyObject_CallFunction((PyObject*)user_arg, "");
887
888 if (!ret) {
889 if (_enable_callback_tracebacks) {
890 PyErr_Print();
891 } else {
892 PyErr_Clear();
893 }
894
Mark Dickinson934896d2009-02-21 20:59:32 +0000895 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000896 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000897 } else {
898 rc = (int)PyObject_IsTrue(ret);
899 Py_DECREF(ret);
900 }
901
Georg Brandldfd73442009-04-05 11:47:34 +0000902#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000903 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000904#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000905 return rc;
906}
907
Gerhard Häringf9cee222010-03-05 15:20:03 +0000908static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000909{
910 PyObject* authorizer_cb;
911
912 static char *kwlist[] = { "authorizer_callback", NULL };
913 int rc;
914
Gerhard Häringf9cee222010-03-05 15:20:03 +0000915 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
916 return NULL;
917 }
918
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000919 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
920 kwlist, &authorizer_cb)) {
921 return NULL;
922 }
923
924 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
925
926 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000927 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000928 return NULL;
929 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000930 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
931 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000932
933 Py_INCREF(Py_None);
934 return Py_None;
935 }
936}
937
Gerhard Häringf9cee222010-03-05 15:20:03 +0000938static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000939{
940 PyObject* progress_handler;
941 int n;
942
943 static char *kwlist[] = { "progress_handler", "n", NULL };
944
Gerhard Häringf9cee222010-03-05 15:20:03 +0000945 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
946 return NULL;
947 }
948
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000949 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
950 kwlist, &progress_handler, &n)) {
951 return NULL;
952 }
953
954 if (progress_handler == Py_None) {
955 /* None clears the progress handler previously set */
956 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
957 } else {
958 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000959 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
960 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000961 }
962
963 Py_INCREF(Py_None);
964 return Py_None;
965}
966
Gerhard Häringf9cee222010-03-05 15:20:03 +0000967#ifdef HAVE_LOAD_EXTENSION
968static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
969{
970 int rc;
971 int onoff;
972
973 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
974 return NULL;
975 }
976
977 if (!PyArg_ParseTuple(args, "i", &onoff)) {
978 return NULL;
979 }
980
981 rc = sqlite3_enable_load_extension(self->db, onoff);
982
983 if (rc != SQLITE_OK) {
984 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
985 return NULL;
986 } else {
987 Py_INCREF(Py_None);
988 return Py_None;
989 }
990}
991
992static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
993{
994 int rc;
995 char* extension_name;
996 char* errmsg;
997
998 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
999 return NULL;
1000 }
1001
1002 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1003 return NULL;
1004 }
1005
1006 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1007 if (rc != 0) {
1008 PyErr_SetString(pysqlite_OperationalError, errmsg);
1009 return NULL;
1010 } else {
1011 Py_INCREF(Py_None);
1012 return Py_None;
1013 }
1014}
1015#endif
1016
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001017int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001018{
Georg Brandldfd73442009-04-05 11:47:34 +00001019#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020 if (self->check_same_thread) {
1021 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001022 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001023 "SQLite objects created in a thread can only be used in that same thread."
1024 "The object was created in thread id %ld and this is thread id %ld",
1025 self->thread_ident, PyThread_get_thread_ident());
1026 return 0;
1027 }
1028
1029 }
Georg Brandldfd73442009-04-05 11:47:34 +00001030#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001031 return 1;
1032}
1033
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001034static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001035{
1036 Py_INCREF(self->isolation_level);
1037 return self->isolation_level;
1038}
1039
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001040static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001041{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001042 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001043 return NULL;
1044 } else {
1045 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1046 }
1047}
1048
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001049static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001050{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001051 PyObject* res;
1052 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001053 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001054
1055 Py_XDECREF(self->isolation_level);
1056
1057 if (self->begin_statement) {
1058 PyMem_Free(self->begin_statement);
1059 self->begin_statement = NULL;
1060 }
1061
1062 if (isolation_level == Py_None) {
1063 Py_INCREF(Py_None);
1064 self->isolation_level = Py_None;
1065
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001066 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001067 if (!res) {
1068 return -1;
1069 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001070 Py_DECREF(res);
1071
1072 self->inTransaction = 0;
1073 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001074 const char *statement;
1075 Py_ssize_t size;
1076
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001077 Py_INCREF(isolation_level);
1078 self->isolation_level = isolation_level;
1079
Georg Brandlceab6102007-11-25 00:45:05 +00001080 if (!begin_word) {
1081 begin_word = PyUnicode_FromString("BEGIN ");
1082 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083 }
Georg Brandlceab6102007-11-25 00:45:05 +00001084 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085 if (!begin_statement) {
1086 return -1;
1087 }
1088
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001089 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001090 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001091 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001092 return -1;
1093 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001094 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001095 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001096 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001097 return -1;
1098 }
1099
Neal Norwitzefee9f52007-10-27 02:50:52 +00001100 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001101 Py_DECREF(begin_statement);
1102 }
1103
1104 return 0;
1105}
1106
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001107PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001108{
1109 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001110 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001111 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001112 int rc;
1113
Gerhard Häringf9cee222010-03-05 15:20:03 +00001114 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1115 return NULL;
1116 }
1117
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001118 if (!PyArg_ParseTuple(args, "O", &sql)) {
1119 return NULL;
1120 }
1121
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001122 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001123
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001124 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 if (!statement) {
1126 return NULL;
1127 }
1128
Victor Stinner0201f442010-03-13 03:28:34 +00001129 statement->db = NULL;
1130 statement->st = NULL;
1131 statement->sql = NULL;
1132 statement->in_use = 0;
1133 statement->in_weakreflist = NULL;
1134
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001135 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001136
1137 if (rc != SQLITE_OK) {
1138 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001139 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001140 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001141 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001142 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001143 (void)pysqlite_statement_reset(statement);
1144 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001145 }
1146
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001147 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001148 } else {
1149 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1150 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001151 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001152 goto error;
1153 }
1154
1155 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001156 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001157 goto error;
1158 }
1159
1160 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001161 }
1162
Thomas Wouters477c8d52006-05-27 19:21:47 +00001163error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001164 return (PyObject*)statement;
1165}
1166
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001167PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001168{
1169 PyObject* cursor = 0;
1170 PyObject* result = 0;
1171 PyObject* method = 0;
1172
1173 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1174 if (!cursor) {
1175 goto error;
1176 }
1177
1178 method = PyObject_GetAttrString(cursor, "execute");
1179 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001180 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181 goto error;
1182 }
1183
1184 result = PyObject_CallObject(method, args);
1185 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001186 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001187 }
1188
1189error:
1190 Py_XDECREF(result);
1191 Py_XDECREF(method);
1192
1193 return cursor;
1194}
1195
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001196PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197{
1198 PyObject* cursor = 0;
1199 PyObject* result = 0;
1200 PyObject* method = 0;
1201
1202 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1203 if (!cursor) {
1204 goto error;
1205 }
1206
1207 method = PyObject_GetAttrString(cursor, "executemany");
1208 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001209 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001210 goto error;
1211 }
1212
1213 result = PyObject_CallObject(method, args);
1214 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001215 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001216 }
1217
1218error:
1219 Py_XDECREF(result);
1220 Py_XDECREF(method);
1221
1222 return cursor;
1223}
1224
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001225PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001226{
1227 PyObject* cursor = 0;
1228 PyObject* result = 0;
1229 PyObject* method = 0;
1230
1231 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1232 if (!cursor) {
1233 goto error;
1234 }
1235
1236 method = PyObject_GetAttrString(cursor, "executescript");
1237 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001238 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001239 goto error;
1240 }
1241
1242 result = PyObject_CallObject(method, args);
1243 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001244 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001245 }
1246
1247error:
1248 Py_XDECREF(result);
1249 Py_XDECREF(method);
1250
1251 return cursor;
1252}
1253
1254/* ------------------------- COLLATION CODE ------------------------ */
1255
1256static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001257pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001258 void* context,
1259 int text1_length, const void* text1_data,
1260 int text2_length, const void* text2_data)
1261{
1262 PyObject* callback = (PyObject*)context;
1263 PyObject* string1 = 0;
1264 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001265#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001267#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268 PyObject* retval = NULL;
1269 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001270#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001272#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001273
1274 if (PyErr_Occurred()) {
1275 goto finally;
1276 }
1277
Guido van Rossum98297ee2007-11-06 21:34:58 +00001278 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1279 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280
1281 if (!string1 || !string2) {
1282 goto finally; /* failed to allocate strings */
1283 }
1284
1285 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1286
1287 if (!retval) {
1288 /* execution failed */
1289 goto finally;
1290 }
1291
Christian Heimes217cfd12007-12-02 14:31:20 +00001292 result = PyLong_AsLong(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001293 if (PyErr_Occurred()) {
1294 result = 0;
1295 }
1296
1297finally:
1298 Py_XDECREF(string1);
1299 Py_XDECREF(string2);
1300 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001301#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001302 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001303#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001304 return result;
1305}
1306
1307static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001308pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001309{
1310 PyObject* retval = NULL;
1311
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001312 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001313 goto finally;
1314 }
1315
1316 sqlite3_interrupt(self->db);
1317
1318 Py_INCREF(Py_None);
1319 retval = Py_None;
1320
1321finally:
1322 return retval;
1323}
1324
Christian Heimesbbe741d2008-03-28 10:53:29 +00001325/* Function author: Paul Kippes <kippesp@gmail.com>
1326 * Class method of Connection to call the Python function _iterdump
1327 * of the sqlite3 module.
1328 */
1329static PyObject *
1330pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1331{
1332 PyObject* retval = NULL;
1333 PyObject* module = NULL;
1334 PyObject* module_dict;
1335 PyObject* pyfn_iterdump;
1336
1337 if (!pysqlite_check_connection(self)) {
1338 goto finally;
1339 }
1340
1341 module = PyImport_ImportModule(MODULE_NAME ".dump");
1342 if (!module) {
1343 goto finally;
1344 }
1345
1346 module_dict = PyModule_GetDict(module);
1347 if (!module_dict) {
1348 goto finally;
1349 }
1350
1351 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1352 if (!pyfn_iterdump) {
1353 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1354 goto finally;
1355 }
1356
1357 args = PyTuple_New(1);
1358 if (!args) {
1359 goto finally;
1360 }
1361 Py_INCREF(self);
1362 PyTuple_SetItem(args, 0, (PyObject*)self);
1363 retval = PyObject_CallObject(pyfn_iterdump, args);
1364
1365finally:
1366 Py_XDECREF(args);
1367 Py_XDECREF(module);
1368 return retval;
1369}
1370
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001371static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001372pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001373{
1374 PyObject* callable;
1375 PyObject* uppercase_name = 0;
1376 PyObject* name;
1377 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001378 Py_UNICODE* chk;
1379 Py_ssize_t i, len;
1380 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001381 int rc;
1382
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001383 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001384 goto finally;
1385 }
1386
Gerhard Häring6d214562007-08-10 18:15:11 +00001387 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001388 goto finally;
1389 }
1390
1391 uppercase_name = PyObject_CallMethod(name, "upper", "");
1392 if (!uppercase_name) {
1393 goto finally;
1394 }
1395
Victor Stinner35466c52010-04-22 11:23:23 +00001396 len = PyUnicode_GET_SIZE(uppercase_name);
1397 chk = PyUnicode_AS_UNICODE(uppercase_name);
1398 for (i=0; i<len; i++, chk++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001399 if ((*chk >= '0' && *chk <= '9')
1400 || (*chk >= 'A' && *chk <= 'Z')
1401 || (*chk == '_'))
1402 {
Victor Stinner35466c52010-04-22 11:23:23 +00001403 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001404 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001405 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001406 goto finally;
1407 }
1408 }
1409
Victor Stinner35466c52010-04-22 11:23:23 +00001410 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1411 if (!uppercase_name_str)
1412 goto finally;
1413
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001414 if (callable != Py_None && !PyCallable_Check(callable)) {
1415 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1416 goto finally;
1417 }
1418
1419 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001420 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1421 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001422 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001423 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1424 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001425 }
1426
1427 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001428 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001429 SQLITE_UTF8,
1430 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001431 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001432 if (rc != SQLITE_OK) {
1433 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001434 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001435 goto finally;
1436 }
1437
1438finally:
1439 Py_XDECREF(uppercase_name);
1440
1441 if (PyErr_Occurred()) {
1442 retval = NULL;
1443 } else {
1444 Py_INCREF(Py_None);
1445 retval = Py_None;
1446 }
1447
1448 return retval;
1449}
1450
Christian Heimesbbe741d2008-03-28 10:53:29 +00001451/* Called when the connection is used as a context manager. Returns itself as a
1452 * convenience to the caller. */
1453static PyObject *
1454pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1455{
1456 Py_INCREF(self);
1457 return (PyObject*)self;
1458}
1459
1460/** Called when the connection is used as a context manager. If there was any
1461 * exception, a rollback takes place; otherwise we commit. */
1462static PyObject *
1463pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1464{
1465 PyObject* exc_type, *exc_value, *exc_tb;
1466 char* method_name;
1467 PyObject* result;
1468
1469 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1470 return NULL;
1471 }
1472
1473 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1474 method_name = "commit";
1475 } else {
1476 method_name = "rollback";
1477 }
1478
1479 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1480 if (!result) {
1481 return NULL;
1482 }
1483 Py_DECREF(result);
1484
1485 Py_RETURN_FALSE;
1486}
1487
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001488static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001489PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490
1491static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001492 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1493 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001494 {NULL}
1495};
1496
1497static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001498 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001499 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001500 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001501 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001502 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001503 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001504 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001505 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001506 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001507 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001508 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001509 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001510 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001511 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001512 #ifdef HAVE_LOAD_EXTENSION
1513 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1514 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1515 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1516 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1517 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001518 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1519 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001520 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001522 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001524 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001525 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001526 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001527 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001528 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001529 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001530 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001531 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001532 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1533 PyDoc_STR("For context manager. Non-standard.")},
1534 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1535 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001536 {NULL, NULL}
1537};
1538
1539static struct PyMemberDef connection_members[] =
1540{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001541 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1542 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1543 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1544 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1545 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1546 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1547 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1548 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1549 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1550 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001551 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1552 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001553 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
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}