blob: 5f899e88ff995239bb53260633260d3d49328092 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* connection.c - the connection type
2 *
3 * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
4 *
5 * This file is part of pysqlite.
6 *
7 * 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"
31#include "sqlitecompat.h"
32
33#include "pythread.h"
34
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000035static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036
Thomas Wouters0e3f5912006-08-11 14:57:12 +000037
38void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
39{
40 /* in older SQLite versions, calling sqlite3_result_error in callbacks
41 * triggers a bug in SQLite that leads either to irritating results or
42 * segfaults, depending on the SQLite version */
43#if SQLITE_VERSION_NUMBER >= 3003003
44 sqlite3_result_error(ctx, errmsg, len);
45#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000046 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000047#endif
48}
49
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000050int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000051{
52 static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
53
54 char* database;
55 int detect_types = 0;
56 PyObject* isolation_level = NULL;
57 PyObject* factory = NULL;
58 int check_same_thread = 1;
59 int cached_statements = 100;
60 double timeout = 5.0;
61 int rc;
62
63 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist,
64 &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
65 {
66 return -1;
67 }
68
69 self->begin_statement = NULL;
70
71 self->statement_cache = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000072 self->statements = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073
74 Py_INCREF(Py_None);
75 self->row_factory = Py_None;
76
77 Py_INCREF(&PyUnicode_Type);
78 self->text_factory = (PyObject*)&PyUnicode_Type;
79
80 Py_BEGIN_ALLOW_THREADS
81 rc = sqlite3_open(database, &self->db);
82 Py_END_ALLOW_THREADS
83
84 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000085 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 return -1;
87 }
88
89 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +000090 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +000091 if (!isolation_level) {
92 return -1;
93 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094 } else {
95 Py_INCREF(isolation_level);
96 }
97 self->isolation_level = NULL;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000098 pysqlite_connection_set_isolation_level(self, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000099 Py_DECREF(isolation_level);
100
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000101 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000102 if (PyErr_Occurred()) {
103 return -1;
104 }
105
Thomas Wouters477c8d52006-05-27 19:21:47 +0000106 self->statements = PyList_New(0);
107 if (!self->statements) {
108 return -1;
109 }
110 self->created_statements = 0;
111
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112 /* By default, the Cache class INCREFs the factory in its initializer, and
113 * decrefs it in its deallocator method. Since this would create a circular
114 * reference here, we're breaking it by decrementing self, and telling the
115 * cache class to not decref the factory (self) in its deallocator.
116 */
117 self->statement_cache->decref_factory = 0;
118 Py_DECREF(self);
119
120 self->inTransaction = 0;
121 self->detect_types = detect_types;
122 self->timeout = timeout;
123 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
124
125 self->thread_ident = PyThread_get_thread_ident();
126 self->check_same_thread = check_same_thread;
127
128 self->function_pinboard = PyDict_New();
129 if (!self->function_pinboard) {
130 return -1;
131 }
132
133 self->collations = PyDict_New();
134 if (!self->collations) {
135 return -1;
136 }
137
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000138 self->Warning = pysqlite_Warning;
139 self->Error = pysqlite_Error;
140 self->InterfaceError = pysqlite_InterfaceError;
141 self->DatabaseError = pysqlite_DatabaseError;
142 self->DataError = pysqlite_DataError;
143 self->OperationalError = pysqlite_OperationalError;
144 self->IntegrityError = pysqlite_IntegrityError;
145 self->InternalError = pysqlite_InternalError;
146 self->ProgrammingError = pysqlite_ProgrammingError;
147 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148
149 return 0;
150}
151
Thomas Wouters477c8d52006-05-27 19:21:47 +0000152/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000153void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000155 pysqlite_Node* node;
156 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000157
158 node = self->statement_cache->first;
159
160 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000161 statement = (pysqlite_Statement*)(node->data);
162 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000163 node = node->next;
164 }
165
166 Py_DECREF(self->statement_cache);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000167 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000168 Py_DECREF(self);
169 self->statement_cache->decref_factory = 0;
170}
171
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000172void pysqlite_reset_all_statements(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000174 int i;
175 PyObject* weakref;
176 PyObject* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000177
Thomas Wouters477c8d52006-05-27 19:21:47 +0000178 for (i = 0; i < PyList_Size(self->statements); i++) {
179 weakref = PyList_GetItem(self->statements, i);
180 statement = PyWeakref_GetObject(weakref);
181 if (statement != Py_None) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000182 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000183 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000184 }
185}
186
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000187void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000188{
189 Py_XDECREF(self->statement_cache);
190
191 /* Clean up if user has not called .close() explicitly. */
192 if (self->db) {
193 Py_BEGIN_ALLOW_THREADS
194 sqlite3_close(self->db);
195 Py_END_ALLOW_THREADS
196 }
197
198 if (self->begin_statement) {
199 PyMem_Free(self->begin_statement);
200 }
201 Py_XDECREF(self->isolation_level);
202 Py_XDECREF(self->function_pinboard);
203 Py_XDECREF(self->row_factory);
204 Py_XDECREF(self->text_factory);
205 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000206 Py_XDECREF(self->statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000208 Py_Type(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209}
210
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000211PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212{
213 static char *kwlist[] = {"factory", NULL, NULL};
214 PyObject* factory = NULL;
215 PyObject* cursor;
216
217
218 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
219 &factory)) {
220 return NULL;
221 }
222
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000223 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000224 return NULL;
225 }
226
227 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000228 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229 }
230
231 cursor = PyObject_CallFunction(factory, "O", self);
232
233 if (cursor && self->row_factory != Py_None) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000234 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000235 Py_INCREF(self->row_factory);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000236 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237 }
238
239 return cursor;
240}
241
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000242PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243{
244 int rc;
245
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000246 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247 return NULL;
248 }
249
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000250 pysqlite_flush_statement_cache(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000251
252 if (self->db) {
253 Py_BEGIN_ALLOW_THREADS
254 rc = sqlite3_close(self->db);
255 Py_END_ALLOW_THREADS
256
257 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000258 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259 return NULL;
260 } else {
261 self->db = NULL;
262 }
263 }
264
265 Py_INCREF(Py_None);
266 return Py_None;
267}
268
269/*
270 * Checks if a connection object is usable (i. e. not closed).
271 *
272 * 0 => error; 1 => ok
273 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000274int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000275{
276 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000277 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000278 return 0;
279 } else {
280 return 1;
281 }
282}
283
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000284PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285{
286 int rc;
287 const char* tail;
288 sqlite3_stmt* statement;
289
290 Py_BEGIN_ALLOW_THREADS
291 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
292 Py_END_ALLOW_THREADS
293
294 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000295 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296 goto error;
297 }
298
299 rc = _sqlite_step_with_busyhandler(statement, self);
300 if (rc == SQLITE_DONE) {
301 self->inTransaction = 1;
302 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000303 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304 }
305
306 Py_BEGIN_ALLOW_THREADS
307 rc = sqlite3_finalize(statement);
308 Py_END_ALLOW_THREADS
309
310 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000311 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312 }
313
314error:
315 if (PyErr_Occurred()) {
316 return NULL;
317 } else {
318 Py_INCREF(Py_None);
319 return Py_None;
320 }
321}
322
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000323PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324{
325 int rc;
326 const char* tail;
327 sqlite3_stmt* statement;
328
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000329 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 return NULL;
331 }
332
333 if (self->inTransaction) {
334 Py_BEGIN_ALLOW_THREADS
335 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
336 Py_END_ALLOW_THREADS
337 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000338 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 goto error;
340 }
341
342 rc = _sqlite_step_with_busyhandler(statement, self);
343 if (rc == SQLITE_DONE) {
344 self->inTransaction = 0;
345 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000346 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347 }
348
349 Py_BEGIN_ALLOW_THREADS
350 rc = sqlite3_finalize(statement);
351 Py_END_ALLOW_THREADS
352 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000353 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000354 }
355
356 }
357
358error:
359 if (PyErr_Occurred()) {
360 return NULL;
361 } else {
362 Py_INCREF(Py_None);
363 return Py_None;
364 }
365}
366
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000367PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368{
369 int rc;
370 const char* tail;
371 sqlite3_stmt* statement;
372
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000373 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374 return NULL;
375 }
376
377 if (self->inTransaction) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000378 pysqlite_reset_all_statements(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379
380 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000381 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 Py_END_ALLOW_THREADS
383 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000384 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 goto error;
386 }
387
388 rc = _sqlite_step_with_busyhandler(statement, self);
389 if (rc == SQLITE_DONE) {
390 self->inTransaction = 0;
391 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000392 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000393 }
394
395 Py_BEGIN_ALLOW_THREADS
396 rc = sqlite3_finalize(statement);
397 Py_END_ALLOW_THREADS
398 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000399 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 }
401
402 }
403
404error:
405 if (PyErr_Occurred()) {
406 return NULL;
407 } else {
408 Py_INCREF(Py_None);
409 return Py_None;
410 }
411}
412
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000413void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000414{
415 long longval;
416 const char* buffer;
417 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000418
Thomas Wouters477c8d52006-05-27 19:21:47 +0000419 if ((!py_val) || PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420 sqlite3_result_null(context);
421 } else if (py_val == Py_None) {
422 sqlite3_result_null(context);
423 } else if (PyInt_Check(py_val)) {
424 longval = PyInt_AsLong(py_val);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425 sqlite3_result_int64(context, (PY_LONG_LONG)longval);
426 } else if (PyFloat_Check(py_val)) {
427 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000428 } else if (PyString_Check(py_val)) {
429 sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
430 } else if (PyUnicode_Check(py_val)) {
431 sqlite3_result_text(context, PyUnicode_AsString(py_val), -1, SQLITE_TRANSIENT);
432 } else if (PyObject_CheckBuffer(py_val)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000433 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
434 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000435 } else {
436 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000438 } else {
439 /* TODO: raise error */
440 }
441}
442
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000443PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000444{
445 PyObject* args;
446 int i;
447 sqlite3_value* cur_value;
448 PyObject* cur_py_value;
449 const char* val_str;
450 PY_LONG_LONG val_int;
451 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000452
453 args = PyTuple_New(argc);
454 if (!args) {
455 return NULL;
456 }
457
458 for (i = 0; i < argc; i++) {
459 cur_value = argv[i];
460 switch (sqlite3_value_type(argv[i])) {
461 case SQLITE_INTEGER:
462 val_int = sqlite3_value_int64(cur_value);
463 cur_py_value = PyInt_FromLong((long)val_int);
464 break;
465 case SQLITE_FLOAT:
466 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
467 break;
468 case SQLITE_TEXT:
469 val_str = (const char*)sqlite3_value_text(cur_value);
470 cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
471 /* TODO: have a way to show errors here */
472 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000473 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474 Py_INCREF(Py_None);
475 cur_py_value = Py_None;
476 }
477 break;
478 case SQLITE_BLOB:
479 buflen = sqlite3_value_bytes(cur_value);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000480 cur_py_value = PyBytes_FromStringAndSize(
481 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000482 break;
483 case SQLITE_NULL:
484 default:
485 Py_INCREF(Py_None);
486 cur_py_value = Py_None;
487 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000488
489 if (!cur_py_value) {
490 Py_DECREF(args);
491 return NULL;
492 }
493
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000494 PyTuple_SetItem(args, i, cur_py_value);
495
496 }
497
498 return args;
499}
500
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000501void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502{
503 PyObject* args;
504 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000505 PyObject* py_retval = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000506
507 PyGILState_STATE threadstate;
508
509 threadstate = PyGILState_Ensure();
510
511 py_func = (PyObject*)sqlite3_user_data(context);
512
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000513 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000514 if (args) {
515 py_retval = PyObject_CallObject(py_func, args);
516 Py_DECREF(args);
517 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000518
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000519 if (py_retval) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000520 _pysqlite_set_result(context, py_retval);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000521 Py_DECREF(py_retval);
522 } else {
523 if (_enable_callback_tracebacks) {
524 PyErr_Print();
525 } else {
526 PyErr_Clear();
527 }
528 _sqlite3_result_error(context, "user-defined function raised exception", -1);
529 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000530
531 PyGILState_Release(threadstate);
532}
533
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000534static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535{
536 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000537 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538 PyObject* aggregate_class;
539 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000540 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541
542 PyGILState_STATE threadstate;
543
544 threadstate = PyGILState_Ensure();
545
546 aggregate_class = (PyObject*)sqlite3_user_data(context);
547
548 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
549
550 if (*aggregate_instance == 0) {
551 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
552
Thomas Wouters477c8d52006-05-27 19:21:47 +0000553 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000555 if (_enable_callback_tracebacks) {
556 PyErr_Print();
557 } else {
558 PyErr_Clear();
559 }
560 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000561 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562 }
563 }
564
565 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000566 if (!stepmethod) {
567 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000568 }
569
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000570 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000571 if (!args) {
572 goto error;
573 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000574
575 function_result = PyObject_CallObject(stepmethod, args);
576 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000577
Thomas Wouters477c8d52006-05-27 19:21:47 +0000578 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000579 if (_enable_callback_tracebacks) {
580 PyErr_Print();
581 } else {
582 PyErr_Clear();
583 }
584 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000585 }
586
Thomas Wouters477c8d52006-05-27 19:21:47 +0000587error:
588 Py_XDECREF(stepmethod);
589 Py_XDECREF(function_result);
590
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000591 PyGILState_Release(threadstate);
592}
593
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000594void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000596 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597 PyObject** aggregate_instance;
598 PyObject* aggregate_class;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000599
600 PyGILState_STATE threadstate;
601
602 threadstate = PyGILState_Ensure();
603
604 aggregate_class = (PyObject*)sqlite3_user_data(context);
605
606 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
607 if (!*aggregate_instance) {
608 /* this branch is executed if there was an exception in the aggregate's
609 * __init__ */
610
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000612 }
613
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
615 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000616 if (_enable_callback_tracebacks) {
617 PyErr_Print();
618 } else {
619 PyErr_Clear();
620 }
621 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
622 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000623 _pysqlite_set_result(context, function_result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624 }
625
Thomas Wouters477c8d52006-05-27 19:21:47 +0000626error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000627 Py_XDECREF(*aggregate_instance);
628 Py_XDECREF(function_result);
629
630 PyGILState_Release(threadstate);
631}
632
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000633void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000634{
635 PyObject* new_list;
636 PyObject* weakref;
637 int i;
638
639 /* we only need to do this once in a while */
640 if (self->created_statements++ < 200) {
641 return;
642 }
643
644 self->created_statements = 0;
645
646 new_list = PyList_New(0);
647 if (!new_list) {
648 return;
649 }
650
651 for (i = 0; i < PyList_Size(self->statements); i++) {
652 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000653 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 if (PyList_Append(new_list, weakref) != 0) {
655 Py_DECREF(new_list);
656 return;
657 }
658 }
659 }
660
661 Py_DECREF(self->statements);
662 self->statements = new_list;
663}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000664
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000665PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666{
667 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
668
669 PyObject* func;
670 char* name;
671 int narg;
672 int rc;
673
674 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
675 &name, &narg, &func))
676 {
677 return NULL;
678 }
679
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000680 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000681
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 if (rc != SQLITE_OK) {
683 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000684 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000685 return NULL;
686 } else {
687 PyDict_SetItem(self->function_pinboard, func, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688
Thomas Wouters477c8d52006-05-27 19:21:47 +0000689 Py_INCREF(Py_None);
690 return Py_None;
691 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692}
693
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000694PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000695{
696 PyObject* aggregate_class;
697
698 int n_arg;
699 char* name;
700 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
701 int rc;
702
703 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
704 kwlist, &name, &n_arg, &aggregate_class)) {
705 return NULL;
706 }
707
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000708 rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000710 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000711 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000712 return NULL;
713 } else {
714 PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None);
715
716 Py_INCREF(Py_None);
717 return Py_None;
718 }
719}
720
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000721static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000722{
723 PyObject *ret;
724 int rc;
725 PyGILState_STATE gilstate;
726
727 gilstate = PyGILState_Ensure();
728 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
729
730 if (!ret) {
731 if (_enable_callback_tracebacks) {
732 PyErr_Print();
733 } else {
734 PyErr_Clear();
735 }
736
737 rc = SQLITE_DENY;
738 } else {
739 if (PyInt_Check(ret)) {
740 rc = (int)PyInt_AsLong(ret);
741 } else {
742 rc = SQLITE_DENY;
743 }
744 Py_DECREF(ret);
745 }
746
747 PyGILState_Release(gilstate);
748 return rc;
749}
750
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000751PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000752{
753 PyObject* authorizer_cb;
754
755 static char *kwlist[] = { "authorizer_callback", NULL };
756 int rc;
757
758 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
759 kwlist, &authorizer_cb)) {
760 return NULL;
761 }
762
763 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
764
765 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000766 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000767 return NULL;
768 } else {
769 PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None);
770
771 Py_INCREF(Py_None);
772 return Py_None;
773 }
774}
775
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000776int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000777{
778 if (self->check_same_thread) {
779 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000780 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000781 "SQLite objects created in a thread can only be used in that same thread."
782 "The object was created in thread id %ld and this is thread id %ld",
783 self->thread_ident, PyThread_get_thread_ident());
784 return 0;
785 }
786
787 }
788
789 return 1;
790}
791
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000792static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000793{
794 Py_INCREF(self->isolation_level);
795 return self->isolation_level;
796}
797
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000798static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000799{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000800 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000801 return NULL;
802 } else {
803 return Py_BuildValue("i", sqlite3_total_changes(self->db));
804 }
805}
806
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000807static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000808{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000809 PyObject* res;
810 PyObject* begin_statement;
811
812 Py_XDECREF(self->isolation_level);
813
814 if (self->begin_statement) {
815 PyMem_Free(self->begin_statement);
816 self->begin_statement = NULL;
817 }
818
819 if (isolation_level == Py_None) {
820 Py_INCREF(Py_None);
821 self->isolation_level = Py_None;
822
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000823 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824 if (!res) {
825 return -1;
826 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827 Py_DECREF(res);
828
829 self->inTransaction = 0;
830 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000831 const char *statement;
832 Py_ssize_t size;
833
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834 Py_INCREF(isolation_level);
835 self->isolation_level = isolation_level;
836
Neal Norwitzefee9f52007-10-27 02:50:52 +0000837 begin_statement = PyUnicode_FromString("BEGIN ");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838 if (!begin_statement) {
839 return -1;
840 }
Neal Norwitzefee9f52007-10-27 02:50:52 +0000841 PyUnicode_Concat(begin_statement, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000842 if (!begin_statement) {
843 return -1;
844 }
845
Neal Norwitzefee9f52007-10-27 02:50:52 +0000846 statement = PyUnicode_AsStringAndSize(begin_statement, &size);
847 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848 if (!self->begin_statement) {
849 return -1;
850 }
851
Neal Norwitzefee9f52007-10-27 02:50:52 +0000852 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000853 Py_DECREF(begin_statement);
854 }
855
856 return 0;
857}
858
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000859PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000860{
861 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000862 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864 int rc;
865
866 if (!PyArg_ParseTuple(args, "O", &sql)) {
867 return NULL;
868 }
869
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000870 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000871
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000872 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000873 if (!statement) {
874 return NULL;
875 }
876
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000877 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000878
879 if (rc != SQLITE_OK) {
880 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000881 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000883 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000884 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000885 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000886 }
887
888 Py_DECREF(statement);
889 statement = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890 } else {
891 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
892 if (!weakref) {
893 Py_DECREF(statement);
894 statement = 0;
895 goto error;
896 }
897
898 if (PyList_Append(self->statements, weakref) != 0) {
899 Py_DECREF(weakref);
900 statement = 0;
901 goto error;
902 }
903
904 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905 }
906
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000908 return (PyObject*)statement;
909}
910
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000911PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000912{
913 PyObject* cursor = 0;
914 PyObject* result = 0;
915 PyObject* method = 0;
916
917 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
918 if (!cursor) {
919 goto error;
920 }
921
922 method = PyObject_GetAttrString(cursor, "execute");
923 if (!method) {
924 Py_DECREF(cursor);
925 cursor = 0;
926 goto error;
927 }
928
929 result = PyObject_CallObject(method, args);
930 if (!result) {
931 Py_DECREF(cursor);
932 cursor = 0;
933 }
934
935error:
936 Py_XDECREF(result);
937 Py_XDECREF(method);
938
939 return cursor;
940}
941
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000942PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000943{
944 PyObject* cursor = 0;
945 PyObject* result = 0;
946 PyObject* method = 0;
947
948 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
949 if (!cursor) {
950 goto error;
951 }
952
953 method = PyObject_GetAttrString(cursor, "executemany");
954 if (!method) {
955 Py_DECREF(cursor);
956 cursor = 0;
957 goto error;
958 }
959
960 result = PyObject_CallObject(method, args);
961 if (!result) {
962 Py_DECREF(cursor);
963 cursor = 0;
964 }
965
966error:
967 Py_XDECREF(result);
968 Py_XDECREF(method);
969
970 return cursor;
971}
972
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000973PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000974{
975 PyObject* cursor = 0;
976 PyObject* result = 0;
977 PyObject* method = 0;
978
979 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
980 if (!cursor) {
981 goto error;
982 }
983
984 method = PyObject_GetAttrString(cursor, "executescript");
985 if (!method) {
986 Py_DECREF(cursor);
987 cursor = 0;
988 goto error;
989 }
990
991 result = PyObject_CallObject(method, args);
992 if (!result) {
993 Py_DECREF(cursor);
994 cursor = 0;
995 }
996
997error:
998 Py_XDECREF(result);
999 Py_XDECREF(method);
1000
1001 return cursor;
1002}
1003
1004/* ------------------------- COLLATION CODE ------------------------ */
1005
1006static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001007pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001008 void* context,
1009 int text1_length, const void* text1_data,
1010 int text2_length, const void* text2_data)
1011{
1012 PyObject* callback = (PyObject*)context;
1013 PyObject* string1 = 0;
1014 PyObject* string2 = 0;
1015 PyGILState_STATE gilstate;
1016
1017 PyObject* retval = NULL;
1018 int result = 0;
1019
1020 gilstate = PyGILState_Ensure();
1021
1022 if (PyErr_Occurred()) {
1023 goto finally;
1024 }
1025
1026 string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length);
1027 string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length);
1028
1029 if (!string1 || !string2) {
1030 goto finally; /* failed to allocate strings */
1031 }
1032
1033 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1034
1035 if (!retval) {
1036 /* execution failed */
1037 goto finally;
1038 }
1039
1040 result = PyInt_AsLong(retval);
1041 if (PyErr_Occurred()) {
1042 result = 0;
1043 }
1044
1045finally:
1046 Py_XDECREF(string1);
1047 Py_XDECREF(string2);
1048 Py_XDECREF(retval);
1049
1050 PyGILState_Release(gilstate);
1051
1052 return result;
1053}
1054
1055static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001056pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001057{
1058 PyObject* retval = NULL;
1059
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001060 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001061 goto finally;
1062 }
1063
1064 sqlite3_interrupt(self->db);
1065
1066 Py_INCREF(Py_None);
1067 retval = Py_None;
1068
1069finally:
1070 return retval;
1071}
1072
1073static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001074pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001075{
1076 PyObject* callable;
1077 PyObject* uppercase_name = 0;
1078 PyObject* name;
1079 PyObject* retval;
1080 char* chk;
1081 int rc;
1082
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001083 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001084 goto finally;
1085 }
1086
Gerhard Häring6d214562007-08-10 18:15:11 +00001087 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001088 goto finally;
1089 }
1090
1091 uppercase_name = PyObject_CallMethod(name, "upper", "");
1092 if (!uppercase_name) {
1093 goto finally;
1094 }
1095
1096 chk = PyString_AsString(uppercase_name);
1097 while (*chk) {
1098 if ((*chk >= '0' && *chk <= '9')
1099 || (*chk >= 'A' && *chk <= 'Z')
1100 || (*chk == '_'))
1101 {
1102 chk++;
1103 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001104 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001105 goto finally;
1106 }
1107 }
1108
1109 if (callable != Py_None && !PyCallable_Check(callable)) {
1110 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1111 goto finally;
1112 }
1113
1114 if (callable != Py_None) {
1115 PyDict_SetItem(self->collations, uppercase_name, callable);
1116 } else {
1117 PyDict_DelItem(self->collations, uppercase_name);
1118 }
1119
1120 rc = sqlite3_create_collation(self->db,
1121 PyString_AsString(uppercase_name),
1122 SQLITE_UTF8,
1123 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001124 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 if (rc != SQLITE_OK) {
1126 PyDict_DelItem(self->collations, uppercase_name);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001127 _pysqlite_seterror(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001128 goto finally;
1129 }
1130
1131finally:
1132 Py_XDECREF(uppercase_name);
1133
1134 if (PyErr_Occurred()) {
1135 retval = NULL;
1136 } else {
1137 Py_INCREF(Py_None);
1138 retval = Py_None;
1139 }
1140
1141 return retval;
1142}
1143
1144static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001145PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146
1147static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001148 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1149 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001150 {NULL}
1151};
1152
1153static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001154 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001156 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001157 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001158 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001159 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001160 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001161 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001162 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001164 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001165 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001166 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001167 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001168 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001169 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001170 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001171 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001172 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001174 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001175 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001176 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001177 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001178 {NULL, NULL}
1179};
1180
1181static struct PyMemberDef connection_members[] =
1182{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001183 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1184 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1185 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1186 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1187 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1188 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1189 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1190 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1191 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1192 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001193 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1194 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001195 {NULL}
1196};
1197
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001198PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001199 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001200 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001201 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001202 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001203 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001204 0, /* tp_print */
1205 0, /* tp_getattr */
1206 0, /* tp_setattr */
1207 0, /* tp_compare */
1208 0, /* tp_repr */
1209 0, /* tp_as_number */
1210 0, /* tp_as_sequence */
1211 0, /* tp_as_mapping */
1212 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001213 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001214 0, /* tp_str */
1215 0, /* tp_getattro */
1216 0, /* tp_setattro */
1217 0, /* tp_as_buffer */
1218 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1219 connection_doc, /* tp_doc */
1220 0, /* tp_traverse */
1221 0, /* tp_clear */
1222 0, /* tp_richcompare */
1223 0, /* tp_weaklistoffset */
1224 0, /* tp_iter */
1225 0, /* tp_iternext */
1226 connection_methods, /* tp_methods */
1227 connection_members, /* tp_members */
1228 connection_getset, /* tp_getset */
1229 0, /* tp_base */
1230 0, /* tp_dict */
1231 0, /* tp_descr_get */
1232 0, /* tp_descr_set */
1233 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001234 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001235 0, /* tp_alloc */
1236 0, /* tp_new */
1237 0 /* tp_free */
1238};
1239
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001240extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001241{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001242 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1243 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001244}