815595e2db0bb58fece03a6faf1b240bfe524351
[joel/debian/python-pysqlite2.git] / src / connection.c
1 /* connection.c - the connection type
2  *
3  * Copyright (C) 2004-2007 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
35 #define ACTION_FINALIZE 1
36 #define ACTION_RESET 2
37
38 static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
39
40
41 void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
42 {
43     /* in older SQLite versions, calling sqlite3_result_error in callbacks
44      * triggers a bug in SQLite that leads either to irritating results or
45      * segfaults, depending on the SQLite version */
46 #if SQLITE_VERSION_NUMBER >= 3003003
47     sqlite3_result_error(ctx, errmsg, len);
48 #else
49     PyErr_SetString(pysqlite_OperationalError, errmsg);
50 #endif
51 }
52
53 int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
54 {
55     static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
56
57     PyObject* database;
58     int detect_types = 0;
59     PyObject* isolation_level = NULL;
60     PyObject* factory = NULL;
61     int check_same_thread = 1;
62     int cached_statements = 100;
63     double timeout = 5.0;
64     int rc;
65     PyObject* class_attr = NULL;
66     PyObject* class_attr_str = NULL;
67     int is_apsw_connection = 0;
68
69     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOi", kwlist,
70                                      &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
71     {
72         return -1;
73     }
74
75     self->begin_statement = NULL;
76
77     self->statement_cache = NULL;
78     self->statements = NULL;
79
80     Py_INCREF(Py_None);
81     self->row_factory = Py_None;
82
83     Py_INCREF(&PyUnicode_Type);
84     self->text_factory = (PyObject*)&PyUnicode_Type;
85
86     if (PyString_Check(database)) {
87         Py_BEGIN_ALLOW_THREADS
88         rc = sqlite3_open(PyString_AsString(database), &self->db);
89         Py_END_ALLOW_THREADS
90
91         if (rc != SQLITE_OK) {
92             _pysqlite_seterror(self->db, NULL);
93             return -1;
94         }
95     } else {
96         /* Create a pysqlite connection from a APSW connection */
97         class_attr = PyObject_GetAttrString(database, "__class__");
98         if (class_attr) {
99             class_attr_str = PyObject_Str(class_attr);
100             if (class_attr_str) {
101                 if (strcmp(PyString_AsString(class_attr_str), "<type 'apsw.Connection'>") == 0) {
102                     /* In the APSW Connection object, the first entry after
103                      * PyObject_HEAD is the sqlite3* we want to get hold of.
104                      * Luckily, this is the same layout as we have in our
105                      * pysqlite_Connection */
106                     self->db = ((pysqlite_Connection*)database)->db;
107
108                     Py_INCREF(database);
109                     self->apsw_connection = database;
110                     is_apsw_connection = 1;
111                 }
112             }
113         }
114         Py_XDECREF(class_attr_str);
115         Py_XDECREF(class_attr);
116
117         if (!is_apsw_connection) {
118             PyErr_SetString(PyExc_ValueError, "database parameter must be string or APSW Connection object");
119             return -1;
120         }
121     }
122
123     if (!isolation_level) {
124         isolation_level = PyString_FromString("");
125         if (!isolation_level) {
126             return -1;
127         }
128     } else {
129         Py_INCREF(isolation_level);
130     }
131     self->isolation_level = NULL;
132     pysqlite_connection_set_isolation_level(self, isolation_level);
133     Py_DECREF(isolation_level);
134
135     self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
136     if (PyErr_Occurred()) {
137         return -1;
138     }
139
140     self->statements = PyList_New(0);
141     if (!self->statements) {
142         return -1;
143     }
144     self->created_statements = 0;
145
146     /* By default, the Cache class INCREFs the factory in its initializer, and
147      * decrefs it in its deallocator method. Since this would create a circular
148      * reference here, we're breaking it by decrementing self, and telling the
149      * cache class to not decref the factory (self) in its deallocator.
150      */
151     self->statement_cache->decref_factory = 0;
152     Py_DECREF(self);
153
154     self->inTransaction = 0;
155     self->detect_types = detect_types;
156     self->timeout = timeout;
157     (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
158
159     self->thread_ident = PyThread_get_thread_ident();
160     self->check_same_thread = check_same_thread;
161
162     self->function_pinboard = PyDict_New();
163     if (!self->function_pinboard) {
164         return -1;
165     }
166
167     self->collations = PyDict_New();
168     if (!self->collations) {
169         return -1;
170     }
171
172     self->Warning               = pysqlite_Warning;
173     self->Error                 = pysqlite_Error;
174     self->InterfaceError        = pysqlite_InterfaceError;
175     self->DatabaseError         = pysqlite_DatabaseError;
176     self->DataError             = pysqlite_DataError;
177     self->OperationalError      = pysqlite_OperationalError;
178     self->IntegrityError        = pysqlite_IntegrityError;
179     self->InternalError         = pysqlite_InternalError;
180     self->ProgrammingError      = pysqlite_ProgrammingError;
181     self->NotSupportedError     = pysqlite_NotSupportedError;
182
183     return 0;
184 }
185
186 /* Empty the entire statement cache of this connection */
187 void pysqlite_flush_statement_cache(pysqlite_Connection* self)
188 {
189     pysqlite_Node* node;
190     pysqlite_Statement* statement;
191
192     node = self->statement_cache->first;
193
194     while (node) {
195         statement = (pysqlite_Statement*)(node->data);
196         (void)pysqlite_statement_finalize(statement);
197         node = node->next;
198     }
199
200     Py_DECREF(self->statement_cache);
201     self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
202     Py_DECREF(self);
203     self->statement_cache->decref_factory = 0;
204 }
205
206 /* action in (ACTION_RESET, ACTION_FINALIZE) */
207 void pysqlite_do_all_statements(pysqlite_Connection* self, int action)
208 {
209     int i;
210     PyObject* weakref;
211     PyObject* statement;
212
213     for (i = 0; i < PyList_Size(self->statements); i++) {
214         weakref = PyList_GetItem(self->statements, i);
215         statement = PyWeakref_GetObject(weakref);
216         if (statement != Py_None) {
217             if (action == ACTION_RESET) {
218                 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
219             } else {
220                 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
221             }
222         }
223     }
224 }
225
226 void pysqlite_connection_dealloc(pysqlite_Connection* self)
227 {
228     PyObject* ret = NULL;
229
230     Py_XDECREF(self->statement_cache);
231
232     /* Clean up if user has not called .close() explicitly. */
233     if (self->db) {
234         Py_BEGIN_ALLOW_THREADS
235         sqlite3_close(self->db);
236         Py_END_ALLOW_THREADS
237     } else if (self->apsw_connection) {
238         ret = PyObject_CallMethod(self->apsw_connection, "close", "");
239         Py_XDECREF(ret);
240         Py_XDECREF(self->apsw_connection);
241     }
242
243     if (self->begin_statement) {
244         PyMem_Free(self->begin_statement);
245     }
246     Py_XDECREF(self->isolation_level);
247     Py_XDECREF(self->function_pinboard);
248     Py_XDECREF(self->row_factory);
249     Py_XDECREF(self->text_factory);
250     Py_XDECREF(self->collations);
251     Py_XDECREF(self->statements);
252
253     self->ob_type->tp_free((PyObject*)self);
254 }
255
256 PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
257 {
258     static char *kwlist[] = {"factory", NULL, NULL};
259     PyObject* factory = NULL;
260     PyObject* cursor;
261
262
263     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
264                                      &factory)) {
265         return NULL;
266     }
267
268     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
269         return NULL;
270     }
271
272     if (factory == NULL) {
273         factory = (PyObject*)&pysqlite_CursorType;
274     }
275
276     cursor = PyObject_CallFunction(factory, "O", self);
277
278     if (cursor && self->row_factory != Py_None) {
279         Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
280         Py_INCREF(self->row_factory);
281         ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
282     }
283
284     return cursor;
285 }
286
287 PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
288 {
289     PyObject* ret;
290     int rc;
291
292     if (!pysqlite_check_thread(self)) {
293         return NULL;
294     }
295
296     pysqlite_do_all_statements(self, ACTION_FINALIZE);
297
298     if (self->db) {
299         if (self->apsw_connection) {
300             ret = PyObject_CallMethod(self->apsw_connection, "close", "");
301             Py_XDECREF(ret);
302             Py_XDECREF(self->apsw_connection);
303             self->apsw_connection = NULL;
304             self->db = NULL;
305         } else {
306             Py_BEGIN_ALLOW_THREADS
307             rc = sqlite3_close(self->db);
308             Py_END_ALLOW_THREADS
309
310             if (rc != SQLITE_OK) {
311                 _pysqlite_seterror(self->db, NULL);
312                 return NULL;
313             } else {
314                 self->db = NULL;
315             }
316         }
317     }
318
319     Py_INCREF(Py_None);
320     return Py_None;
321 }
322
323 /*
324  * Checks if a connection object is usable (i. e. not closed).
325  *
326  * 0 => error; 1 => ok
327  */
328 int pysqlite_check_connection(pysqlite_Connection* con)
329 {
330     if (!con->db) {
331         PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
332         return 0;
333     } else {
334         return 1;
335     }
336 }
337
338 PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
339 {
340     int rc;
341     const char* tail;
342     sqlite3_stmt* statement;
343
344     Py_BEGIN_ALLOW_THREADS
345     rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
346     Py_END_ALLOW_THREADS
347
348     if (rc != SQLITE_OK) {
349         _pysqlite_seterror(self->db, statement);
350         goto error;
351     }
352
353     rc = _sqlite_step_with_busyhandler(statement, self);
354     if (rc == SQLITE_DONE) {
355         self->inTransaction = 1;
356     } else {
357         _pysqlite_seterror(self->db, statement);
358     }
359
360     Py_BEGIN_ALLOW_THREADS
361     rc = sqlite3_finalize(statement);
362     Py_END_ALLOW_THREADS
363
364     if (rc != SQLITE_OK && !PyErr_Occurred()) {
365         _pysqlite_seterror(self->db, NULL);
366     }
367
368 error:
369     if (PyErr_Occurred()) {
370         return NULL;
371     } else {
372         Py_INCREF(Py_None);
373         return Py_None;
374     }
375 }
376
377 PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
378 {
379     int rc;
380     const char* tail;
381     sqlite3_stmt* statement;
382
383     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
384         return NULL;
385     }
386
387     if (self->inTransaction) {
388         Py_BEGIN_ALLOW_THREADS
389         rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
390         Py_END_ALLOW_THREADS
391         if (rc != SQLITE_OK) {
392             _pysqlite_seterror(self->db, NULL);
393             goto error;
394         }
395
396         rc = _sqlite_step_with_busyhandler(statement, self);
397         if (rc == SQLITE_DONE) {
398             self->inTransaction = 0;
399         } else {
400             _pysqlite_seterror(self->db, statement);
401         }
402
403         Py_BEGIN_ALLOW_THREADS
404         rc = sqlite3_finalize(statement);
405         Py_END_ALLOW_THREADS
406         if (rc != SQLITE_OK && !PyErr_Occurred()) {
407             _pysqlite_seterror(self->db, NULL);
408         }
409
410     }
411
412 error:
413     if (PyErr_Occurred()) {
414         return NULL;
415     } else {
416         Py_INCREF(Py_None);
417         return Py_None;
418     }
419 }
420
421 PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
422 {
423     int rc;
424     const char* tail;
425     sqlite3_stmt* statement;
426
427     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
428         return NULL;
429     }
430
431     if (self->inTransaction) {
432         pysqlite_do_all_statements(self, ACTION_RESET);
433
434         Py_BEGIN_ALLOW_THREADS
435         rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
436         Py_END_ALLOW_THREADS
437         if (rc != SQLITE_OK) {
438             _pysqlite_seterror(self->db, NULL);
439             goto error;
440         }
441
442         rc = _sqlite_step_with_busyhandler(statement, self);
443         if (rc == SQLITE_DONE) {
444             self->inTransaction = 0;
445         } else {
446             _pysqlite_seterror(self->db, statement);
447         }
448
449         Py_BEGIN_ALLOW_THREADS
450         rc = sqlite3_finalize(statement);
451         Py_END_ALLOW_THREADS
452         if (rc != SQLITE_OK && !PyErr_Occurred()) {
453             _pysqlite_seterror(self->db, NULL);
454         }
455
456     }
457
458 error:
459     if (PyErr_Occurred()) {
460         return NULL;
461     } else {
462         Py_INCREF(Py_None);
463         return Py_None;
464     }
465 }
466
467 void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
468 {
469     long longval;
470     const char* buffer;
471     Py_ssize_t buflen;
472     PyObject* stringval;
473
474     if ((!py_val) || PyErr_Occurred()) {
475         sqlite3_result_null(context);
476     } else if (py_val == Py_None) {
477         sqlite3_result_null(context);
478     } else if (PyInt_Check(py_val)) {
479         longval = PyInt_AsLong(py_val);
480         sqlite3_result_int64(context, (PY_LONG_LONG)longval);
481     } else if (PyFloat_Check(py_val)) {
482         sqlite3_result_double(context, PyFloat_AsDouble(py_val));
483     } else if (PyBuffer_Check(py_val)) {
484         if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
485             PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
486         } else {
487             sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
488         }
489     } else if (PyString_Check(py_val)) {
490         sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
491     } else if (PyUnicode_Check(py_val)) {
492         stringval = PyUnicode_AsUTF8String(py_val);
493         if (stringval) {
494             sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
495             Py_DECREF(stringval);
496         }
497     } else {
498         /* TODO: raise error */
499     }
500 }
501
502 PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
503 {
504     PyObject* args;
505     int i;
506     sqlite3_value* cur_value;
507     PyObject* cur_py_value;
508     const char* val_str;
509     PY_LONG_LONG val_int;
510     Py_ssize_t buflen;
511     void* raw_buffer;
512
513     args = PyTuple_New(argc);
514     if (!args) {
515         return NULL;
516     }
517
518     for (i = 0; i < argc; i++) {
519         cur_value = argv[i];
520         switch (sqlite3_value_type(argv[i])) {
521             case SQLITE_INTEGER:
522                 val_int = sqlite3_value_int64(cur_value);
523                 cur_py_value = PyInt_FromLong((long)val_int);
524                 break;
525             case SQLITE_FLOAT:
526                 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
527                 break;
528             case SQLITE_TEXT:
529                 val_str = (const char*)sqlite3_value_text(cur_value);
530                 cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
531                 /* TODO: have a way to show errors here */
532                 if (!cur_py_value) {
533                     PyErr_Clear();
534                     Py_INCREF(Py_None);
535                     cur_py_value = Py_None;
536                 }
537                 break;
538             case SQLITE_BLOB:
539                 buflen = sqlite3_value_bytes(cur_value);
540                 cur_py_value = PyBuffer_New(buflen);
541                 if (!cur_py_value) {
542                     break;
543                 }
544                 if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) {
545                     Py_DECREF(cur_py_value);
546                     cur_py_value = NULL;
547                     break;
548                 }
549                 memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen);
550                 break;
551             case SQLITE_NULL:
552             default:
553                 Py_INCREF(Py_None);
554                 cur_py_value = Py_None;
555         }
556
557         if (!cur_py_value) {
558             Py_DECREF(args);
559             return NULL;
560         }
561
562         PyTuple_SetItem(args, i, cur_py_value);
563
564     }
565
566     return args;
567 }
568
569 void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
570 {
571     PyObject* args;
572     PyObject* py_func;
573     PyObject* py_retval = NULL;
574
575     PyGILState_STATE threadstate;
576
577     threadstate = PyGILState_Ensure();
578
579     py_func = (PyObject*)sqlite3_user_data(context);
580
581     args = _pysqlite_build_py_params(context, argc, argv);
582     if (args) {
583         py_retval = PyObject_CallObject(py_func, args);
584         Py_DECREF(args);
585     }
586
587     if (py_retval) {
588         _pysqlite_set_result(context, py_retval);
589         Py_DECREF(py_retval);
590     } else {
591         if (_enable_callback_tracebacks) {
592             PyErr_Print();
593         } else {
594             PyErr_Clear();
595         }
596         _sqlite3_result_error(context, "user-defined function raised exception", -1);
597     }
598
599     PyGILState_Release(threadstate);
600 }
601
602 static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
603 {
604     PyObject* args;
605     PyObject* function_result = NULL;
606     PyObject* aggregate_class;
607     PyObject** aggregate_instance;
608     PyObject* stepmethod = NULL;
609
610     PyGILState_STATE threadstate;
611
612     threadstate = PyGILState_Ensure();
613
614     aggregate_class = (PyObject*)sqlite3_user_data(context);
615
616     aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
617
618     if (*aggregate_instance == 0) {
619         *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
620
621         if (PyErr_Occurred()) {
622             *aggregate_instance = 0;
623             if (_enable_callback_tracebacks) {
624                 PyErr_Print();
625             } else {
626                 PyErr_Clear();
627             }
628             _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
629             goto error;
630         }
631     }
632
633     stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
634     if (!stepmethod) {
635         goto error;
636     }
637
638     args = _pysqlite_build_py_params(context, argc, params);
639     if (!args) {
640         goto error;
641     }
642
643     function_result = PyObject_CallObject(stepmethod, args);
644     Py_DECREF(args);
645
646     if (!function_result) {
647         if (_enable_callback_tracebacks) {
648             PyErr_Print();
649         } else {
650             PyErr_Clear();
651         }
652         _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
653     }
654
655 error:
656     Py_XDECREF(stepmethod);
657     Py_XDECREF(function_result);
658
659     PyGILState_Release(threadstate);
660 }
661
662 void _pysqlite_final_callback(sqlite3_context* context)
663 {
664     PyObject* function_result = NULL;
665     PyObject** aggregate_instance;
666     PyObject* aggregate_class;
667
668     PyGILState_STATE threadstate;
669
670     threadstate = PyGILState_Ensure();
671
672     aggregate_class = (PyObject*)sqlite3_user_data(context);
673
674     aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
675     if (!*aggregate_instance) {
676         /* this branch is executed if there was an exception in the aggregate's
677          * __init__ */
678
679         goto error;
680     }
681
682     function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
683     if (!function_result) {
684         if (_enable_callback_tracebacks) {
685             PyErr_Print();
686         } else {
687             PyErr_Clear();
688         }
689         _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
690     } else {
691         _pysqlite_set_result(context, function_result);
692     }
693
694 error:
695     Py_XDECREF(*aggregate_instance);
696     Py_XDECREF(function_result);
697
698     PyGILState_Release(threadstate);
699 }
700
701 void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
702 {
703     PyObject* new_list;
704     PyObject* weakref;
705     int i;
706
707     /* we only need to do this once in a while */
708     if (self->created_statements++ < 200) {
709         return;
710     }
711
712     self->created_statements = 0;
713
714     new_list = PyList_New(0);
715     if (!new_list) {
716         return;
717     }
718
719     for (i = 0; i < PyList_Size(self->statements); i++) {
720         weakref = PyList_GetItem(self->statements, i);
721         if (PyWeakref_GetObject(weakref) != Py_None) {
722             if (PyList_Append(new_list, weakref) != 0) {
723                 Py_DECREF(new_list);
724                 return;
725             }
726         }
727     }
728
729     Py_DECREF(self->statements);
730     self->statements = new_list;
731 }
732
733 PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
734 {
735     static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
736
737     PyObject* func;
738     char* name;
739     int narg;
740     int rc;
741
742     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
743                                      &name, &narg, &func))
744     {
745         return NULL;
746     }
747
748     rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
749
750     if (rc != SQLITE_OK) {
751         /* Workaround for SQLite bug: no error code or string is available here */
752         PyErr_SetString(pysqlite_OperationalError, "Error creating function");
753         return NULL;
754     } else {
755         PyDict_SetItem(self->function_pinboard, func, Py_None);
756
757         Py_INCREF(Py_None);
758         return Py_None;
759     }
760 }
761
762 PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
763 {
764     PyObject* aggregate_class;
765
766     int n_arg;
767     char* name;
768     static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
769     int rc;
770
771     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
772                                       kwlist, &name, &n_arg, &aggregate_class)) {
773         return NULL;
774     }
775
776     rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback);
777     if (rc != SQLITE_OK) {
778         /* Workaround for SQLite bug: no error code or string is available here */
779         PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
780         return NULL;
781     } else {
782         PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None);
783
784         Py_INCREF(Py_None);
785         return Py_None;
786     }
787 }
788
789 static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
790 {
791     PyObject *ret;
792     int rc;
793     PyGILState_STATE gilstate;
794
795     gilstate = PyGILState_Ensure();
796     ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
797
798     if (!ret) {
799         if (_enable_callback_tracebacks) {
800             PyErr_Print();
801         } else {
802             PyErr_Clear();
803         }
804
805         rc = SQLITE_DENY;
806     } else {
807         if (PyInt_Check(ret)) {
808             rc = (int)PyInt_AsLong(ret);
809         } else {
810             rc = SQLITE_DENY;
811         }
812         Py_DECREF(ret);
813     }
814
815     PyGILState_Release(gilstate);
816     return rc;
817 }
818
819 static int _progress_handler(void* user_arg)
820 {
821     int rc;
822     PyObject *ret;
823     PyGILState_STATE gilstate;
824
825     gilstate = PyGILState_Ensure();
826     ret = PyObject_CallFunction((PyObject*)user_arg, "");
827
828     if (!ret) {
829         if (_enable_callback_tracebacks) {
830             PyErr_Print();
831         } else {
832             PyErr_Clear();
833         }
834
835         /* abort query if error occured */
836         rc = 1; 
837     } else {
838         rc = (int)PyObject_IsTrue(ret);
839     }
840
841     Py_DECREF(ret);
842     PyGILState_Release(gilstate);
843     return rc;
844 }
845
846 PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
847 {
848     PyObject* authorizer_cb;
849
850     static char *kwlist[] = { "authorizer_callback", NULL };
851     int rc;
852
853     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
854                                       kwlist, &authorizer_cb)) {
855         return NULL;
856     }
857
858     rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
859
860     if (rc != SQLITE_OK) {
861         PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
862         return NULL;
863     } else {
864         PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None);
865
866         Py_INCREF(Py_None);
867         return Py_None;
868     }
869 }
870
871 PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
872 {
873     PyObject* progress_handler;
874     int n;
875
876     static char *kwlist[] = { "progress_handler", "n", NULL };
877
878     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
879                                       kwlist, &progress_handler, &n)) {
880         return NULL;
881     }
882
883     if (progress_handler == Py_None) {
884         /* None clears the progress handler previously set */
885         sqlite3_progress_handler(self->db, 0, 0, (void*)0);
886     } else {
887         sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
888         PyDict_SetItem(self->function_pinboard, progress_handler, Py_None);
889     }
890
891     Py_INCREF(Py_None);
892     return Py_None;
893 }
894
895 int pysqlite_check_thread(pysqlite_Connection* self)
896 {
897     if (self->check_same_thread) {
898         if (PyThread_get_thread_ident() != self->thread_ident) {
899             PyErr_Format(pysqlite_ProgrammingError,
900                         "SQLite objects created in a thread can only be used in that same thread."
901                         "The object was created in thread id %ld and this is thread id %ld",
902                         self->thread_ident, PyThread_get_thread_ident());
903             return 0;
904         }
905
906     }
907
908     return 1;
909 }
910
911 static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
912 {
913     Py_INCREF(self->isolation_level);
914     return self->isolation_level;
915 }
916
917 static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
918 {
919     if (!pysqlite_check_connection(self)) {
920         return NULL;
921     } else {
922         return Py_BuildValue("i", sqlite3_total_changes(self->db));
923     }
924 }
925
926 static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
927 {
928     PyObject* res;
929     PyObject* begin_statement;
930
931     Py_XDECREF(self->isolation_level);
932
933     if (self->begin_statement) {
934         PyMem_Free(self->begin_statement);
935         self->begin_statement = NULL;
936     }
937
938     if (isolation_level == Py_None) {
939         Py_INCREF(Py_None);
940         self->isolation_level = Py_None;
941
942         res = pysqlite_connection_commit(self, NULL);
943         if (!res) {
944             return -1;
945         }
946         Py_DECREF(res);
947
948         self->inTransaction = 0;
949     } else {
950         Py_INCREF(isolation_level);
951         self->isolation_level = isolation_level;
952
953         begin_statement = PyString_FromString("BEGIN ");
954         if (!begin_statement) {
955             return -1;
956         }
957         PyString_Concat(&begin_statement, isolation_level);
958         if (!begin_statement) {
959             return -1;
960         }
961
962         self->begin_statement = PyMem_Malloc(PyString_Size(begin_statement) + 2);
963         if (!self->begin_statement) {
964             return -1;
965         }
966
967         strcpy(self->begin_statement, PyString_AsString(begin_statement));
968         Py_DECREF(begin_statement);
969     }
970
971     return 0;
972 }
973
974 PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
975 {
976     PyObject* sql;
977     pysqlite_Statement* statement;
978     PyObject* weakref;
979     int rc;
980
981     if (!PyArg_ParseTuple(args, "O", &sql)) {
982         return NULL;
983     }
984
985     _pysqlite_drop_unused_statement_references(self);
986
987     statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
988     if (!statement) {
989         return NULL;
990     }
991
992     rc = pysqlite_statement_create(statement, self, sql);
993
994     if (rc != SQLITE_OK) {
995         if (rc == PYSQLITE_TOO_MUCH_SQL) {
996             PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
997         } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
998             PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
999         } else {
1000             (void)pysqlite_statement_reset(statement);
1001             _pysqlite_seterror(self->db, NULL);
1002         }
1003
1004         Py_DECREF(statement);
1005         statement = 0;
1006     } else {
1007         weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1008         if (!weakref) {
1009             Py_DECREF(statement);
1010             statement = 0;
1011             goto error;
1012         }
1013
1014         if (PyList_Append(self->statements, weakref) != 0) {
1015             Py_DECREF(weakref);
1016             statement = 0;
1017             goto error;
1018         }
1019
1020         Py_DECREF(weakref);
1021     }
1022
1023 error:
1024     return (PyObject*)statement;
1025 }
1026
1027 PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1028 {
1029     PyObject* cursor = 0;
1030     PyObject* result = 0;
1031     PyObject* method = 0;
1032
1033     cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1034     if (!cursor) {
1035         goto error;
1036     }
1037
1038     method = PyObject_GetAttrString(cursor, "execute");
1039     if (!method) {
1040         Py_DECREF(cursor);
1041         cursor = 0;
1042         goto error;
1043     }
1044
1045     result = PyObject_CallObject(method, args);
1046     if (!result) {
1047         Py_DECREF(cursor);
1048         cursor = 0;
1049     }
1050
1051 error:
1052     Py_XDECREF(result);
1053     Py_XDECREF(method);
1054
1055     return cursor;
1056 }
1057
1058 PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1059 {
1060     PyObject* cursor = 0;
1061     PyObject* result = 0;
1062     PyObject* method = 0;
1063
1064     cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1065     if (!cursor) {
1066         goto error;
1067     }
1068
1069     method = PyObject_GetAttrString(cursor, "executemany");
1070     if (!method) {
1071         Py_DECREF(cursor);
1072         cursor = 0;
1073         goto error;
1074     }
1075
1076     result = PyObject_CallObject(method, args);
1077     if (!result) {
1078         Py_DECREF(cursor);
1079         cursor = 0;
1080     }
1081
1082 error:
1083     Py_XDECREF(result);
1084     Py_XDECREF(method);
1085
1086     return cursor;
1087 }
1088
1089 PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1090 {
1091     PyObject* cursor = 0;
1092     PyObject* result = 0;
1093     PyObject* method = 0;
1094
1095     cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1096     if (!cursor) {
1097         goto error;
1098     }
1099
1100     method = PyObject_GetAttrString(cursor, "executescript");
1101     if (!method) {
1102         Py_DECREF(cursor);
1103         cursor = 0;
1104         goto error;
1105     }
1106
1107     result = PyObject_CallObject(method, args);
1108     if (!result) {
1109         Py_DECREF(cursor);
1110         cursor = 0;
1111     }
1112
1113 error:
1114     Py_XDECREF(result);
1115     Py_XDECREF(method);
1116
1117     return cursor;
1118 }
1119
1120 /* ------------------------- COLLATION CODE ------------------------ */
1121
1122 static int
1123 pysqlite_collation_callback(
1124         void* context,
1125         int text1_length, const void* text1_data,
1126         int text2_length, const void* text2_data)
1127 {
1128     PyObject* callback = (PyObject*)context;
1129     PyObject* string1 = 0;
1130     PyObject* string2 = 0;
1131     PyGILState_STATE gilstate;
1132
1133     PyObject* retval = NULL;
1134     int result = 0;
1135
1136     gilstate = PyGILState_Ensure();
1137
1138     if (PyErr_Occurred()) {
1139         goto finally;
1140     }
1141
1142     string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length);
1143     string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length);
1144
1145     if (!string1 || !string2) {
1146         goto finally; /* failed to allocate strings */
1147     }
1148
1149     retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1150
1151     if (!retval) {
1152         /* execution failed */
1153         goto finally;
1154     }
1155
1156     result = PyInt_AsLong(retval);
1157     if (PyErr_Occurred()) {
1158         result = 0;
1159     }
1160
1161 finally:
1162     Py_XDECREF(string1);
1163     Py_XDECREF(string2);
1164     Py_XDECREF(retval);
1165
1166     PyGILState_Release(gilstate);
1167
1168     return result;
1169 }
1170
1171 static PyObject *
1172 pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
1173 {
1174     PyObject* retval = NULL;
1175
1176     if (!pysqlite_check_connection(self)) {
1177         goto finally;
1178     }
1179
1180     sqlite3_interrupt(self->db);
1181
1182     Py_INCREF(Py_None);
1183     retval = Py_None;
1184
1185 finally:
1186     return retval;
1187 }
1188
1189 static PyObject *
1190 pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
1191 {
1192     PyObject* callable;
1193     PyObject* uppercase_name = 0;
1194     PyObject* name;
1195     PyObject* retval;
1196     char* chk;
1197     int rc;
1198
1199     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1200         goto finally;
1201     }
1202
1203     if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyString_Type, &name, &callable)) {
1204         goto finally;
1205     }
1206
1207     uppercase_name = PyObject_CallMethod(name, "upper", "");
1208     if (!uppercase_name) {
1209         goto finally;
1210     }
1211
1212     chk = PyString_AsString(uppercase_name);
1213     while (*chk) {
1214         if ((*chk >= '0' && *chk <= '9')
1215          || (*chk >= 'A' && *chk <= 'Z')
1216          || (*chk == '_'))
1217         {
1218             chk++;
1219         } else {
1220             PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
1221             goto finally;
1222         }
1223     }
1224
1225     if (callable != Py_None && !PyCallable_Check(callable)) {
1226         PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1227         goto finally;
1228     }
1229
1230     if (callable != Py_None) {
1231         PyDict_SetItem(self->collations, uppercase_name, callable);
1232     } else {
1233         PyDict_DelItem(self->collations, uppercase_name);
1234     }
1235
1236     rc = sqlite3_create_collation(self->db,
1237                                   PyString_AsString(uppercase_name),
1238                                   SQLITE_UTF8,
1239                                   (callable != Py_None) ? callable : NULL,
1240                                   (callable != Py_None) ? pysqlite_collation_callback : NULL);
1241     if (rc != SQLITE_OK) {
1242         PyDict_DelItem(self->collations, uppercase_name);
1243         _pysqlite_seterror(self->db, NULL);
1244         goto finally;
1245     }
1246
1247 finally:
1248     Py_XDECREF(uppercase_name);
1249
1250     if (PyErr_Occurred()) {
1251         retval = NULL;
1252     } else {
1253         Py_INCREF(Py_None);
1254         retval = Py_None;
1255     }
1256
1257     return retval;
1258 }
1259
1260 /* Called when the connection is used as a context manager. Returns itself as a
1261  * convenience to the caller. */
1262 static PyObject *
1263 pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1264 {
1265     Py_INCREF(self);
1266     return (PyObject*)self;
1267 }
1268
1269 /** Called when the connection is used as a context manager. If there was any
1270  * exception, a rollback takes place; otherwise we commit. */
1271 static PyObject *
1272 pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1273 {
1274     PyObject* exc_type, *exc_value, *exc_tb;
1275     char* method_name;
1276     PyObject* result;
1277
1278     if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1279         return NULL;
1280     }
1281
1282     if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1283         method_name = "commit";
1284     } else {
1285         method_name = "rollback";
1286     }
1287
1288     result = PyObject_CallMethod((PyObject*)self, method_name, "");
1289     if (!result) {
1290         return NULL;
1291     }
1292     Py_DECREF(result);
1293
1294     Py_INCREF(Py_False);
1295     return Py_False;
1296 }
1297
1298 static char connection_doc[] =
1299 PyDoc_STR("SQLite database connection object.");
1300
1301 static PyGetSetDef connection_getset[] = {
1302     {"isolation_level",  (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1303     {"total_changes",  (getter)pysqlite_connection_get_total_changes, (setter)0},
1304     {NULL}
1305 };
1306
1307 static PyMethodDef connection_methods[] = {
1308     {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
1309         PyDoc_STR("Return a cursor for the connection.")},
1310     {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
1311         PyDoc_STR("Closes the connection.")},
1312     {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
1313         PyDoc_STR("Commit the current transaction.")},
1314     {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
1315         PyDoc_STR("Roll back the current transaction.")},
1316     {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
1317         PyDoc_STR("Creates a new function. Non-standard.")},
1318     {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
1319         PyDoc_STR("Creates a new aggregate. Non-standard.")},
1320     {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
1321         PyDoc_STR("Sets authorizer callback. Non-standard.")},
1322     {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1323         PyDoc_STR("Sets progress handler callback. Non-standard.")},
1324     {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
1325         PyDoc_STR("Executes a SQL statement. Non-standard.")},
1326     {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
1327         PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
1328     {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
1329         PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
1330     {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
1331         PyDoc_STR("Creates a collation function. Non-standard.")},
1332     {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
1333         PyDoc_STR("Abort any pending database operation. Non-standard.")},
1334     {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1335         PyDoc_STR("For context manager. Non-standard.")},
1336     {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1337         PyDoc_STR("For context manager. Non-standard.")},
1338     {NULL, NULL}
1339 };
1340
1341 static struct PyMemberDef connection_members[] =
1342 {
1343     {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), RO},
1344     {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), RO},
1345     {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), RO},
1346     {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), RO},
1347     {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), RO},
1348     {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), RO},
1349     {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), RO},
1350     {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), RO},
1351     {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), RO},
1352     {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), RO},
1353     {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1354     {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
1355     {NULL}
1356 };
1357
1358 PyTypeObject pysqlite_ConnectionType = {
1359         PyObject_HEAD_INIT(NULL)
1360         0,                                              /* ob_size */
1361         MODULE_NAME ".Connection",                      /* tp_name */
1362         sizeof(pysqlite_Connection),                    /* tp_basicsize */
1363         0,                                              /* tp_itemsize */
1364         (destructor)pysqlite_connection_dealloc,        /* tp_dealloc */
1365         0,                                              /* tp_print */
1366         0,                                              /* tp_getattr */
1367         0,                                              /* tp_setattr */
1368         0,                                              /* tp_compare */
1369         0,                                              /* tp_repr */
1370         0,                                              /* tp_as_number */
1371         0,                                              /* tp_as_sequence */
1372         0,                                              /* tp_as_mapping */
1373         0,                                              /* tp_hash */
1374         (ternaryfunc)pysqlite_connection_call,          /* tp_call */
1375         0,                                              /* tp_str */
1376         0,                                              /* tp_getattro */
1377         0,                                              /* tp_setattro */
1378         0,                                              /* tp_as_buffer */
1379         Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,         /* tp_flags */
1380         connection_doc,                                 /* tp_doc */
1381         0,                                              /* tp_traverse */
1382         0,                                              /* tp_clear */
1383         0,                                              /* tp_richcompare */
1384         0,                                              /* tp_weaklistoffset */
1385         0,                                              /* tp_iter */
1386         0,                                              /* tp_iternext */
1387         connection_methods,                             /* tp_methods */
1388         connection_members,                             /* tp_members */
1389         connection_getset,                              /* tp_getset */
1390         0,                                              /* tp_base */
1391         0,                                              /* tp_dict */
1392         0,                                              /* tp_descr_get */
1393         0,                                              /* tp_descr_set */
1394         0,                                              /* tp_dictoffset */
1395         (initproc)pysqlite_connection_init,             /* tp_init */
1396         0,                                              /* tp_alloc */
1397         0,                                              /* tp_new */
1398         0                                               /* tp_free */
1399 };
1400
1401 extern int pysqlite_connection_setup_types(void)
1402 {
1403     pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1404     return PyType_Ready(&pysqlite_ConnectionType);
1405 }