blob: 439542e5139307270131afac0ffd9f174880bd14 [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 {
Martin Panterbf2dca92016-07-11 07:51:37 +0000119 /* Create a pysqlite connection from an APSW connection */
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000120 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
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300231 Py_SETREF(self->statement_cache,
Serhiy Storchaka5951f232015-12-24 10:35:35 +0200232 (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
Benjamin Petersona72d15c2017-09-13 21:20:29 -0700296 Py_TYPE(self)->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{
Serhiy Storchakae63af902016-08-29 14:29:55 +0300327 static char *kwlist[] = {"factory", NULL};
Anthony Baxterc51ee692006-04-01 00:57:31 +0000328 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
Serhiy Storchakae63af902016-08-29 14:29:55 +0300344 cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL);
345 if (cursor == NULL)
346 return NULL;
347 if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
348 PyErr_Format(PyExc_TypeError,
349 "factory must return a cursor, not %.100s",
350 Py_TYPE(cursor)->tp_name);
351 Py_DECREF(cursor);
352 return NULL;
353 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000354
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000355 _pysqlite_drop_unused_cursor_references(self);
356
Anthony Baxterc51ee692006-04-01 00:57:31 +0000357 if (cursor && self->row_factory != Py_None) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000358 Py_INCREF(self->row_factory);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300359 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000360 }
361
362 return cursor;
363}
364
Gerhard Häring0741a602007-01-14 01:43:50 +0000365PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000366{
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000367 PyObject* ret;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000368 int rc;
369
Gerhard Häring0741a602007-01-14 01:43:50 +0000370 if (!pysqlite_check_thread(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000371 return NULL;
372 }
373
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000374 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000375
376 if (self->db) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000377 if (self->apsw_connection) {
378 ret = PyObject_CallMethod(self->apsw_connection, "close", "");
379 Py_XDECREF(ret);
Serhiy Storchaka98a97222014-02-09 13:14:04 +0200380 Py_CLEAR(self->apsw_connection);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000381 self->db = NULL;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000382 } else {
383 Py_BEGIN_ALLOW_THREADS
384 rc = sqlite3_close(self->db);
385 Py_END_ALLOW_THREADS
386
387 if (rc != SQLITE_OK) {
388 _pysqlite_seterror(self->db, NULL);
389 return NULL;
390 } else {
391 self->db = NULL;
392 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000393 }
394 }
395
396 Py_INCREF(Py_None);
397 return Py_None;
398}
399
400/*
401 * Checks if a connection object is usable (i. e. not closed).
402 *
403 * 0 => error; 1 => ok
404 */
Gerhard Häring0741a602007-01-14 01:43:50 +0000405int pysqlite_check_connection(pysqlite_Connection* con)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000406{
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000407 if (!con->initialized) {
408 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
409 return 0;
410 }
411
Anthony Baxterc51ee692006-04-01 00:57:31 +0000412 if (!con->db) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000413 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000414 return 0;
415 } else {
416 return 1;
417 }
418}
419
Gerhard Häring0741a602007-01-14 01:43:50 +0000420PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000421{
422 int rc;
423 const char* tail;
424 sqlite3_stmt* statement;
425
426 Py_BEGIN_ALLOW_THREADS
427 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
428 Py_END_ALLOW_THREADS
429
430 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000431 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000432 goto error;
433 }
434
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000435 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000436 if (rc == SQLITE_DONE) {
437 self->inTransaction = 1;
438 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000439 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000440 }
441
442 Py_BEGIN_ALLOW_THREADS
443 rc = sqlite3_finalize(statement);
444 Py_END_ALLOW_THREADS
445
446 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000447 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000448 }
449
450error:
451 if (PyErr_Occurred()) {
452 return NULL;
453 } else {
454 Py_INCREF(Py_None);
455 return Py_None;
456 }
457}
458
Gerhard Häring0741a602007-01-14 01:43:50 +0000459PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000460{
461 int rc;
462 const char* tail;
463 sqlite3_stmt* statement;
464
Gerhard Häring0741a602007-01-14 01:43:50 +0000465 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000466 return NULL;
467 }
468
469 if (self->inTransaction) {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000470
Anthony Baxterc51ee692006-04-01 00:57:31 +0000471 Py_BEGIN_ALLOW_THREADS
472 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
473 Py_END_ALLOW_THREADS
474 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000475 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000476 goto error;
477 }
478
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000479 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000480 if (rc == SQLITE_DONE) {
481 self->inTransaction = 0;
482 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000483 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000484 }
485
486 Py_BEGIN_ALLOW_THREADS
487 rc = sqlite3_finalize(statement);
488 Py_END_ALLOW_THREADS
489 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000490 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000491 }
492
493 }
494
495error:
496 if (PyErr_Occurred()) {
497 return NULL;
498 } else {
499 Py_INCREF(Py_None);
500 return Py_None;
501 }
502}
503
Gerhard Häring0741a602007-01-14 01:43:50 +0000504PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000505{
506 int rc;
507 const char* tail;
508 sqlite3_stmt* statement;
509
Gerhard Häring0741a602007-01-14 01:43:50 +0000510 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000511 return NULL;
512 }
513
514 if (self->inTransaction) {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000515 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000516
517 Py_BEGIN_ALLOW_THREADS
518 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
519 Py_END_ALLOW_THREADS
520 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000521 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000522 goto error;
523 }
524
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000525 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000526 if (rc == SQLITE_DONE) {
527 self->inTransaction = 0;
528 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000529 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000530 }
531
532 Py_BEGIN_ALLOW_THREADS
533 rc = sqlite3_finalize(statement);
534 Py_END_ALLOW_THREADS
535 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000536 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000537 }
538
539 }
540
541error:
542 if (PyErr_Occurred()) {
543 return NULL;
544 } else {
545 Py_INCREF(Py_None);
546 return Py_None;
547 }
548}
549
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200550static int
551_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000552{
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200553 if (py_val == Py_None) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000554 sqlite3_result_null(context);
555 } else if (PyInt_Check(py_val)) {
Ned Deily77e77a12012-05-19 23:35:05 -0700556 sqlite3_result_int64(context, (sqlite_int64)PyInt_AsLong(py_val));
Petri Lehtinen4ab701b2012-02-21 13:58:40 +0200557 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200558 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
559 if (value == -1 && PyErr_Occurred())
560 return -1;
561 sqlite3_result_int64(context, value);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000562 } else if (PyFloat_Check(py_val)) {
563 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
564 } else if (PyBuffer_Check(py_val)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200565 const char* buffer;
566 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000567 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
568 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200569 return -1;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000570 }
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200571 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000572 } else if (PyString_Check(py_val)) {
573 sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000574 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200575 PyObject * stringval = PyUnicode_AsUTF8String(py_val);
576 if (!stringval)
577 return -1;
578 sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
579 Py_DECREF(stringval);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000580 } else {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200581 return -1;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000582 }
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200583 return 0;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000584}
585
Gerhard Häring0741a602007-01-14 01:43:50 +0000586PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000587{
588 PyObject* args;
589 int i;
590 sqlite3_value* cur_value;
591 PyObject* cur_py_value;
592 const char* val_str;
Neal Norwitz95f0e4c2006-04-01 09:08:06 +0000593 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000594 void* raw_buffer;
595
596 args = PyTuple_New(argc);
Anthony Baxter72289a62006-04-04 06:29:05 +0000597 if (!args) {
598 return NULL;
599 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000600
601 for (i = 0; i < argc; i++) {
602 cur_value = argv[i];
603 switch (sqlite3_value_type(argv[i])) {
604 case SQLITE_INTEGER:
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200605 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
Anthony Baxterc51ee692006-04-01 00:57:31 +0000606 break;
607 case SQLITE_FLOAT:
608 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
609 break;
610 case SQLITE_TEXT:
611 val_str = (const char*)sqlite3_value_text(cur_value);
612 cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
613 /* TODO: have a way to show errors here */
614 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000615 PyErr_Clear();
Anthony Baxterc51ee692006-04-01 00:57:31 +0000616 Py_INCREF(Py_None);
617 cur_py_value = Py_None;
618 }
619 break;
620 case SQLITE_BLOB:
621 buflen = sqlite3_value_bytes(cur_value);
622 cur_py_value = PyBuffer_New(buflen);
623 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000624 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000625 }
626 if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000627 Py_DECREF(cur_py_value);
628 cur_py_value = NULL;
629 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000630 }
631 memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen);
632 break;
633 case SQLITE_NULL:
634 default:
635 Py_INCREF(Py_None);
636 cur_py_value = Py_None;
637 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000638
639 if (!cur_py_value) {
640 Py_DECREF(args);
641 return NULL;
642 }
643
Anthony Baxterc51ee692006-04-01 00:57:31 +0000644 PyTuple_SetItem(args, i, cur_py_value);
645
646 }
647
648 return args;
649}
650
Gerhard Häring0741a602007-01-14 01:43:50 +0000651void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000652{
653 PyObject* args;
654 PyObject* py_func;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000655 PyObject* py_retval = NULL;
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200656 int ok;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000657
Georg Brandld3eaa742009-04-05 11:07:14 +0000658#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000659 PyGILState_STATE threadstate;
660
661 threadstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000662#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000663
664 py_func = (PyObject*)sqlite3_user_data(context);
665
Gerhard Häring0741a602007-01-14 01:43:50 +0000666 args = _pysqlite_build_py_params(context, argc, argv);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000667 if (args) {
668 py_retval = PyObject_CallObject(py_func, args);
669 Py_DECREF(args);
670 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000671
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200672 ok = 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000673 if (py_retval) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200674 ok = _pysqlite_set_result(context, py_retval) == 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000675 Py_DECREF(py_retval);
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200676 }
677 if (!ok) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000678 if (_enable_callback_tracebacks) {
679 PyErr_Print();
680 } else {
681 PyErr_Clear();
682 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000683 _sqlite3_result_error(context, "user-defined function raised exception", -1);
Gerhard Häring1541ef02006-06-13 22:24:47 +0000684 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000685
Georg Brandld3eaa742009-04-05 11:07:14 +0000686#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000687 PyGILState_Release(threadstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000688#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000689}
690
Gerhard Häring0741a602007-01-14 01:43:50 +0000691static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000692{
693 PyObject* args;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000694 PyObject* function_result = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000695 PyObject* aggregate_class;
696 PyObject** aggregate_instance;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000697 PyObject* stepmethod = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000698
Georg Brandld3eaa742009-04-05 11:07:14 +0000699#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000700 PyGILState_STATE threadstate;
701
702 threadstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000703#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000704
705 aggregate_class = (PyObject*)sqlite3_user_data(context);
706
707 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
708
709 if (*aggregate_instance == 0) {
710 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
711
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000712 if (PyErr_Occurred()) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000713 *aggregate_instance = 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000714 if (_enable_callback_tracebacks) {
715 PyErr_Print();
716 } else {
717 PyErr_Clear();
718 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000719 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000720 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000721 }
722 }
723
724 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000725 if (!stepmethod) {
726 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000727 }
728
Gerhard Häring0741a602007-01-14 01:43:50 +0000729 args = _pysqlite_build_py_params(context, argc, params);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000730 if (!args) {
731 goto error;
732 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000733
734 function_result = PyObject_CallObject(stepmethod, args);
735 Py_DECREF(args);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000736
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000737 if (!function_result) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000738 if (_enable_callback_tracebacks) {
739 PyErr_Print();
740 } else {
741 PyErr_Clear();
742 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000743 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000744 }
745
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000746error:
747 Py_XDECREF(stepmethod);
748 Py_XDECREF(function_result);
749
Georg Brandld3eaa742009-04-05 11:07:14 +0000750#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000751 PyGILState_Release(threadstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000752#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000753}
754
Gerhard Häring0741a602007-01-14 01:43:50 +0000755void _pysqlite_final_callback(sqlite3_context* context)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000756{
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200757 PyObject* function_result;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000758 PyObject** aggregate_instance;
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200759 int ok;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000760
Georg Brandld3eaa742009-04-05 11:07:14 +0000761#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000762 PyGILState_STATE threadstate;
763
764 threadstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000765#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000766
Anthony Baxterc51ee692006-04-01 00:57:31 +0000767 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
768 if (!*aggregate_instance) {
769 /* this branch is executed if there was an exception in the aggregate's
770 * __init__ */
771
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000772 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000773 }
774
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000775 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200776 Py_DECREF(*aggregate_instance);
777
778 ok = 0;
779 if (function_result) {
780 ok = _pysqlite_set_result(context, function_result) == 0;
781 Py_DECREF(function_result);
782 }
783 if (!ok) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000784 if (_enable_callback_tracebacks) {
785 PyErr_Print();
786 } else {
787 PyErr_Clear();
788 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000789 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000790 }
791
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000792error:
Martin Panter38d335b2016-10-20 03:56:48 +0000793 ; /* necessary for --without-threads flag */
Georg Brandld3eaa742009-04-05 11:07:14 +0000794#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000795 PyGILState_Release(threadstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000796#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000797}
798
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000799static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000800{
801 PyObject* new_list;
802 PyObject* weakref;
803 int i;
804
805 /* we only need to do this once in a while */
806 if (self->created_statements++ < 200) {
807 return;
808 }
809
810 self->created_statements = 0;
811
812 new_list = PyList_New(0);
813 if (!new_list) {
814 return;
815 }
816
817 for (i = 0; i < PyList_Size(self->statements); i++) {
818 weakref = PyList_GetItem(self->statements, i);
Gerhard Häringecd20102006-06-19 21:17:35 +0000819 if (PyWeakref_GetObject(weakref) != Py_None) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000820 if (PyList_Append(new_list, weakref) != 0) {
821 Py_DECREF(new_list);
822 return;
823 }
824 }
825 }
826
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300827 Py_SETREF(self->statements, new_list);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000828}
Anthony Baxterc51ee692006-04-01 00:57:31 +0000829
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000830static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
831{
832 PyObject* new_list;
833 PyObject* weakref;
834 int i;
835
836 /* we only need to do this once in a while */
837 if (self->created_cursors++ < 200) {
838 return;
839 }
840
841 self->created_cursors = 0;
842
843 new_list = PyList_New(0);
844 if (!new_list) {
845 return;
846 }
847
848 for (i = 0; i < PyList_Size(self->cursors); i++) {
849 weakref = PyList_GetItem(self->cursors, i);
850 if (PyWeakref_GetObject(weakref) != Py_None) {
851 if (PyList_Append(new_list, weakref) != 0) {
852 Py_DECREF(new_list);
853 return;
854 }
855 }
856 }
857
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300858 Py_SETREF(self->cursors, new_list);
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000859}
860
Gerhard Häring0741a602007-01-14 01:43:50 +0000861PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000862{
863 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
864
865 PyObject* func;
866 char* name;
867 int narg;
868 int rc;
869
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000870 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
871 return NULL;
872 }
873
Anthony Baxterc51ee692006-04-01 00:57:31 +0000874 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
875 &name, &narg, &func))
876 {
877 return NULL;
878 }
879
Gerhard Häring0741a602007-01-14 01:43:50 +0000880 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000881
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000882 if (rc != SQLITE_OK) {
883 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000884 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000885 return NULL;
886 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000887 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
888 return NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000889
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000890 Py_INCREF(Py_None);
891 return Py_None;
892 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000893}
894
Gerhard Häring0741a602007-01-14 01:43:50 +0000895PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000896{
897 PyObject* aggregate_class;
898
899 int n_arg;
900 char* name;
901 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
902 int rc;
903
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000904 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
905 return NULL;
906 }
907
Anthony Baxterc51ee692006-04-01 00:57:31 +0000908 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
909 kwlist, &name, &n_arg, &aggregate_class)) {
910 return NULL;
911 }
912
Gerhard Häring0741a602007-01-14 01:43:50 +0000913 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 +0000914 if (rc != SQLITE_OK) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000915 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000916 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000917 return NULL;
918 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000919 if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
920 return NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000921
922 Py_INCREF(Py_None);
923 return Py_None;
924 }
925}
926
Gerhard Häring0741a602007-01-14 01:43:50 +0000927static 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 +0000928{
929 PyObject *ret;
930 int rc;
Georg Brandld3eaa742009-04-05 11:07:14 +0000931#ifdef WITH_THREAD
Gerhard Häring1541ef02006-06-13 22:24:47 +0000932 PyGILState_STATE gilstate;
933
934 gilstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000935#endif
Gerhard Häring1541ef02006-06-13 22:24:47 +0000936 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
937
938 if (!ret) {
939 if (_enable_callback_tracebacks) {
940 PyErr_Print();
941 } else {
942 PyErr_Clear();
943 }
944
945 rc = SQLITE_DENY;
946 } else {
947 if (PyInt_Check(ret)) {
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200948 rc = _PyInt_AsInt(ret);
949 if (rc == -1 && PyErr_Occurred())
950 rc = SQLITE_DENY;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000951 } else {
952 rc = SQLITE_DENY;
953 }
954 Py_DECREF(ret);
955 }
956
Georg Brandld3eaa742009-04-05 11:07:14 +0000957#ifdef WITH_THREAD
Gerhard Häring1541ef02006-06-13 22:24:47 +0000958 PyGILState_Release(gilstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000959#endif
Gerhard Häring1541ef02006-06-13 22:24:47 +0000960 return rc;
961}
962
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000963static int _progress_handler(void* user_arg)
964{
965 int rc;
966 PyObject *ret;
Georg Brandld3eaa742009-04-05 11:07:14 +0000967#ifdef WITH_THREAD
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000968 PyGILState_STATE gilstate;
969
970 gilstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +0000971#endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000972 ret = PyObject_CallFunction((PyObject*)user_arg, "");
973
974 if (!ret) {
975 if (_enable_callback_tracebacks) {
976 PyErr_Print();
977 } else {
978 PyErr_Clear();
979 }
980
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000981 /* abort query if error occurred */
Serhiy Storchaka35c52b62013-02-07 16:59:34 +0200982 rc = 1;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000983 } else {
984 rc = (int)PyObject_IsTrue(ret);
Neal Norwitzca752f32008-03-03 04:37:45 +0000985 Py_DECREF(ret);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000986 }
987
Georg Brandld3eaa742009-04-05 11:07:14 +0000988#ifdef WITH_THREAD
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000989 PyGILState_Release(gilstate);
Georg Brandld3eaa742009-04-05 11:07:14 +0000990#endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000991 return rc;
992}
993
Gerhard Häring3bbb6722010-03-05 09:12:37 +0000994static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häring1541ef02006-06-13 22:24:47 +0000995{
996 PyObject* authorizer_cb;
997
998 static char *kwlist[] = { "authorizer_callback", NULL };
999 int rc;
1000
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001001 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1002 return NULL;
1003 }
1004
Gerhard Häring1541ef02006-06-13 22:24:47 +00001005 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1006 kwlist, &authorizer_cb)) {
1007 return NULL;
1008 }
1009
1010 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1011
1012 if (rc != SQLITE_OK) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001013 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Gerhard Häring1541ef02006-06-13 22:24:47 +00001014 return NULL;
1015 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001016 if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
1017 return NULL;
Gerhard Häring1541ef02006-06-13 22:24:47 +00001018
1019 Py_INCREF(Py_None);
1020 return Py_None;
1021 }
1022}
1023
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001024static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001025{
1026 PyObject* progress_handler;
1027 int n;
1028
1029 static char *kwlist[] = { "progress_handler", "n", NULL };
1030
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001031 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1032 return NULL;
1033 }
1034
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001035 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1036 kwlist, &progress_handler, &n)) {
1037 return NULL;
1038 }
1039
1040 if (progress_handler == Py_None) {
1041 /* None clears the progress handler previously set */
1042 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1043 } else {
1044 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001045 if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
1046 return NULL;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001047 }
1048
1049 Py_INCREF(Py_None);
1050 return Py_None;
1051}
1052
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001053#ifdef HAVE_LOAD_EXTENSION
1054static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1055{
1056 int rc;
1057 int onoff;
1058
1059 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1060 return NULL;
1061 }
1062
1063 if (!PyArg_ParseTuple(args, "i", &onoff)) {
1064 return NULL;
1065 }
1066
1067 rc = sqlite3_enable_load_extension(self->db, onoff);
1068
1069 if (rc != SQLITE_OK) {
1070 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1071 return NULL;
1072 } else {
1073 Py_INCREF(Py_None);
1074 return Py_None;
1075 }
1076}
1077
1078static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1079{
1080 int rc;
1081 char* extension_name;
1082 char* errmsg;
1083
1084 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1085 return NULL;
1086 }
1087
1088 if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1089 return NULL;
1090 }
1091
1092 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1093 if (rc != 0) {
1094 PyErr_SetString(pysqlite_OperationalError, errmsg);
1095 return NULL;
1096 } else {
1097 Py_INCREF(Py_None);
1098 return Py_None;
1099 }
1100}
1101#endif
1102
Gerhard Häring0741a602007-01-14 01:43:50 +00001103int pysqlite_check_thread(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001104{
Georg Brandld3eaa742009-04-05 11:07:14 +00001105#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +00001106 if (self->check_same_thread) {
1107 if (PyThread_get_thread_ident() != self->thread_ident) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001108 PyErr_Format(pysqlite_ProgrammingError,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001109 "SQLite objects created in a thread can only be used in that same thread."
1110 "The object was created in thread id %ld and this is thread id %ld",
1111 self->thread_ident, PyThread_get_thread_ident());
1112 return 0;
1113 }
1114
1115 }
Georg Brandld3eaa742009-04-05 11:07:14 +00001116#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +00001117 return 1;
1118}
1119
Gerhard Häring0741a602007-01-14 01:43:50 +00001120static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001121{
1122 Py_INCREF(self->isolation_level);
1123 return self->isolation_level;
1124}
1125
Gerhard Häring0741a602007-01-14 01:43:50 +00001126static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Anthony Baxter72289a62006-04-04 06:29:05 +00001127{
Gerhard Häring0741a602007-01-14 01:43:50 +00001128 if (!pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001129 return NULL;
1130 } else {
1131 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1132 }
1133}
1134
Gerhard Häring0741a602007-01-14 01:43:50 +00001135static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001136{
Anthony Baxterc51ee692006-04-01 00:57:31 +00001137 PyObject* res;
1138 PyObject* begin_statement;
Georg Brandla24869a2008-07-16 22:33:18 +00001139 char* begin_statement_str;
Anthony Baxterc51ee692006-04-01 00:57:31 +00001140
1141 Py_XDECREF(self->isolation_level);
1142
Neal Norwitz5b030652006-04-16 03:28:17 +00001143 if (self->begin_statement) {
1144 PyMem_Free(self->begin_statement);
1145 self->begin_statement = NULL;
1146 }
1147
Anthony Baxterc51ee692006-04-01 00:57:31 +00001148 if (isolation_level == Py_None) {
1149 Py_INCREF(Py_None);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001150 self->isolation_level = Py_None;
1151
Gerhard Häring0741a602007-01-14 01:43:50 +00001152 res = pysqlite_connection_commit(self, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001153 if (!res) {
1154 return -1;
1155 }
Anthony Baxterc51ee692006-04-01 00:57:31 +00001156 Py_DECREF(res);
1157
1158 self->inTransaction = 0;
1159 } else {
1160 Py_INCREF(isolation_level);
1161 self->isolation_level = isolation_level;
1162
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001163 begin_statement = PyString_FromString("BEGIN ");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001164 if (!begin_statement) {
1165 return -1;
1166 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001167 PyString_Concat(&begin_statement, isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001168 if (!begin_statement) {
1169 return -1;
1170 }
1171
Georg Brandla24869a2008-07-16 22:33:18 +00001172 begin_statement_str = PyString_AsString(begin_statement);
1173 if (!begin_statement_str) {
1174 Py_DECREF(begin_statement);
1175 return -1;
1176 }
1177 self->begin_statement = PyMem_Malloc(strlen(begin_statement_str) + 2);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001178 if (!self->begin_statement) {
Georg Brandla24869a2008-07-16 22:33:18 +00001179 Py_DECREF(begin_statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001180 return -1;
1181 }
1182
Georg Brandla24869a2008-07-16 22:33:18 +00001183 strcpy(self->begin_statement, begin_statement_str);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001184 Py_DECREF(begin_statement);
1185 }
1186
1187 return 0;
1188}
1189
Gerhard Häring0741a602007-01-14 01:43:50 +00001190PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001191{
1192 PyObject* sql;
Gerhard Häring0741a602007-01-14 01:43:50 +00001193 pysqlite_Statement* statement;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001194 PyObject* weakref;
Anthony Baxterc51ee692006-04-01 00:57:31 +00001195 int rc;
1196
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001197 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1198 return NULL;
1199 }
1200
Larry Hastings101b0542015-05-08 09:56:29 -07001201 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection()", kwargs))
1202 return NULL;
1203
Anthony Baxterc51ee692006-04-01 00:57:31 +00001204 if (!PyArg_ParseTuple(args, "O", &sql)) {
1205 return NULL;
1206 }
1207
Gerhard Häring0741a602007-01-14 01:43:50 +00001208 _pysqlite_drop_unused_statement_references(self);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001209
Gerhard Häring0741a602007-01-14 01:43:50 +00001210 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001211 if (!statement) {
1212 return NULL;
1213 }
1214
Victor Stinner6e055d72010-03-13 03:27:07 +00001215 statement->db = NULL;
1216 statement->st = NULL;
1217 statement->sql = NULL;
1218 statement->in_use = 0;
1219 statement->in_weakreflist = NULL;
1220
Gerhard Häring0741a602007-01-14 01:43:50 +00001221 rc = pysqlite_statement_create(statement, self, sql);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001222
1223 if (rc != SQLITE_OK) {
1224 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001225 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001226 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka0aa65622014-09-11 13:27:19 +03001227 if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_TypeError))
1228 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001229 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001230 (void)pysqlite_statement_reset(statement);
1231 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001232 }
1233
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001234 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001235 } else {
1236 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1237 if (!weakref) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001238 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001239 goto error;
1240 }
1241
1242 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001243 Py_CLEAR(weakref);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001244 goto error;
1245 }
1246
1247 Py_DECREF(weakref);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001248 }
1249
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001250error:
Anthony Baxterc51ee692006-04-01 00:57:31 +00001251 return (PyObject*)statement;
1252}
1253
Larry Hastings101b0542015-05-08 09:56:29 -07001254PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001255{
1256 PyObject* cursor = 0;
1257 PyObject* result = 0;
1258 PyObject* method = 0;
1259
1260 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1261 if (!cursor) {
1262 goto error;
1263 }
1264
1265 method = PyObject_GetAttrString(cursor, "execute");
1266 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001267 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001268 goto error;
1269 }
1270
1271 result = PyObject_CallObject(method, args);
1272 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001273 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001274 }
1275
1276error:
1277 Py_XDECREF(result);
1278 Py_XDECREF(method);
1279
1280 return cursor;
1281}
1282
Larry Hastings101b0542015-05-08 09:56:29 -07001283PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001284{
1285 PyObject* cursor = 0;
1286 PyObject* result = 0;
1287 PyObject* method = 0;
1288
1289 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1290 if (!cursor) {
1291 goto error;
1292 }
1293
1294 method = PyObject_GetAttrString(cursor, "executemany");
1295 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001296 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001297 goto error;
1298 }
1299
1300 result = PyObject_CallObject(method, args);
1301 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001302 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001303 }
1304
1305error:
1306 Py_XDECREF(result);
1307 Py_XDECREF(method);
1308
1309 return cursor;
1310}
1311
Larry Hastings101b0542015-05-08 09:56:29 -07001312PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001313{
1314 PyObject* cursor = 0;
1315 PyObject* result = 0;
1316 PyObject* method = 0;
1317
1318 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1319 if (!cursor) {
1320 goto error;
1321 }
1322
1323 method = PyObject_GetAttrString(cursor, "executescript");
1324 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001325 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001326 goto error;
1327 }
1328
1329 result = PyObject_CallObject(method, args);
1330 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001331 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001332 }
1333
1334error:
1335 Py_XDECREF(result);
1336 Py_XDECREF(method);
1337
1338 return cursor;
1339}
1340
Anthony Baxter72289a62006-04-04 06:29:05 +00001341/* ------------------------- COLLATION CODE ------------------------ */
1342
1343static int
Gerhard Häring0741a602007-01-14 01:43:50 +00001344pysqlite_collation_callback(
Anthony Baxter72289a62006-04-04 06:29:05 +00001345 void* context,
1346 int text1_length, const void* text1_data,
1347 int text2_length, const void* text2_data)
1348{
1349 PyObject* callback = (PyObject*)context;
1350 PyObject* string1 = 0;
1351 PyObject* string2 = 0;
Georg Brandld3eaa742009-04-05 11:07:14 +00001352#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001353 PyGILState_STATE gilstate;
Georg Brandld3eaa742009-04-05 11:07:14 +00001354#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001355 PyObject* retval = NULL;
Serhiy Storchaka35c52b62013-02-07 16:59:34 +02001356 long longval;
Anthony Baxter72289a62006-04-04 06:29:05 +00001357 int result = 0;
Georg Brandld3eaa742009-04-05 11:07:14 +00001358#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001359 gilstate = PyGILState_Ensure();
Georg Brandld3eaa742009-04-05 11:07:14 +00001360#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001361
1362 if (PyErr_Occurred()) {
1363 goto finally;
1364 }
1365
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001366 string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length);
1367 string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length);
Anthony Baxter72289a62006-04-04 06:29:05 +00001368
1369 if (!string1 || !string2) {
1370 goto finally; /* failed to allocate strings */
1371 }
1372
1373 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1374
1375 if (!retval) {
1376 /* execution failed */
1377 goto finally;
1378 }
1379
Serhiy Storchaka35c52b62013-02-07 16:59:34 +02001380 longval = PyLong_AsLongAndOverflow(retval, &result);
1381 if (longval == -1 && PyErr_Occurred()) {
1382 PyErr_Clear();
Anthony Baxter72289a62006-04-04 06:29:05 +00001383 result = 0;
1384 }
Serhiy Storchaka35c52b62013-02-07 16:59:34 +02001385 else if (!result) {
1386 if (longval > 0)
1387 result = 1;
1388 else if (longval < 0)
1389 result = -1;
1390 }
Anthony Baxter72289a62006-04-04 06:29:05 +00001391
1392finally:
1393 Py_XDECREF(string1);
1394 Py_XDECREF(string2);
1395 Py_XDECREF(retval);
Georg Brandld3eaa742009-04-05 11:07:14 +00001396#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001397 PyGILState_Release(gilstate);
Georg Brandld3eaa742009-04-05 11:07:14 +00001398#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001399 return result;
1400}
1401
1402static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001403pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Gerhard Häring1541ef02006-06-13 22:24:47 +00001404{
1405 PyObject* retval = NULL;
1406
Gerhard Häring0741a602007-01-14 01:43:50 +00001407 if (!pysqlite_check_connection(self)) {
Gerhard Häring1541ef02006-06-13 22:24:47 +00001408 goto finally;
1409 }
1410
1411 sqlite3_interrupt(self->db);
1412
1413 Py_INCREF(Py_None);
1414 retval = Py_None;
1415
1416finally:
1417 return retval;
1418}
1419
Gregory P. Smithb9803422008-03-28 08:32:09 +00001420/* Function author: Paul Kippes <kippesp@gmail.com>
1421 * Class method of Connection to call the Python function _iterdump
1422 * of the sqlite3 module.
1423 */
1424static PyObject *
1425pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1426{
1427 PyObject* retval = NULL;
1428 PyObject* module = NULL;
1429 PyObject* module_dict;
1430 PyObject* pyfn_iterdump;
1431
1432 if (!pysqlite_check_connection(self)) {
1433 goto finally;
1434 }
1435
1436 module = PyImport_ImportModule(MODULE_NAME ".dump");
1437 if (!module) {
1438 goto finally;
1439 }
1440
1441 module_dict = PyModule_GetDict(module);
1442 if (!module_dict) {
1443 goto finally;
1444 }
1445
1446 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1447 if (!pyfn_iterdump) {
1448 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1449 goto finally;
1450 }
1451
1452 args = PyTuple_New(1);
1453 if (!args) {
1454 goto finally;
1455 }
1456 Py_INCREF(self);
1457 PyTuple_SetItem(args, 0, (PyObject*)self);
1458 retval = PyObject_CallObject(pyfn_iterdump, args);
1459
1460finally:
1461 Py_XDECREF(args);
1462 Py_XDECREF(module);
1463 return retval;
1464}
1465
Gerhard Häring1541ef02006-06-13 22:24:47 +00001466static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001467pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Anthony Baxter72289a62006-04-04 06:29:05 +00001468{
1469 PyObject* callable;
1470 PyObject* uppercase_name = 0;
1471 PyObject* name;
1472 PyObject* retval;
1473 char* chk;
1474 int rc;
1475
Gerhard Häring0741a602007-01-14 01:43:50 +00001476 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001477 goto finally;
1478 }
1479
Serhiy Storchaka5170c162016-10-01 08:24:55 +03001480 if (!PyArg_ParseTuple(args, "SO:create_collation(name, callback)",
1481 &name, &callable)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001482 goto finally;
1483 }
1484
Serhiy Storchaka5170c162016-10-01 08:24:55 +03001485 uppercase_name = PyObject_CallMethod((PyObject *)&PyString_Type,
1486 "upper", "O", name);
Anthony Baxter72289a62006-04-04 06:29:05 +00001487 if (!uppercase_name) {
1488 goto finally;
1489 }
1490
Serhiy Storchaka5170c162016-10-01 08:24:55 +03001491 chk = PyString_AS_STRING(uppercase_name);
Anthony Baxter72289a62006-04-04 06:29:05 +00001492 while (*chk) {
1493 if ((*chk >= '0' && *chk <= '9')
1494 || (*chk >= 'A' && *chk <= 'Z')
1495 || (*chk == '_'))
1496 {
1497 chk++;
1498 } else {
Gerhard Häring0741a602007-01-14 01:43:50 +00001499 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Anthony Baxter72289a62006-04-04 06:29:05 +00001500 goto finally;
1501 }
1502 }
1503
1504 if (callable != Py_None && !PyCallable_Check(callable)) {
1505 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1506 goto finally;
1507 }
1508
1509 if (callable != Py_None) {
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001510 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1511 goto finally;
Anthony Baxter72289a62006-04-04 06:29:05 +00001512 } else {
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001513 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1514 goto finally;
Anthony Baxter72289a62006-04-04 06:29:05 +00001515 }
1516
1517 rc = sqlite3_create_collation(self->db,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001518 PyString_AsString(uppercase_name),
Anthony Baxter72289a62006-04-04 06:29:05 +00001519 SQLITE_UTF8,
1520 (callable != Py_None) ? callable : NULL,
Gerhard Häring0741a602007-01-14 01:43:50 +00001521 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001522 if (rc != SQLITE_OK) {
1523 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001524 _pysqlite_seterror(self->db, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001525 goto finally;
1526 }
1527
1528finally:
1529 Py_XDECREF(uppercase_name);
1530
1531 if (PyErr_Occurred()) {
1532 retval = NULL;
1533 } else {
1534 Py_INCREF(Py_None);
1535 retval = Py_None;
1536 }
1537
1538 return retval;
1539}
1540
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001541/* Called when the connection is used as a context manager. Returns itself as a
1542 * convenience to the caller. */
1543static PyObject *
1544pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1545{
1546 Py_INCREF(self);
1547 return (PyObject*)self;
1548}
1549
1550/** Called when the connection is used as a context manager. If there was any
1551 * exception, a rollback takes place; otherwise we commit. */
1552static PyObject *
1553pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1554{
1555 PyObject* exc_type, *exc_value, *exc_tb;
1556 char* method_name;
1557 PyObject* result;
1558
1559 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1560 return NULL;
1561 }
1562
1563 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1564 method_name = "commit";
1565 } else {
1566 method_name = "rollback";
1567 }
1568
1569 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1570 if (!result) {
1571 return NULL;
1572 }
1573 Py_DECREF(result);
1574
1575 Py_INCREF(Py_False);
1576 return Py_False;
1577}
1578
Anthony Baxterc51ee692006-04-01 00:57:31 +00001579static char connection_doc[] =
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001580PyDoc_STR("SQLite database connection object.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001581
1582static PyGetSetDef connection_getset[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001583 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1584 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001585 {NULL}
1586};
1587
1588static PyMethodDef connection_methods[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001589 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001590 PyDoc_STR("Return a cursor for the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001591 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001592 PyDoc_STR("Closes the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001593 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001594 PyDoc_STR("Commit the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001595 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001596 PyDoc_STR("Roll back the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001597 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001598 PyDoc_STR("Creates a new function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001599 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001600 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001601 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001602 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häring3bbb6722010-03-05 09:12:37 +00001603 #ifdef HAVE_LOAD_EXTENSION
1604 {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1605 PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1606 {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1607 PyDoc_STR("Load SQLite extension module. Non-standard.")},
1608 #endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001609 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1610 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001611 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001612 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001613 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001614 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001615 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001616 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001617 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001618 PyDoc_STR("Creates a collation function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001619 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001620 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Gregory P. Smithb9803422008-03-28 08:32:09 +00001621 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Gerhard Häringef2276b2008-09-12 13:54:06 +00001622 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 +00001623 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1624 PyDoc_STR("For context manager. Non-standard.")},
1625 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1626 PyDoc_STR("For context manager. Non-standard.")},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001627 {NULL, NULL}
1628};
1629
1630static struct PyMemberDef connection_members[] =
1631{
Gerhard Häring0741a602007-01-14 01:43:50 +00001632 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), RO},
1633 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), RO},
1634 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), RO},
1635 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), RO},
1636 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), RO},
1637 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), RO},
1638 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), RO},
1639 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), RO},
1640 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), RO},
1641 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), RO},
1642 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1643 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001644 {NULL}
1645};
1646
Gerhard Häring0741a602007-01-14 01:43:50 +00001647PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001648 PyVarObject_HEAD_INIT(NULL, 0)
Anthony Baxter8e7b4902006-04-05 18:25:33 +00001649 MODULE_NAME ".Connection", /* tp_name */
Gerhard Häring0741a602007-01-14 01:43:50 +00001650 sizeof(pysqlite_Connection), /* tp_basicsize */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001651 0, /* tp_itemsize */
Gerhard Häring0741a602007-01-14 01:43:50 +00001652 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001653 0, /* tp_print */
1654 0, /* tp_getattr */
1655 0, /* tp_setattr */
1656 0, /* tp_compare */
1657 0, /* tp_repr */
1658 0, /* tp_as_number */
1659 0, /* tp_as_sequence */
1660 0, /* tp_as_mapping */
1661 0, /* tp_hash */
Gerhard Häring0741a602007-01-14 01:43:50 +00001662 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001663 0, /* tp_str */
1664 0, /* tp_getattro */
1665 0, /* tp_setattro */
1666 0, /* tp_as_buffer */
1667 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1668 connection_doc, /* tp_doc */
1669 0, /* tp_traverse */
1670 0, /* tp_clear */
1671 0, /* tp_richcompare */
1672 0, /* tp_weaklistoffset */
1673 0, /* tp_iter */
1674 0, /* tp_iternext */
1675 connection_methods, /* tp_methods */
1676 connection_members, /* tp_members */
1677 connection_getset, /* tp_getset */
1678 0, /* tp_base */
1679 0, /* tp_dict */
1680 0, /* tp_descr_get */
1681 0, /* tp_descr_set */
1682 0, /* tp_dictoffset */
Gerhard Häring0741a602007-01-14 01:43:50 +00001683 (initproc)pysqlite_connection_init, /* tp_init */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001684 0, /* tp_alloc */
1685 0, /* tp_new */
1686 0 /* tp_free */
1687};
1688
Gerhard Häring0741a602007-01-14 01:43:50 +00001689extern int pysqlite_connection_setup_types(void)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001690{
Gerhard Häring0741a602007-01-14 01:43:50 +00001691 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1692 return PyType_Ready(&pysqlite_ConnectionType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001693}