Update changelog
[joel/debian/python-sqlite.git] / _sqlite.c
1 /*                                            _ _ _
2 **                      _ __  _   _ ___  __ _| (_) |_ ___
3 **                     | '_ \| | | / __|/ _` | | | __/ _ \
4 **                     | |_) | |_| \__ \ (_| | | | ||  __/
5 **                     | .__/ \__, |___/\__, |_|_|\__\___|
6 **                     |_|    |___/        |_|
7 **
8 **               A DB API v2.0 compatible interface to SQLite
9 **                       Embedded Relational Database.
10 **                          Copyright (c) 2001-2003
11 **                  Michael Owens <mike@mikesclutter.com>
12 **                     Gerhard Häring <gh@ghaering.de>
13 **
14 ** All Rights Reserved
15 **
16 ** Permission to use, copy, modify, and distribute this software and its
17 ** documentation for any purpose and without fee is hereby granted, provided
18 ** that the above copyright notice appear in all copies and that both that
19 ** copyright notice and this permission notice appear in supporting
20 ** documentation,
21 **
22 ** This program is distributed in the hope that it will be useful, but WITHOUT
23 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24 ** FOR A PARTICULAR PURPOSE.
25 */
26
27 #include "Python.h"
28 #include "structmember.h"
29
30 #include "sqlite.h"
31
32 #include "port/strsep.h"
33
34 /* Compatibility macros
35  *
36  * From Python 2.2 to 2.3, the way to export the module init function
37  * has changed. These macros keep the code compatible to both ways.
38  */
39 #if PY_VERSION_HEX >= 0x02030000
40 #  define PySQLite_DECLARE_MODINIT_FUNC(name) PyMODINIT_FUNC name(void)
41 #  define PySQLite_MODINIT_FUNC(name)         PyMODINIT_FUNC name(void)
42 #else
43 #  define PySQLite_DECLARE_MODINIT_FUNC(name) void name(void)
44 #  define PySQLite_MODINIT_FUNC(name)         DL_EXPORT(void) name(void)
45 #endif
46
47 /*
48  * These are needed because there is no "official" way to specify
49  * WHERE to save the thread state. (At least not until Python 2.3)
50  */
51 #ifdef WITH_THREAD
52 #  define MY_BEGIN_ALLOW_THREADS(st)    \
53     { st = PyEval_SaveThread(); }
54 #  define MY_END_ALLOW_THREADS(st)      \
55     { PyEval_RestoreThread(st); st = NULL; }
56 #else
57 #  define MY_BEGIN_ALLOW_THREADS(st)
58 #  define MY_END_ALLOW_THREADS(st)      { st = NULL; }
59 #endif
60
61 /*
62  * ** Some compilers do not support the "long long" datatype.  So we have
63  * ** to do a typedef that for 64-bit integers that depends on what compiler
64  * ** is being used.
65  * */
66 #if defined(_MSC_VER) || defined(__BORLANDC__)
67   typedef __int64 sqlite_int64;
68     typedef unsigned __int64 sqlite_uint64;
69 #else
70       typedef long long int sqlite_int64;
71         typedef unsigned long long int sqlite_uint64;
72 #endif
73
74 /*------------------------------------------------------------------------------
75 ** Object Declarations
76 **------------------------------------------------------------------------------
77 */
78
79 /** A connection object */
80 typedef struct
81 {
82     PyObject_HEAD
83     const char* database_name;
84     const char* sql;
85     sqlite* p_db;
86     PyObject* converters;
87     PyObject* expected_types;
88     PyObject* command_logfile;
89     PyThreadState *tstate;
90 } pysqlc;
91
92 /** A result set object. */
93 typedef struct
94 {
95     PyObject_HEAD
96     pysqlc* con;
97     PyObject* p_row_list;
98     PyObject* p_col_def_list;
99     int row_count;
100 } pysqlrs;
101
102 /** Exception objects */
103
104 static PyObject* _sqlite_Warning;
105 static PyObject* _sqlite_Error;
106 static PyObject* _sqlite_DatabaseError;
107 static PyObject* _sqlite_InterfaceError;
108 static PyObject* _sqlite_DataError;
109 static PyObject* _sqlite_OperationalError;
110 static PyObject* _sqlite_IntegrityError;
111 static PyObject* _sqlite_InternalError;
112 static PyObject* _sqlite_ProgrammingError;
113 static PyObject* _sqlite_NotSupportedError;
114
115 static int debug_callbacks = 0;
116
117 #define PRINT_OR_CLEAR_ERROR if (debug_callbacks) PyErr_Print(); else PyErr_Clear();
118
119 /* A tuple describing the minimum required SQLite version */
120 static PyObject* required_sqlite_version;
121
122 /*** Type codes */
123
124 static PyObject* tc_INTEGER;
125 static PyObject* tc_FLOAT;
126 static PyObject* tc_TIMESTAMP;
127 static PyObject* tc_DATE;
128 static PyObject* tc_TIME;
129 static PyObject* tc_INTERVAL;
130 static PyObject* tc_STRING;
131 static PyObject* tc_UNICODESTRING;
132 static PyObject* tc_BINARY;
133
134 /*------------------------------------------------------------------------------
135 ** Function Prototypes
136 **------------------------------------------------------------------------------
137 */
138
139 static int process_record(void* p_data, int num_fields, char** p_fields, char** p_col_names);
140
141 PySQLite_DECLARE_MODINIT_FUNC(init_sqlite);
142 static int _seterror(int returncode, char* errmsg);
143 static void _con_dealloc(pysqlc *self);
144 static PyObject* sqlite_version_info(PyObject* self, PyObject* args);
145 static PyObject* pysqlite_connect(PyObject *self, PyObject *args, PyObject *kwargs);
146 static PyObject* sqlite_library_version(PyObject *self, PyObject *args);
147 static PyObject* sqlite_enable_callback_debugging(PyObject *self, PyObject *args);
148 static PyObject* pysqlite_encode(PyObject *self, PyObject *args);
149 static PyObject* pysqlite_decode(PyObject *self, PyObject *args);
150
151 /* Defined in encode.c */
152 int sqlite_encode_binary(const unsigned char *in, int n, unsigned char *out);
153 int sqlite_decode_binary(const unsigned char *in, unsigned char *out);
154
155 /** Connection Object Methods */
156 static PyObject* _con_get_attr(pysqlc *self, char *attr);
157 static PyObject* _con_close(pysqlc *self, PyObject *args);
158 static PyObject* _con_execute(pysqlc *self, PyObject *args);
159 static PyObject* _con_register_converter(pysqlc* self, PyObject *args, PyObject* kwargs);
160 static PyObject* _con_set_expected_types(pysqlc* self, PyObject *args, PyObject* kwargs);
161 static PyObject* _con_create_function(pysqlc *self, PyObject *args, PyObject *kwargs);
162 static PyObject* _con_create_aggregate(pysqlc *self, PyObject *args, PyObject *kwargs);
163 static PyObject* _con_sqlite_exec(pysqlc *self, PyObject *args, PyObject *kwargs);
164 static PyObject* _con_sqlite_last_insert_rowid(pysqlc *self, PyObject *args);
165 static PyObject* _con_sqlite_changes(pysqlc *self, PyObject *args);
166 static PyObject* _con_sqlite_busy_handler(pysqlc* self, PyObject *args, PyObject* kwargs);
167 static PyObject* _con_sqlite_busy_timeout(pysqlc* self, PyObject *args, PyObject* kwargs);
168 static PyObject* _con_set_command_logfile(pysqlc* self, PyObject *args, PyObject* kwargs);
169
170 /** Result set Object Methods */
171 static void _rs_dealloc(pysqlrs* self);
172 static PyObject* _rs_get_attr(pysqlrs* self, char *attr);
173
174 #ifdef _MSC_VER
175 #define staticforward extern
176 #endif
177
178 staticforward PyMethodDef _con_methods[];
179 staticforward struct memberlist _con_memberlist[];
180
181 PyTypeObject pysqlc_Type =
182 {
183     PyObject_HEAD_INIT(NULL)
184     0,
185     "Connection",
186     sizeof(pysqlc),
187     0,
188     (destructor) _con_dealloc,
189     0,
190     (getattrfunc) _con_get_attr,
191     (setattrfunc) NULL,
192 };
193
194 PyTypeObject pysqlrs_Type =
195 {
196     PyObject_HEAD_INIT(NULL)
197     0,
198     "ResultSet",
199     sizeof(pysqlrs),
200     0,
201     (destructor) _rs_dealloc,
202     0,
203     (getattrfunc) _rs_get_attr,
204     (setattrfunc) NULL,
205 };
206
207 static void
208 _con_dealloc(pysqlc* self)
209 {
210     if(self)
211     {
212         if(self->p_db != 0)
213         {
214             /* Close the database */
215             sqlite_close(self->p_db);
216             self->p_db = 0;
217         }
218
219         if(self->sql != NULL)
220         {
221             /* Free last SQL statement string */
222             free((void*)self->sql);
223             self->sql = NULL;
224         }
225
226         if(self->database_name != NULL)
227         {
228             /* Free database name string */
229             free((void*)self->database_name);
230             self->database_name = NULL;
231         }
232
233         Py_DECREF(self->converters);
234         Py_DECREF(self->expected_types);
235         Py_DECREF(self->command_logfile);
236
237         PyObject_Del(self);
238     }
239 }
240
241 static char pysqlite_connect_doc[] =
242 "connect(db, mode=0777) -> Connection.\n\
243 Opens a new database connection.";
244
245 /* return a new instance of sqlite_connection */
246 PyObject* pysqlite_connect(PyObject *self, PyObject *args, PyObject *kwargs)
247 {
248     const char* db_name = 0;
249     int mode = 0777;
250     char *errmsg;
251
252     pysqlc* obj;
253
254     static char *kwlist[] = { "filename", "mode", NULL };
255
256     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i:pysqlite_connect",
257                                       kwlist, &db_name, &mode))
258     {
259         return NULL;
260     }
261
262     if ((obj = PyObject_New(pysqlc, &pysqlc_Type)) == NULL)
263     {
264         return NULL;
265     }
266
267     /* Open the database */
268     obj->p_db = sqlite_open(db_name, mode, &errmsg);
269
270     if(obj->p_db == 0 || errmsg != NULL)
271     {
272         PyObject_Del(obj);
273         if (errmsg != NULL)
274         {
275             PyErr_SetString(_sqlite_DatabaseError, errmsg);
276             sqlite_freemem(errmsg);
277         }
278         else
279         {
280             PyErr_SetString(_sqlite_DatabaseError, "Could not open database.");
281         }
282         return NULL;
283     }
284
285     /* Assign the database name */
286     if ((obj->database_name = strdup(db_name)) == NULL)
287     {
288         PyErr_SetString(PyExc_MemoryError, "Cannot allocate memory for database name.");
289         return NULL;
290     }
291
292     /* Init sql string to NULL */
293     obj->sql = NULL;
294
295     /* Set the thread state to NULL */
296     obj->tstate = NULL;
297
298     if ((obj->converters = PyDict_New()) == NULL)
299     {
300         PyErr_SetString(PyExc_MemoryError, "Cannot allocate memory for converters.");
301         return NULL;
302     }
303
304     Py_INCREF(Py_None);
305     obj->expected_types = Py_None;
306
307     Py_INCREF(Py_None);
308     obj->command_logfile = Py_None;
309
310     /* Get column type information */
311     (void)sqlite_exec(obj->p_db, "pragma show_datatypes=ON", (sqlite_callback)0, (void*)0, &errmsg);
312
313     return (PyObject *) obj;
314 }
315
316 static PyObject* _con_get_attr(pysqlc *self, char *attr)
317 {
318     PyObject *res;
319
320     res = Py_FindMethod(_con_methods, (PyObject *) self,attr);
321
322     if(NULL != res)
323     {
324         return res;
325     }
326     else
327     {
328         PyErr_Clear();
329         return PyMember_Get((char *) self, _con_memberlist, attr);
330     }
331 }
332
333 static char _con_close_doc [] =
334 "close()\n\
335 Close the database connection.";
336
337 static PyObject* _con_close(pysqlc *self, PyObject *args)
338 {
339     if (!PyArg_ParseTuple(args,""))
340     {
341         return NULL;
342     }
343
344     if(self->p_db != 0)
345     {
346         /* Close the database */
347         sqlite_close(self->p_db);
348         self->p_db = 0;
349     }
350     else
351     {
352         PyErr_SetString(_sqlite_ProgrammingError, "Database is not open.");
353         return NULL;
354     }
355
356     Py_INCREF(Py_None);
357
358     return Py_None;
359 }
360
361 static void function_callback(sqlite_func *context, int argc, const char **argv)
362 {
363     int i;
364     PyObject* function_result;
365     PyObject* args;
366     PyObject* userdata;
367     PyObject* func;
368     PyObject* s;
369     pysqlc* con;
370
371     userdata = (PyObject*)sqlite_user_data(context);
372     func = PyTuple_GetItem(userdata, 0);
373     con = (pysqlc*)PyTuple_GetItem(userdata, 1);
374     MY_END_ALLOW_THREADS(con->tstate)
375
376     args = PyTuple_New(argc);
377     for (i = 0; i < argc; i++)
378     {
379         if (argv[i] == NULL)
380         {
381             Py_INCREF(Py_None);
382             PyTuple_SetItem(args, i, Py_None);
383         }
384         else
385         {
386             PyTuple_SetItem(args, i, PyString_FromString(argv[i]));
387         }
388     }
389
390     function_result = PyObject_CallObject(func, args);
391     Py_DECREF(args);
392
393     if (PyErr_Occurred())
394     {
395         PRINT_OR_CLEAR_ERROR
396         sqlite_set_result_error(context, NULL, -1);
397         MY_BEGIN_ALLOW_THREADS(con->tstate)
398         return;
399     }
400
401     if (function_result == Py_None)
402     {
403         sqlite_set_result_string(context, NULL, -1);
404     }
405     else
406     {
407         s = PyObject_Str(function_result);
408         sqlite_set_result_string(context, PyString_AsString(s), -1);
409         Py_DECREF(s);
410     }
411
412     Py_DECREF(function_result);
413     MY_BEGIN_ALLOW_THREADS(con->tstate)
414 }
415
416 static void aggregate_step(sqlite_func *context, int argc, const char **argv)
417 {
418     int i;
419     PyObject* args;
420     PyObject* function_result;
421     PyObject* userdata;
422     PyObject* aggregate_class;
423     pysqlc* con;
424     PyObject** aggregate_instance;
425     PyObject* stepmethod;
426
427     userdata = (PyObject*)sqlite_user_data(context);
428     aggregate_class = PyTuple_GetItem(userdata, 0);
429
430     con = (pysqlc*)PyTuple_GetItem(userdata, 1);
431     MY_END_ALLOW_THREADS(con->tstate)
432
433     aggregate_instance = (PyObject**)sqlite_aggregate_context(context, sizeof(PyObject*));
434
435     if (*aggregate_instance == 0) {
436         args = PyTuple_New(0);
437         *aggregate_instance = PyObject_CallObject(aggregate_class, args);
438         Py_DECREF(args);
439
440         if (PyErr_Occurred())
441         {
442             PRINT_OR_CLEAR_ERROR
443             MY_BEGIN_ALLOW_THREADS(con->tstate)
444             return;
445         }
446     }
447
448     stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
449     if (!stepmethod)
450     {
451         /* PRINT_OR_CLEAR_ERROR */
452         MY_BEGIN_ALLOW_THREADS(con->tstate)
453         return;
454     }
455
456     args = PyTuple_New(argc);
457     for (i = 0; i < argc; i++) {
458         if (argv[i] == NULL) {
459             Py_INCREF(Py_None);
460             PyTuple_SetItem(args, i, Py_None);
461         } else {
462             PyTuple_SetItem(args, i, PyString_FromString(argv[i]));
463         }
464     }
465
466     if (PyErr_Occurred())
467     {
468         PRINT_OR_CLEAR_ERROR
469     }
470
471     function_result = PyObject_CallObject(stepmethod, args);
472     Py_DECREF(args);
473     Py_DECREF(stepmethod);
474
475     if (function_result == NULL)
476     {
477         PRINT_OR_CLEAR_ERROR
478         /* Don't use sqlite_set_result_error here. Else an assertion in
479          * the SQLite code will trigger and create a core dump.
480          */
481     }
482     else
483     {
484         Py_DECREF(function_result);
485     }
486
487     MY_BEGIN_ALLOW_THREADS(con->tstate)
488 }
489
490 static void aggregate_finalize(sqlite_func *context)
491 {
492     PyObject* args;
493     PyObject* function_result;
494     PyObject* s;
495     PyObject** aggregate_instance;
496     PyObject* userdata;
497     pysqlc* con;
498     PyObject* aggregate_class;
499     PyObject* finalizemethod;
500
501     userdata = (PyObject*)sqlite_user_data(context);
502     aggregate_class = PyTuple_GetItem(userdata, 0);
503     con = (pysqlc*)PyTuple_GetItem(userdata, 1);
504     MY_END_ALLOW_THREADS(con->tstate)
505
506     aggregate_instance = (PyObject**)sqlite_aggregate_context(context, sizeof(PyObject*));
507
508     finalizemethod = PyObject_GetAttrString(*aggregate_instance, "finalize");
509
510     if (!finalizemethod)
511     {
512         PyErr_SetString(PyExc_ValueError, "finalize method missing");
513         goto error;
514     }
515
516     args = PyTuple_New(0);
517     function_result = PyObject_CallObject(finalizemethod, args);
518     Py_DECREF(args);
519     Py_DECREF(finalizemethod);
520
521     if (PyErr_Occurred())
522     {
523         PRINT_OR_CLEAR_ERROR
524         sqlite_set_result_error(context, NULL, -1);
525     }
526     else if (function_result == Py_None)
527     {
528         Py_DECREF(function_result);
529         sqlite_set_result_string(context, NULL, -1);
530     }
531     else
532     {
533         s = PyObject_Str(function_result);
534         Py_DECREF(function_result);
535         sqlite_set_result_string(context, PyString_AsString(s), -1);
536         Py_DECREF(s);
537     }
538
539 error:
540     Py_XDECREF(*aggregate_instance);
541
542     MY_BEGIN_ALLOW_THREADS(con->tstate)
543 }
544
545 static int sqlite_busy_handler_callback(void* void_data, const char* tablename, int num_busy)
546 {
547     PyObject* data;
548     PyObject* func;
549     PyObject* userdata;
550     PyObject* args;
551     PyObject* function_result;
552     pysqlc* con;
553     int result_int;
554
555     data = (PyObject*)void_data;
556
557     func = PyTuple_GetItem(data, 0);
558     userdata = PyTuple_GetItem(data, 1);
559     con = (pysqlc*)PyTuple_GetItem(data, 2);
560
561     MY_END_ALLOW_THREADS(con->tstate)
562
563     args = PyTuple_New(3);
564     Py_INCREF(userdata);
565     PyTuple_SetItem(args, 0, userdata);
566     PyTuple_SetItem(args, 1, PyString_FromString(tablename));
567     PyTuple_SetItem(args, 2, PyInt_FromLong((long)num_busy));
568
569     function_result = PyObject_CallObject(func, args);
570     Py_DECREF(args);
571
572     if (PyErr_Occurred())
573     {
574         PRINT_OR_CLEAR_ERROR
575         MY_BEGIN_ALLOW_THREADS(con->tstate)
576         return 0;
577     }
578
579     result_int = PyObject_IsTrue(function_result);
580
581     Py_DECREF(function_result);
582
583     MY_BEGIN_ALLOW_THREADS(con->tstate)
584
585     return result_int;
586 }
587
588 static char _con_sqlite_busy_handler_doc[] =
589 "sqlite_busy_handler(func, data)\n\
590 Register a busy handler.\n\
591 \n\
592     The sqlite_busy_handler() procedure can be used to register a busy\n\
593     callback with an open SQLite database. The busy callback will be invoked\n\
594     whenever SQLite tries to access a database that is locked. The callback\n\
595     will typically do some other useful work, or perhaps sleep, in order to\n\
596     give the lock a chance to clear. If the callback returns non-zero, then\n\
597     SQLite tries again to access the database and the cycle repeats. If the\n\
598     callback returns zero, then SQLite aborts the current operation and returns\n\
599     SQLITE_BUSY, which PySQLite will make throw an OperationalError.\n\
600     \n\
601     The arguments to sqlite_busy_handler() are the callback function (func) and\n\
602     an additional argument (data) that will be passed to the busy callback\n\
603     function.\n\
604     \n\
605     When the busy callback is invoked, it is sent three arguments. The first\n\
606     argument will be the 'data' that was set as the third argument to\n\
607     sqlite_busy_handler. The second will be the name of the database table or\n\
608     index that SQLite was trying to access and the third one will be the number\n\
609     of times that the library has attempted to access the database table or\n\
610     index.";
611
612 static PyObject* _con_sqlite_busy_handler(pysqlc* self, PyObject *args, PyObject* kwargs)
613 {
614     static char *kwlist[] = {"func", "data", NULL};
615     PyObject* func;
616     PyObject* data = Py_None;
617     PyObject* userdata;
618
619     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:sqlite_busy_handler",
620                                       kwlist, &func, &data))
621     {
622         return NULL;
623     }
624
625     if ((userdata = PyTuple_New(3)) == NULL)
626     {
627         return NULL;
628     }
629     Py_INCREF(func); PyTuple_SetItem(userdata, 0, func);
630     Py_INCREF(data); PyTuple_SetItem(userdata, 1, data);
631     Py_INCREF(self); PyTuple_SetItem(userdata, 2, (PyObject*)self);
632
633     sqlite_busy_handler(self->p_db, &sqlite_busy_handler_callback, userdata);
634
635     Py_INCREF(Py_None);
636     return Py_None;
637 }
638
639 static char _con_sqlite_busy_timeout_doc[] =
640 "sqlite_busy_timeout(milliseconds)\n\
641 Register a busy handler that will wait for a specific time before giving up.\n\
642 \n\
643     This is a convenience routine that will install a busy handler (see\n\
644     sqlite_busy_handler) that will sleep for n milliseconds before\n\
645     giving up (i. e. return SQLITE_BUSY/throw OperationalError).";
646
647 static PyObject* _con_sqlite_busy_timeout(pysqlc* self, PyObject *args, PyObject* kwargs)
648 {
649     int timeout;
650     static char *kwlist[] = {"timeout", NULL};
651
652     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:sqlite_busy_timeout",
653                                       kwlist, &timeout))
654     {
655         return NULL;
656     }
657
658     sqlite_busy_timeout(self->p_db, timeout);
659
660     Py_INCREF(Py_None);
661     return Py_None;
662 }
663
664 static char _con_create_function_doc[] =
665 "create_function(name, n_args, func)\n\
666 Create a new SQL function.\n\
667 \n\
668     A new function under the name 'name', with 'n_args' arguments is created.\n\
669     The callback 'func' will be called for this function.";
670
671 static PyObject* _con_create_function(pysqlc* self, PyObject *args, PyObject* kwargs)
672 {
673     int n_args;
674     char* name;
675     PyObject* func;
676     PyObject* userdata;
677     static char *kwlist[] = {"name", "n_args", "func", NULL};
678
679     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_function",
680                                      kwlist, &name, &n_args,
681                                      &func))
682     {
683         return NULL;
684     }
685
686     if (!(userdata = PyTuple_New(2))) return NULL;
687     Py_INCREF(func);
688     PyTuple_SetItem(userdata, 0, func);
689     Py_INCREF(self);
690     PyTuple_SetItem(userdata, 1, (PyObject*)self);
691
692     if (!PyCallable_Check(func))
693     {
694         PyErr_SetString(PyExc_ValueError, "func must be a callable!");
695         return NULL;
696     }
697
698     Py_INCREF(func);
699     if (0 != sqlite_create_function(self->p_db, name, n_args, &function_callback, (void*)userdata))
700     {
701         PyErr_SetString(_sqlite_ProgrammingError, "Cannot create function.");
702         return NULL;
703     }
704     else
705     {
706         Py_INCREF(Py_None);
707         return Py_None;
708     }
709 }
710
711 static char _con_create_aggregate_doc[] =
712 "create_aggregate(name, n_args, step_func, finalize_func)\n\
713 Create a new SQL function.\n\
714 \n\
715     A new aggregate function under the name 'name', with 'n_args' arguments to\n\
716     the 'step_func' function will be created. 'finalize_func' will be called\n\
717     without arguments for finishing the aggregate.";
718
719 static PyObject* _con_create_aggregate(pysqlc* self, PyObject *args, PyObject* kwargs)
720 {
721     PyObject* aggregate_class;
722
723     int n_args;
724     char* name;
725     static char *kwlist[] = { "name", "n_args", "aggregate_class", NULL };
726     PyObject* userdata;
727
728     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
729                                       kwlist, &name, &n_args, &aggregate_class))
730     {
731         return NULL;
732     }
733
734     if (!(userdata = PyTuple_New(2))) return NULL;
735     Py_INCREF(aggregate_class);
736     PyTuple_SetItem(userdata, 0, aggregate_class);
737     Py_INCREF(self);
738     PyTuple_SetItem(userdata, 1, (PyObject*)self);
739
740     if (0 != sqlite_create_aggregate(self->p_db, name, n_args, &aggregate_step, &aggregate_finalize, (void*)userdata))
741     {
742         PyErr_SetString(_sqlite_ProgrammingError, "Cannot create aggregate.");
743         return NULL;
744     }
745     else
746     {
747         Py_INCREF(Py_None);
748         return Py_None;
749     }
750 }
751
752 static char _con_set_command_logfile_doc[] =
753 "set_command_logfile(logfile)\n\
754 Registers a writeable file-like object as logfile where all SQL commands\n\
755 that get executed are written to.";
756
757 static PyObject* _con_set_command_logfile(pysqlc* self, PyObject *args, PyObject* kwargs)
758 {
759     PyObject* logfile;
760     PyObject* o;
761
762     static char *kwlist[] = { "logfile", NULL };
763
764     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_command_logfile",
765                                       kwlist, &logfile))
766     {
767         return NULL;
768     }
769
770     if (logfile == Py_None)
771     {
772         Py_INCREF(Py_None);
773         return Py_None;
774     }
775
776     o = PyObject_GetAttrString(logfile, "write");
777     if (!o)
778     {
779         PyErr_SetString(PyExc_ValueError, "logfile must have a 'write' attribute!");
780         return NULL;
781     }
782
783     if (!PyCallable_Check(o))
784     {
785         PyErr_SetString(PyExc_ValueError, "logfile must have a callable 'write' attribute!");
786         Py_DECREF(o);
787         return NULL;
788     }
789
790     Py_DECREF(o);
791     Py_INCREF(logfile);
792     self->command_logfile = logfile;
793
794     Py_INCREF(Py_None);
795     return Py_None;
796 }
797
798 int sqlite_exec_callback(void* pArg, int argc, char **argv, char **columnNames)
799 {
800     PyObject* parg;
801     PyObject* callback;
802     PyObject* arg1;
803     pysqlc* con;
804     PyObject* values;
805     PyObject* colnames;
806     PyObject* calling_args;
807     PyObject* function_result;
808     int i;
809
810     parg = (PyObject*)pArg;
811
812     callback = PyTuple_GetItem(parg, 0);
813     arg1 = PyTuple_GetItem(parg, 1);
814     con = (pysqlc*)PyTuple_GetItem(parg, 2);
815
816     MY_END_ALLOW_THREADS(con->tstate)
817
818     colnames = PyTuple_New(argc);
819     for (i = 0; i < argc; i++)
820     {
821         PyTuple_SetItem(colnames, i, PyString_FromString(columnNames[i]));
822     }
823
824     values = PyTuple_New(argc);
825     for (i = 0; i < argc; i++)
826     {
827         if (argv[i] == NULL)
828         {
829             Py_INCREF(Py_None);
830             PyTuple_SetItem(values, i, Py_None);
831         }
832         else
833         {
834             PyTuple_SetItem(values, i, PyString_FromString(argv[i]));
835         }
836     }
837
838     calling_args = PyTuple_New(3);
839     Py_INCREF(arg1);
840     PyTuple_SetItem(calling_args, 0, arg1);
841     PyTuple_SetItem(calling_args, 1, values);
842     PyTuple_SetItem(calling_args, 2, colnames);
843
844     function_result = PyObject_CallObject(callback, calling_args);
845     if (PyErr_Occurred())
846     {
847         PRINT_OR_CLEAR_ERROR
848         MY_BEGIN_ALLOW_THREADS(con->tstate)
849         return 1;
850     }
851
852     Py_DECREF(function_result);
853     Py_DECREF(calling_args);
854
855     MY_BEGIN_ALLOW_THREADS(con->tstate)
856     return 0;
857 }
858
859 static char _con_sqlite_exec_doc[] =
860 "sqlite_exec(sql, func, arg, use_types=0)\n\
861 Execute SQL.\n\
862 \n\
863     Executes the SQL string 'sql' and uses the callback function 'func' for\n\
864     each returned row. The argument 'arg' will be passed to the callback\n\
865     function.\n\
866     \n\
867     The signature of the callback function is (arg, values, colnames{, types}).\n\
868     types is ommitted unless use_types is true. If you use 'use_types', you\n\
869     MUST have issued 'pragma show_datatypes=ON' before.";
870
871 static PyObject* _con_sqlite_exec(pysqlc* self, PyObject *args, PyObject* kwargs)
872 {
873     static char *kwlist[] = {"sql", "func", "arg", "use_types", NULL};
874     char* sql;
875     PyObject* callback;
876     PyObject* arg1;
877     int use_types = 0;
878
879     PyObject* cb_args;
880
881     /* TODO add errmsg handling */
882     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sOO|i:sqlite_exec",
883                                       kwlist, &sql, &callback, &arg1, &use_types))
884     {
885         return NULL;
886     }
887
888     cb_args = PyTuple_New(3);
889     Py_INCREF(callback);
890     Py_INCREF(arg1);
891     Py_INCREF(self);
892     PyTuple_SetItem(cb_args, 0, callback);
893     PyTuple_SetItem(cb_args, 1, arg1);
894     PyTuple_SetItem(cb_args, 2, (PyObject*)self);
895
896     MY_BEGIN_ALLOW_THREADS(self->tstate)
897     sqlite_exec(self->p_db, sql, &sqlite_exec_callback, cb_args, NULL);
898     MY_END_ALLOW_THREADS(self->tstate)
899
900     Py_DECREF(cb_args);
901
902     Py_INCREF(Py_None);
903     return Py_None;
904 }
905
906 static PyObject* _con_sqlite_last_insert_rowid(pysqlc *self, PyObject *args)
907 {
908     PyObject* value;
909
910     if (!PyArg_ParseTuple(args,""))
911     {
912         return NULL;
913     }
914
915     value = PyInt_FromLong((long)sqlite_last_insert_rowid(self->p_db));
916
917     return value;
918 }
919
920 static PyObject* _con_sqlite_changes(pysqlc *self, PyObject *args)
921 {
922     PyObject* value;
923
924     if (!PyArg_ParseTuple(args,""))
925     {
926         return NULL;
927     }
928
929     value = PyInt_FromLong((long)sqlite_changes(self->p_db));
930
931     return value;
932 }
933
934 static PyObject * sqlite_library_version(PyObject *self, PyObject *args)
935 {
936     if (!PyArg_ParseTuple(args, ""))
937     {
938         return NULL;
939     }
940
941     return Py_BuildValue("s", sqlite_libversion());
942 }
943
944 static PyObject* sqlite_enable_callback_debugging(PyObject *self, PyObject *args)
945 {
946     if (!PyArg_ParseTuple(args, "i", &debug_callbacks))
947     {
948         return NULL;
949     }
950
951     Py_INCREF(Py_None);
952     return Py_None;
953 }
954
955 static char pysqlite_encode_doc[] =
956 "encode(s) -> encoded binary string.\n\
957 Encode binary string 's' for storage in SQLite.";
958
959 static PyObject* pysqlite_encode(PyObject *self, PyObject *args)
960 {
961     char *in, *out;
962     int n;
963     PyObject *res;
964
965     if (!PyArg_ParseTuple(args, "s#", &in, &n))
966     {
967         return NULL;
968     }
969
970     /* See comments in encode.c for details on maximum size of encoded data. */
971     out = malloc(2 + (257 * (sqlite_uint64)n) / 254);
972     if (out == NULL)
973     {
974         return PyErr_NoMemory();
975     }
976     sqlite_encode_binary(in, n, out);
977     res = Py_BuildValue("s", out);
978     free(out);
979     return res;
980 }
981
982 static char pysqlite_decode_doc[] =
983 "decode(s) -> decoded binary string.\n\
984 Decode encoded binary string retrieved from SQLite.";
985
986 static PyObject* pysqlite_decode(PyObject *self, PyObject *args)
987 {
988     char *in, *out;
989     int n;
990     PyObject *res;
991
992     if (!PyArg_ParseTuple(args, "s", &in))
993     {
994         return NULL;
995     }
996
997     /* Decoded string is always shorter than encoded string. */
998     out = malloc(strlen(in));
999     if (out == NULL)
1000     {
1001         return PyErr_NoMemory();
1002     }
1003     n = sqlite_decode_binary(in, out);
1004     res = Py_BuildValue("s#", out, n);
1005     free(out);
1006     return res;
1007 }
1008
1009 static int _seterror(int returncode,  char *errmsg)
1010 {
1011     switch (returncode)
1012     {
1013         case SQLITE_OK:
1014             PyErr_Clear();
1015             break;
1016         case SQLITE_ERROR:
1017             PyErr_SetString(_sqlite_DatabaseError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1018             break;
1019         case SQLITE_INTERNAL:
1020             PyErr_SetString(_sqlite_InternalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1021             break;
1022         case SQLITE_PERM:
1023             PyErr_SetString(_sqlite_OperationalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1024             break;
1025         case SQLITE_ABORT:
1026             PyErr_SetString(_sqlite_OperationalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1027             break;
1028         case SQLITE_BUSY:
1029             PyErr_SetString(_sqlite_OperationalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1030             break;
1031         case SQLITE_LOCKED:
1032             PyErr_SetString(_sqlite_OperationalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1033             break;
1034         case SQLITE_NOMEM:
1035             (void)PyErr_NoMemory();
1036             break;
1037         case SQLITE_READONLY:
1038             PyErr_SetString(_sqlite_DatabaseError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1039             break;
1040         case SQLITE_INTERRUPT:
1041             PyErr_SetString(_sqlite_OperationalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1042             break;
1043         case SQLITE_IOERR:
1044             PyErr_SetString(_sqlite_OperationalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1045             break;
1046         case SQLITE_CORRUPT:
1047             PyErr_SetString(_sqlite_DatabaseError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1048             break;
1049         case SQLITE_NOTFOUND:
1050             PyErr_SetString(_sqlite_InternalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1051             break;
1052         case SQLITE_FULL:
1053             PyErr_SetString(_sqlite_DatabaseError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1054             break;
1055         case SQLITE_CANTOPEN:
1056             PyErr_SetString(_sqlite_DatabaseError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1057             break;
1058         case SQLITE_PROTOCOL:
1059             PyErr_SetString(_sqlite_OperationalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1060             break;
1061         case SQLITE_EMPTY:
1062             PyErr_SetString(_sqlite_InternalError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1063             break;
1064         case SQLITE_SCHEMA:
1065             PyErr_SetString(_sqlite_DatabaseError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1066             break;
1067         case SQLITE_TOOBIG:
1068             PyErr_SetString(_sqlite_DataError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1069             break;
1070         case SQLITE_CONSTRAINT:
1071             PyErr_SetString(_sqlite_IntegrityError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1072             break;
1073         case SQLITE_MISMATCH:
1074             PyErr_SetString(_sqlite_IntegrityError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1075             break;
1076         case SQLITE_MISUSE:
1077             PyErr_SetString(_sqlite_ProgrammingError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1078             break;
1079         default:
1080             PyErr_SetString(_sqlite_DatabaseError, (errmsg!=NULL)?errmsg:sqlite_error_string(returncode));
1081     }
1082     sqlite_freemem(errmsg);
1083     return returncode;
1084 }
1085
1086 static PyObject* _con_execute(pysqlc* self, PyObject *args)
1087 {
1088     int ret;
1089     int record_number;
1090     char* sql;
1091     pysqlrs* p_rset;
1092     char *errmsg;
1093     char* buf;
1094     char* iterator;
1095     char* token;
1096     PyObject* logfile_writemethod;
1097     PyObject* logfile_writeargs;
1098
1099     record_number = 0;
1100
1101     if(!PyArg_ParseTuple(args,"s:execute", &sql))
1102     {
1103         return NULL;
1104     }
1105
1106     if(self->p_db == 0)
1107     {
1108         /* There is no open database. */
1109         PyErr_SetString(_sqlite_ProgrammingError, "There is no open database.");
1110         return NULL;
1111     }
1112
1113     if(self->sql != NULL)
1114     {
1115         /* Free last SQL statment string */
1116         free((void*)self->sql);
1117         self->sql = NULL;
1118     }
1119
1120     /* Save SQL statement */
1121     self->sql = strdup(sql);
1122
1123     /* Log SQL statement */
1124     if (self->command_logfile != Py_None)
1125     {
1126         logfile_writemethod = PyObject_GetAttrString(self->command_logfile,
1127                                                     "write");
1128         logfile_writeargs = PyTuple_New(1);
1129         PyTuple_SetItem(logfile_writeargs, 0, PyString_FromString(sql));
1130
1131         PyObject_CallObject(logfile_writemethod, logfile_writeargs);
1132
1133         Py_DECREF(logfile_writeargs);
1134
1135         logfile_writeargs = PyTuple_New(1);
1136         PyTuple_SetItem(logfile_writeargs, 0, PyString_FromString("\n"));
1137         PyObject_CallObject(logfile_writemethod, logfile_writeargs);
1138
1139         Py_DECREF(logfile_writeargs);
1140         Py_DECREF(logfile_writemethod);
1141
1142         if (PyErr_Occurred())
1143         {
1144             free((void*)(self->sql));
1145             self->sql = NULL;
1146             return NULL;
1147         }
1148     }
1149
1150     p_rset = PyObject_New(pysqlrs, &pysqlrs_Type);
1151     if (p_rset == NULL)
1152     {
1153         return NULL;
1154     }
1155
1156     Py_INCREF(self);
1157     p_rset->con = self;
1158     p_rset->p_row_list = PyList_New(0);
1159     p_rset->p_col_def_list = NULL;
1160     p_rset->row_count = 0;
1161
1162     if (strstr(sql, "-- types "))
1163     {
1164         Py_DECREF(self->expected_types);
1165         self->expected_types = PyList_New(0);
1166         if (PyErr_Occurred())
1167         {
1168             Py_INCREF(Py_None);
1169             self->expected_types = Py_None;
1170             return NULL;
1171         }
1172
1173         if ((buf = strdup(sql)) == NULL)
1174         {
1175             PyErr_SetString(PyExc_MemoryError, "Cannot allocate buffer for copying SQL statement!");
1176             return NULL;
1177         }
1178
1179         iterator = buf + strlen("-- types ");
1180
1181         if (*iterator == 0)
1182         {
1183             free(buf);
1184             PyErr_SetString(PyExc_ValueError, "Illegal pragma!");
1185             return NULL;
1186         }
1187
1188         while (iterator != NULL)
1189         {
1190             token = pysqlite_strsep(&iterator, ",");
1191             while (*token == ' ')
1192             {
1193                 token++;
1194             }
1195
1196             PyList_Append(self->expected_types, Py_BuildValue("s", token));
1197         }
1198
1199         free(buf);
1200         p_rset->p_col_def_list = PyTuple_New(0);
1201         return (PyObject*)p_rset;
1202     }
1203
1204     /* Run a query: process_record is called back for each record returned. */
1205     MY_BEGIN_ALLOW_THREADS(self->tstate)
1206     ret = sqlite_exec( self->p_db,
1207                        sql,
1208                        process_record,
1209                        p_rset,
1210                        &errmsg);
1211     MY_END_ALLOW_THREADS(self->tstate)
1212
1213     Py_DECREF(self->expected_types);
1214     Py_INCREF(Py_None);
1215     self->expected_types = Py_None;
1216
1217     /* Maybe there occurred an error in a user-defined function */
1218     if (PyErr_Occurred())
1219     {
1220         free((void*)(self->sql));
1221         self->sql = NULL;
1222         Py_DECREF(p_rset);
1223         return NULL;
1224     }
1225
1226     if (p_rset->p_col_def_list == NULL)
1227     {
1228         p_rset->p_col_def_list = PyTuple_New(0);
1229     }
1230
1231     if(_seterror(ret, errmsg) != SQLITE_OK)
1232     {
1233         free((void*)(self->sql));
1234         self->sql = NULL;
1235         Py_DECREF(p_rset);
1236         return NULL;
1237     }
1238
1239     return (PyObject*)p_rset;
1240 }
1241
1242 int process_record(void* p_data, int num_fields, char** p_fields, char** p_col_names)
1243 {
1244     int i;
1245     pysqlrs* p_rset;
1246     PyObject* p_row;
1247     PyObject* p_col_def;
1248
1249     int l, j;
1250     char type_name[255];
1251     PyObject* type_code;
1252
1253     PyObject* expected_types;
1254     PyObject* expected_type_name = NULL;
1255     PyObject* converters;
1256     PyObject* converted;
1257     PyObject* callable;
1258     PyObject* callable_args;
1259
1260     p_rset = (pysqlrs*)p_data;
1261     MY_END_ALLOW_THREADS(p_rset->con->tstate)
1262
1263     expected_types = p_rset->con->expected_types;
1264     converters = p_rset->con->converters;
1265
1266     if(p_rset->row_count == 0)
1267     {
1268         if ((p_rset->p_col_def_list = PyTuple_New(num_fields)) == NULL)
1269         {
1270             PRINT_OR_CLEAR_ERROR
1271             MY_BEGIN_ALLOW_THREADS(p_rset->con->tstate)
1272             return 1;
1273         }
1274
1275         for (i=0; i < num_fields; i++)
1276         {
1277             p_col_def = PyTuple_New(7);
1278
1279             /* 1. Column Name */
1280             PyTuple_SetItem(p_col_def, 0, Py_BuildValue("s", p_col_names[i]));
1281
1282             /* 2. Type code */
1283             /* Make a copy of column type. */
1284             if (p_col_names[num_fields + i] == NULL)
1285             {
1286                 strcpy(type_name, "TEXT");
1287             }
1288             else
1289             {
1290                 strncpy(type_name, p_col_names[num_fields + i], sizeof(type_name) - 1);
1291             }
1292
1293             /* Get its length. */
1294             l = strlen(type_name);
1295
1296             /* Convert to uppercase. */
1297             for(j=0; j < l; j++)
1298             {
1299                 type_name[j] = toupper(type_name[j]);
1300             }
1301
1302             /* Init/unset value */
1303             type_code = NULL;
1304
1305             /* Try to determine column type. */
1306             if (strstr(type_name, "INTERVAL"))
1307             {
1308                 type_code = tc_INTERVAL;
1309             }
1310             else if (strstr(type_name, "INT"))
1311             {
1312                 type_code = tc_INTEGER;
1313             }
1314             else if (strstr(type_name, "CHAR")
1315                   || strstr(type_name, "TEXT"))
1316             {
1317                 type_code = tc_STRING;
1318             }
1319             else if (strstr(type_name, "UNICODE"))
1320             {
1321                 type_code = tc_UNICODESTRING;
1322             }
1323             else if (strstr(type_name, "BINARY")
1324                   || strstr(type_name, "BLOB"))
1325             {
1326                 type_code = tc_BINARY;
1327             }
1328             else if (strstr(type_name, "FLOAT")
1329                   || strstr(type_name, "NUMERIC")
1330                   || strstr(type_name, "NUMBER")
1331                   || strstr(type_name, "DECIMAL")
1332                   || strstr(type_name, "REAL")
1333                   || strstr(type_name, "DOUBLE"))
1334             {
1335                 type_code = tc_FLOAT;
1336             }
1337             else if (strstr(type_name, "TIMESTAMP"))
1338             {
1339                 type_code = tc_TIMESTAMP;
1340             }
1341             else if (strstr(type_name, "DATE"))
1342             {
1343                 type_code = tc_DATE;
1344             }
1345             else if (strstr(type_name, "TIME"))
1346             {
1347                 type_code = tc_TIME;
1348             }
1349             else if (type_code == NULL)
1350             {
1351                 type_code = Py_None;
1352             }
1353
1354             /* Assign type. */
1355             Py_INCREF(type_code);
1356             PyTuple_SetItem(p_col_def, 1, type_code);
1357
1358             /* 3. Display Size */
1359             Py_INCREF(Py_None);
1360             PyTuple_SetItem(p_col_def, 2, Py_None);
1361
1362             /* 4. Internal Size */
1363             Py_INCREF(Py_None);
1364             PyTuple_SetItem(p_col_def, 3, Py_None);
1365
1366             /* 5. Precision */
1367             Py_INCREF(Py_None);
1368             PyTuple_SetItem(p_col_def, 4, Py_None);
1369
1370             /* 6. Scale */
1371             Py_INCREF(Py_None);
1372             PyTuple_SetItem(p_col_def, 5, Py_None);
1373
1374             /* 7. NULL Okay */
1375             Py_INCREF(Py_None);
1376             PyTuple_SetItem(p_col_def, 6, Py_None);
1377
1378             PyTuple_SetItem(p_rset->p_col_def_list, i, p_col_def);
1379         }
1380     }
1381
1382     if (p_fields != NULL)
1383     {
1384         /* Create a row */
1385         p_row = PyTuple_New(num_fields);
1386
1387         p_rset->row_count++;
1388
1389         for (i=0; i < num_fields; i++)
1390         {
1391             /* Store the field value */
1392             if(p_fields[i] != 0)
1393             {
1394                 p_col_def = PyTuple_GetItem(p_rset->p_col_def_list, i);
1395
1396                 type_code = PyTuple_GetItem(p_col_def, 1);
1397
1398                 if (expected_types != Py_None)
1399                 {
1400                     if (i < PySequence_Length(expected_types))
1401                     {
1402                         expected_type_name = PySequence_GetItem(expected_types, i);
1403                         callable = PyDict_GetItem(converters, expected_type_name);
1404                         if (callable == NULL)
1405                         {
1406                             Py_INCREF(Py_None);
1407                             PyTuple_SetItem(p_row, i, Py_None);
1408                         }
1409                         else
1410                         {
1411                             callable_args = PyTuple_New(1);
1412                             PyTuple_SetItem(callable_args, 0, Py_BuildValue("s", p_fields[i]));
1413
1414                             converted = PyObject_CallObject(callable, callable_args);
1415                             if (PyErr_Occurred())
1416                             {
1417                                 PRINT_OR_CLEAR_ERROR
1418                                 Py_INCREF(Py_None);
1419                                 converted = Py_None;
1420                             }
1421
1422                             PyTuple_SetItem(p_row, i, converted);
1423
1424                             Py_DECREF(callable_args);
1425                         }
1426                     }
1427                     else
1428                     {
1429                         Py_INCREF(Py_None);
1430                         PyTuple_SetItem(p_row, i, Py_None);
1431                     }
1432                 }
1433                 else if (type_code == tc_INTEGER)
1434                 {
1435                     PyTuple_SetItem(p_row, i, Py_BuildValue("i", atol(p_fields[i])));
1436                 }
1437                 else if (type_code == tc_FLOAT)
1438                 {
1439                     PyTuple_SetItem(p_row, i, Py_BuildValue("f", atof(p_fields[i])));
1440                 }
1441                 else if (type_code == tc_DATE || type_code == tc_TIME
1442                         || type_code == tc_TIMESTAMP || type_code == tc_INTERVAL)
1443                 {
1444                     if (type_code == tc_DATE)
1445                         expected_type_name = PyString_FromString("date");
1446                     else if (type_code == tc_TIME)
1447                         expected_type_name = PyString_FromString("time");
1448                     else if (type_code == tc_TIMESTAMP)
1449                         expected_type_name = PyString_FromString("timestamp");
1450                     else if (type_code == tc_INTERVAL)
1451                         expected_type_name = PyString_FromString("interval");
1452
1453                     callable = PyDict_GetItem(converters, expected_type_name);
1454                     if (callable == NULL)
1455                     {
1456                         PyTuple_SetItem(p_row, i, PyString_FromString(p_fields[i]));
1457                     }
1458                     else
1459                     {
1460                         callable_args = PyTuple_New(1);
1461                         PyTuple_SetItem(callable_args, 0, Py_BuildValue("s", p_fields[i]));
1462
1463                         converted = PyObject_CallObject(callable, callable_args);
1464                         if (PyErr_Occurred())
1465                         {
1466                             PRINT_OR_CLEAR_ERROR
1467                             converted = PyString_FromString(p_fields[i]);
1468                         }
1469
1470                         PyTuple_SetItem(p_row, i, converted);
1471
1472                         Py_DECREF(callable_args);
1473                     }
1474
1475                     Py_DECREF(expected_type_name);
1476                 }
1477                 else if ((type_code == tc_UNICODESTRING) || (type_code == tc_BINARY))
1478                 {
1479                     if (type_code == tc_UNICODESTRING)
1480                         expected_type_name = PyString_FromString("unicode");
1481                     else
1482                         expected_type_name = PyString_FromString("binary");
1483
1484                     callable = PyDict_GetItem(converters, expected_type_name);
1485
1486                     if (callable == NULL)
1487                     {
1488                         PyTuple_SetItem(p_row, i, PyString_FromString(p_fields[i]));
1489                     }
1490                     else
1491                     {
1492                         callable_args = PyTuple_New(1);
1493                         PyTuple_SetItem(callable_args, 0, Py_BuildValue("s", p_fields[i]));
1494
1495                         converted = PyObject_CallObject(callable, callable_args);
1496                         if (PyErr_Occurred())
1497                         {
1498                             PRINT_OR_CLEAR_ERROR
1499                             converted = PyString_FromString(p_fields[i]);
1500                         }
1501
1502                         PyTuple_SetItem(p_row, i, converted);
1503
1504                         Py_DECREF(callable_args);
1505                     }
1506
1507                     Py_DECREF(expected_type_name);
1508                 }
1509                 else
1510                 {
1511                     PyTuple_SetItem(p_row, i, Py_BuildValue("s", p_fields[i]));
1512                 }
1513             }
1514             else
1515             {
1516                 /* A NULL field */
1517                 Py_INCREF(Py_None);
1518                 PyTuple_SetItem(p_row, i, Py_None);
1519             }
1520         }
1521
1522         PyList_Append(p_rset->p_row_list, p_row);
1523         Py_DECREF(p_row);
1524     }
1525
1526     MY_BEGIN_ALLOW_THREADS(p_rset->con->tstate)
1527     return 0;
1528 }
1529
1530 static PyObject* _con_register_converter(pysqlc* self, PyObject *args, PyObject* kwargs)
1531 {
1532     static char *kwlist[] = { "name", "converter", NULL };
1533
1534     PyObject* name;
1535     PyObject* converter;
1536
1537     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO:register_converter",
1538                                       kwlist, &name, &converter))
1539     {
1540         return NULL;
1541     }
1542
1543     if (!PyString_Check(name)) {
1544         PyErr_SetString(PyExc_ValueError, "name must be a string");
1545         return NULL;
1546     }
1547
1548     PyDict_SetItem(self->converters, name, converter);
1549
1550     Py_INCREF(Py_None);
1551     return Py_None;
1552 }
1553
1554 static PyObject* _con_set_expected_types(pysqlc* self, PyObject *args, PyObject* kwargs)
1555 {
1556     static char *kwlist[] = {"types", NULL};
1557
1558     PyObject* types;
1559
1560     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_expected_types", kwlist, &types))
1561     {
1562         return NULL;
1563     }
1564
1565     if ((types != Py_None) && (!PySequence_Check(types)))
1566     {
1567         PyErr_SetString(PyExc_ValueError, "types must be a sequence");
1568         return NULL;
1569     }
1570
1571     Py_DECREF(self->expected_types);
1572
1573     Py_INCREF(types);
1574     self->expected_types = types;
1575
1576     Py_INCREF(Py_None);
1577     return Py_None;
1578 }
1579
1580 /*------------------------------------------------------------------------------
1581 ** Result Set Object Implementation
1582 **------------------------------------------------------------------------------
1583 */
1584
1585 static struct memberlist _rs_memberlist[] =
1586 {
1587     {"row_list", T_OBJECT, offsetof(pysqlrs, p_row_list),     RO},
1588     {"col_defs", T_OBJECT, offsetof(pysqlrs, p_col_def_list), RO},
1589     {"rowcount", T_INT, offsetof(pysqlrs, row_count),         RO},
1590     {NULL}
1591 };
1592
1593 static PyMethodDef _rs_methods[] =
1594 {
1595     { NULL, NULL}
1596 };
1597
1598 static void
1599 _rs_dealloc(pysqlrs* self)
1600 {
1601     if(self)
1602     {
1603         Py_DECREF(self->con);
1604
1605         if(self->p_row_list != 0)
1606         {
1607             Py_DECREF(self->p_row_list);
1608
1609             self->p_row_list = 0;
1610         }
1611
1612         if(self->p_col_def_list != 0)
1613         {
1614             Py_DECREF(self->p_col_def_list);
1615
1616             self->p_col_def_list = 0;
1617         }
1618
1619         PyObject_Del(self);
1620     }
1621 }
1622
1623 static PyObject* _rs_get_attr(pysqlrs *self, char *attr)
1624 {
1625     PyObject *res;
1626
1627     res = Py_FindMethod(_rs_methods, (PyObject *) self,attr);
1628
1629     if(NULL != res)
1630     {
1631         return res;
1632     }
1633     else
1634     {
1635         PyErr_Clear();
1636         return PyMember_Get((char *) self, _rs_memberlist, attr);
1637     }
1638 }
1639
1640 static PyObject* sqlite_version_info(PyObject* self, PyObject* args)
1641 {
1642     PyObject* vi_list;
1643     PyObject* vi_tuple;
1644     char* buf;
1645     char* iterator;
1646     char* token;
1647
1648     if (!PyArg_ParseTuple(args, ""))
1649     {
1650         return NULL;
1651     }
1652
1653     buf = strdup(sqlite_libversion());
1654     iterator = buf;
1655
1656     vi_list = PyList_New(0);
1657
1658     while ((token = pysqlite_strsep(&iterator, ".")) != NULL)
1659     {
1660         PyList_Append(vi_list, PyInt_FromLong((long)atoi(token)));
1661     }
1662
1663     vi_tuple = PyList_AsTuple(vi_list);
1664     Py_DECREF(vi_list);
1665
1666     return vi_tuple;
1667 }
1668
1669
1670 /*------------------------------------------------------------------------------
1671 ** Module Definitions / Initialization
1672 **------------------------------------------------------------------------------
1673 */
1674 static PyMethodDef pysqlite_functions[] =
1675 {
1676     { "connect", (PyCFunction)pysqlite_connect, METH_VARARGS | METH_KEYWORDS, pysqlite_connect_doc},
1677     { "sqlite_version", (PyCFunction)sqlite_library_version, METH_VARARGS},
1678     { "sqlite_version_info", (PyCFunction)sqlite_version_info, METH_VARARGS},
1679     { "enable_callback_debugging", (PyCFunction)sqlite_enable_callback_debugging, METH_VARARGS},
1680     { "encode", (PyCFunction)pysqlite_encode, METH_VARARGS, pysqlite_encode_doc},
1681     { "decode", (PyCFunction)pysqlite_decode, METH_VARARGS, pysqlite_decode_doc},
1682     { NULL, NULL }
1683 };
1684
1685 /*------------------------------------------------------------------------------
1686 ** Connection Object Implementation
1687 **------------------------------------------------------------------------------
1688 */
1689
1690 static struct memberlist _con_memberlist[] =
1691 {
1692     {"sql",             T_STRING, offsetof(pysqlc, sql), RO},
1693     {"filename",        T_STRING, offsetof(pysqlc, database_name), RO},
1694     {NULL}
1695 };
1696
1697 static PyMethodDef _con_methods[] =
1698 {
1699     {"close", (PyCFunction) _con_close, METH_VARARGS, _con_close_doc},
1700     {"execute",  (PyCFunction)_con_execute, METH_VARARGS},
1701     {"register_converter", (PyCFunction)_con_register_converter, METH_VARARGS | METH_KEYWORDS},
1702     {"set_expected_types", (PyCFunction)_con_set_expected_types, METH_VARARGS | METH_KEYWORDS},
1703     {"set_command_logfile", (PyCFunction)_con_set_command_logfile, METH_VARARGS | METH_KEYWORDS, _con_set_command_logfile_doc},
1704     {"create_function", (PyCFunction)_con_create_function, METH_VARARGS | METH_KEYWORDS, _con_create_function_doc},
1705     {"create_aggregate", (PyCFunction)_con_create_aggregate, METH_VARARGS | METH_KEYWORDS, _con_create_aggregate_doc},
1706     {"sqlite_exec", (PyCFunction)_con_sqlite_exec, METH_VARARGS | METH_KEYWORDS, _con_sqlite_exec_doc},
1707     {"sqlite_last_insert_rowid", (PyCFunction)_con_sqlite_last_insert_rowid, METH_VARARGS},
1708     {"sqlite_changes", (PyCFunction)_con_sqlite_changes, METH_VARARGS},
1709     {"sqlite_busy_handler", (PyCFunction)_con_sqlite_busy_handler, METH_VARARGS | METH_KEYWORDS, _con_sqlite_busy_handler_doc},
1710     {"sqlite_busy_timeout", (PyCFunction)_con_sqlite_busy_timeout, METH_VARARGS | METH_KEYWORDS, _con_sqlite_busy_timeout_doc},
1711     { NULL, NULL}
1712 };
1713
1714 PySQLite_MODINIT_FUNC(init_sqlite)
1715 {
1716     PyObject *module, *dict;
1717     PyObject* sqlite_version;
1718     PyObject* args;
1719     long tc = 0L;
1720
1721     pysqlc_Type.ob_type = &PyType_Type;
1722     pysqlrs_Type.ob_type = &PyType_Type;
1723
1724     module = Py_InitModule("_sqlite", pysqlite_functions);
1725
1726     if (!(dict = PyModule_GetDict(module)))
1727     {
1728         goto error;
1729     }
1730
1731     required_sqlite_version = PyTuple_New(3);
1732     PyTuple_SetItem(required_sqlite_version, 0, PyInt_FromLong((long)2));
1733     PyTuple_SetItem(required_sqlite_version, 1, PyInt_FromLong((long)5));
1734     PyTuple_SetItem(required_sqlite_version, 2, PyInt_FromLong((long)6));
1735
1736     args = PyTuple_New(0);
1737     sqlite_version = sqlite_version_info(NULL, args);
1738     Py_DECREF(args);
1739     if (PyObject_Compare(sqlite_version, required_sqlite_version) < 0)
1740     {
1741         Py_DECREF(sqlite_version);
1742         PyErr_SetString(PyExc_ImportError, "Need to be linked against SQLite 2.5.6 or higher.");
1743         return;
1744     }
1745     Py_DECREF(sqlite_version);
1746
1747     /*** Initialize type codes */
1748     tc_INTEGER = PyInt_FromLong(tc++);
1749     tc_FLOAT = PyInt_FromLong(tc++);
1750     tc_TIMESTAMP = PyInt_FromLong(tc++);
1751     tc_DATE = PyInt_FromLong(tc++);
1752     tc_TIME = PyInt_FromLong(tc++);
1753     tc_INTERVAL = PyInt_FromLong(tc++);
1754     tc_STRING = PyInt_FromLong(tc++);
1755     tc_UNICODESTRING = PyInt_FromLong(tc++);
1756     tc_BINARY = PyInt_FromLong(tc++);
1757
1758     PyDict_SetItemString(dict, "INTEGER", tc_INTEGER);
1759     PyDict_SetItemString(dict, "FLOAT", tc_FLOAT);
1760     PyDict_SetItemString(dict, "TIMESTAMP", tc_TIMESTAMP);
1761     PyDict_SetItemString(dict, "DATE", tc_DATE);
1762     PyDict_SetItemString(dict, "TIME", tc_TIME);
1763     PyDict_SetItemString(dict, "INTERVAL", tc_INTERVAL);
1764     PyDict_SetItemString(dict, "STRING", tc_STRING);
1765     PyDict_SetItemString(dict, "UNICODESTRING", tc_UNICODESTRING);
1766     PyDict_SetItemString(dict, "BINARY", tc_BINARY);
1767
1768     /*** Create DB-API Exception hierarchy */
1769
1770     _sqlite_Error = PyErr_NewException("_sqlite.Error", PyExc_StandardError, NULL);
1771     PyDict_SetItemString(dict, "Error", _sqlite_Error);
1772
1773     _sqlite_Warning = PyErr_NewException("_sqlite.Warning", PyExc_StandardError, NULL);
1774     PyDict_SetItemString(dict, "Warning", _sqlite_Warning);
1775
1776     /* Error subclasses */
1777
1778     _sqlite_InterfaceError = PyErr_NewException("_sqlite.InterfaceError", _sqlite_Error, NULL);
1779     PyDict_SetItemString(dict, "InterfaceError", _sqlite_InterfaceError);
1780
1781     _sqlite_DatabaseError = PyErr_NewException("_sqlite.DatabaseError", _sqlite_Error, NULL);
1782     PyDict_SetItemString(dict, "DatabaseError", _sqlite_DatabaseError);
1783
1784     /* DatabaseError subclasses */
1785
1786     _sqlite_InternalError = PyErr_NewException("_sqlite.InternalError", _sqlite_DatabaseError, NULL);
1787     PyDict_SetItemString(dict, "InternalError", _sqlite_InternalError);
1788
1789     _sqlite_OperationalError = PyErr_NewException("_sqlite.OperationalError", _sqlite_DatabaseError, NULL);
1790     PyDict_SetItemString(dict, "OperationalError", _sqlite_OperationalError);
1791
1792     _sqlite_ProgrammingError = PyErr_NewException("_sqlite.ProgrammingError", _sqlite_DatabaseError, NULL);
1793     PyDict_SetItemString(dict, "ProgrammingError", _sqlite_ProgrammingError);
1794
1795     _sqlite_IntegrityError = PyErr_NewException("_sqlite.IntegrityError", _sqlite_DatabaseError,NULL);
1796     PyDict_SetItemString(dict, "IntegrityError", _sqlite_IntegrityError);
1797
1798     _sqlite_DataError = PyErr_NewException("_sqlite.DataError", _sqlite_DatabaseError, NULL);
1799     PyDict_SetItemString(dict, "DataError", _sqlite_DataError);
1800
1801     _sqlite_NotSupportedError = PyErr_NewException("_sqlite.NotSupportedError", _sqlite_DatabaseError, NULL);
1802     PyDict_SetItemString(dict, "NotSupportedError", _sqlite_NotSupportedError);
1803
1804   error:
1805
1806     if (PyErr_Occurred())
1807     {
1808         PyErr_SetString(PyExc_ImportError, "sqlite: init failed");
1809     }
1810 }