Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1 | /* 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 | */ |
| 23 | |
| 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 | |
| 38 | PyObject* Error, *Warning, *InterfaceError, *DatabaseError, *InternalError, |
| 39 | *OperationalError, *ProgrammingError, *IntegrityError, *DataError, |
| 40 | *NotSupportedError, *OptimizedUnicode; |
| 41 | |
| 42 | PyObject* time_time; |
| 43 | PyObject* time_sleep; |
| 44 | |
| 45 | PyObject* converters; |
| 46 | |
| 47 | static PyObject* module_connect(PyObject* self, PyObject* args, PyObject* |
| 48 | kwargs) |
| 49 | { |
| 50 | /* Python seems to have no way of extracting a single keyword-arg at |
| 51 | * C-level, so this code is redundant with the one in connection_init in |
| 52 | * connection.c and must always be copied from there ... */ |
| 53 | |
| 54 | static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL}; |
| 55 | char* database; |
| 56 | int detect_types = 0; |
| 57 | PyObject* isolation_level; |
| 58 | PyObject* factory = NULL; |
| 59 | int check_same_thread = 1; |
| 60 | int cached_statements; |
| 61 | double timeout = 5.0; |
| 62 | |
| 63 | PyObject* result; |
| 64 | |
| 65 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist, |
| 66 | &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements)) |
| 67 | { |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | if (factory == NULL) { |
| 72 | factory = (PyObject*)&ConnectionType; |
| 73 | } |
| 74 | |
| 75 | result = PyObject_Call(factory, args, kwargs); |
| 76 | |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | static PyObject* module_complete(PyObject* self, PyObject* args, PyObject* |
| 81 | kwargs) |
| 82 | { |
| 83 | static char *kwlist[] = {"statement", NULL, NULL}; |
| 84 | char* statement; |
| 85 | |
| 86 | PyObject* result; |
| 87 | |
| 88 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement)) |
| 89 | { |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | if (sqlite3_complete(statement)) { |
| 94 | result = Py_True; |
| 95 | } else { |
| 96 | result = Py_False; |
| 97 | } |
| 98 | |
| 99 | Py_INCREF(result); |
| 100 | |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | #ifdef HAVE_SHARED_CACHE |
| 105 | static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject* |
| 106 | kwargs) |
| 107 | { |
| 108 | static char *kwlist[] = {"do_enable", NULL, NULL}; |
| 109 | int do_enable; |
| 110 | int rc; |
| 111 | |
| 112 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable)) |
| 113 | { |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | rc = sqlite3_enable_shared_cache(do_enable); |
| 118 | |
| 119 | if (rc != SQLITE_OK) { |
| 120 | PyErr_SetString(OperationalError, "Changing the shared_cache flag failed"); |
| 121 | return NULL; |
| 122 | } else { |
| 123 | Py_INCREF(Py_None); |
| 124 | return Py_None; |
| 125 | } |
| 126 | } |
| 127 | #endif /* HAVE_SHARED_CACHE */ |
| 128 | |
| 129 | static PyObject* module_register_adapter(PyObject* self, PyObject* args, PyObject* kwargs) |
| 130 | { |
| 131 | PyTypeObject* type; |
| 132 | PyObject* caster; |
| 133 | |
| 134 | if (!PyArg_ParseTuple(args, "OO", &type, &caster)) { |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | microprotocols_add(type, (PyObject*)&SQLitePrepareProtocolType, caster); |
| 139 | |
| 140 | Py_INCREF(Py_None); |
| 141 | return Py_None; |
| 142 | } |
| 143 | |
| 144 | static PyObject* module_register_converter(PyObject* self, PyObject* args, PyObject* kwargs) |
| 145 | { |
| 146 | PyObject* name; |
| 147 | PyObject* callable; |
| 148 | |
| 149 | if (!PyArg_ParseTuple(args, "OO", &name, &callable)) { |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | PyDict_SetItem(converters, name, callable); |
| 154 | |
| 155 | Py_INCREF(Py_None); |
| 156 | return Py_None; |
| 157 | } |
| 158 | |
| 159 | void converters_init(PyObject* dict) |
| 160 | { |
| 161 | converters = PyDict_New(); |
| 162 | |
| 163 | PyDict_SetItemString(dict, "converters", converters); |
| 164 | } |
| 165 | |
| 166 | static PyMethodDef module_methods[] = { |
| 167 | {"connect", (PyCFunction)module_connect, METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Creates a connection.")}, |
| 168 | {"complete_statement", (PyCFunction)module_complete, METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Checks if a string contains a complete SQL statement.")}, |
| 169 | #ifdef HAVE_SHARED_CACHE |
| 170 | {"enable_shared_cache", (PyCFunction)module_enable_shared_cache, METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Enable or disable shared cache mode for the calling thread.")}, |
| 171 | #endif |
| 172 | {"register_adapter", (PyCFunction)module_register_adapter, METH_VARARGS, PyDoc_STR("Registers an adapter with pysqlite's adapter registry.")}, |
| 173 | {"register_converter", (PyCFunction)module_register_converter, METH_VARARGS, PyDoc_STR("Registers a converter with pysqlite.")}, |
| 174 | {"adapt", (PyCFunction)psyco_microprotocols_adapt, METH_VARARGS, psyco_microprotocols_adapt_doc}, |
| 175 | {NULL, NULL} |
| 176 | }; |
| 177 | |
| 178 | PyMODINIT_FUNC init_sqlite3(void) |
| 179 | { |
| 180 | PyObject *module, *dict; |
| 181 | PyObject* time_module; |
| 182 | |
| 183 | module = Py_InitModule("_sqlite3", module_methods); |
| 184 | |
| 185 | if ( |
| 186 | (row_setup_types() < 0) || |
| 187 | (cursor_setup_types() < 0) || |
| 188 | (connection_setup_types() < 0) || |
| 189 | (cache_setup_types() < 0) || |
| 190 | (statement_setup_types() < 0) || |
| 191 | (prepare_protocol_setup_types() < 0) |
| 192 | ) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | Py_INCREF(&ConnectionType); |
| 197 | PyModule_AddObject(module, "Connection", (PyObject*) &ConnectionType); |
| 198 | Py_INCREF(&CursorType); |
| 199 | PyModule_AddObject(module, "Cursor", (PyObject*) &CursorType); |
| 200 | Py_INCREF(&CacheType); |
| 201 | PyModule_AddObject(module, "Statement", (PyObject*)&StatementType); |
| 202 | Py_INCREF(&StatementType); |
| 203 | PyModule_AddObject(module, "Cache", (PyObject*) &CacheType); |
| 204 | Py_INCREF(&SQLitePrepareProtocolType); |
| 205 | PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &SQLitePrepareProtocolType); |
| 206 | Py_INCREF(&RowType); |
| 207 | PyModule_AddObject(module, "Row", (PyObject*) &RowType); |
| 208 | |
| 209 | if (!(dict = PyModule_GetDict(module))) |
| 210 | { |
| 211 | goto error; |
| 212 | } |
| 213 | |
| 214 | /*** Create DB-API Exception hierarchy */ |
| 215 | |
| 216 | Error = PyErr_NewException("sqlite3.Error", PyExc_StandardError, NULL); |
| 217 | PyDict_SetItemString(dict, "Error", Error); |
| 218 | |
| 219 | Warning = PyErr_NewException("sqlite3.Warning", PyExc_StandardError, NULL); |
| 220 | PyDict_SetItemString(dict, "Warning", Warning); |
| 221 | |
| 222 | /* Error subclasses */ |
| 223 | |
| 224 | InterfaceError = PyErr_NewException("sqlite3.InterfaceError", Error, NULL); |
| 225 | PyDict_SetItemString(dict, "InterfaceError", InterfaceError); |
| 226 | |
| 227 | DatabaseError = PyErr_NewException("sqlite3.DatabaseError", Error, NULL); |
| 228 | PyDict_SetItemString(dict, "DatabaseError", DatabaseError); |
| 229 | |
| 230 | /* DatabaseError subclasses */ |
| 231 | |
| 232 | InternalError = PyErr_NewException("sqlite3.InternalError", DatabaseError, NULL); |
| 233 | PyDict_SetItemString(dict, "InternalError", InternalError); |
| 234 | |
| 235 | OperationalError = PyErr_NewException("sqlite3.OperationalError", DatabaseError, NULL); |
| 236 | PyDict_SetItemString(dict, "OperationalError", OperationalError); |
| 237 | |
| 238 | ProgrammingError = PyErr_NewException("sqlite3.ProgrammingError", DatabaseError, NULL); |
| 239 | PyDict_SetItemString(dict, "ProgrammingError", ProgrammingError); |
| 240 | |
| 241 | IntegrityError = PyErr_NewException("sqlite3.IntegrityError", DatabaseError,NULL); |
| 242 | PyDict_SetItemString(dict, "IntegrityError", IntegrityError); |
| 243 | |
| 244 | DataError = PyErr_NewException("sqlite3.DataError", DatabaseError, NULL); |
| 245 | PyDict_SetItemString(dict, "DataError", DataError); |
| 246 | |
| 247 | NotSupportedError = PyErr_NewException("sqlite3.NotSupportedError", DatabaseError, NULL); |
| 248 | PyDict_SetItemString(dict, "NotSupportedError", NotSupportedError); |
| 249 | |
| 250 | Py_INCREF((PyObject*)&PyCell_Type); |
| 251 | OptimizedUnicode = (PyObject*)&PyCell_Type; |
| 252 | PyDict_SetItemString(dict, "OptimizedUnicode", OptimizedUnicode); |
| 253 | |
| 254 | PyDict_SetItemString(dict, "PARSE_DECLTYPES", PyInt_FromLong(PARSE_DECLTYPES)); |
| 255 | PyDict_SetItemString(dict, "PARSE_COLNAMES", PyInt_FromLong(PARSE_COLNAMES)); |
| 256 | |
| 257 | PyDict_SetItemString(dict, "version", PyString_FromString(PYSQLITE_VERSION)); |
| 258 | PyDict_SetItemString(dict, "sqlite_version", PyString_FromString(sqlite3_libversion())); |
| 259 | |
| 260 | /* initialize microprotocols layer */ |
| 261 | microprotocols_init(dict); |
| 262 | |
| 263 | /* initialize the default converters */ |
| 264 | converters_init(dict); |
| 265 | |
| 266 | time_module = PyImport_ImportModule("time"); |
| 267 | time_time = PyObject_GetAttrString(time_module, "time"); |
| 268 | time_sleep = PyObject_GetAttrString(time_module, "sleep"); |
| 269 | |
| 270 | /* Original comment form _bsddb.c in the Python core. This is also still |
| 271 | * needed nowadays for Python 2.3/2.4. |
| 272 | * |
| 273 | * PyEval_InitThreads is called here due to a quirk in python 1.5 |
| 274 | * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>: |
| 275 | * The global interepreter lock is not initialized until the first |
| 276 | * thread is created using thread.start_new_thread() or fork() is |
| 277 | * called. that would cause the ALLOW_THREADS here to segfault due |
| 278 | * to a null pointer reference if no threads or child processes |
| 279 | * have been created. This works around that and is a no-op if |
| 280 | * threads have already been initialized. |
| 281 | * (see pybsddb-users mailing list post on 2002-08-07) |
| 282 | */ |
| 283 | PyEval_InitThreads(); |
| 284 | |
| 285 | error: |
| 286 | if (PyErr_Occurred()) |
| 287 | { |
| 288 | PyErr_SetString(PyExc_ImportError, "_sqlite3: init failed"); |
| 289 | } |
| 290 | } |