blob: d89b4c1f40b1a87b4acbc8af596920caf879d51f [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;
Gerhard Häring0741a602007-01-14 01:43:50 +0000155 pysqlite_connection_set_isolation_level(self, isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000156 Py_DECREF(isolation_level);
157
Gerhard Häring0741a602007-01-14 01:43:50 +0000158 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000159 if (PyErr_Occurred()) {
160 return -1;
161 }
162
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000163 self->created_statements = 0;
164 self->created_cursors = 0;
165
166 /* Create lists of weak references to statements/cursors */
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000167 self->statements = PyList_New(0);
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000168 self->cursors = PyList_New(0);
169 if (!self->statements || !self->cursors) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000170 return -1;
171 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000172
Anthony Baxterc51ee692006-04-01 00:57:31 +0000173 /* By default, the Cache class INCREFs the factory in its initializer, and
174 * decrefs it in its deallocator method. Since this would create a circular
175 * reference here, we're breaking it by decrementing self, and telling the
176 * cache class to not decref the factory (self) in its deallocator.
177 */
178 self->statement_cache->decref_factory = 0;
179 Py_DECREF(self);
180
181 self->inTransaction = 0;
182 self->detect_types = detect_types;
183 self->timeout = timeout;
184 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandld3eaa742009-04-05 11:07:14 +0000185#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000186 self->thread_ident = PyThread_get_thread_ident();
Georg Brandld3eaa742009-04-05 11:07:14 +0000187#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000188 self->check_same_thread = check_same_thread;
189
190 self->function_pinboard = PyDict_New();
Anthony Baxter72289a62006-04-04 06:29:05 +0000191 if (!self->function_pinboard) {
192 return -1;
193 }
194
195 self->collations = PyDict_New();
196 if (!self->collations) {
197 return -1;
198 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000199
Gerhard Häring0741a602007-01-14 01:43:50 +0000200 self->Warning = pysqlite_Warning;
201 self->Error = pysqlite_Error;
202 self->InterfaceError = pysqlite_InterfaceError;
203 self->DatabaseError = pysqlite_DatabaseError;
204 self->DataError = pysqlite_DataError;
205 self->OperationalError = pysqlite_OperationalError;
206 self->IntegrityError = pysqlite_IntegrityError;
207 self->InternalError = pysqlite_InternalError;
208 self->ProgrammingError = pysqlite_ProgrammingError;
209 self->NotSupportedError = pysqlite_NotSupportedError;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000210
211 return 0;
212}
213
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000214/* Empty the entire statement cache of this connection */
Gerhard Häring0741a602007-01-14 01:43:50 +0000215void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000216{
Gerhard Häring0741a602007-01-14 01:43:50 +0000217 pysqlite_Node* node;
218 pysqlite_Statement* statement;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000219
220 node = self->statement_cache->first;
221
222 while (node) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000223 statement = (pysqlite_Statement*)(node->data);
224 (void)pysqlite_statement_finalize(statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000225 node = node->next;
226 }
227
228 Py_DECREF(self->statement_cache);
Gerhard Häring0741a602007-01-14 01:43:50 +0000229 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000230 Py_DECREF(self);
231 self->statement_cache->decref_factory = 0;
232}
233
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000234/* action in (ACTION_RESET, ACTION_FINALIZE) */
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000235void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000236{
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000237 int i;
238 PyObject* weakref;
239 PyObject* statement;
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000240 pysqlite_Cursor* cursor;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000241
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000242 for (i = 0; i < PyList_Size(self->statements); i++) {
243 weakref = PyList_GetItem(self->statements, i);
244 statement = PyWeakref_GetObject(weakref);
245 if (statement != Py_None) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000246 if (action == ACTION_RESET) {
247 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
248 } else {
249 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
250 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000251 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000252 }
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000253
254 if (reset_cursors) {
255 for (i = 0; i < PyList_Size(self->cursors); i++) {
256 weakref = PyList_GetItem(self->cursors, i);
257 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
258 if ((PyObject*)cursor != Py_None) {
259 cursor->reset = 1;
260 }
261 }
262 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000263}
264
Gerhard Häring0741a602007-01-14 01:43:50 +0000265void pysqlite_connection_dealloc(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000266{
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000267 PyObject* ret = NULL;
268
Anthony Baxterc51ee692006-04-01 00:57:31 +0000269 Py_XDECREF(self->statement_cache);
270
271 /* Clean up if user has not called .close() explicitly. */
272 if (self->db) {
273 Py_BEGIN_ALLOW_THREADS
274 sqlite3_close(self->db);
275 Py_END_ALLOW_THREADS
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000276 } else if (self->apsw_connection) {
277 ret = PyObject_CallMethod(self->apsw_connection, "close", "");
278 Py_XDECREF(ret);
279 Py_XDECREF(self->apsw_connection);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000280 }
281
282 if (self->begin_statement) {
283 PyMem_Free(self->begin_statement);
284 }
285 Py_XDECREF(self->isolation_level);
286 Py_XDECREF(self->function_pinboard);
287 Py_XDECREF(self->row_factory);
288 Py_XDECREF(self->text_factory);
Anthony Baxter72289a62006-04-04 06:29:05 +0000289 Py_XDECREF(self->collations);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000290 Py_XDECREF(self->statements);
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000291 Py_XDECREF(self->cursors);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000292
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000293 self->ob_type->tp_free((PyObject*)self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000294}
295
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000296/*
297 * Registers a cursor with the connection.
298 *
299 * 0 => error; 1 => ok
300 */
301int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
302{
303 PyObject* weakref;
304
305 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
306 if (!weakref) {
307 goto error;
308 }
309
310 if (PyList_Append(connection->cursors, weakref) != 0) {
311 Py_CLEAR(weakref);
312 goto error;
313 }
314
315 Py_DECREF(weakref);
316
317 return 1;
318error:
319 return 0;
320}
321
Gerhard Häring0741a602007-01-14 01:43:50 +0000322PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000323{
324 static char *kwlist[] = {"factory", NULL, NULL};
325 PyObject* factory = NULL;
326 PyObject* cursor;
327
Anthony Baxterc51ee692006-04-01 00:57:31 +0000328 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
329 &factory)) {
330 return NULL;
331 }
332
Gerhard Häring0741a602007-01-14 01:43:50 +0000333 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000334 return NULL;
335 }
336
337 if (factory == NULL) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000338 factory = (PyObject*)&pysqlite_CursorType;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000339 }
340
341 cursor = PyObject_CallFunction(factory, "O", self);
342
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000343 _pysqlite_drop_unused_cursor_references(self);
344
Anthony Baxterc51ee692006-04-01 00:57:31 +0000345 if (cursor && self->row_factory != Py_None) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000346 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000347 Py_INCREF(self->row_factory);
Gerhard Häring0741a602007-01-14 01:43:50 +0000348 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000349 }
350
351 return cursor;
352}
353
Gerhard Häring0741a602007-01-14 01:43:50 +0000354PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000355{
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000356 PyObject* ret;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000357 int rc;
358
Gerhard Häring0741a602007-01-14 01:43:50 +0000359 if (!pysqlite_check_thread(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000360 return NULL;
361 }
362
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000363 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000364
365 if (self->db) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000366 if (self->apsw_connection) {
367 ret = PyObject_CallMethod(self->apsw_connection, "close", "");
368 Py_XDECREF(ret);
369 Py_XDECREF(self->apsw_connection);
370 self->apsw_connection = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000371 self->db = NULL;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000372 } else {
373 Py_BEGIN_ALLOW_THREADS
374 rc = sqlite3_close(self->db);
375 Py_END_ALLOW_THREADS
376
377 if (rc != SQLITE_OK) {
378 _pysqlite_seterror(self->db, NULL);
379 return NULL;
380 } else {
381 self->db = NULL;
382 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000383 }
384 }
385
386 Py_INCREF(Py_None);
387 return Py_None;
388}
389
390/*
391 * Checks if a connection object is usable (i. e. not closed).
392 *
393 * 0 => error; 1 => ok
394 */
Gerhard Häring0741a602007-01-14 01:43:50 +0000395int pysqlite_check_connection(pysqlite_Connection* con)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000396{
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000397 if (!con->initialized) {
398 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
399 return 0;
400 }
401
Anthony Baxterc51ee692006-04-01 00:57:31 +0000402 if (!con->db) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000403 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000404 return 0;
405 } else {
406 return 1;
407 }
408}
409
Gerhard Häring0741a602007-01-14 01:43:50 +0000410PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000411{
412 int rc;
413 const char* tail;
414 sqlite3_stmt* statement;
415
416 Py_BEGIN_ALLOW_THREADS
417 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
418 Py_END_ALLOW_THREADS
419
420 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000421 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000422 goto error;
423 }
424
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000425 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000426 if (rc == SQLITE_DONE) {
427 self->inTransaction = 1;
428 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000429 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000430 }
431
432 Py_BEGIN_ALLOW_THREADS
433 rc = sqlite3_finalize(statement);
434 Py_END_ALLOW_THREADS
435
436 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000437 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000438 }
439
440error:
441 if (PyErr_Occurred()) {
442 return NULL;
443 } else {
444 Py_INCREF(Py_None);
445 return Py_None;
446 }
447}
448
Gerhard Häring0741a602007-01-14 01:43:50 +0000449PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000450{
451 int rc;
452 const char* tail;
453 sqlite3_stmt* statement;
454
Gerhard Häring0741a602007-01-14 01:43:50 +0000455 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000456 return NULL;
457 }
458
459 if (self->inTransaction) {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000460 pysqlite_do_all_statements(self, ACTION_RESET, 0);
461
Anthony Baxterc51ee692006-04-01 00:57:31 +0000462 Py_BEGIN_ALLOW_THREADS
463 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
464 Py_END_ALLOW_THREADS
465 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000466 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000467 goto error;
468 }
469
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000470 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000471 if (rc == SQLITE_DONE) {
472 self->inTransaction = 0;
473 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000474 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000475 }
476
477 Py_BEGIN_ALLOW_THREADS
478 rc = sqlite3_finalize(statement);
479 Py_END_ALLOW_THREADS
480 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000481 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000482 }
483
484 }
485
486error:
487 if (PyErr_Occurred()) {
488 return NULL;
489 } else {
490 Py_INCREF(Py_None);
491 return Py_None;
492 }
493}
494
Gerhard Häring0741a602007-01-14 01:43:50 +0000495PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000496{
497 int rc;
498 const char* tail;
499 sqlite3_stmt* statement;
500
Gerhard Häring0741a602007-01-14 01:43:50 +0000501 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000502 return NULL;
503 }
504
505 if (self->inTransaction) {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000506 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000507
508 Py_BEGIN_ALLOW_THREADS
509 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
510 Py_END_ALLOW_THREADS
511 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000512 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000513 goto error;
514 }
515
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000516 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000517 if (rc == SQLITE_DONE) {
518 self->inTransaction = 0;
519 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000520 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000521 }
522
523 Py_BEGIN_ALLOW_THREADS
524 rc = sqlite3_finalize(statement);
525 Py_END_ALLOW_THREADS
526 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000527 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000528 }
529
530 }
531
532error:
533 if (PyErr_Occurred()) {
534 return NULL;
535 } else {
536 Py_INCREF(Py_None);
537 return Py_None;
538 }
539}
540
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200541static int
542_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000543{
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200544 if (py_val == Py_None) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000545 sqlite3_result_null(context);
546 } else if (PyInt_Check(py_val)) {
Ned Deily77e77a12012-05-19 23:35:05 -0700547 sqlite3_result_int64(context, (sqlite_int64)PyInt_AsLong(py_val));
Petri Lehtinen4ab701b2012-02-21 13:58:40 +0200548 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200549 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
550 if (value == -1 && PyErr_Occurred())
551 return -1;
552 sqlite3_result_int64(context, value);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000553 } else if (PyFloat_Check(py_val)) {
554 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
555 } else if (PyBuffer_Check(py_val)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200556 const char* buffer;
557 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000558 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
559 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200560 return -1;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000561 }
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200562 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000563 } else if (PyString_Check(py_val)) {
564 sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000565 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200566 PyObject * stringval = PyUnicode_AsUTF8String(py_val);
567 if (!stringval)
568 return -1;
569 sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
570 Py_DECREF(stringval);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000571 } else {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200572 return -1;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000573 }
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200574 return 0;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000575}
576
Gerhard Häring0741a602007-01-14 01:43:50 +0000577PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000578{
579 PyObject* args;
580 int i;
581 sqlite3_value* cur_value;
582 PyObject* cur_py_value;
583 const char* val_str;
Neal Norwitz95f0e4c2006-04-01 09:08:06 +0000584 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000585 void* raw_buffer;
586
587 args = PyTuple_New(argc);
Anthony Baxter72289a62006-04-04 06:29:05 +0000588 if (!args) {
589 return NULL;
590 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000591
592 for (i = 0; i < argc; i++) {
593 cur_value = argv[i];
594 switch (sqlite3_value_type(argv[i])) {
595 case SQLITE_INTEGER:
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200596 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Anthony Baxterc51ee692006-04-01 00:57:31 +0000597 break;
598 case SQLITE_FLOAT:
599 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
600 break;
601 case SQLITE_TEXT:
602 val_str = (const char*)sqlite3_value_text(cur_value);
603 cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
604 /* TODO: have a way to show errors here */
605 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000606 PyErr_Clear();
Anthony Baxterc51ee692006-04-01 00:57:31 +0000607 Py_INCREF(Py_None);
608 cur_py_value = Py_None;
609 }
610 break;
611 case SQLITE_BLOB:
612 buflen = sqlite3_value_bytes(cur_value);
613 cur_py_value = PyBuffer_New(buflen);
614 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000615 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000616 }
617 if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000618 Py_DECREF(cur_py_value);
619 cur_py_value = NULL;
620 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000621 }
622 memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen);
623 break;
624 case SQLITE_NULL:
625 default:
626 Py_INCREF(Py_None);
627 cur_py_value = Py_None;
628 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000629
630 if (!cur_py_value) {
631 Py_DECREF(args);
632 return NULL;
633 }
634
Anthony Baxterc51ee692006-04-01 00:57:31 +0000635 PyTuple_SetItem(args, i, cur_py_value);
636
637 }
638
639 return args;
640}
641
Gerhard Häring0741a602007-01-14 01:43:50 +0000642void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000643{
644 PyObject* args;
645 PyObject* py_func;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000646 PyObject* py_retval = NULL;
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200647 int ok;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000648
Georg Brandld3eaa742009-04-05 11:07:14 +0000649#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000650 PyGILState_STATE threadstate;
651
652 threadstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000653#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000654
655 py_func = (PyObject*)sqlite3_user_data(context);
656
Gerhard Häring0741a602007-01-14 01:43:50 +0000657 args = _pysqlite_build_py_params(context, argc, argv);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000658 if (args) {
659 py_retval = PyObject_CallObject(py_func, args);
660 Py_DECREF(args);
661 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000662
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200663 ok = 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000664 if (py_retval) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200665 ok = _pysqlite_set_result(context, py_retval) == 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000666 Py_DECREF(py_retval);
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200667 }
668 if (!ok) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000669 if (_enable_callback_tracebacks) {
670 PyErr_Print();
671 } else {
672 PyErr_Clear();
673 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000674 _sqlite3_result_error(context, "user-defined function raised exception", -1);
Gerhard Häring1541ef02006-06-13 22:24:47 +0000675 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000676
Georg Brandld3eaa742009-04-05 11:07:14 +0000677#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000678 PyGILState_Release(threadstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000679#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000680}
681
Gerhard Häring0741a602007-01-14 01:43:50 +0000682static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000683{
684 PyObject* args;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000685 PyObject* function_result = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000686 PyObject* aggregate_class;
687 PyObject** aggregate_instance;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000688 PyObject* stepmethod = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000689
Georg Brandld3eaa742009-04-05 11:07:14 +0000690#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000691 PyGILState_STATE threadstate;
692
693 threadstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000694#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000695
696 aggregate_class = (PyObject*)sqlite3_user_data(context);
697
698 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
699
700 if (*aggregate_instance == 0) {
701 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
702
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000703 if (PyErr_Occurred()) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000704 *aggregate_instance = 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000705 if (_enable_callback_tracebacks) {
706 PyErr_Print();
707 } else {
708 PyErr_Clear();
709 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000710 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000711 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000712 }
713 }
714
715 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000716 if (!stepmethod) {
717 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000718 }
719
Gerhard Häring0741a602007-01-14 01:43:50 +0000720 args = _pysqlite_build_py_params(context, argc, params);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000721 if (!args) {
722 goto error;
723 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000724
725 function_result = PyObject_CallObject(stepmethod, args);
726 Py_DECREF(args);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000727
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000728 if (!function_result) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000729 if (_enable_callback_tracebacks) {
730 PyErr_Print();
731 } else {
732 PyErr_Clear();
733 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000734 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000735 }
736
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000737error:
738 Py_XDECREF(stepmethod);
739 Py_XDECREF(function_result);
740
Georg Brandld3eaa742009-04-05 11:07:14 +0000741#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000742 PyGILState_Release(threadstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000743#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000744}
745
Gerhard Häring0741a602007-01-14 01:43:50 +0000746void _pysqlite_final_callback(sqlite3_context* context)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000747{
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200748 PyObject* function_result;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000749 PyObject** aggregate_instance;
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200750 int ok;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000751
Georg Brandld3eaa742009-04-05 11:07:14 +0000752#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000753 PyGILState_STATE threadstate;
754
755 threadstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000756#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000757
Anthony Baxterc51ee692006-04-01 00:57:31 +0000758 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
759 if (!*aggregate_instance) {
760 /* this branch is executed if there was an exception in the aggregate's
761 * __init__ */
762
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000763 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000764 }
765
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000766 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200767 Py_DECREF(*aggregate_instance);
768
769 ok = 0;
770 if (function_result) {
771 ok = _pysqlite_set_result(context, function_result) == 0;
772 Py_DECREF(function_result);
773 }
774 if (!ok) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000775 if (_enable_callback_tracebacks) {
776 PyErr_Print();
777 } else {
778 PyErr_Clear();
779 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000780 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000781 }
782
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000783error:
Georg Brandld3eaa742009-04-05 11:07:14 +0000784#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000785 PyGILState_Release(threadstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000786#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000787}
788
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000789static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000790{
791 PyObject* new_list;
792 PyObject* weakref;
793 int i;
794
795 /* we only need to do this once in a while */
796 if (self->created_statements++ < 200) {
797 return;
798 }
799
800 self->created_statements = 0;
801
802 new_list = PyList_New(0);
803 if (!new_list) {
804 return;
805 }
806
807 for (i = 0; i < PyList_Size(self->statements); i++) {
808 weakref = PyList_GetItem(self->statements, i);
Gerhard Häringecd20102006-06-19 21:17:35 +0000809 if (PyWeakref_GetObject(weakref) != Py_None) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000810 if (PyList_Append(new_list, weakref) != 0) {
811 Py_DECREF(new_list);
812 return;
813 }
814 }
815 }
816
817 Py_DECREF(self->statements);
818 self->statements = new_list;
819}
Anthony Baxterc51ee692006-04-01 00:57:31 +0000820
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000821static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
822{
823 PyObject* new_list;
824 PyObject* weakref;
825 int i;
826
827 /* we only need to do this once in a while */
828 if (self->created_cursors++ < 200) {
829 return;
830 }
831
832 self->created_cursors = 0;
833
834 new_list = PyList_New(0);
835 if (!new_list) {
836 return;
837 }
838
839 for (i = 0; i < PyList_Size(self->cursors); i++) {
840 weakref = PyList_GetItem(self->cursors, i);
841 if (PyWeakref_GetObject(weakref) != Py_None) {
842 if (PyList_Append(new_list, weakref) != 0) {
843 Py_DECREF(new_list);
844 return;
845 }
846 }
847 }
848
849 Py_DECREF(self->cursors);
850 self->cursors = new_list;
851}
852
Gerhard Häring0741a602007-01-14 01:43:50 +0000853PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000854{
855 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
856
857 PyObject* func;
858 char* name;
859 int narg;
860 int rc;
861
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000862 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
863 return NULL;
864 }
865
Anthony Baxterc51ee692006-04-01 00:57:31 +0000866 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
867 &name, &narg, &func))
868 {
869 return NULL;
870 }
871
Gerhard Häring0741a602007-01-14 01:43:50 +0000872 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000873
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000874 if (rc != SQLITE_OK) {
875 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000876 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000877 return NULL;
878 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000879 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
880 return NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000881
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000882 Py_INCREF(Py_None);
883 return Py_None;
884 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000885}
886
Gerhard Häring0741a602007-01-14 01:43:50 +0000887PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000888{
889 PyObject* aggregate_class;
890
891 int n_arg;
892 char* name;
893 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
894 int rc;
895
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000896 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
897 return NULL;
898 }
899
Anthony Baxterc51ee692006-04-01 00:57:31 +0000900 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
901 kwlist, &name, &n_arg, &aggregate_class)) {
902 return NULL;
903 }
904
Gerhard Häring0741a602007-01-14 01:43:50 +0000905 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 +0000906 if (rc != SQLITE_OK) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000907 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000908 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000909 return NULL;
910 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000911 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
912 return NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000913
914 Py_INCREF(Py_None);
915 return Py_None;
916 }
917}
918
Gerhard Häring0741a602007-01-14 01:43:50 +0000919static 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 +0000920{
921 PyObject *ret;
922 int rc;
Georg Brandld3eaa742009-04-05 11:07:14 +0000923#ifdef WITH_THREAD
Gerhard Häring1541ef02006-06-13 22:24:47 +0000924 PyGILState_STATE gilstate;
925
926 gilstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000927#endif
Gerhard Häring1541ef02006-06-13 22:24:47 +0000928 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
929
930 if (!ret) {
931 if (_enable_callback_tracebacks) {
932 PyErr_Print();
933 } else {
934 PyErr_Clear();
935 }
936
937 rc = SQLITE_DENY;
938 } else {
939 if (PyInt_Check(ret)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200940 rc = _PyInt_AsInt(ret);
941 if (rc == -1 && PyErr_Occurred())
942 rc = SQLITE_DENY;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000943 } else {
944 rc = SQLITE_DENY;
945 }
946 Py_DECREF(ret);
947 }
948
Georg Brandld3eaa742009-04-05 11:07:14 +0000949#ifdef WITH_THREAD
Gerhard Häring1541ef02006-06-13 22:24:47 +0000950 PyGILState_Release(gilstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000951#endif
Gerhard Häring1541ef02006-06-13 22:24:47 +0000952 return rc;
953}
954
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000955static int _progress_handler(void* user_arg)
956{
957 int rc;
958 PyObject *ret;
Georg Brandld3eaa742009-04-05 11:07:14 +0000959#ifdef WITH_THREAD
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000960 PyGILState_STATE gilstate;
961
962 gilstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000963#endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000964 ret = PyObject_CallFunction((PyObject*)user_arg, "");
965
966 if (!ret) {
967 if (_enable_callback_tracebacks) {
968 PyErr_Print();
969 } else {
970 PyErr_Clear();
971 }
972
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000973 /* abort query if error occurred */
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200974 rc = 1;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000975 } else {
976 rc = (int)PyObject_IsTrue(ret);
Neal Norwitzca752f32008-03-03 04:37:45 +0000977 Py_DECREF(ret);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000978 }
979
Georg Brandld3eaa742009-04-05 11:07:14 +0000980#ifdef WITH_THREAD
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000981 PyGILState_Release(gilstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000982#endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000983 return rc;
984}
985
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000986static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häring1541ef02006-06-13 22:24:47 +0000987{
988 PyObject* authorizer_cb;
989
990 static char *kwlist[] = { "authorizer_callback", NULL };
991 int rc;
992
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000993 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
994 return NULL;
995 }
996
Gerhard Häring1541ef02006-06-13 22:24:47 +0000997 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
998 kwlist, &authorizer_cb)) {
999 return NULL;
1000 }
1001
1002 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1003
1004 if (rc != SQLITE_OK) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001005 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Gerhard Häring1541ef02006-06-13 22:24:47 +00001006 return NULL;
1007 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001008 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1009 return NULL;
Gerhard Häring1541ef02006-06-13 22:24:47 +00001010
1011 Py_INCREF(Py_None);
1012 return Py_None;
1013 }
1014}
1015
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001016static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001017{
1018 PyObject* progress_handler;
1019 int n;
1020
1021 static char *kwlist[] = { "progress_handler", "n", NULL };
1022
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001023 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1024 return NULL;
1025 }
1026
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001027 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1028 kwlist, &progress_handler, &n)) {
1029 return NULL;
1030 }
1031
1032 if (progress_handler == Py_None) {
1033 /* None clears the progress handler previously set */
1034 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1035 } else {
1036 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001037 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1038 return NULL;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001039 }
1040
1041 Py_INCREF(Py_None);
1042 return Py_None;
1043}
1044
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001045#ifdef HAVE_LOAD_EXTENSION
1046static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1047{
1048 int rc;
1049 int onoff;
1050
1051 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1052 return NULL;
1053 }
1054
1055 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1056 return NULL;
1057 }
1058
1059 rc = sqlite3_enable_load_extension(self->db, onoff);
1060
1061 if (rc != SQLITE_OK) {
1062 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1063 return NULL;
1064 } else {
1065 Py_INCREF(Py_None);
1066 return Py_None;
1067 }
1068}
1069
1070static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1071{
1072 int rc;
1073 char* extension_name;
1074 char* errmsg;
1075
1076 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1077 return NULL;
1078 }
1079
1080 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1081 return NULL;
1082 }
1083
1084 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1085 if (rc != 0) {
1086 PyErr_SetString(pysqlite_OperationalError, errmsg);
1087 return NULL;
1088 } else {
1089 Py_INCREF(Py_None);
1090 return Py_None;
1091 }
1092}
1093#endif
1094
Gerhard Häring0741a602007-01-14 01:43:50 +00001095int pysqlite_check_thread(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001096{
Georg Brandld3eaa742009-04-05 11:07:14 +00001097#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +00001098 if (self->check_same_thread) {
1099 if (PyThread_get_thread_ident() != self->thread_ident) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001100 PyErr_Format(pysqlite_ProgrammingError,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001101 "SQLite objects created in a thread can only be used in that same thread."
1102 "The object was created in thread id %ld and this is thread id %ld",
1103 self->thread_ident, PyThread_get_thread_ident());
1104 return 0;
1105 }
1106
1107 }
Georg Brandld3eaa742009-04-05 11:07:14 +00001108#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +00001109 return 1;
1110}
1111
Gerhard Häring0741a602007-01-14 01:43:50 +00001112static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001113{
1114 Py_INCREF(self->isolation_level);
1115 return self->isolation_level;
1116}
1117
Gerhard Häring0741a602007-01-14 01:43:50 +00001118static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Anthony Baxter72289a62006-04-04 06:29:05 +00001119{
Gerhard Häring0741a602007-01-14 01:43:50 +00001120 if (!pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001121 return NULL;
1122 } else {
1123 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1124 }
1125}
1126
Gerhard Häring0741a602007-01-14 01:43:50 +00001127static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001128{
Anthony Baxterc51ee692006-04-01 00:57:31 +00001129 PyObject* res;
1130 PyObject* begin_statement;
Georg Brandla24869a2008-07-16 22:33:18 +00001131 char* begin_statement_str;
Anthony Baxterc51ee692006-04-01 00:57:31 +00001132
1133 Py_XDECREF(self->isolation_level);
1134
Neal Norwitz5b030652006-04-16 03:28:17 +00001135 if (self->begin_statement) {
1136 PyMem_Free(self->begin_statement);
1137 self->begin_statement = NULL;
1138 }
1139
Anthony Baxterc51ee692006-04-01 00:57:31 +00001140 if (isolation_level == Py_None) {
1141 Py_INCREF(Py_None);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001142 self->isolation_level = Py_None;
1143
Gerhard Häring0741a602007-01-14 01:43:50 +00001144 res = pysqlite_connection_commit(self, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001145 if (!res) {
1146 return -1;
1147 }
Anthony Baxterc51ee692006-04-01 00:57:31 +00001148 Py_DECREF(res);
1149
1150 self->inTransaction = 0;
1151 } else {
1152 Py_INCREF(isolation_level);
1153 self->isolation_level = isolation_level;
1154
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001155 begin_statement = PyString_FromString("BEGIN ");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001156 if (!begin_statement) {
1157 return -1;
1158 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001159 PyString_Concat(&begin_statement, isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001160 if (!begin_statement) {
1161 return -1;
1162 }
1163
Georg Brandla24869a2008-07-16 22:33:18 +00001164 begin_statement_str = PyString_AsString(begin_statement);
1165 if (!begin_statement_str) {
1166 Py_DECREF(begin_statement);
1167 return -1;
1168 }
1169 self->begin_statement = PyMem_Malloc(strlen(begin_statement_str) + 2);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001170 if (!self->begin_statement) {
Georg Brandla24869a2008-07-16 22:33:18 +00001171 Py_DECREF(begin_statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001172 return -1;
1173 }
1174
Georg Brandla24869a2008-07-16 22:33:18 +00001175 strcpy(self->begin_statement, begin_statement_str);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001176 Py_DECREF(begin_statement);
1177 }
1178
1179 return 0;
1180}
1181
Gerhard Häring0741a602007-01-14 01:43:50 +00001182PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001183{
1184 PyObject* sql;
Gerhard Häring0741a602007-01-14 01:43:50 +00001185 pysqlite_Statement* statement;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001186 PyObject* weakref;
Anthony Baxterc51ee692006-04-01 00:57:31 +00001187 int rc;
1188
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001189 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1190 return NULL;
1191 }
1192
Anthony Baxterc51ee692006-04-01 00:57:31 +00001193 if (!PyArg_ParseTuple(args, "O", &sql)) {
1194 return NULL;
1195 }
1196
Gerhard Häring0741a602007-01-14 01:43:50 +00001197 _pysqlite_drop_unused_statement_references(self);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001198
Gerhard Häring0741a602007-01-14 01:43:50 +00001199 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001200 if (!statement) {
1201 return NULL;
1202 }
1203
Victor Stinner6e055d72010-03-13 03:27:07 +00001204 statement->db = NULL;
1205 statement->st = NULL;
1206 statement->sql = NULL;
1207 statement->in_use = 0;
1208 statement->in_weakreflist = NULL;
1209
Gerhard Häring0741a602007-01-14 01:43:50 +00001210 rc = pysqlite_statement_create(statement, self, sql);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001211
1212 if (rc != SQLITE_OK) {
1213 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001214 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001215 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001216 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001217 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001218 (void)pysqlite_statement_reset(statement);
1219 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001220 }
1221
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001222 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001223 } else {
1224 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1225 if (!weakref) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001226 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001227 goto error;
1228 }
1229
1230 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001231 Py_CLEAR(weakref);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001232 goto error;
1233 }
1234
1235 Py_DECREF(weakref);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001236 }
1237
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001238error:
Anthony Baxterc51ee692006-04-01 00:57:31 +00001239 return (PyObject*)statement;
1240}
1241
Gerhard Häring0741a602007-01-14 01:43:50 +00001242PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001243{
1244 PyObject* cursor = 0;
1245 PyObject* result = 0;
1246 PyObject* method = 0;
1247
1248 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1249 if (!cursor) {
1250 goto error;
1251 }
1252
1253 method = PyObject_GetAttrString(cursor, "execute");
1254 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001255 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001256 goto error;
1257 }
1258
1259 result = PyObject_CallObject(method, args);
1260 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001261 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001262 }
1263
1264error:
1265 Py_XDECREF(result);
1266 Py_XDECREF(method);
1267
1268 return cursor;
1269}
1270
Gerhard Häring0741a602007-01-14 01:43:50 +00001271PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001272{
1273 PyObject* cursor = 0;
1274 PyObject* result = 0;
1275 PyObject* method = 0;
1276
1277 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1278 if (!cursor) {
1279 goto error;
1280 }
1281
1282 method = PyObject_GetAttrString(cursor, "executemany");
1283 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001284 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001285 goto error;
1286 }
1287
1288 result = PyObject_CallObject(method, args);
1289 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001290 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001291 }
1292
1293error:
1294 Py_XDECREF(result);
1295 Py_XDECREF(method);
1296
1297 return cursor;
1298}
1299
Gerhard Häring0741a602007-01-14 01:43:50 +00001300PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001301{
1302 PyObject* cursor = 0;
1303 PyObject* result = 0;
1304 PyObject* method = 0;
1305
1306 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1307 if (!cursor) {
1308 goto error;
1309 }
1310
1311 method = PyObject_GetAttrString(cursor, "executescript");
1312 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001313 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001314 goto error;
1315 }
1316
1317 result = PyObject_CallObject(method, args);
1318 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001319 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001320 }
1321
1322error:
1323 Py_XDECREF(result);
1324 Py_XDECREF(method);
1325
1326 return cursor;
1327}
1328
Anthony Baxter72289a62006-04-04 06:29:05 +00001329/* ------------------------- COLLATION CODE ------------------------ */
1330
1331static int
Gerhard Häring0741a602007-01-14 01:43:50 +00001332pysqlite_collation_callback(
Anthony Baxter72289a62006-04-04 06:29:05 +00001333 void* context,
1334 int text1_length, const void* text1_data,
1335 int text2_length, const void* text2_data)
1336{
1337 PyObject* callback = (PyObject*)context;
1338 PyObject* string1 = 0;
1339 PyObject* string2 = 0;
Georg Brandld3eaa742009-04-05 11:07:14 +00001340#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001341 PyGILState_STATE gilstate;
Georg Brandld3eaa742009-04-05 11:07:14 +00001342#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001343 PyObject* retval = NULL;
Serhiy Storchaka35c52b62013-02-07 16:59:34 +02001344 long longval;
Anthony Baxter72289a62006-04-04 06:29:05 +00001345 int result = 0;
Georg Brandld3eaa742009-04-05 11:07:14 +00001346#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001347 gilstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +00001348#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001349
1350 if (PyErr_Occurred()) {
1351 goto finally;
1352 }
1353
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001354 string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length);
1355 string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length);
Anthony Baxter72289a62006-04-04 06:29:05 +00001356
1357 if (!string1 || !string2) {
1358 goto finally; /* failed to allocate strings */
1359 }
1360
1361 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1362
1363 if (!retval) {
1364 /* execution failed */
1365 goto finally;
1366 }
1367
Serhiy Storchaka35c52b62013-02-07 16:59:34 +02001368 longval = PyLong_AsLongAndOverflow(retval, &result);
1369 if (longval == -1 && PyErr_Occurred()) {
1370 PyErr_Clear();
Anthony Baxter72289a62006-04-04 06:29:05 +00001371 result = 0;
1372 }
Serhiy Storchaka35c52b62013-02-07 16:59:34 +02001373 else if (!result) {
1374 if (longval > 0)
1375 result = 1;
1376 else if (longval < 0)
1377 result = -1;
1378 }
Anthony Baxter72289a62006-04-04 06:29:05 +00001379
1380finally:
1381 Py_XDECREF(string1);
1382 Py_XDECREF(string2);
1383 Py_XDECREF(retval);
Georg Brandld3eaa742009-04-05 11:07:14 +00001384#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001385 PyGILState_Release(gilstate);
Georg Brandld3eaa742009-04-05 11:07:14 +00001386#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001387 return result;
1388}
1389
1390static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001391pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Gerhard Häring1541ef02006-06-13 22:24:47 +00001392{
1393 PyObject* retval = NULL;
1394
Gerhard Häring0741a602007-01-14 01:43:50 +00001395 if (!pysqlite_check_connection(self)) {
Gerhard Häring1541ef02006-06-13 22:24:47 +00001396 goto finally;
1397 }
1398
1399 sqlite3_interrupt(self->db);
1400
1401 Py_INCREF(Py_None);
1402 retval = Py_None;
1403
1404finally:
1405 return retval;
1406}
1407
Gregory P. Smithb9803422008-03-28 08:32:09 +00001408/* Function author: Paul Kippes <kippesp@gmail.com>
1409 * Class method of Connection to call the Python function _iterdump
1410 * of the sqlite3 module.
1411 */
1412static PyObject *
1413pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1414{
1415 PyObject* retval = NULL;
1416 PyObject* module = NULL;
1417 PyObject* module_dict;
1418 PyObject* pyfn_iterdump;
1419
1420 if (!pysqlite_check_connection(self)) {
1421 goto finally;
1422 }
1423
1424 module = PyImport_ImportModule(MODULE_NAME ".dump");
1425 if (!module) {
1426 goto finally;
1427 }
1428
1429 module_dict = PyModule_GetDict(module);
1430 if (!module_dict) {
1431 goto finally;
1432 }
1433
1434 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1435 if (!pyfn_iterdump) {
1436 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1437 goto finally;
1438 }
1439
1440 args = PyTuple_New(1);
1441 if (!args) {
1442 goto finally;
1443 }
1444 Py_INCREF(self);
1445 PyTuple_SetItem(args, 0, (PyObject*)self);
1446 retval = PyObject_CallObject(pyfn_iterdump, args);
1447
1448finally:
1449 Py_XDECREF(args);
1450 Py_XDECREF(module);
1451 return retval;
1452}
1453
Gerhard Häring1541ef02006-06-13 22:24:47 +00001454static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001455pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Anthony Baxter72289a62006-04-04 06:29:05 +00001456{
1457 PyObject* callable;
1458 PyObject* uppercase_name = 0;
1459 PyObject* name;
1460 PyObject* retval;
1461 char* chk;
1462 int rc;
1463
Gerhard Häring0741a602007-01-14 01:43:50 +00001464 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001465 goto finally;
1466 }
1467
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001468 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyString_Type, &name, &callable)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001469 goto finally;
1470 }
1471
1472 uppercase_name = PyObject_CallMethod(name, "upper", "");
1473 if (!uppercase_name) {
1474 goto finally;
1475 }
1476
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001477 chk = PyString_AsString(uppercase_name);
Anthony Baxter72289a62006-04-04 06:29:05 +00001478 while (*chk) {
1479 if ((*chk >= '0' && *chk <= '9')
1480 || (*chk >= 'A' && *chk <= 'Z')
1481 || (*chk == '_'))
1482 {
1483 chk++;
1484 } else {
Gerhard Häring0741a602007-01-14 01:43:50 +00001485 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Anthony Baxter72289a62006-04-04 06:29:05 +00001486 goto finally;
1487 }
1488 }
1489
1490 if (callable != Py_None && !PyCallable_Check(callable)) {
1491 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1492 goto finally;
1493 }
1494
1495 if (callable != Py_None) {
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001496 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1497 goto finally;
Anthony Baxter72289a62006-04-04 06:29:05 +00001498 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001499 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1500 goto finally;
Anthony Baxter72289a62006-04-04 06:29:05 +00001501 }
1502
1503 rc = sqlite3_create_collation(self->db,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001504 PyString_AsString(uppercase_name),
Anthony Baxter72289a62006-04-04 06:29:05 +00001505 SQLITE_UTF8,
1506 (callable != Py_None) ? callable : NULL,
Gerhard Häring0741a602007-01-14 01:43:50 +00001507 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001508 if (rc != SQLITE_OK) {
1509 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001510 _pysqlite_seterror(self->db, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001511 goto finally;
1512 }
1513
1514finally:
1515 Py_XDECREF(uppercase_name);
1516
1517 if (PyErr_Occurred()) {
1518 retval = NULL;
1519 } else {
1520 Py_INCREF(Py_None);
1521 retval = Py_None;
1522 }
1523
1524 return retval;
1525}
1526
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001527/* Called when the connection is used as a context manager. Returns itself as a
1528 * convenience to the caller. */
1529static PyObject *
1530pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1531{
1532 Py_INCREF(self);
1533 return (PyObject*)self;
1534}
1535
1536/** Called when the connection is used as a context manager. If there was any
1537 * exception, a rollback takes place; otherwise we commit. */
1538static PyObject *
1539pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1540{
1541 PyObject* exc_type, *exc_value, *exc_tb;
1542 char* method_name;
1543 PyObject* result;
1544
1545 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1546 return NULL;
1547 }
1548
1549 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1550 method_name = "commit";
1551 } else {
1552 method_name = "rollback";
1553 }
1554
1555 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1556 if (!result) {
1557 return NULL;
1558 }
1559 Py_DECREF(result);
1560
1561 Py_INCREF(Py_False);
1562 return Py_False;
1563}
1564
Anthony Baxterc51ee692006-04-01 00:57:31 +00001565static char connection_doc[] =
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001566PyDoc_STR("SQLite database connection object.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001567
1568static PyGetSetDef connection_getset[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001569 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1570 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001571 {NULL}
1572};
1573
1574static PyMethodDef connection_methods[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001575 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001576 PyDoc_STR("Return a cursor for the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001577 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001578 PyDoc_STR("Closes the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001579 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001580 PyDoc_STR("Commit the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001581 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001582 PyDoc_STR("Roll back the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001583 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001584 PyDoc_STR("Creates a new function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001585 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001586 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001587 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001588 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001589 #ifdef HAVE_LOAD_EXTENSION
1590 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1591 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1592 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1593 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1594 #endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001595 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1596 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001597 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001598 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001599 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001600 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001601 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001602 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001603 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001604 PyDoc_STR("Creates a collation function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001605 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001606 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Gregory P. Smithb9803422008-03-28 08:32:09 +00001607 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Gerhard Häringef2276b2008-09-12 13:54:06 +00001608 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 +00001609 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1610 PyDoc_STR("For context manager. Non-standard.")},
1611 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1612 PyDoc_STR("For context manager. Non-standard.")},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001613 {NULL, NULL}
1614};
1615
1616static struct PyMemberDef connection_members[] =
1617{
Gerhard Häring0741a602007-01-14 01:43:50 +00001618 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), RO},
1619 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), RO},
1620 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), RO},
1621 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), RO},
1622 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), RO},
1623 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), RO},
1624 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), RO},
1625 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), RO},
1626 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), RO},
1627 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), RO},
1628 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1629 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001630 {NULL}
1631};
1632
Gerhard Häring0741a602007-01-14 01:43:50 +00001633PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001634 PyVarObject_HEAD_INIT(NULL, 0)
Anthony Baxter8e7b4902006-04-05 18:25:33 +00001635 MODULE_NAME ".Connection", /* tp_name */
Gerhard Häring0741a602007-01-14 01:43:50 +00001636 sizeof(pysqlite_Connection), /* tp_basicsize */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001637 0, /* tp_itemsize */
Gerhard Häring0741a602007-01-14 01:43:50 +00001638 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001639 0, /* tp_print */
1640 0, /* tp_getattr */
1641 0, /* tp_setattr */
1642 0, /* tp_compare */
1643 0, /* tp_repr */
1644 0, /* tp_as_number */
1645 0, /* tp_as_sequence */
1646 0, /* tp_as_mapping */
1647 0, /* tp_hash */
Gerhard Häring0741a602007-01-14 01:43:50 +00001648 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001649 0, /* tp_str */
1650 0, /* tp_getattro */
1651 0, /* tp_setattro */
1652 0, /* tp_as_buffer */
1653 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1654 connection_doc, /* tp_doc */
1655 0, /* tp_traverse */
1656 0, /* tp_clear */
1657 0, /* tp_richcompare */
1658 0, /* tp_weaklistoffset */
1659 0, /* tp_iter */
1660 0, /* tp_iternext */
1661 connection_methods, /* tp_methods */
1662 connection_members, /* tp_members */
1663 connection_getset, /* tp_getset */
1664 0, /* tp_base */
1665 0, /* tp_dict */
1666 0, /* tp_descr_get */
1667 0, /* tp_descr_set */
1668 0, /* tp_dictoffset */
Gerhard Häring0741a602007-01-14 01:43:50 +00001669 (initproc)pysqlite_connection_init, /* tp_init */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001670 0, /* tp_alloc */
1671 0, /* tp_new */
1672 0 /* tp_free */
1673};
1674
Gerhard Häring0741a602007-01-14 01:43:50 +00001675extern int pysqlite_connection_setup_types(void)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001676{
Gerhard Häring0741a602007-01-14 01:43:50 +00001677 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1678 return PyType_Ready(&pysqlite_ConnectionType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001679}