blob: 8cf2d6aa80c9e444afa154753a7bed7dfd3717ee [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;
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100112 if (pysqlite_connection_set_isolation_level(self, isolation_level) < 0) {
113 Py_DECREF(isolation_level);
114 return -1;
115 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116 Py_DECREF(isolation_level);
117
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000118 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 if (PyErr_Occurred()) {
120 return -1;
121 }
122
Gerhard Häringf9cee222010-03-05 15:20:03 +0000123 self->created_statements = 0;
124 self->created_cursors = 0;
125
126 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000127 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000128 self->cursors = PyList_New(0);
129 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000130 return -1;
131 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000132
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000133 /* By default, the Cache class INCREFs the factory in its initializer, and
134 * decrefs it in its deallocator method. Since this would create a circular
135 * reference here, we're breaking it by decrementing self, and telling the
136 * cache class to not decref the factory (self) in its deallocator.
137 */
138 self->statement_cache->decref_factory = 0;
139 Py_DECREF(self);
140
141 self->inTransaction = 0;
142 self->detect_types = detect_types;
143 self->timeout = timeout;
144 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000145#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000147#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 self->check_same_thread = check_same_thread;
149
150 self->function_pinboard = PyDict_New();
151 if (!self->function_pinboard) {
152 return -1;
153 }
154
155 self->collations = PyDict_New();
156 if (!self->collations) {
157 return -1;
158 }
159
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000160 self->Warning = pysqlite_Warning;
161 self->Error = pysqlite_Error;
162 self->InterfaceError = pysqlite_InterfaceError;
163 self->DatabaseError = pysqlite_DatabaseError;
164 self->DataError = pysqlite_DataError;
165 self->OperationalError = pysqlite_OperationalError;
166 self->IntegrityError = pysqlite_IntegrityError;
167 self->InternalError = pysqlite_InternalError;
168 self->ProgrammingError = pysqlite_ProgrammingError;
169 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000170
171 return 0;
172}
173
Thomas Wouters477c8d52006-05-27 19:21:47 +0000174/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000175void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000176{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000177 pysqlite_Node* node;
178 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179
180 node = self->statement_cache->first;
181
182 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000183 statement = (pysqlite_Statement*)(node->data);
184 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000185 node = node->next;
186 }
187
188 Py_DECREF(self->statement_cache);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000189 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 Py_DECREF(self);
191 self->statement_cache->decref_factory = 0;
192}
193
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000194/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häringf9cee222010-03-05 15:20:03 +0000195void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000197 int i;
198 PyObject* weakref;
199 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000200 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201
Thomas Wouters477c8d52006-05-27 19:21:47 +0000202 for (i = 0; i < PyList_Size(self->statements); i++) {
203 weakref = PyList_GetItem(self->statements, i);
204 statement = PyWeakref_GetObject(weakref);
205 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500206 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000207 if (action == ACTION_RESET) {
208 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
209 } else {
210 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
211 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500212 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000213 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000214 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000215
216 if (reset_cursors) {
217 for (i = 0; i < PyList_Size(self->cursors); i++) {
218 weakref = PyList_GetItem(self->cursors, i);
219 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
220 if ((PyObject*)cursor != Py_None) {
221 cursor->reset = 1;
222 }
223 }
224 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225}
226
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000227void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000228{
229 Py_XDECREF(self->statement_cache);
230
231 /* Clean up if user has not called .close() explicitly. */
232 if (self->db) {
233 Py_BEGIN_ALLOW_THREADS
234 sqlite3_close(self->db);
235 Py_END_ALLOW_THREADS
236 }
237
238 if (self->begin_statement) {
239 PyMem_Free(self->begin_statement);
240 }
241 Py_XDECREF(self->isolation_level);
242 Py_XDECREF(self->function_pinboard);
243 Py_XDECREF(self->row_factory);
244 Py_XDECREF(self->text_factory);
245 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000246 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000247 Py_XDECREF(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248
Christian Heimes90aa7642007-12-19 02:45:37 +0000249 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250}
251
Gerhard Häringf9cee222010-03-05 15:20:03 +0000252/*
253 * Registers a cursor with the connection.
254 *
255 * 0 => error; 1 => ok
256 */
257int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
258{
259 PyObject* weakref;
260
261 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
262 if (!weakref) {
263 goto error;
264 }
265
266 if (PyList_Append(connection->cursors, weakref) != 0) {
267 Py_CLEAR(weakref);
268 goto error;
269 }
270
271 Py_DECREF(weakref);
272
273 return 1;
274error:
275 return 0;
276}
277
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000278PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000279{
280 static char *kwlist[] = {"factory", NULL, NULL};
281 PyObject* factory = NULL;
282 PyObject* cursor;
283
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000284 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
285 &factory)) {
286 return NULL;
287 }
288
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000289 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000290 return NULL;
291 }
292
293 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000294 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000295 }
296
297 cursor = PyObject_CallFunction(factory, "O", self);
298
Gerhard Häringf9cee222010-03-05 15:20:03 +0000299 _pysqlite_drop_unused_cursor_references(self);
300
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000301 if (cursor && self->row_factory != Py_None) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000302 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303 Py_INCREF(self->row_factory);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000304 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000305 }
306
307 return cursor;
308}
309
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000310PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000311{
312 int rc;
313
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000314 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315 return NULL;
316 }
317
Gerhard Häringf9cee222010-03-05 15:20:03 +0000318 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319
320 if (self->db) {
321 Py_BEGIN_ALLOW_THREADS
322 rc = sqlite3_close(self->db);
323 Py_END_ALLOW_THREADS
324
325 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000326 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327 return NULL;
328 } else {
329 self->db = NULL;
330 }
331 }
332
333 Py_INCREF(Py_None);
334 return Py_None;
335}
336
337/*
338 * Checks if a connection object is usable (i. e. not closed).
339 *
340 * 0 => error; 1 => ok
341 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000342int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000344 if (!con->initialized) {
345 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
346 return 0;
347 }
348
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000349 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000350 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351 return 0;
352 } else {
353 return 1;
354 }
355}
356
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000357PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358{
359 int rc;
360 const char* tail;
361 sqlite3_stmt* statement;
362
363 Py_BEGIN_ALLOW_THREADS
364 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
365 Py_END_ALLOW_THREADS
366
367 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000368 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369 goto error;
370 }
371
Benjamin Petersond7b03282008-09-13 15:58:53 +0000372 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000373 if (rc == SQLITE_DONE) {
374 self->inTransaction = 1;
375 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000376 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377 }
378
379 Py_BEGIN_ALLOW_THREADS
380 rc = sqlite3_finalize(statement);
381 Py_END_ALLOW_THREADS
382
383 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000384 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 }
386
387error:
388 if (PyErr_Occurred()) {
389 return NULL;
390 } else {
391 Py_INCREF(Py_None);
392 return Py_None;
393 }
394}
395
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000396PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000397{
398 int rc;
399 const char* tail;
400 sqlite3_stmt* statement;
401
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000402 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000403 return NULL;
404 }
405
406 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000407 pysqlite_do_all_statements(self, ACTION_RESET, 0);
408
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 Py_BEGIN_ALLOW_THREADS
410 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
411 Py_END_ALLOW_THREADS
412 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000413 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000414 goto error;
415 }
416
Benjamin Petersond7b03282008-09-13 15:58:53 +0000417 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000418 if (rc == SQLITE_DONE) {
419 self->inTransaction = 0;
420 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000421 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000422 }
423
424 Py_BEGIN_ALLOW_THREADS
425 rc = sqlite3_finalize(statement);
426 Py_END_ALLOW_THREADS
427 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000428 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000429 }
430
431 }
432
433error:
434 if (PyErr_Occurred()) {
435 return NULL;
436 } else {
437 Py_INCREF(Py_None);
438 return Py_None;
439 }
440}
441
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000442PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443{
444 int rc;
445 const char* tail;
446 sqlite3_stmt* statement;
447
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000448 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000449 return NULL;
450 }
451
452 if (self->inTransaction) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000453 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000454
455 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000456 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457 Py_END_ALLOW_THREADS
458 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000459 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000460 goto error;
461 }
462
Benjamin Petersond7b03282008-09-13 15:58:53 +0000463 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 if (rc == SQLITE_DONE) {
465 self->inTransaction = 0;
466 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000467 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000468 }
469
470 Py_BEGIN_ALLOW_THREADS
471 rc = sqlite3_finalize(statement);
472 Py_END_ALLOW_THREADS
473 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000474 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 }
476
477 }
478
479error:
480 if (PyErr_Occurred()) {
481 return NULL;
482 } else {
483 Py_INCREF(Py_None);
484 return Py_None;
485 }
486}
487
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200488static int
489_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000490{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200491 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000493 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200494 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
495 if (value == -1 && PyErr_Occurred())
496 return -1;
497 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000498 } else if (PyFloat_Check(py_val)) {
499 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000500 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200501 const char *str = _PyUnicode_AsString(py_val);
502 if (str == NULL)
503 return -1;
504 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000505 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200506 const char* buffer;
507 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
509 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200510 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200512 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200514 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200516 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517}
518
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000519PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520{
521 PyObject* args;
522 int i;
523 sqlite3_value* cur_value;
524 PyObject* cur_py_value;
525 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527
528 args = PyTuple_New(argc);
529 if (!args) {
530 return NULL;
531 }
532
533 for (i = 0; i < argc; i++) {
534 cur_value = argv[i];
535 switch (sqlite3_value_type(argv[i])) {
536 case SQLITE_INTEGER:
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200537 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538 break;
539 case SQLITE_FLOAT:
540 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
541 break;
542 case SQLITE_TEXT:
543 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000544 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545 /* TODO: have a way to show errors here */
546 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000547 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548 Py_INCREF(Py_None);
549 cur_py_value = Py_None;
550 }
551 break;
552 case SQLITE_BLOB:
553 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000554 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000555 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556 break;
557 case SQLITE_NULL:
558 default:
559 Py_INCREF(Py_None);
560 cur_py_value = Py_None;
561 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000562
563 if (!cur_py_value) {
564 Py_DECREF(args);
565 return NULL;
566 }
567
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000568 PyTuple_SetItem(args, i, cur_py_value);
569
570 }
571
572 return args;
573}
574
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000575void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000576{
577 PyObject* args;
578 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000579 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200580 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000581
Georg Brandldfd73442009-04-05 11:47:34 +0000582#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000583 PyGILState_STATE threadstate;
584
585 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000586#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587
588 py_func = (PyObject*)sqlite3_user_data(context);
589
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000590 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000591 if (args) {
592 py_retval = PyObject_CallObject(py_func, args);
593 Py_DECREF(args);
594 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200596 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000597 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200598 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000599 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200600 }
601 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000602 if (_enable_callback_tracebacks) {
603 PyErr_Print();
604 } else {
605 PyErr_Clear();
606 }
607 _sqlite3_result_error(context, "user-defined function raised exception", -1);
608 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000609
Georg Brandldfd73442009-04-05 11:47:34 +0000610#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000612#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000613}
614
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000615static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616{
617 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000618 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000619 PyObject* aggregate_class;
620 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000621 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622
Georg Brandldfd73442009-04-05 11:47:34 +0000623#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624 PyGILState_STATE threadstate;
625
626 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000627#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628
629 aggregate_class = (PyObject*)sqlite3_user_data(context);
630
631 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
632
633 if (*aggregate_instance == 0) {
634 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
635
Thomas Wouters477c8d52006-05-27 19:21:47 +0000636 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000638 if (_enable_callback_tracebacks) {
639 PyErr_Print();
640 } else {
641 PyErr_Clear();
642 }
643 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000645 }
646 }
647
648 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649 if (!stepmethod) {
650 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651 }
652
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000653 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 if (!args) {
655 goto error;
656 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000657
658 function_result = PyObject_CallObject(stepmethod, args);
659 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660
Thomas Wouters477c8d52006-05-27 19:21:47 +0000661 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000662 if (_enable_callback_tracebacks) {
663 PyErr_Print();
664 } else {
665 PyErr_Clear();
666 }
667 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668 }
669
Thomas Wouters477c8d52006-05-27 19:21:47 +0000670error:
671 Py_XDECREF(stepmethod);
672 Py_XDECREF(function_result);
673
Georg Brandldfd73442009-04-05 11:47:34 +0000674#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000675 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000676#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677}
678
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000679void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200681 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000682 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200683 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200684 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000685
Georg Brandldfd73442009-04-05 11:47:34 +0000686#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000687 PyGILState_STATE threadstate;
688
689 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000690#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000691
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
693 if (!*aggregate_instance) {
694 /* this branch is executed if there was an exception in the aggregate's
695 * __init__ */
696
Thomas Wouters477c8d52006-05-27 19:21:47 +0000697 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000698 }
699
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200700 function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, "");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200701 Py_DECREF(*aggregate_instance);
702
703 ok = 0;
704 if (function_result) {
705 ok = _pysqlite_set_result(context, function_result) == 0;
706 Py_DECREF(function_result);
707 }
708 if (!ok) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000709 if (_enable_callback_tracebacks) {
710 PyErr_Print();
711 } else {
712 PyErr_Clear();
713 }
714 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715 }
716
Thomas Wouters477c8d52006-05-27 19:21:47 +0000717error:
Georg Brandldfd73442009-04-05 11:47:34 +0000718#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000720#endif
Victor Stinnerb84fc0f2013-08-28 01:44:42 +0200721 /* explicit return to avoid a compilation error if WITH_THREAD
722 is not defined */
723 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000724}
725
Gerhard Häringf9cee222010-03-05 15:20:03 +0000726static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000727{
728 PyObject* new_list;
729 PyObject* weakref;
730 int i;
731
732 /* we only need to do this once in a while */
733 if (self->created_statements++ < 200) {
734 return;
735 }
736
737 self->created_statements = 0;
738
739 new_list = PyList_New(0);
740 if (!new_list) {
741 return;
742 }
743
744 for (i = 0; i < PyList_Size(self->statements); i++) {
745 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000746 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000747 if (PyList_Append(new_list, weakref) != 0) {
748 Py_DECREF(new_list);
749 return;
750 }
751 }
752 }
753
754 Py_DECREF(self->statements);
755 self->statements = new_list;
756}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757
Gerhard Häringf9cee222010-03-05 15:20:03 +0000758static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
759{
760 PyObject* new_list;
761 PyObject* weakref;
762 int i;
763
764 /* we only need to do this once in a while */
765 if (self->created_cursors++ < 200) {
766 return;
767 }
768
769 self->created_cursors = 0;
770
771 new_list = PyList_New(0);
772 if (!new_list) {
773 return;
774 }
775
776 for (i = 0; i < PyList_Size(self->cursors); i++) {
777 weakref = PyList_GetItem(self->cursors, i);
778 if (PyWeakref_GetObject(weakref) != Py_None) {
779 if (PyList_Append(new_list, weakref) != 0) {
780 Py_DECREF(new_list);
781 return;
782 }
783 }
784 }
785
786 Py_DECREF(self->cursors);
787 self->cursors = new_list;
788}
789
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000790PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791{
792 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
793
794 PyObject* func;
795 char* name;
796 int narg;
797 int rc;
798
Gerhard Häringf9cee222010-03-05 15:20:03 +0000799 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
800 return NULL;
801 }
802
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000803 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
804 &name, &narg, &func))
805 {
806 return NULL;
807 }
808
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000809 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000810
Thomas Wouters477c8d52006-05-27 19:21:47 +0000811 if (rc != SQLITE_OK) {
812 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000813 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814 return NULL;
815 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000816 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
817 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819 Py_INCREF(Py_None);
820 return Py_None;
821 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000822}
823
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000824PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825{
826 PyObject* aggregate_class;
827
828 int n_arg;
829 char* name;
830 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
831 int rc;
832
Gerhard Häringf9cee222010-03-05 15:20:03 +0000833 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
834 return NULL;
835 }
836
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
838 kwlist, &name, &n_arg, &aggregate_class)) {
839 return NULL;
840 }
841
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000842 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 +0000843 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000844 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000845 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846 return NULL;
847 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000848 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
849 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000850
851 Py_INCREF(Py_None);
852 return Py_None;
853 }
854}
855
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000856static 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 +0000857{
858 PyObject *ret;
859 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000860#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000861 PyGILState_STATE gilstate;
862
863 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000864#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000865 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
866
867 if (!ret) {
868 if (_enable_callback_tracebacks) {
869 PyErr_Print();
870 } else {
871 PyErr_Clear();
872 }
873
874 rc = SQLITE_DENY;
875 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000876 if (PyLong_Check(ret)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200877 rc = _PyLong_AsInt(ret);
878 if (rc == -1 && PyErr_Occurred())
879 rc = SQLITE_DENY;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000880 } else {
881 rc = SQLITE_DENY;
882 }
883 Py_DECREF(ret);
884 }
885
Georg Brandldfd73442009-04-05 11:47:34 +0000886#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000887 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000888#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000889 return rc;
890}
891
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000892static int _progress_handler(void* user_arg)
893{
894 int rc;
895 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000896#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000897 PyGILState_STATE gilstate;
898
899 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000900#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000901 ret = PyObject_CallFunction((PyObject*)user_arg, "");
902
903 if (!ret) {
904 if (_enable_callback_tracebacks) {
905 PyErr_Print();
906 } else {
907 PyErr_Clear();
908 }
909
Mark Dickinson934896d2009-02-21 20:59:32 +0000910 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000911 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000912 } else {
913 rc = (int)PyObject_IsTrue(ret);
914 Py_DECREF(ret);
915 }
916
Georg Brandldfd73442009-04-05 11:47:34 +0000917#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000918 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000919#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000920 return rc;
921}
922
Antoine Pitrou5bfa0622011-04-04 00:12:04 +0200923static void _trace_callback(void* user_arg, const char* statement_string)
924{
925 PyObject *py_statement = NULL;
926 PyObject *ret = NULL;
927
928#ifdef WITH_THREAD
929 PyGILState_STATE gilstate;
930
931 gilstate = PyGILState_Ensure();
932#endif
933 py_statement = PyUnicode_DecodeUTF8(statement_string,
934 strlen(statement_string), "replace");
935 if (py_statement) {
936 ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
937 Py_DECREF(py_statement);
938 }
939
940 if (ret) {
941 Py_DECREF(ret);
942 } else {
943 if (_enable_callback_tracebacks) {
944 PyErr_Print();
945 } else {
946 PyErr_Clear();
947 }
948 }
949
950#ifdef WITH_THREAD
951 PyGILState_Release(gilstate);
952#endif
953}
954
Gerhard Häringf9cee222010-03-05 15:20:03 +0000955static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000956{
957 PyObject* authorizer_cb;
958
959 static char *kwlist[] = { "authorizer_callback", NULL };
960 int rc;
961
Gerhard Häringf9cee222010-03-05 15:20:03 +0000962 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
963 return NULL;
964 }
965
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000966 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
967 kwlist, &authorizer_cb)) {
968 return NULL;
969 }
970
971 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
972
973 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000974 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000975 return NULL;
976 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000977 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
978 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000979
980 Py_INCREF(Py_None);
981 return Py_None;
982 }
983}
984
Gerhard Häringf9cee222010-03-05 15:20:03 +0000985static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000986{
987 PyObject* progress_handler;
988 int n;
989
990 static char *kwlist[] = { "progress_handler", "n", NULL };
991
Gerhard Häringf9cee222010-03-05 15:20:03 +0000992 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
993 return NULL;
994 }
995
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000996 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
997 kwlist, &progress_handler, &n)) {
998 return NULL;
999 }
1000
1001 if (progress_handler == Py_None) {
1002 /* None clears the progress handler previously set */
1003 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1004 } else {
1005 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häringf9cee222010-03-05 15:20:03 +00001006 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1007 return NULL;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001008 }
1009
1010 Py_INCREF(Py_None);
1011 return Py_None;
1012}
1013
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001014static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1015{
1016 PyObject* trace_callback;
1017
1018 static char *kwlist[] = { "trace_callback", NULL };
1019
1020 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1021 return NULL;
1022 }
1023
1024 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1025 kwlist, &trace_callback)) {
1026 return NULL;
1027 }
1028
1029 if (trace_callback == Py_None) {
1030 /* None clears the trace callback previously set */
1031 sqlite3_trace(self->db, 0, (void*)0);
1032 } else {
1033 if (PyDict_SetItem(self->function_pinboard, trace_callback, Py_None) == -1)
1034 return NULL;
1035 sqlite3_trace(self->db, _trace_callback, trace_callback);
1036 }
1037
1038 Py_INCREF(Py_None);
1039 return Py_None;
1040}
1041
Gerhard Häringf9cee222010-03-05 15:20:03 +00001042#ifdef HAVE_LOAD_EXTENSION
1043static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1044{
1045 int rc;
1046 int onoff;
1047
1048 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1049 return NULL;
1050 }
1051
1052 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1053 return NULL;
1054 }
1055
1056 rc = sqlite3_enable_load_extension(self->db, onoff);
1057
1058 if (rc != SQLITE_OK) {
1059 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1060 return NULL;
1061 } else {
1062 Py_INCREF(Py_None);
1063 return Py_None;
1064 }
1065}
1066
1067static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1068{
1069 int rc;
1070 char* extension_name;
1071 char* errmsg;
1072
1073 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1074 return NULL;
1075 }
1076
1077 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1078 return NULL;
1079 }
1080
1081 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1082 if (rc != 0) {
1083 PyErr_SetString(pysqlite_OperationalError, errmsg);
1084 return NULL;
1085 } else {
1086 Py_INCREF(Py_None);
1087 return Py_None;
1088 }
1089}
1090#endif
1091
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001092int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001093{
Georg Brandldfd73442009-04-05 11:47:34 +00001094#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001095 if (self->check_same_thread) {
1096 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001097 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098 "SQLite objects created in a thread can only be used in that same thread."
1099 "The object was created in thread id %ld and this is thread id %ld",
1100 self->thread_ident, PyThread_get_thread_ident());
1101 return 0;
1102 }
1103
1104 }
Georg Brandldfd73442009-04-05 11:47:34 +00001105#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001106 return 1;
1107}
1108
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001109static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001110{
1111 Py_INCREF(self->isolation_level);
1112 return self->isolation_level;
1113}
1114
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001115static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001116{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001117 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001118 return NULL;
1119 } else {
1120 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1121 }
1122}
1123
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001124static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001126 PyObject* res;
1127 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +00001128 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001129
1130 Py_XDECREF(self->isolation_level);
1131
1132 if (self->begin_statement) {
1133 PyMem_Free(self->begin_statement);
1134 self->begin_statement = NULL;
1135 }
1136
1137 if (isolation_level == Py_None) {
1138 Py_INCREF(Py_None);
1139 self->isolation_level = Py_None;
1140
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001141 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001142 if (!res) {
1143 return -1;
1144 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001145 Py_DECREF(res);
1146
1147 self->inTransaction = 0;
1148 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +00001149 const char *statement;
1150 Py_ssize_t size;
1151
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152 Py_INCREF(isolation_level);
1153 self->isolation_level = isolation_level;
1154
Georg Brandlceab6102007-11-25 00:45:05 +00001155 if (!begin_word) {
1156 begin_word = PyUnicode_FromString("BEGIN ");
1157 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001158 }
Georg Brandlceab6102007-11-25 00:45:05 +00001159 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001160 if (!begin_statement) {
1161 return -1;
1162 }
1163
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001164 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +00001165 if (!statement) {
Victor Stinnerff27d6b2010-03-13 00:57:22 +00001166 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +00001167 return -1;
1168 }
Neal Norwitzefee9f52007-10-27 02:50:52 +00001169 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001171 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001172 return -1;
1173 }
1174
Neal Norwitzefee9f52007-10-27 02:50:52 +00001175 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001176 Py_DECREF(begin_statement);
1177 }
1178
1179 return 0;
1180}
1181
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001182PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001183{
1184 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001185 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001186 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001187 int rc;
1188
Gerhard Häringf9cee222010-03-05 15:20:03 +00001189 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1190 return NULL;
1191 }
1192
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001193 if (!PyArg_ParseTuple(args, "O", &sql)) {
1194 return NULL;
1195 }
1196
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001197 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001198
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001199 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001200 if (!statement) {
1201 return NULL;
1202 }
1203
Victor Stinner0201f442010-03-13 03:28:34 +00001204 statement->db = NULL;
1205 statement->st = NULL;
1206 statement->sql = NULL;
1207 statement->in_use = 0;
1208 statement->in_weakreflist = NULL;
1209
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001210 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001211
1212 if (rc != SQLITE_OK) {
1213 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001214 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001215 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001216 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001217 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001218 (void)pysqlite_statement_reset(statement);
1219 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001220 }
1221
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001222 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001223 } else {
1224 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1225 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001226 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001227 goto error;
1228 }
1229
1230 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001231 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001232 goto error;
1233 }
1234
1235 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001236 }
1237
Thomas Wouters477c8d52006-05-27 19:21:47 +00001238error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001239 return (PyObject*)statement;
1240}
1241
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001242PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001243{
1244 PyObject* cursor = 0;
1245 PyObject* result = 0;
1246 PyObject* method = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001247 _Py_IDENTIFIER(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001248
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001249 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 if (!cursor) {
1251 goto error;
1252 }
1253
1254 method = PyObject_GetAttrString(cursor, "execute");
1255 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001256 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001257 goto error;
1258 }
1259
1260 result = PyObject_CallObject(method, args);
1261 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001262 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001263 }
1264
1265error:
1266 Py_XDECREF(result);
1267 Py_XDECREF(method);
1268
1269 return cursor;
1270}
1271
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001272PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001273{
1274 PyObject* cursor = 0;
1275 PyObject* result = 0;
1276 PyObject* method = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001277 _Py_IDENTIFIER(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001278
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001279 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280 if (!cursor) {
1281 goto error;
1282 }
1283
1284 method = PyObject_GetAttrString(cursor, "executemany");
1285 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001286 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287 goto error;
1288 }
1289
1290 result = PyObject_CallObject(method, args);
1291 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001292 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001293 }
1294
1295error:
1296 Py_XDECREF(result);
1297 Py_XDECREF(method);
1298
1299 return cursor;
1300}
1301
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001302PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001303{
1304 PyObject* cursor = 0;
1305 PyObject* result = 0;
1306 PyObject* method = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001307 _Py_IDENTIFIER(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001308
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001309 cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001310 if (!cursor) {
1311 goto error;
1312 }
1313
1314 method = PyObject_GetAttrString(cursor, "executescript");
1315 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001316 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001317 goto error;
1318 }
1319
1320 result = PyObject_CallObject(method, args);
1321 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001322 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001323 }
1324
1325error:
1326 Py_XDECREF(result);
1327 Py_XDECREF(method);
1328
1329 return cursor;
1330}
1331
1332/* ------------------------- COLLATION CODE ------------------------ */
1333
1334static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001335pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001336 void* context,
1337 int text1_length, const void* text1_data,
1338 int text2_length, const void* text2_data)
1339{
1340 PyObject* callback = (PyObject*)context;
1341 PyObject* string1 = 0;
1342 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001343#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001344 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001345#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001346 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001347 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001348 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001349#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001350 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001351#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001352
1353 if (PyErr_Occurred()) {
1354 goto finally;
1355 }
1356
Guido van Rossum98297ee2007-11-06 21:34:58 +00001357 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1358 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001359
1360 if (!string1 || !string2) {
1361 goto finally; /* failed to allocate strings */
1362 }
1363
1364 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1365
1366 if (!retval) {
1367 /* execution failed */
1368 goto finally;
1369 }
1370
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001371 longval = PyLong_AsLongAndOverflow(retval, &result);
1372 if (longval == -1 && PyErr_Occurred()) {
1373 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001374 result = 0;
1375 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001376 else if (!result) {
1377 if (longval > 0)
1378 result = 1;
1379 else if (longval < 0)
1380 result = -1;
1381 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001382
1383finally:
1384 Py_XDECREF(string1);
1385 Py_XDECREF(string2);
1386 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001387#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001388 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001389#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001390 return result;
1391}
1392
1393static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001394pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001395{
1396 PyObject* retval = NULL;
1397
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001398 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001399 goto finally;
1400 }
1401
1402 sqlite3_interrupt(self->db);
1403
1404 Py_INCREF(Py_None);
1405 retval = Py_None;
1406
1407finally:
1408 return retval;
1409}
1410
Christian Heimesbbe741d2008-03-28 10:53:29 +00001411/* Function author: Paul Kippes <kippesp@gmail.com>
1412 * Class method of Connection to call the Python function _iterdump
1413 * of the sqlite3 module.
1414 */
1415static PyObject *
1416pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1417{
1418 PyObject* retval = NULL;
1419 PyObject* module = NULL;
1420 PyObject* module_dict;
1421 PyObject* pyfn_iterdump;
1422
1423 if (!pysqlite_check_connection(self)) {
1424 goto finally;
1425 }
1426
1427 module = PyImport_ImportModule(MODULE_NAME ".dump");
1428 if (!module) {
1429 goto finally;
1430 }
1431
1432 module_dict = PyModule_GetDict(module);
1433 if (!module_dict) {
1434 goto finally;
1435 }
1436
1437 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1438 if (!pyfn_iterdump) {
1439 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1440 goto finally;
1441 }
1442
1443 args = PyTuple_New(1);
1444 if (!args) {
1445 goto finally;
1446 }
1447 Py_INCREF(self);
1448 PyTuple_SetItem(args, 0, (PyObject*)self);
1449 retval = PyObject_CallObject(pyfn_iterdump, args);
1450
1451finally:
1452 Py_XDECREF(args);
1453 Py_XDECREF(module);
1454 return retval;
1455}
1456
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001457static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001458pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001459{
1460 PyObject* callable;
1461 PyObject* uppercase_name = 0;
1462 PyObject* name;
1463 PyObject* retval;
Victor Stinner35466c52010-04-22 11:23:23 +00001464 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001465 _Py_IDENTIFIER(upper);
Victor Stinner35466c52010-04-22 11:23:23 +00001466 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001467 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001468 unsigned int kind;
1469 void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001470
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001471 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001472 goto finally;
1473 }
1474
Gerhard Häring6d214562007-08-10 18:15:11 +00001475 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001476 goto finally;
1477 }
1478
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001479 uppercase_name = _PyObject_CallMethodId(name, &PyId_upper, "");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001480 if (!uppercase_name) {
1481 goto finally;
1482 }
1483
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001484 if (PyUnicode_READY(uppercase_name))
1485 goto finally;
1486 len = PyUnicode_GET_LENGTH(uppercase_name);
1487 kind = PyUnicode_KIND(uppercase_name);
1488 data = PyUnicode_DATA(uppercase_name);
1489 for (i=0; i<len; i++) {
1490 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1491 if ((ch >= '0' && ch <= '9')
1492 || (ch >= 'A' && ch <= 'Z')
1493 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001494 {
Victor Stinner35466c52010-04-22 11:23:23 +00001495 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001496 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001497 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001498 goto finally;
1499 }
1500 }
1501
Victor Stinner35466c52010-04-22 11:23:23 +00001502 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1503 if (!uppercase_name_str)
1504 goto finally;
1505
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001506 if (callable != Py_None && !PyCallable_Check(callable)) {
1507 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1508 goto finally;
1509 }
1510
1511 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001512 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1513 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001514 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001515 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1516 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001517 }
1518
1519 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001520 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 SQLITE_UTF8,
1522 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001523 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001524 if (rc != SQLITE_OK) {
1525 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001526 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001527 goto finally;
1528 }
1529
1530finally:
1531 Py_XDECREF(uppercase_name);
1532
1533 if (PyErr_Occurred()) {
1534 retval = NULL;
1535 } else {
1536 Py_INCREF(Py_None);
1537 retval = Py_None;
1538 }
1539
1540 return retval;
1541}
1542
Christian Heimesbbe741d2008-03-28 10:53:29 +00001543/* Called when the connection is used as a context manager. Returns itself as a
1544 * convenience to the caller. */
1545static PyObject *
1546pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1547{
1548 Py_INCREF(self);
1549 return (PyObject*)self;
1550}
1551
1552/** Called when the connection is used as a context manager. If there was any
1553 * exception, a rollback takes place; otherwise we commit. */
1554static PyObject *
1555pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1556{
1557 PyObject* exc_type, *exc_value, *exc_tb;
1558 char* method_name;
1559 PyObject* result;
1560
1561 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1562 return NULL;
1563 }
1564
1565 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1566 method_name = "commit";
1567 } else {
1568 method_name = "rollback";
1569 }
1570
1571 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1572 if (!result) {
1573 return NULL;
1574 }
1575 Py_DECREF(result);
1576
1577 Py_RETURN_FALSE;
1578}
1579
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001580static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001581PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001582
1583static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001584 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1585 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001586 {NULL}
1587};
1588
1589static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001590 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001591 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001592 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001593 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001594 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001595 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001596 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001597 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001598 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001599 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001600 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001601 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001602 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001603 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringf9cee222010-03-05 15:20:03 +00001604 #ifdef HAVE_LOAD_EXTENSION
1605 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1606 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1607 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1608 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1609 #endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001610 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1611 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001612 {"set_trace_callback", (PyCFunction)pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1613 PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001614 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001615 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001616 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001617 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001618 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001619 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001620 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001621 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001622 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001623 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001624 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001625 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001626 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1627 PyDoc_STR("For context manager. Non-standard.")},
1628 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1629 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001630 {NULL, NULL}
1631};
1632
1633static struct PyMemberDef connection_members[] =
1634{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001635 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1636 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1637 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1638 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1639 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1640 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1641 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1642 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1643 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1644 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001645 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1646 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
R. David Murrayd35251d2010-06-01 01:32:12 +00001647 {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001648 {NULL}
1649};
1650
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001651PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001652 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001653 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001654 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001655 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001656 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001657 0, /* tp_print */
1658 0, /* tp_getattr */
1659 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001660 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001661 0, /* tp_repr */
1662 0, /* tp_as_number */
1663 0, /* tp_as_sequence */
1664 0, /* tp_as_mapping */
1665 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001666 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001667 0, /* tp_str */
1668 0, /* tp_getattro */
1669 0, /* tp_setattro */
1670 0, /* tp_as_buffer */
1671 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1672 connection_doc, /* tp_doc */
1673 0, /* tp_traverse */
1674 0, /* tp_clear */
1675 0, /* tp_richcompare */
1676 0, /* tp_weaklistoffset */
1677 0, /* tp_iter */
1678 0, /* tp_iternext */
1679 connection_methods, /* tp_methods */
1680 connection_members, /* tp_members */
1681 connection_getset, /* tp_getset */
1682 0, /* tp_base */
1683 0, /* tp_dict */
1684 0, /* tp_descr_get */
1685 0, /* tp_descr_set */
1686 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001687 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001688 0, /* tp_alloc */
1689 0, /* tp_new */
1690 0 /* tp_free */
1691};
1692
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001693extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001694{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001695 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1696 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001697}