blob: 0ed196d0e39e034edc62b3fb835d0078c4d62928 [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +00001/* connection.c - the connection type
2 *
Gerhard Häring3bbb6722010-03-05 09:12:37 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Anthony Baxterc51ee692006-04-01 00:57:31 +00004 *
5 * This file is part of pysqlite.
Anthony Baxter72289a62006-04-04 06:29:05 +00006 *
Anthony Baxterc51ee692006-04-01 00:57:31 +00007 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "cache.h"
25#include "module.h"
26#include "connection.h"
27#include "statement.h"
28#include "cursor.h"
29#include "prepare_protocol.h"
30#include "util.h"
Anthony Baxter72289a62006-04-04 06:29:05 +000031#include "sqlitecompat.h"
32
Anthony Baxterc51ee692006-04-01 00:57:31 +000033#include "pythread.h"
34
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000035#define ACTION_FINALIZE 1
36#define ACTION_RESET 2
37
Gerhard Häring3bbb6722010-03-05 09:12:37 +000038#if SQLITE_VERSION_NUMBER >= 3003008
39#ifndef SQLITE_OMIT_LOAD_EXTENSION
40#define HAVE_LOAD_EXTENSION
41#endif
42#endif
43
Gerhard Häring0741a602007-01-14 01:43:50 +000044static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Gerhard Häring3bbb6722010-03-05 09:12:37 +000045static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Anthony Baxterc51ee692006-04-01 00:57:31 +000046
Gerhard Häringb2e88162006-06-14 22:28:37 +000047
Gerhard Häring6e1afcf2008-09-12 18:58:57 +000048static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Gerhard Häringb2e88162006-06-14 22:28:37 +000049{
50 /* in older SQLite versions, calling sqlite3_result_error in callbacks
51 * triggers a bug in SQLite that leads either to irritating results or
52 * segfaults, depending on the SQLite version */
53#if SQLITE_VERSION_NUMBER >= 3003003
54 sqlite3_result_error(ctx, errmsg, len);
Neal Norwitzfe7d0c32006-06-15 04:54:29 +000055#else
Gerhard Häring0741a602007-01-14 01:43:50 +000056 PyErr_SetString(pysqlite_OperationalError, errmsg);
Gerhard Häringb2e88162006-06-14 22:28:37 +000057#endif
58}
59
Gerhard Häring0741a602007-01-14 01:43:50 +000060int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +000061{
62 static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
63
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000064 PyObject* database;
Anthony Baxterc51ee692006-04-01 00:57:31 +000065 int detect_types = 0;
66 PyObject* isolation_level = NULL;
67 PyObject* factory = NULL;
68 int check_same_thread = 1;
69 int cached_statements = 100;
70 double timeout = 5.0;
71 int rc;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000072 PyObject* class_attr = NULL;
73 PyObject* class_attr_str = NULL;
74 int is_apsw_connection = 0;
75 PyObject* database_utf8;
Anthony Baxterc51ee692006-04-01 00:57:31 +000076
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000077 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOi", kwlist,
Anthony Baxterc51ee692006-04-01 00:57:31 +000078 &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
79 {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000080 return -1;
Anthony Baxterc51ee692006-04-01 00:57:31 +000081 }
82
Gerhard Häring3bbb6722010-03-05 09:12:37 +000083 self->initialized = 1;
84
Anthony Baxterc51ee692006-04-01 00:57:31 +000085 self->begin_statement = NULL;
86
87 self->statement_cache = NULL;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000088 self->statements = NULL;
Gerhard Häring3bbb6722010-03-05 09:12:37 +000089 self->cursors = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +000090
91 Py_INCREF(Py_None);
92 self->row_factory = Py_None;
93
94 Py_INCREF(&PyUnicode_Type);
95 self->text_factory = (PyObject*)&PyUnicode_Type;
96
Gregory P. Smithdd96db62008-06-09 04:58:54 +000097 if (PyString_Check(database) || PyUnicode_Check(database)) {
98 if (PyString_Check(database)) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000099 database_utf8 = database;
100 Py_INCREF(database_utf8);
101 } else {
102 database_utf8 = PyUnicode_AsUTF8String(database);
103 if (!database_utf8) {
104 return -1;
105 }
106 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000107
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000108 Py_BEGIN_ALLOW_THREADS
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000109 rc = sqlite3_open(PyString_AsString(database_utf8), &self->db);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000110 Py_END_ALLOW_THREADS
111
112 Py_DECREF(database_utf8);
113
114 if (rc != SQLITE_OK) {
115 _pysqlite_seterror(self->db, NULL);
116 return -1;
117 }
118 } else {
119 /* Create a pysqlite connection from a APSW connection */
120 class_attr = PyObject_GetAttrString(database, "__class__");
121 if (class_attr) {
122 class_attr_str = PyObject_Str(class_attr);
123 if (class_attr_str) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000124 if (strcmp(PyString_AsString(class_attr_str), "<type 'apsw.Connection'>") == 0) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000125 /* In the APSW Connection object, the first entry after
126 * PyObject_HEAD is the sqlite3* we want to get hold of.
127 * Luckily, this is the same layout as we have in our
128 * pysqlite_Connection */
129 self->db = ((pysqlite_Connection*)database)->db;
130
131 Py_INCREF(database);
132 self->apsw_connection = database;
133 is_apsw_connection = 1;
134 }
135 }
136 }
137 Py_XDECREF(class_attr_str);
138 Py_XDECREF(class_attr);
139
140 if (!is_apsw_connection) {
141 PyErr_SetString(PyExc_ValueError, "database parameter must be string or APSW Connection object");
142 return -1;
143 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000144 }
145
146 if (!isolation_level) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000147 isolation_level = PyString_FromString("");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000148 if (!isolation_level) {
149 return -1;
150 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000151 } else {
152 Py_INCREF(isolation_level);
153 }
154 self->isolation_level = NULL;
Victor Stinner94502192013-12-19 16:44:48 +0100155 if (pysqlite_connection_set_isolation_level(self, isolation_level) < 0) {
156 Py_DECREF(isolation_level);
157 return -1;
158 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000159 Py_DECREF(isolation_level);
160
Gerhard Häring0741a602007-01-14 01:43:50 +0000161 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000162 if (PyErr_Occurred()) {
163 return -1;
164 }
165
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000166 self->created_statements = 0;
167 self->created_cursors = 0;
168
169 /* Create lists of weak references to statements/cursors */
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000170 self->statements = PyList_New(0);
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000171 self->cursors = PyList_New(0);
172 if (!self->statements || !self->cursors) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000173 return -1;
174 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000175
Anthony Baxterc51ee692006-04-01 00:57:31 +0000176 /* By default, the Cache class INCREFs the factory in its initializer, and
177 * decrefs it in its deallocator method. Since this would create a circular
178 * reference here, we're breaking it by decrementing self, and telling the
179 * cache class to not decref the factory (self) in its deallocator.
180 */
181 self->statement_cache->decref_factory = 0;
182 Py_DECREF(self);
183
184 self->inTransaction = 0;
185 self->detect_types = detect_types;
186 self->timeout = timeout;
187 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandld3eaa742009-04-05 11:07:14 +0000188#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000189 self->thread_ident = PyThread_get_thread_ident();
Georg Brandld3eaa742009-04-05 11:07:14 +0000190#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000191 self->check_same_thread = check_same_thread;
192
193 self->function_pinboard = PyDict_New();
Anthony Baxter72289a62006-04-04 06:29:05 +0000194 if (!self->function_pinboard) {
195 return -1;
196 }
197
198 self->collations = PyDict_New();
199 if (!self->collations) {
200 return -1;
201 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000202
Gerhard Häring0741a602007-01-14 01:43:50 +0000203 self->Warning = pysqlite_Warning;
204 self->Error = pysqlite_Error;
205 self->InterfaceError = pysqlite_InterfaceError;
206 self->DatabaseError = pysqlite_DatabaseError;
207 self->DataError = pysqlite_DataError;
208 self->OperationalError = pysqlite_OperationalError;
209 self->IntegrityError = pysqlite_IntegrityError;
210 self->InternalError = pysqlite_InternalError;
211 self->ProgrammingError = pysqlite_ProgrammingError;
212 self->NotSupportedError = pysqlite_NotSupportedError;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000213
214 return 0;
215}
216
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000217/* Empty the entire statement cache of this connection */
Gerhard Häring0741a602007-01-14 01:43:50 +0000218void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000219{
Gerhard Häring0741a602007-01-14 01:43:50 +0000220 pysqlite_Node* node;
221 pysqlite_Statement* statement;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000222
223 node = self->statement_cache->first;
224
225 while (node) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000226 statement = (pysqlite_Statement*)(node->data);
227 (void)pysqlite_statement_finalize(statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000228 node = node->next;
229 }
230
231 Py_DECREF(self->statement_cache);
Gerhard Häring0741a602007-01-14 01:43:50 +0000232 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000233 Py_DECREF(self);
234 self->statement_cache->decref_factory = 0;
235}
236
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000237/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000238void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000239{
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000240 int i;
241 PyObject* weakref;
242 PyObject* statement;
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000243 pysqlite_Cursor* cursor;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000244
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000245 for (i = 0; i < PyList_Size(self->statements); i++) {
246 weakref = PyList_GetItem(self->statements, i);
247 statement = PyWeakref_GetObject(weakref);
248 if (statement != Py_None) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000249 if (action == ACTION_RESET) {
250 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
251 } else {
252 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
253 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000254 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000255 }
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000256
257 if (reset_cursors) {
258 for (i = 0; i < PyList_Size(self->cursors); i++) {
259 weakref = PyList_GetItem(self->cursors, i);
260 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
261 if ((PyObject*)cursor != Py_None) {
262 cursor->reset = 1;
263 }
264 }
265 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000266}
267
Gerhard Häring0741a602007-01-14 01:43:50 +0000268void pysqlite_connection_dealloc(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000269{
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000270 PyObject* ret = NULL;
271
Anthony Baxterc51ee692006-04-01 00:57:31 +0000272 Py_XDECREF(self->statement_cache);
273
274 /* Clean up if user has not called .close() explicitly. */
275 if (self->db) {
276 Py_BEGIN_ALLOW_THREADS
277 sqlite3_close(self->db);
278 Py_END_ALLOW_THREADS
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000279 } else if (self->apsw_connection) {
280 ret = PyObject_CallMethod(self->apsw_connection, "close", "");
281 Py_XDECREF(ret);
282 Py_XDECREF(self->apsw_connection);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000283 }
284
285 if (self->begin_statement) {
286 PyMem_Free(self->begin_statement);
287 }
288 Py_XDECREF(self->isolation_level);
289 Py_XDECREF(self->function_pinboard);
290 Py_XDECREF(self->row_factory);
291 Py_XDECREF(self->text_factory);
Anthony Baxter72289a62006-04-04 06:29:05 +0000292 Py_XDECREF(self->collations);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000293 Py_XDECREF(self->statements);
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000294 Py_XDECREF(self->cursors);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000295
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000296 self->ob_type->tp_free((PyObject*)self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000297}
298
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000299/*
300 * Registers a cursor with the connection.
301 *
302 * 0 => error; 1 => ok
303 */
304int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
305{
306 PyObject* weakref;
307
308 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
309 if (!weakref) {
310 goto error;
311 }
312
313 if (PyList_Append(connection->cursors, weakref) != 0) {
314 Py_CLEAR(weakref);
315 goto error;
316 }
317
318 Py_DECREF(weakref);
319
320 return 1;
321error:
322 return 0;
323}
324
Gerhard Häring0741a602007-01-14 01:43:50 +0000325PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000326{
327 static char *kwlist[] = {"factory", NULL, NULL};
328 PyObject* factory = NULL;
329 PyObject* cursor;
330
Anthony Baxterc51ee692006-04-01 00:57:31 +0000331 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
332 &factory)) {
333 return NULL;
334 }
335
Gerhard Häring0741a602007-01-14 01:43:50 +0000336 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000337 return NULL;
338 }
339
340 if (factory == NULL) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000341 factory = (PyObject*)&pysqlite_CursorType;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000342 }
343
344 cursor = PyObject_CallFunction(factory, "O", self);
345
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000346 _pysqlite_drop_unused_cursor_references(self);
347
Anthony Baxterc51ee692006-04-01 00:57:31 +0000348 if (cursor && self->row_factory != Py_None) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000349 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000350 Py_INCREF(self->row_factory);
Gerhard Häring0741a602007-01-14 01:43:50 +0000351 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000352 }
353
354 return cursor;
355}
356
Gerhard Häring0741a602007-01-14 01:43:50 +0000357PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000358{
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000359 PyObject* ret;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000360 int rc;
361
Gerhard Häring0741a602007-01-14 01:43:50 +0000362 if (!pysqlite_check_thread(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000363 return NULL;
364 }
365
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000366 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000367
368 if (self->db) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000369 if (self->apsw_connection) {
370 ret = PyObject_CallMethod(self->apsw_connection, "close", "");
371 Py_XDECREF(ret);
Serhiy Storchaka98a97222014-02-09 13:14:04 +0200372 Py_CLEAR(self->apsw_connection);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000373 self->db = NULL;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000374 } else {
375 Py_BEGIN_ALLOW_THREADS
376 rc = sqlite3_close(self->db);
377 Py_END_ALLOW_THREADS
378
379 if (rc != SQLITE_OK) {
380 _pysqlite_seterror(self->db, NULL);
381 return NULL;
382 } else {
383 self->db = NULL;
384 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000385 }
386 }
387
388 Py_INCREF(Py_None);
389 return Py_None;
390}
391
392/*
393 * Checks if a connection object is usable (i. e. not closed).
394 *
395 * 0 => error; 1 => ok
396 */
Gerhard Häring0741a602007-01-14 01:43:50 +0000397int pysqlite_check_connection(pysqlite_Connection* con)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000398{
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000399 if (!con->initialized) {
400 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
401 return 0;
402 }
403
Anthony Baxterc51ee692006-04-01 00:57:31 +0000404 if (!con->db) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000405 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000406 return 0;
407 } else {
408 return 1;
409 }
410}
411
Gerhard Häring0741a602007-01-14 01:43:50 +0000412PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000413{
414 int rc;
415 const char* tail;
416 sqlite3_stmt* statement;
417
418 Py_BEGIN_ALLOW_THREADS
419 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
420 Py_END_ALLOW_THREADS
421
422 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000423 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000424 goto error;
425 }
426
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000427 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000428 if (rc == SQLITE_DONE) {
429 self->inTransaction = 1;
430 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000431 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000432 }
433
434 Py_BEGIN_ALLOW_THREADS
435 rc = sqlite3_finalize(statement);
436 Py_END_ALLOW_THREADS
437
438 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000439 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000440 }
441
442error:
443 if (PyErr_Occurred()) {
444 return NULL;
445 } else {
446 Py_INCREF(Py_None);
447 return Py_None;
448 }
449}
450
Gerhard Häring0741a602007-01-14 01:43:50 +0000451PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000452{
453 int rc;
454 const char* tail;
455 sqlite3_stmt* statement;
456
Gerhard Häring0741a602007-01-14 01:43:50 +0000457 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000458 return NULL;
459 }
460
461 if (self->inTransaction) {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000462 pysqlite_do_all_statements(self, ACTION_RESET, 0);
463
Anthony Baxterc51ee692006-04-01 00:57:31 +0000464 Py_BEGIN_ALLOW_THREADS
465 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
466 Py_END_ALLOW_THREADS
467 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000468 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000469 goto error;
470 }
471
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000472 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000473 if (rc == SQLITE_DONE) {
474 self->inTransaction = 0;
475 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000476 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000477 }
478
479 Py_BEGIN_ALLOW_THREADS
480 rc = sqlite3_finalize(statement);
481 Py_END_ALLOW_THREADS
482 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000483 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000484 }
485
486 }
487
488error:
489 if (PyErr_Occurred()) {
490 return NULL;
491 } else {
492 Py_INCREF(Py_None);
493 return Py_None;
494 }
495}
496
Gerhard Häring0741a602007-01-14 01:43:50 +0000497PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000498{
499 int rc;
500 const char* tail;
501 sqlite3_stmt* statement;
502
Gerhard Häring0741a602007-01-14 01:43:50 +0000503 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000504 return NULL;
505 }
506
507 if (self->inTransaction) {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000508 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000509
510 Py_BEGIN_ALLOW_THREADS
511 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
512 Py_END_ALLOW_THREADS
513 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000514 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000515 goto error;
516 }
517
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000518 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000519 if (rc == SQLITE_DONE) {
520 self->inTransaction = 0;
521 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000522 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000523 }
524
525 Py_BEGIN_ALLOW_THREADS
526 rc = sqlite3_finalize(statement);
527 Py_END_ALLOW_THREADS
528 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000529 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000530 }
531
532 }
533
534error:
535 if (PyErr_Occurred()) {
536 return NULL;
537 } else {
538 Py_INCREF(Py_None);
539 return Py_None;
540 }
541}
542
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200543static int
544_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000545{
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200546 if (py_val == Py_None) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000547 sqlite3_result_null(context);
548 } else if (PyInt_Check(py_val)) {
Ned Deily77e77a12012-05-19 23:35:05 -0700549 sqlite3_result_int64(context, (sqlite_int64)PyInt_AsLong(py_val));
Petri Lehtinen4ab701b2012-02-21 13:58:40 +0200550 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200551 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
552 if (value == -1 && PyErr_Occurred())
553 return -1;
554 sqlite3_result_int64(context, value);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000555 } else if (PyFloat_Check(py_val)) {
556 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
557 } else if (PyBuffer_Check(py_val)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200558 const char* buffer;
559 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000560 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
561 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200562 return -1;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000563 }
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200564 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000565 } else if (PyString_Check(py_val)) {
566 sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000567 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200568 PyObject * stringval = PyUnicode_AsUTF8String(py_val);
569 if (!stringval)
570 return -1;
571 sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
572 Py_DECREF(stringval);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000573 } else {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200574 return -1;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000575 }
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200576 return 0;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000577}
578
Gerhard Häring0741a602007-01-14 01:43:50 +0000579PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000580{
581 PyObject* args;
582 int i;
583 sqlite3_value* cur_value;
584 PyObject* cur_py_value;
585 const char* val_str;
Neal Norwitz95f0e4c2006-04-01 09:08:06 +0000586 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000587 void* raw_buffer;
588
589 args = PyTuple_New(argc);
Anthony Baxter72289a62006-04-04 06:29:05 +0000590 if (!args) {
591 return NULL;
592 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000593
594 for (i = 0; i < argc; i++) {
595 cur_value = argv[i];
596 switch (sqlite3_value_type(argv[i])) {
597 case SQLITE_INTEGER:
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200598 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Anthony Baxterc51ee692006-04-01 00:57:31 +0000599 break;
600 case SQLITE_FLOAT:
601 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
602 break;
603 case SQLITE_TEXT:
604 val_str = (const char*)sqlite3_value_text(cur_value);
605 cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
606 /* TODO: have a way to show errors here */
607 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000608 PyErr_Clear();
Anthony Baxterc51ee692006-04-01 00:57:31 +0000609 Py_INCREF(Py_None);
610 cur_py_value = Py_None;
611 }
612 break;
613 case SQLITE_BLOB:
614 buflen = sqlite3_value_bytes(cur_value);
615 cur_py_value = PyBuffer_New(buflen);
616 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000617 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000618 }
619 if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000620 Py_DECREF(cur_py_value);
621 cur_py_value = NULL;
622 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000623 }
624 memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen);
625 break;
626 case SQLITE_NULL:
627 default:
628 Py_INCREF(Py_None);
629 cur_py_value = Py_None;
630 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000631
632 if (!cur_py_value) {
633 Py_DECREF(args);
634 return NULL;
635 }
636
Anthony Baxterc51ee692006-04-01 00:57:31 +0000637 PyTuple_SetItem(args, i, cur_py_value);
638
639 }
640
641 return args;
642}
643
Gerhard Häring0741a602007-01-14 01:43:50 +0000644void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000645{
646 PyObject* args;
647 PyObject* py_func;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000648 PyObject* py_retval = NULL;
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200649 int ok;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000650
Georg Brandld3eaa742009-04-05 11:07:14 +0000651#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000652 PyGILState_STATE threadstate;
653
654 threadstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000655#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000656
657 py_func = (PyObject*)sqlite3_user_data(context);
658
Gerhard Häring0741a602007-01-14 01:43:50 +0000659 args = _pysqlite_build_py_params(context, argc, argv);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000660 if (args) {
661 py_retval = PyObject_CallObject(py_func, args);
662 Py_DECREF(args);
663 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000664
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200665 ok = 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000666 if (py_retval) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200667 ok = _pysqlite_set_result(context, py_retval) == 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000668 Py_DECREF(py_retval);
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200669 }
670 if (!ok) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000671 if (_enable_callback_tracebacks) {
672 PyErr_Print();
673 } else {
674 PyErr_Clear();
675 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000676 _sqlite3_result_error(context, "user-defined function raised exception", -1);
Gerhard Häring1541ef02006-06-13 22:24:47 +0000677 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000678
Georg Brandld3eaa742009-04-05 11:07:14 +0000679#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000680 PyGILState_Release(threadstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000681#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000682}
683
Gerhard Häring0741a602007-01-14 01:43:50 +0000684static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000685{
686 PyObject* args;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000687 PyObject* function_result = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000688 PyObject* aggregate_class;
689 PyObject** aggregate_instance;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000690 PyObject* stepmethod = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000691
Georg Brandld3eaa742009-04-05 11:07:14 +0000692#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000693 PyGILState_STATE threadstate;
694
695 threadstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000696#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000697
698 aggregate_class = (PyObject*)sqlite3_user_data(context);
699
700 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
701
702 if (*aggregate_instance == 0) {
703 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
704
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000705 if (PyErr_Occurred()) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000706 *aggregate_instance = 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000707 if (_enable_callback_tracebacks) {
708 PyErr_Print();
709 } else {
710 PyErr_Clear();
711 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000712 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000713 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000714 }
715 }
716
717 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000718 if (!stepmethod) {
719 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000720 }
721
Gerhard Häring0741a602007-01-14 01:43:50 +0000722 args = _pysqlite_build_py_params(context, argc, params);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000723 if (!args) {
724 goto error;
725 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000726
727 function_result = PyObject_CallObject(stepmethod, args);
728 Py_DECREF(args);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000729
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000730 if (!function_result) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000731 if (_enable_callback_tracebacks) {
732 PyErr_Print();
733 } else {
734 PyErr_Clear();
735 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000736 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000737 }
738
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000739error:
740 Py_XDECREF(stepmethod);
741 Py_XDECREF(function_result);
742
Georg Brandld3eaa742009-04-05 11:07:14 +0000743#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000744 PyGILState_Release(threadstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000745#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000746}
747
Gerhard Häring0741a602007-01-14 01:43:50 +0000748void _pysqlite_final_callback(sqlite3_context* context)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000749{
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200750 PyObject* function_result;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000751 PyObject** aggregate_instance;
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200752 int ok;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000753
Georg Brandld3eaa742009-04-05 11:07:14 +0000754#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000755 PyGILState_STATE threadstate;
756
757 threadstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000758#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000759
Anthony Baxterc51ee692006-04-01 00:57:31 +0000760 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
761 if (!*aggregate_instance) {
762 /* this branch is executed if there was an exception in the aggregate's
763 * __init__ */
764
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000765 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000766 }
767
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000768 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200769 Py_DECREF(*aggregate_instance);
770
771 ok = 0;
772 if (function_result) {
773 ok = _pysqlite_set_result(context, function_result) == 0;
774 Py_DECREF(function_result);
775 }
776 if (!ok) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000777 if (_enable_callback_tracebacks) {
778 PyErr_Print();
779 } else {
780 PyErr_Clear();
781 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000782 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000783 }
784
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000785error:
Georg Brandld3eaa742009-04-05 11:07:14 +0000786#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000787 PyGILState_Release(threadstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000788#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000789}
790
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000791static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000792{
793 PyObject* new_list;
794 PyObject* weakref;
795 int i;
796
797 /* we only need to do this once in a while */
798 if (self->created_statements++ < 200) {
799 return;
800 }
801
802 self->created_statements = 0;
803
804 new_list = PyList_New(0);
805 if (!new_list) {
806 return;
807 }
808
809 for (i = 0; i < PyList_Size(self->statements); i++) {
810 weakref = PyList_GetItem(self->statements, i);
Gerhard Häringecd20102006-06-19 21:17:35 +0000811 if (PyWeakref_GetObject(weakref) != Py_None) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000812 if (PyList_Append(new_list, weakref) != 0) {
813 Py_DECREF(new_list);
814 return;
815 }
816 }
817 }
818
819 Py_DECREF(self->statements);
820 self->statements = new_list;
821}
Anthony Baxterc51ee692006-04-01 00:57:31 +0000822
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000823static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
824{
825 PyObject* new_list;
826 PyObject* weakref;
827 int i;
828
829 /* we only need to do this once in a while */
830 if (self->created_cursors++ < 200) {
831 return;
832 }
833
834 self->created_cursors = 0;
835
836 new_list = PyList_New(0);
837 if (!new_list) {
838 return;
839 }
840
841 for (i = 0; i < PyList_Size(self->cursors); i++) {
842 weakref = PyList_GetItem(self->cursors, i);
843 if (PyWeakref_GetObject(weakref) != Py_None) {
844 if (PyList_Append(new_list, weakref) != 0) {
845 Py_DECREF(new_list);
846 return;
847 }
848 }
849 }
850
851 Py_DECREF(self->cursors);
852 self->cursors = new_list;
853}
854
Gerhard Häring0741a602007-01-14 01:43:50 +0000855PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000856{
857 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
858
859 PyObject* func;
860 char* name;
861 int narg;
862 int rc;
863
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000864 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
865 return NULL;
866 }
867
Anthony Baxterc51ee692006-04-01 00:57:31 +0000868 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
869 &name, &narg, &func))
870 {
871 return NULL;
872 }
873
Gerhard Häring0741a602007-01-14 01:43:50 +0000874 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000875
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000876 if (rc != SQLITE_OK) {
877 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000878 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000879 return NULL;
880 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000881 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
882 return NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000883
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000884 Py_INCREF(Py_None);
885 return Py_None;
886 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000887}
888
Gerhard Häring0741a602007-01-14 01:43:50 +0000889PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000890{
891 PyObject* aggregate_class;
892
893 int n_arg;
894 char* name;
895 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
896 int rc;
897
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000898 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
899 return NULL;
900 }
901
Anthony Baxterc51ee692006-04-01 00:57:31 +0000902 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
903 kwlist, &name, &n_arg, &aggregate_class)) {
904 return NULL;
905 }
906
Gerhard Häring0741a602007-01-14 01:43:50 +0000907 rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000908 if (rc != SQLITE_OK) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000909 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000910 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000911 return NULL;
912 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000913 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
914 return NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000915
916 Py_INCREF(Py_None);
917 return Py_None;
918 }
919}
920
Gerhard Häring0741a602007-01-14 01:43:50 +0000921static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
Gerhard Häring1541ef02006-06-13 22:24:47 +0000922{
923 PyObject *ret;
924 int rc;
Georg Brandld3eaa742009-04-05 11:07:14 +0000925#ifdef WITH_THREAD
Gerhard Häring1541ef02006-06-13 22:24:47 +0000926 PyGILState_STATE gilstate;
927
928 gilstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000929#endif
Gerhard Häring1541ef02006-06-13 22:24:47 +0000930 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
931
932 if (!ret) {
933 if (_enable_callback_tracebacks) {
934 PyErr_Print();
935 } else {
936 PyErr_Clear();
937 }
938
939 rc = SQLITE_DENY;
940 } else {
941 if (PyInt_Check(ret)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200942 rc = _PyInt_AsInt(ret);
943 if (rc == -1 && PyErr_Occurred())
944 rc = SQLITE_DENY;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000945 } else {
946 rc = SQLITE_DENY;
947 }
948 Py_DECREF(ret);
949 }
950
Georg Brandld3eaa742009-04-05 11:07:14 +0000951#ifdef WITH_THREAD
Gerhard Häring1541ef02006-06-13 22:24:47 +0000952 PyGILState_Release(gilstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000953#endif
Gerhard Häring1541ef02006-06-13 22:24:47 +0000954 return rc;
955}
956
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000957static int _progress_handler(void* user_arg)
958{
959 int rc;
960 PyObject *ret;
Georg Brandld3eaa742009-04-05 11:07:14 +0000961#ifdef WITH_THREAD
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000962 PyGILState_STATE gilstate;
963
964 gilstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000965#endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000966 ret = PyObject_CallFunction((PyObject*)user_arg, "");
967
968 if (!ret) {
969 if (_enable_callback_tracebacks) {
970 PyErr_Print();
971 } else {
972 PyErr_Clear();
973 }
974
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000975 /* abort query if error occurred */
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200976 rc = 1;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000977 } else {
978 rc = (int)PyObject_IsTrue(ret);
Neal Norwitzca752f32008-03-03 04:37:45 +0000979 Py_DECREF(ret);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000980 }
981
Georg Brandld3eaa742009-04-05 11:07:14 +0000982#ifdef WITH_THREAD
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000983 PyGILState_Release(gilstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000984#endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000985 return rc;
986}
987
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000988static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häring1541ef02006-06-13 22:24:47 +0000989{
990 PyObject* authorizer_cb;
991
992 static char *kwlist[] = { "authorizer_callback", NULL };
993 int rc;
994
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000995 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
996 return NULL;
997 }
998
Gerhard Häring1541ef02006-06-13 22:24:47 +0000999 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1000 kwlist, &authorizer_cb)) {
1001 return NULL;
1002 }
1003
1004 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1005
1006 if (rc != SQLITE_OK) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001007 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Gerhard Häring1541ef02006-06-13 22:24:47 +00001008 return NULL;
1009 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001010 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1011 return NULL;
Gerhard Häring1541ef02006-06-13 22:24:47 +00001012
1013 Py_INCREF(Py_None);
1014 return Py_None;
1015 }
1016}
1017
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001018static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001019{
1020 PyObject* progress_handler;
1021 int n;
1022
1023 static char *kwlist[] = { "progress_handler", "n", NULL };
1024
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001025 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1026 return NULL;
1027 }
1028
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001029 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1030 kwlist, &progress_handler, &n)) {
1031 return NULL;
1032 }
1033
1034 if (progress_handler == Py_None) {
1035 /* None clears the progress handler previously set */
1036 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1037 } else {
1038 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001039 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1040 return NULL;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001041 }
1042
1043 Py_INCREF(Py_None);
1044 return Py_None;
1045}
1046
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001047#ifdef HAVE_LOAD_EXTENSION
1048static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1049{
1050 int rc;
1051 int onoff;
1052
1053 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1054 return NULL;
1055 }
1056
1057 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1058 return NULL;
1059 }
1060
1061 rc = sqlite3_enable_load_extension(self->db, onoff);
1062
1063 if (rc != SQLITE_OK) {
1064 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1065 return NULL;
1066 } else {
1067 Py_INCREF(Py_None);
1068 return Py_None;
1069 }
1070}
1071
1072static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1073{
1074 int rc;
1075 char* extension_name;
1076 char* errmsg;
1077
1078 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1079 return NULL;
1080 }
1081
1082 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1083 return NULL;
1084 }
1085
1086 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1087 if (rc != 0) {
1088 PyErr_SetString(pysqlite_OperationalError, errmsg);
1089 return NULL;
1090 } else {
1091 Py_INCREF(Py_None);
1092 return Py_None;
1093 }
1094}
1095#endif
1096
Gerhard Häring0741a602007-01-14 01:43:50 +00001097int pysqlite_check_thread(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001098{
Georg Brandld3eaa742009-04-05 11:07:14 +00001099#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +00001100 if (self->check_same_thread) {
1101 if (PyThread_get_thread_ident() != self->thread_ident) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001102 PyErr_Format(pysqlite_ProgrammingError,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001103 "SQLite objects created in a thread can only be used in that same thread."
1104 "The object was created in thread id %ld and this is thread id %ld",
1105 self->thread_ident, PyThread_get_thread_ident());
1106 return 0;
1107 }
1108
1109 }
Georg Brandld3eaa742009-04-05 11:07:14 +00001110#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +00001111 return 1;
1112}
1113
Gerhard Häring0741a602007-01-14 01:43:50 +00001114static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001115{
1116 Py_INCREF(self->isolation_level);
1117 return self->isolation_level;
1118}
1119
Gerhard Häring0741a602007-01-14 01:43:50 +00001120static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Anthony Baxter72289a62006-04-04 06:29:05 +00001121{
Gerhard Häring0741a602007-01-14 01:43:50 +00001122 if (!pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001123 return NULL;
1124 } else {
1125 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1126 }
1127}
1128
Gerhard Häring0741a602007-01-14 01:43:50 +00001129static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001130{
Anthony Baxterc51ee692006-04-01 00:57:31 +00001131 PyObject* res;
1132 PyObject* begin_statement;
Georg Brandla24869a2008-07-16 22:33:18 +00001133 char* begin_statement_str;
Anthony Baxterc51ee692006-04-01 00:57:31 +00001134
1135 Py_XDECREF(self->isolation_level);
1136
Neal Norwitz5b030652006-04-16 03:28:17 +00001137 if (self->begin_statement) {
1138 PyMem_Free(self->begin_statement);
1139 self->begin_statement = NULL;
1140 }
1141
Anthony Baxterc51ee692006-04-01 00:57:31 +00001142 if (isolation_level == Py_None) {
1143 Py_INCREF(Py_None);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001144 self->isolation_level = Py_None;
1145
Gerhard Häring0741a602007-01-14 01:43:50 +00001146 res = pysqlite_connection_commit(self, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001147 if (!res) {
1148 return -1;
1149 }
Anthony Baxterc51ee692006-04-01 00:57:31 +00001150 Py_DECREF(res);
1151
1152 self->inTransaction = 0;
1153 } else {
1154 Py_INCREF(isolation_level);
1155 self->isolation_level = isolation_level;
1156
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001157 begin_statement = PyString_FromString("BEGIN ");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001158 if (!begin_statement) {
1159 return -1;
1160 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001161 PyString_Concat(&begin_statement, isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001162 if (!begin_statement) {
1163 return -1;
1164 }
1165
Georg Brandla24869a2008-07-16 22:33:18 +00001166 begin_statement_str = PyString_AsString(begin_statement);
1167 if (!begin_statement_str) {
1168 Py_DECREF(begin_statement);
1169 return -1;
1170 }
1171 self->begin_statement = PyMem_Malloc(strlen(begin_statement_str) + 2);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001172 if (!self->begin_statement) {
Georg Brandla24869a2008-07-16 22:33:18 +00001173 Py_DECREF(begin_statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001174 return -1;
1175 }
1176
Georg Brandla24869a2008-07-16 22:33:18 +00001177 strcpy(self->begin_statement, begin_statement_str);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001178 Py_DECREF(begin_statement);
1179 }
1180
1181 return 0;
1182}
1183
Gerhard Häring0741a602007-01-14 01:43:50 +00001184PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001185{
1186 PyObject* sql;
Gerhard Häring0741a602007-01-14 01:43:50 +00001187 pysqlite_Statement* statement;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001188 PyObject* weakref;
Anthony Baxterc51ee692006-04-01 00:57:31 +00001189 int rc;
1190
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001191 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1192 return NULL;
1193 }
1194
Anthony Baxterc51ee692006-04-01 00:57:31 +00001195 if (!PyArg_ParseTuple(args, "O", &sql)) {
1196 return NULL;
1197 }
1198
Gerhard Häring0741a602007-01-14 01:43:50 +00001199 _pysqlite_drop_unused_statement_references(self);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001200
Gerhard Häring0741a602007-01-14 01:43:50 +00001201 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001202 if (!statement) {
1203 return NULL;
1204 }
1205
Victor Stinner6e055d72010-03-13 03:27:07 +00001206 statement->db = NULL;
1207 statement->st = NULL;
1208 statement->sql = NULL;
1209 statement->in_use = 0;
1210 statement->in_weakreflist = NULL;
1211
Gerhard Häring0741a602007-01-14 01:43:50 +00001212 rc = pysqlite_statement_create(statement, self, sql);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001213
1214 if (rc != SQLITE_OK) {
1215 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001216 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001217 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka0aa65622014-09-11 13:27:19 +03001218 if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_TypeError))
1219 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001220 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001221 (void)pysqlite_statement_reset(statement);
1222 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001223 }
1224
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001225 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001226 } else {
1227 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1228 if (!weakref) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001229 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001230 goto error;
1231 }
1232
1233 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001234 Py_CLEAR(weakref);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001235 goto error;
1236 }
1237
1238 Py_DECREF(weakref);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001239 }
1240
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001241error:
Anthony Baxterc51ee692006-04-01 00:57:31 +00001242 return (PyObject*)statement;
1243}
1244
Gerhard Häring0741a602007-01-14 01:43:50 +00001245PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001246{
1247 PyObject* cursor = 0;
1248 PyObject* result = 0;
1249 PyObject* method = 0;
1250
1251 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1252 if (!cursor) {
1253 goto error;
1254 }
1255
1256 method = PyObject_GetAttrString(cursor, "execute");
1257 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001258 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001259 goto error;
1260 }
1261
1262 result = PyObject_CallObject(method, args);
1263 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001264 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001265 }
1266
1267error:
1268 Py_XDECREF(result);
1269 Py_XDECREF(method);
1270
1271 return cursor;
1272}
1273
Gerhard Häring0741a602007-01-14 01:43:50 +00001274PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001275{
1276 PyObject* cursor = 0;
1277 PyObject* result = 0;
1278 PyObject* method = 0;
1279
1280 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1281 if (!cursor) {
1282 goto error;
1283 }
1284
1285 method = PyObject_GetAttrString(cursor, "executemany");
1286 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001287 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001288 goto error;
1289 }
1290
1291 result = PyObject_CallObject(method, args);
1292 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001293 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001294 }
1295
1296error:
1297 Py_XDECREF(result);
1298 Py_XDECREF(method);
1299
1300 return cursor;
1301}
1302
Gerhard Häring0741a602007-01-14 01:43:50 +00001303PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001304{
1305 PyObject* cursor = 0;
1306 PyObject* result = 0;
1307 PyObject* method = 0;
1308
1309 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1310 if (!cursor) {
1311 goto error;
1312 }
1313
1314 method = PyObject_GetAttrString(cursor, "executescript");
1315 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001316 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001317 goto error;
1318 }
1319
1320 result = PyObject_CallObject(method, args);
1321 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001322 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001323 }
1324
1325error:
1326 Py_XDECREF(result);
1327 Py_XDECREF(method);
1328
1329 return cursor;
1330}
1331
Anthony Baxter72289a62006-04-04 06:29:05 +00001332/* ------------------------- COLLATION CODE ------------------------ */
1333
1334static int
Gerhard Häring0741a602007-01-14 01:43:50 +00001335pysqlite_collation_callback(
Anthony Baxter72289a62006-04-04 06:29:05 +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 Brandld3eaa742009-04-05 11:07:14 +00001343#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001344 PyGILState_STATE gilstate;
Georg Brandld3eaa742009-04-05 11:07:14 +00001345#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001346 PyObject* retval = NULL;
Serhiy Storchaka35c52b62013-02-07 16:59:34 +02001347 long longval;
Anthony Baxter72289a62006-04-04 06:29:05 +00001348 int result = 0;
Georg Brandld3eaa742009-04-05 11:07:14 +00001349#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001350 gilstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +00001351#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001352
1353 if (PyErr_Occurred()) {
1354 goto finally;
1355 }
1356
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001357 string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length);
1358 string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length);
Anthony Baxter72289a62006-04-04 06:29:05 +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 Storchaka35c52b62013-02-07 16:59:34 +02001371 longval = PyLong_AsLongAndOverflow(retval, &result);
1372 if (longval == -1 && PyErr_Occurred()) {
1373 PyErr_Clear();
Anthony Baxter72289a62006-04-04 06:29:05 +00001374 result = 0;
1375 }
Serhiy Storchaka35c52b62013-02-07 16:59:34 +02001376 else if (!result) {
1377 if (longval > 0)
1378 result = 1;
1379 else if (longval < 0)
1380 result = -1;
1381 }
Anthony Baxter72289a62006-04-04 06:29:05 +00001382
1383finally:
1384 Py_XDECREF(string1);
1385 Py_XDECREF(string2);
1386 Py_XDECREF(retval);
Georg Brandld3eaa742009-04-05 11:07:14 +00001387#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001388 PyGILState_Release(gilstate);
Georg Brandld3eaa742009-04-05 11:07:14 +00001389#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001390 return result;
1391}
1392
1393static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001394pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Gerhard Häring1541ef02006-06-13 22:24:47 +00001395{
1396 PyObject* retval = NULL;
1397
Gerhard Häring0741a602007-01-14 01:43:50 +00001398 if (!pysqlite_check_connection(self)) {
Gerhard Häring1541ef02006-06-13 22:24:47 +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
Gregory P. Smithb9803422008-03-28 08:32:09 +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
Gerhard Häring1541ef02006-06-13 22:24:47 +00001457static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001458pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Anthony Baxter72289a62006-04-04 06:29:05 +00001459{
1460 PyObject* callable;
1461 PyObject* uppercase_name = 0;
1462 PyObject* name;
1463 PyObject* retval;
1464 char* chk;
1465 int rc;
1466
Gerhard Häring0741a602007-01-14 01:43:50 +00001467 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001468 goto finally;
1469 }
1470
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001471 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyString_Type, &name, &callable)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001472 goto finally;
1473 }
1474
1475 uppercase_name = PyObject_CallMethod(name, "upper", "");
1476 if (!uppercase_name) {
1477 goto finally;
1478 }
1479
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001480 chk = PyString_AsString(uppercase_name);
Anthony Baxter72289a62006-04-04 06:29:05 +00001481 while (*chk) {
1482 if ((*chk >= '0' && *chk <= '9')
1483 || (*chk >= 'A' && *chk <= 'Z')
1484 || (*chk == '_'))
1485 {
1486 chk++;
1487 } else {
Gerhard Häring0741a602007-01-14 01:43:50 +00001488 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Anthony Baxter72289a62006-04-04 06:29:05 +00001489 goto finally;
1490 }
1491 }
1492
1493 if (callable != Py_None && !PyCallable_Check(callable)) {
1494 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1495 goto finally;
1496 }
1497
1498 if (callable != Py_None) {
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001499 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1500 goto finally;
Anthony Baxter72289a62006-04-04 06:29:05 +00001501 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001502 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1503 goto finally;
Anthony Baxter72289a62006-04-04 06:29:05 +00001504 }
1505
1506 rc = sqlite3_create_collation(self->db,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001507 PyString_AsString(uppercase_name),
Anthony Baxter72289a62006-04-04 06:29:05 +00001508 SQLITE_UTF8,
1509 (callable != Py_None) ? callable : NULL,
Gerhard Häring0741a602007-01-14 01:43:50 +00001510 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001511 if (rc != SQLITE_OK) {
1512 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001513 _pysqlite_seterror(self->db, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001514 goto finally;
1515 }
1516
1517finally:
1518 Py_XDECREF(uppercase_name);
1519
1520 if (PyErr_Occurred()) {
1521 retval = NULL;
1522 } else {
1523 Py_INCREF(Py_None);
1524 retval = Py_None;
1525 }
1526
1527 return retval;
1528}
1529
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001530/* Called when the connection is used as a context manager. Returns itself as a
1531 * convenience to the caller. */
1532static PyObject *
1533pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1534{
1535 Py_INCREF(self);
1536 return (PyObject*)self;
1537}
1538
1539/** Called when the connection is used as a context manager. If there was any
1540 * exception, a rollback takes place; otherwise we commit. */
1541static PyObject *
1542pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1543{
1544 PyObject* exc_type, *exc_value, *exc_tb;
1545 char* method_name;
1546 PyObject* result;
1547
1548 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1549 return NULL;
1550 }
1551
1552 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1553 method_name = "commit";
1554 } else {
1555 method_name = "rollback";
1556 }
1557
1558 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1559 if (!result) {
1560 return NULL;
1561 }
1562 Py_DECREF(result);
1563
1564 Py_INCREF(Py_False);
1565 return Py_False;
1566}
1567
Anthony Baxterc51ee692006-04-01 00:57:31 +00001568static char connection_doc[] =
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001569PyDoc_STR("SQLite database connection object.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001570
1571static PyGetSetDef connection_getset[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001572 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1573 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001574 {NULL}
1575};
1576
1577static PyMethodDef connection_methods[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001578 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001579 PyDoc_STR("Return a cursor for the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001580 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001581 PyDoc_STR("Closes the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001582 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001583 PyDoc_STR("Commit the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001584 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001585 PyDoc_STR("Roll back the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001586 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001587 PyDoc_STR("Creates a new function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001588 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001589 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001590 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001591 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001592 #ifdef HAVE_LOAD_EXTENSION
1593 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1594 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1595 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1596 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1597 #endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001598 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1599 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001600 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001601 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001602 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001603 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001604 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001605 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001606 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001607 PyDoc_STR("Creates a collation function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001608 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001609 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Gregory P. Smithb9803422008-03-28 08:32:09 +00001610 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Gerhard Häringef2276b2008-09-12 13:54:06 +00001611 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001612 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1613 PyDoc_STR("For context manager. Non-standard.")},
1614 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1615 PyDoc_STR("For context manager. Non-standard.")},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001616 {NULL, NULL}
1617};
1618
1619static struct PyMemberDef connection_members[] =
1620{
Gerhard Häring0741a602007-01-14 01:43:50 +00001621 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), RO},
1622 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), RO},
1623 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), RO},
1624 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), RO},
1625 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), RO},
1626 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), RO},
1627 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), RO},
1628 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), RO},
1629 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), RO},
1630 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), RO},
1631 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1632 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001633 {NULL}
1634};
1635
Gerhard Häring0741a602007-01-14 01:43:50 +00001636PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001637 PyVarObject_HEAD_INIT(NULL, 0)
Anthony Baxter8e7b4902006-04-05 18:25:33 +00001638 MODULE_NAME ".Connection", /* tp_name */
Gerhard Häring0741a602007-01-14 01:43:50 +00001639 sizeof(pysqlite_Connection), /* tp_basicsize */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001640 0, /* tp_itemsize */
Gerhard Häring0741a602007-01-14 01:43:50 +00001641 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001642 0, /* tp_print */
1643 0, /* tp_getattr */
1644 0, /* tp_setattr */
1645 0, /* tp_compare */
1646 0, /* tp_repr */
1647 0, /* tp_as_number */
1648 0, /* tp_as_sequence */
1649 0, /* tp_as_mapping */
1650 0, /* tp_hash */
Gerhard Häring0741a602007-01-14 01:43:50 +00001651 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001652 0, /* tp_str */
1653 0, /* tp_getattro */
1654 0, /* tp_setattro */
1655 0, /* tp_as_buffer */
1656 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1657 connection_doc, /* tp_doc */
1658 0, /* tp_traverse */
1659 0, /* tp_clear */
1660 0, /* tp_richcompare */
1661 0, /* tp_weaklistoffset */
1662 0, /* tp_iter */
1663 0, /* tp_iternext */
1664 connection_methods, /* tp_methods */
1665 connection_members, /* tp_members */
1666 connection_getset, /* tp_getset */
1667 0, /* tp_base */
1668 0, /* tp_dict */
1669 0, /* tp_descr_get */
1670 0, /* tp_descr_set */
1671 0, /* tp_dictoffset */
Gerhard Häring0741a602007-01-14 01:43:50 +00001672 (initproc)pysqlite_connection_init, /* tp_init */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001673 0, /* tp_alloc */
1674 0, /* tp_new */
1675 0 /* tp_free */
1676};
1677
Gerhard Häring0741a602007-01-14 01:43:50 +00001678extern int pysqlite_connection_setup_types(void)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001679{
Gerhard Häring0741a602007-01-14 01:43:50 +00001680 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1681 return PyType_Ready(&pysqlite_ConnectionType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001682}