blob: 4525c61acb4158a57f4bfbf7f58cef6bf0258862 [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001 /* module.c - the module itself
2 *
3 * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
4 *
5 * This file is part of pysqlite.
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000023
24#include "connection.h"
25#include "statement.h"
26#include "cursor.h"
27#include "cache.h"
28#include "prepare_protocol.h"
29#include "microprotocols.h"
30#include "row.h"
31
32#if SQLITE_VERSION_NUMBER >= 3003003
33#define HAVE_SHARED_CACHE
34#endif
35
36/* static objects at module-level */
37
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000038PyObject* pysqlite_Error, *pysqlite_Warning, *pysqlite_InterfaceError, *pysqlite_DatabaseError,
39 *pysqlite_InternalError, *pysqlite_OperationalError, *pysqlite_ProgrammingError,
40 *pysqlite_IntegrityError, *pysqlite_DataError, *pysqlite_NotSupportedError, *pysqlite_OptimizedUnicode;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041
42PyObject* converters;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000043int _enable_callback_tracebacks;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044
45static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
46 kwargs)
47{
48 /* Python seems to have no way of extracting a single keyword-arg at
49 * C-level, so this code is redundant with the one in connection_init in
50 * connection.c and must always be copied from there ... */
51
52 static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
53 char* database;
54 int detect_types = 0;
55 PyObject* isolation_level;
56 PyObject* factory = NULL;
57 int check_same_thread = 1;
58 int cached_statements;
59 double timeout = 5.0;
60
61 PyObject* result;
62
63 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist,
64 &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
65 {
66 return NULL;
67 }
68
69 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000070 factory = (PyObject*)&pysqlite_ConnectionType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071 }
72
73 result = PyObject_Call(factory, args, kwargs);
74
75 return result;
76}
77
78static PyObject* module_complete(PyObject* self, PyObject* args, PyObject*
79 kwargs)
80{
81 static char *kwlist[] = {"statement", NULL, NULL};
82 char* statement;
83
84 PyObject* result;
85
86 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement))
87 {
88 return NULL;
89 }
90
91 if (sqlite3_complete(statement)) {
92 result = Py_True;
93 } else {
94 result = Py_False;
95 }
96
97 Py_INCREF(result);
98
99 return result;
100}
101
102#ifdef HAVE_SHARED_CACHE
103static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject*
104 kwargs)
105{
106 static char *kwlist[] = {"do_enable", NULL, NULL};
107 int do_enable;
108 int rc;
109
110 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable))
111 {
112 return NULL;
113 }
114
115 rc = sqlite3_enable_shared_cache(do_enable);
116
117 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000118 PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 return NULL;
120 } else {
121 Py_INCREF(Py_None);
122 return Py_None;
123 }
124}
125#endif /* HAVE_SHARED_CACHE */
126
127static PyObject* module_register_adapter(PyObject* self, PyObject* args, PyObject* kwargs)
128{
129 PyTypeObject* type;
130 PyObject* caster;
131
132 if (!PyArg_ParseTuple(args, "OO", &type, &caster)) {
133 return NULL;
134 }
135
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000136 microprotocols_add(type, (PyObject*)&pysqlite_PrepareProtocolType, caster);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000137
138 Py_INCREF(Py_None);
139 return Py_None;
140}
141
142static PyObject* module_register_converter(PyObject* self, PyObject* args, PyObject* kwargs)
143{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000144 PyObject* orig_name;
145 PyObject* name = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 PyObject* callable;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000147 PyObject* retval = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000149 if (!PyArg_ParseTuple(args, "SO", &orig_name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150 return NULL;
151 }
152
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000153 /* convert the name to upper case */
154 name = PyObject_CallMethod(orig_name, "upper", "");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000155 if (!name) {
156 goto error;
157 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000158
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000159 if (PyDict_SetItem(converters, name, callable) != 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000160 goto error;
161 }
162
163 Py_INCREF(Py_None);
164 retval = Py_None;
165error:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000166 Py_XDECREF(name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000167 return retval;
168}
169
170static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args, PyObject* kwargs)
171{
172 if (!PyArg_ParseTuple(args, "i", &_enable_callback_tracebacks)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173 return NULL;
174 }
175
176 Py_INCREF(Py_None);
177 return Py_None;
178}
179
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000180static void converters_init(PyObject* dict)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000181{
182 converters = PyDict_New();
183 if (!converters) {
184 return;
185 }
186
187 PyDict_SetItemString(dict, "converters", converters);
188}
189
190static PyMethodDef module_methods[] = {
191 {"connect", (PyCFunction)module_connect, METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Creates a connection.")},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000192 {"complete_statement", (PyCFunction)module_complete, METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Checks if a string contains a complete SQL statement. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193#ifdef HAVE_SHARED_CACHE
Thomas Wouters477c8d52006-05-27 19:21:47 +0000194 {"enable_shared_cache", (PyCFunction)module_enable_shared_cache, METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Enable or disable shared cache mode for the calling thread. Experimental/Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000196 {"register_adapter", (PyCFunction)module_register_adapter, METH_VARARGS, PyDoc_STR("Registers an adapter with pysqlite's adapter registry. Non-standard.")},
197 {"register_converter", (PyCFunction)module_register_converter, METH_VARARGS, PyDoc_STR("Registers a converter with pysqlite. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 {"adapt", (PyCFunction)psyco_microprotocols_adapt, METH_VARARGS, psyco_microprotocols_adapt_doc},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000199 {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks, METH_VARARGS, PyDoc_STR("Enable or disable callback functions throwing errors to stderr.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200 {NULL, NULL}
201};
202
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000203struct _IntConstantPair {
204 char* constant_name;
205 int constant_value;
206};
207
208typedef struct _IntConstantPair IntConstantPair;
209
210static IntConstantPair _int_constants[] = {
211 {"PARSE_DECLTYPES", PARSE_DECLTYPES},
212 {"PARSE_COLNAMES", PARSE_COLNAMES},
213
214 {"SQLITE_OK", SQLITE_OK},
215 {"SQLITE_DENY", SQLITE_DENY},
216 {"SQLITE_IGNORE", SQLITE_IGNORE},
217 {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX},
218 {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE},
219 {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX},
220 {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE},
221 {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER},
222 {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW},
223 {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER},
224 {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW},
225 {"SQLITE_DELETE", SQLITE_DELETE},
226 {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX},
227 {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE},
228 {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX},
229 {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE},
230 {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER},
231 {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW},
232 {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER},
233 {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW},
234 {"SQLITE_INSERT", SQLITE_INSERT},
235 {"SQLITE_PRAGMA", SQLITE_PRAGMA},
236 {"SQLITE_READ", SQLITE_READ},
237 {"SQLITE_SELECT", SQLITE_SELECT},
238 {"SQLITE_TRANSACTION", SQLITE_TRANSACTION},
239 {"SQLITE_UPDATE", SQLITE_UPDATE},
240 {"SQLITE_ATTACH", SQLITE_ATTACH},
241 {"SQLITE_DETACH", SQLITE_DETACH},
242#if SQLITE_VERSION_NUMBER >= 3002001
243 {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE},
244 {"SQLITE_REINDEX", SQLITE_REINDEX},
245#endif
246#if SQLITE_VERSION_NUMBER >= 3003000
247 {"SQLITE_ANALYZE", SQLITE_ANALYZE},
248#endif
249 {(char*)NULL, 0}
250};
251
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252PyMODINIT_FUNC init_sqlite3(void)
253{
254 PyObject *module, *dict;
255 PyObject *tmp_obj;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257
258 module = Py_InitModule("_sqlite3", module_methods);
259
260 if (!module ||
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000261 (pysqlite_row_setup_types() < 0) ||
262 (pysqlite_cursor_setup_types() < 0) ||
263 (pysqlite_connection_setup_types() < 0) ||
264 (pysqlite_cache_setup_types() < 0) ||
265 (pysqlite_statement_setup_types() < 0) ||
266 (pysqlite_prepare_protocol_setup_types() < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267 ) {
268 return;
269 }
270
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000271 Py_INCREF(&pysqlite_ConnectionType);
272 PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType);
273 Py_INCREF(&pysqlite_CursorType);
274 PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType);
275 Py_INCREF(&pysqlite_CacheType);
276 PyModule_AddObject(module, "Statement", (PyObject*)&pysqlite_StatementType);
277 Py_INCREF(&pysqlite_StatementType);
278 PyModule_AddObject(module, "Cache", (PyObject*) &pysqlite_CacheType);
279 Py_INCREF(&pysqlite_PrepareProtocolType);
280 PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &pysqlite_PrepareProtocolType);
281 Py_INCREF(&pysqlite_RowType);
282 PyModule_AddObject(module, "Row", (PyObject*) &pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283
284 if (!(dict = PyModule_GetDict(module))) {
285 goto error;
286 }
287
288 /*** Create DB-API Exception hierarchy */
289
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000290 if (!(pysqlite_Error = PyErr_NewException(MODULE_NAME ".Error", PyExc_Exception, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291 goto error;
292 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000293 PyDict_SetItemString(dict, "Error", pysqlite_Error);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000295 if (!(pysqlite_Warning = PyErr_NewException(MODULE_NAME ".Warning", PyExc_Exception, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000296 goto error;
297 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000298 PyDict_SetItemString(dict, "Warning", pysqlite_Warning);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000299
300 /* Error subclasses */
301
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000302 if (!(pysqlite_InterfaceError = PyErr_NewException(MODULE_NAME ".InterfaceError", pysqlite_Error, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303 goto error;
304 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000305 PyDict_SetItemString(dict, "InterfaceError", pysqlite_InterfaceError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000306
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000307 if (!(pysqlite_DatabaseError = PyErr_NewException(MODULE_NAME ".DatabaseError", pysqlite_Error, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308 goto error;
309 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000310 PyDict_SetItemString(dict, "DatabaseError", pysqlite_DatabaseError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000311
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000312 /* pysqlite_DatabaseError subclasses */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000314 if (!(pysqlite_InternalError = PyErr_NewException(MODULE_NAME ".InternalError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315 goto error;
316 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000317 PyDict_SetItemString(dict, "InternalError", pysqlite_InternalError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000319 if (!(pysqlite_OperationalError = PyErr_NewException(MODULE_NAME ".OperationalError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320 goto error;
321 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000322 PyDict_SetItemString(dict, "OperationalError", pysqlite_OperationalError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000324 if (!(pysqlite_ProgrammingError = PyErr_NewException(MODULE_NAME ".ProgrammingError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 goto error;
326 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000327 PyDict_SetItemString(dict, "ProgrammingError", pysqlite_ProgrammingError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000328
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000329 if (!(pysqlite_IntegrityError = PyErr_NewException(MODULE_NAME ".IntegrityError", pysqlite_DatabaseError,NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 goto error;
331 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000332 PyDict_SetItemString(dict, "IntegrityError", pysqlite_IntegrityError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000334 if (!(pysqlite_DataError = PyErr_NewException(MODULE_NAME ".DataError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335 goto error;
336 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000337 PyDict_SetItemString(dict, "DataError", pysqlite_DataError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000339 if (!(pysqlite_NotSupportedError = PyErr_NewException(MODULE_NAME ".NotSupportedError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000340 goto error;
341 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000342 PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000344 /* We just need "something" unique for pysqlite_OptimizedUnicode. It does not really
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000345 * need to be a string subclass. Just anything that can act as a special
346 * marker for us. So I pulled PyCell_Type out of my magic hat.
347 */
348 Py_INCREF((PyObject*)&PyCell_Type);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000349 pysqlite_OptimizedUnicode = (PyObject*)&PyCell_Type;
350 PyDict_SetItemString(dict, "OptimizedUnicode", pysqlite_OptimizedUnicode);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000352 /* Set integer constants */
353 for (i = 0; _int_constants[i].constant_name != 0; i++) {
354 tmp_obj = PyInt_FromLong(_int_constants[i].constant_value);
355 if (!tmp_obj) {
356 goto error;
357 }
358 PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj);
359 Py_DECREF(tmp_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000361
362 if (!(tmp_obj = PyString_FromString(PYSQLITE_VERSION))) {
363 goto error;
364 }
365 PyDict_SetItemString(dict, "version", tmp_obj);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000366 Py_DECREF(tmp_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367
368 if (!(tmp_obj = PyString_FromString(sqlite3_libversion()))) {
369 goto error;
370 }
371 PyDict_SetItemString(dict, "sqlite_version", tmp_obj);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000372 Py_DECREF(tmp_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000373
374 /* initialize microprotocols layer */
375 microprotocols_init(dict);
376
377 /* initialize the default converters */
378 converters_init(dict);
379
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000380 _enable_callback_tracebacks = 0;
381
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 /* Original comment form _bsddb.c in the Python core. This is also still
383 * needed nowadays for Python 2.3/2.4.
384 *
385 * PyEval_InitThreads is called here due to a quirk in python 1.5
386 * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>:
387 * The global interepreter lock is not initialized until the first
388 * thread is created using thread.start_new_thread() or fork() is
389 * called. that would cause the ALLOW_THREADS here to segfault due
390 * to a null pointer reference if no threads or child processes
391 * have been created. This works around that and is a no-op if
392 * threads have already been initialized.
393 * (see pybsddb-users mailing list post on 2002-08-07)
394 */
395 PyEval_InitThreads();
396
397error:
398 if (PyErr_Occurred())
399 {
400 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
401 }
402}