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