»Core Development>Code coverage>Modules/_sqlite/module.c

Python code coverage for Modules/_sqlite/module.c

#countcontent
1n/a/* module.c - the module itself
2n/a *
3n/a * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
4n/a *
5n/a * This file is part of pysqlite.
6n/a *
7n/a * This software is provided 'as-is', without any express or implied
8n/a * warranty. In no event will the authors be held liable for any damages
9n/a * arising from the use of this software.
10n/a *
11n/a * Permission is granted to anyone to use this software for any purpose,
12n/a * including commercial applications, and to alter it and redistribute it
13n/a * freely, subject to the following restrictions:
14n/a *
15n/a * 1. The origin of this software must not be misrepresented; you must not
16n/a * claim that you wrote the original software. If you use this software
17n/a * in a product, an acknowledgment in the product documentation would be
18n/a * appreciated but is not required.
19n/a * 2. Altered source versions must be plainly marked as such, and must not be
20n/a * misrepresented as being the original software.
21n/a * 3. This notice may not be removed or altered from any source distribution.
22n/a */
23n/a
24n/a#include "connection.h"
25n/a#include "statement.h"
26n/a#include "cursor.h"
27n/a#include "cache.h"
28n/a#include "prepare_protocol.h"
29n/a#include "microprotocols.h"
30n/a#include "row.h"
31n/a
32n/a#if SQLITE_VERSION_NUMBER >= 3003003
33n/a#define HAVE_SHARED_CACHE
34n/a#endif
35n/a
36n/a/* static objects at module-level */
37n/a
38n/aPyObject* pysqlite_Error, *pysqlite_Warning, *pysqlite_InterfaceError, *pysqlite_DatabaseError,
39n/a *pysqlite_InternalError, *pysqlite_OperationalError, *pysqlite_ProgrammingError,
40n/a *pysqlite_IntegrityError, *pysqlite_DataError, *pysqlite_NotSupportedError;
41n/a
42n/aPyObject* converters;
43n/aint _enable_callback_tracebacks;
44n/aint pysqlite_BaseTypeAdapted;
45n/a
46n/astatic PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
47n/a kwargs)
48n/a{
49n/a /* Python seems to have no way of extracting a single keyword-arg at
50n/a * C-level, so this code is redundant with the one in connection_init in
51n/a * connection.c and must always be copied from there ... */
52n/a
53n/a static char *kwlist[] = {
54n/a "database", "timeout", "detect_types", "isolation_level",
55n/a "check_same_thread", "factory", "cached_statements", "uri",
56n/a NULL
57n/a };
58n/a char* database;
59n/a int detect_types = 0;
60n/a PyObject* isolation_level;
61n/a PyObject* factory = NULL;
62n/a int check_same_thread = 1;
63n/a int cached_statements;
64n/a int uri = 0;
65n/a double timeout = 5.0;
66n/a
67n/a PyObject* result;
68n/a
69n/a if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOip", kwlist,
70n/a &database, &timeout, &detect_types,
71n/a &isolation_level, &check_same_thread,
72n/a &factory, &cached_statements, &uri))
73n/a {
74n/a return NULL;
75n/a }
76n/a
77n/a if (factory == NULL) {
78n/a factory = (PyObject*)&pysqlite_ConnectionType;
79n/a }
80n/a
81n/a result = PyObject_Call(factory, args, kwargs);
82n/a
83n/a return result;
84n/a}
85n/a
86n/aPyDoc_STRVAR(module_connect_doc,
87n/a"connect(database[, timeout, detect_types, isolation_level,\n\
88n/a check_same_thread, factory, cached_statements, uri])\n\
89n/a\n\
90n/aOpens a connection to the SQLite database file *database*. You can use\n\
91n/a\":memory:\" to open a database connection to a database that resides in\n\
92n/aRAM instead of on disk.");
93n/a
94n/astatic PyObject* module_complete(PyObject* self, PyObject* args, PyObject*
95n/a kwargs)
96n/a{
97n/a static char *kwlist[] = {"statement", NULL, NULL};
98n/a char* statement;
99n/a
100n/a PyObject* result;
101n/a
102n/a if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement))
103n/a {
104n/a return NULL;
105n/a }
106n/a
107n/a if (sqlite3_complete(statement)) {
108n/a result = Py_True;
109n/a } else {
110n/a result = Py_False;
111n/a }
112n/a
113n/a Py_INCREF(result);
114n/a
115n/a return result;
116n/a}
117n/a
118n/aPyDoc_STRVAR(module_complete_doc,
119n/a"complete_statement(sql)\n\
120n/a\n\
121n/aChecks if a string contains a complete SQL statement. Non-standard.");
122n/a
123n/a#ifdef HAVE_SHARED_CACHE
124n/astatic PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject*
125n/a kwargs)
126n/a{
127n/a static char *kwlist[] = {"do_enable", NULL, NULL};
128n/a int do_enable;
129n/a int rc;
130n/a
131n/a if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable))
132n/a {
133n/a return NULL;
134n/a }
135n/a
136n/a rc = sqlite3_enable_shared_cache(do_enable);
137n/a
138n/a if (rc != SQLITE_OK) {
139n/a PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
140n/a return NULL;
141n/a } else {
142n/a Py_RETURN_NONE;
143n/a }
144n/a}
145n/a
146n/aPyDoc_STRVAR(module_enable_shared_cache_doc,
147n/a"enable_shared_cache(do_enable)\n\
148n/a\n\
149n/aEnable or disable shared cache mode for the calling thread.\n\
150n/aExperimental/Non-standard.");
151n/a#endif /* HAVE_SHARED_CACHE */
152n/a
153n/astatic PyObject* module_register_adapter(PyObject* self, PyObject* args)
154n/a{
155n/a PyTypeObject* type;
156n/a PyObject* caster;
157n/a int rc;
158n/a
159n/a if (!PyArg_ParseTuple(args, "OO", &type, &caster)) {
160n/a return NULL;
161n/a }
162n/a
163n/a /* a basic type is adapted; there's a performance optimization if that's not the case
164n/a * (99 % of all usages) */
165n/a if (type == &PyLong_Type || type == &PyFloat_Type
166n/a || type == &PyUnicode_Type || type == &PyByteArray_Type) {
167n/a pysqlite_BaseTypeAdapted = 1;
168n/a }
169n/a
170n/a rc = pysqlite_microprotocols_add(type, (PyObject*)&pysqlite_PrepareProtocolType, caster);
171n/a if (rc == -1)
172n/a return NULL;
173n/a
174n/a Py_RETURN_NONE;
175n/a}
176n/a
177n/aPyDoc_STRVAR(module_register_adapter_doc,
178n/a"register_adapter(type, callable)\n\
179n/a\n\
180n/aRegisters an adapter with pysqlite's adapter registry. Non-standard.");
181n/a
182n/astatic PyObject* module_register_converter(PyObject* self, PyObject* args)
183n/a{
184n/a PyObject* orig_name;
185n/a PyObject* name = NULL;
186n/a PyObject* callable;
187n/a PyObject* retval = NULL;
188n/a _Py_IDENTIFIER(upper);
189n/a
190n/a if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) {
191n/a return NULL;
192n/a }
193n/a
194n/a /* convert the name to upper case */
195n/a name = _PyObject_CallMethodId(orig_name, &PyId_upper, NULL);
196n/a if (!name) {
197n/a goto error;
198n/a }
199n/a
200n/a if (PyDict_SetItem(converters, name, callable) != 0) {
201n/a goto error;
202n/a }
203n/a
204n/a Py_INCREF(Py_None);
205n/a retval = Py_None;
206n/aerror:
207n/a Py_XDECREF(name);
208n/a return retval;
209n/a}
210n/a
211n/aPyDoc_STRVAR(module_register_converter_doc,
212n/a"register_converter(typename, callable)\n\
213n/a\n\
214n/aRegisters a converter with pysqlite. Non-standard.");
215n/a
216n/astatic PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args)
217n/a{
218n/a if (!PyArg_ParseTuple(args, "i", &_enable_callback_tracebacks)) {
219n/a return NULL;
220n/a }
221n/a
222n/a Py_RETURN_NONE;
223n/a}
224n/a
225n/aPyDoc_STRVAR(enable_callback_tracebacks_doc,
226n/a"enable_callback_tracebacks(flag)\n\
227n/a\n\
228n/aEnable or disable callback functions throwing errors to stderr.");
229n/a
230n/astatic void converters_init(PyObject* dict)
231n/a{
232n/a converters = PyDict_New();
233n/a if (!converters) {
234n/a return;
235n/a }
236n/a
237n/a PyDict_SetItemString(dict, "converters", converters);
238n/a}
239n/a
240n/astatic PyMethodDef module_methods[] = {
241n/a {"connect", (PyCFunction)module_connect,
242n/a METH_VARARGS | METH_KEYWORDS, module_connect_doc},
243n/a {"complete_statement", (PyCFunction)module_complete,
244n/a METH_VARARGS | METH_KEYWORDS, module_complete_doc},
245n/a#ifdef HAVE_SHARED_CACHE
246n/a {"enable_shared_cache", (PyCFunction)module_enable_shared_cache,
247n/a METH_VARARGS | METH_KEYWORDS, module_enable_shared_cache_doc},
248n/a#endif
249n/a {"register_adapter", (PyCFunction)module_register_adapter,
250n/a METH_VARARGS, module_register_adapter_doc},
251n/a {"register_converter", (PyCFunction)module_register_converter,
252n/a METH_VARARGS, module_register_converter_doc},
253n/a {"adapt", (PyCFunction)pysqlite_adapt, METH_VARARGS,
254n/a pysqlite_adapt_doc},
255n/a {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks,
256n/a METH_VARARGS, enable_callback_tracebacks_doc},
257n/a {NULL, NULL}
258n/a};
259n/a
260n/astruct _IntConstantPair {
261n/a const char *constant_name;
262n/a int constant_value;
263n/a};
264n/a
265n/atypedef struct _IntConstantPair IntConstantPair;
266n/a
267n/astatic const IntConstantPair _int_constants[] = {
268n/a {"PARSE_DECLTYPES", PARSE_DECLTYPES},
269n/a {"PARSE_COLNAMES", PARSE_COLNAMES},
270n/a
271n/a {"SQLITE_OK", SQLITE_OK},
272n/a {"SQLITE_DENY", SQLITE_DENY},
273n/a {"SQLITE_IGNORE", SQLITE_IGNORE},
274n/a {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX},
275n/a {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE},
276n/a {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX},
277n/a {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE},
278n/a {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER},
279n/a {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW},
280n/a {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER},
281n/a {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW},
282n/a {"SQLITE_DELETE", SQLITE_DELETE},
283n/a {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX},
284n/a {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE},
285n/a {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX},
286n/a {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE},
287n/a {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER},
288n/a {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW},
289n/a {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER},
290n/a {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW},
291n/a {"SQLITE_INSERT", SQLITE_INSERT},
292n/a {"SQLITE_PRAGMA", SQLITE_PRAGMA},
293n/a {"SQLITE_READ", SQLITE_READ},
294n/a {"SQLITE_SELECT", SQLITE_SELECT},
295n/a {"SQLITE_TRANSACTION", SQLITE_TRANSACTION},
296n/a {"SQLITE_UPDATE", SQLITE_UPDATE},
297n/a {"SQLITE_ATTACH", SQLITE_ATTACH},
298n/a {"SQLITE_DETACH", SQLITE_DETACH},
299n/a#if SQLITE_VERSION_NUMBER >= 3002001
300n/a {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE},
301n/a {"SQLITE_REINDEX", SQLITE_REINDEX},
302n/a#endif
303n/a#if SQLITE_VERSION_NUMBER >= 3003000
304n/a {"SQLITE_ANALYZE", SQLITE_ANALYZE},
305n/a#endif
306n/a#if SQLITE_VERSION_NUMBER >= 3003007
307n/a {"SQLITE_CREATE_VTABLE", SQLITE_CREATE_VTABLE},
308n/a {"SQLITE_DROP_VTABLE", SQLITE_DROP_VTABLE},
309n/a#endif
310n/a#if SQLITE_VERSION_NUMBER >= 3003008
311n/a {"SQLITE_FUNCTION", SQLITE_FUNCTION},
312n/a#endif
313n/a#if SQLITE_VERSION_NUMBER >= 3006008
314n/a {"SQLITE_SAVEPOINT", SQLITE_SAVEPOINT},
315n/a#endif
316n/a#if SQLITE_VERSION_NUMBER >= 3008003
317n/a {"SQLITE_RECURSIVE", SQLITE_RECURSIVE},
318n/a#endif
319n/a {(char*)NULL, 0}
320n/a};
321n/a
322n/a
323n/astatic struct PyModuleDef _sqlite3module = {
324n/a PyModuleDef_HEAD_INIT,
325n/a "_sqlite3",
326n/a NULL,
327n/a -1,
328n/a module_methods,
329n/a NULL,
330n/a NULL,
331n/a NULL,
332n/a NULL
333n/a};
334n/a
335n/aPyMODINIT_FUNC PyInit__sqlite3(void)
336n/a{
337n/a PyObject *module, *dict;
338n/a PyObject *tmp_obj;
339n/a int i;
340n/a
341n/a module = PyModule_Create(&_sqlite3module);
342n/a
343n/a if (!module ||
344n/a (pysqlite_row_setup_types() < 0) ||
345n/a (pysqlite_cursor_setup_types() < 0) ||
346n/a (pysqlite_connection_setup_types() < 0) ||
347n/a (pysqlite_cache_setup_types() < 0) ||
348n/a (pysqlite_statement_setup_types() < 0) ||
349n/a (pysqlite_prepare_protocol_setup_types() < 0)
350n/a ) {
351n/a Py_XDECREF(module);
352n/a return NULL;
353n/a }
354n/a
355n/a Py_INCREF(&pysqlite_ConnectionType);
356n/a PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType);
357n/a Py_INCREF(&pysqlite_CursorType);
358n/a PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType);
359n/a Py_INCREF(&pysqlite_CacheType);
360n/a PyModule_AddObject(module, "Statement", (PyObject*)&pysqlite_StatementType);
361n/a Py_INCREF(&pysqlite_StatementType);
362n/a PyModule_AddObject(module, "Cache", (PyObject*) &pysqlite_CacheType);
363n/a Py_INCREF(&pysqlite_PrepareProtocolType);
364n/a PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &pysqlite_PrepareProtocolType);
365n/a Py_INCREF(&pysqlite_RowType);
366n/a PyModule_AddObject(module, "Row", (PyObject*) &pysqlite_RowType);
367n/a
368n/a if (!(dict = PyModule_GetDict(module))) {
369n/a goto error;
370n/a }
371n/a
372n/a /*** Create DB-API Exception hierarchy */
373n/a
374n/a if (!(pysqlite_Error = PyErr_NewException(MODULE_NAME ".Error", PyExc_Exception, NULL))) {
375n/a goto error;
376n/a }
377n/a PyDict_SetItemString(dict, "Error", pysqlite_Error);
378n/a
379n/a if (!(pysqlite_Warning = PyErr_NewException(MODULE_NAME ".Warning", PyExc_Exception, NULL))) {
380n/a goto error;
381n/a }
382n/a PyDict_SetItemString(dict, "Warning", pysqlite_Warning);
383n/a
384n/a /* Error subclasses */
385n/a
386n/a if (!(pysqlite_InterfaceError = PyErr_NewException(MODULE_NAME ".InterfaceError", pysqlite_Error, NULL))) {
387n/a goto error;
388n/a }
389n/a PyDict_SetItemString(dict, "InterfaceError", pysqlite_InterfaceError);
390n/a
391n/a if (!(pysqlite_DatabaseError = PyErr_NewException(MODULE_NAME ".DatabaseError", pysqlite_Error, NULL))) {
392n/a goto error;
393n/a }
394n/a PyDict_SetItemString(dict, "DatabaseError", pysqlite_DatabaseError);
395n/a
396n/a /* pysqlite_DatabaseError subclasses */
397n/a
398n/a if (!(pysqlite_InternalError = PyErr_NewException(MODULE_NAME ".InternalError", pysqlite_DatabaseError, NULL))) {
399n/a goto error;
400n/a }
401n/a PyDict_SetItemString(dict, "InternalError", pysqlite_InternalError);
402n/a
403n/a if (!(pysqlite_OperationalError = PyErr_NewException(MODULE_NAME ".OperationalError", pysqlite_DatabaseError, NULL))) {
404n/a goto error;
405n/a }
406n/a PyDict_SetItemString(dict, "OperationalError", pysqlite_OperationalError);
407n/a
408n/a if (!(pysqlite_ProgrammingError = PyErr_NewException(MODULE_NAME ".ProgrammingError", pysqlite_DatabaseError, NULL))) {
409n/a goto error;
410n/a }
411n/a PyDict_SetItemString(dict, "ProgrammingError", pysqlite_ProgrammingError);
412n/a
413n/a if (!(pysqlite_IntegrityError = PyErr_NewException(MODULE_NAME ".IntegrityError", pysqlite_DatabaseError,NULL))) {
414n/a goto error;
415n/a }
416n/a PyDict_SetItemString(dict, "IntegrityError", pysqlite_IntegrityError);
417n/a
418n/a if (!(pysqlite_DataError = PyErr_NewException(MODULE_NAME ".DataError", pysqlite_DatabaseError, NULL))) {
419n/a goto error;
420n/a }
421n/a PyDict_SetItemString(dict, "DataError", pysqlite_DataError);
422n/a
423n/a if (!(pysqlite_NotSupportedError = PyErr_NewException(MODULE_NAME ".NotSupportedError", pysqlite_DatabaseError, NULL))) {
424n/a goto error;
425n/a }
426n/a PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError);
427n/a
428n/a /* In Python 2.x, setting Connection.text_factory to
429n/a OptimizedUnicode caused Unicode objects to be returned for
430n/a non-ASCII data and bytestrings to be returned for ASCII data.
431n/a Now OptimizedUnicode is an alias for str, so it has no
432n/a effect. */
433n/a Py_INCREF((PyObject*)&PyUnicode_Type);
434n/a PyDict_SetItemString(dict, "OptimizedUnicode", (PyObject*)&PyUnicode_Type);
435n/a
436n/a /* Set integer constants */
437n/a for (i = 0; _int_constants[i].constant_name != 0; i++) {
438n/a tmp_obj = PyLong_FromLong(_int_constants[i].constant_value);
439n/a if (!tmp_obj) {
440n/a goto error;
441n/a }
442n/a PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj);
443n/a Py_DECREF(tmp_obj);
444n/a }
445n/a
446n/a if (!(tmp_obj = PyUnicode_FromString(PYSQLITE_VERSION))) {
447n/a goto error;
448n/a }
449n/a PyDict_SetItemString(dict, "version", tmp_obj);
450n/a Py_DECREF(tmp_obj);
451n/a
452n/a if (!(tmp_obj = PyUnicode_FromString(sqlite3_libversion()))) {
453n/a goto error;
454n/a }
455n/a PyDict_SetItemString(dict, "sqlite_version", tmp_obj);
456n/a Py_DECREF(tmp_obj);
457n/a
458n/a /* initialize microprotocols layer */
459n/a pysqlite_microprotocols_init(dict);
460n/a
461n/a /* initialize the default converters */
462n/a converters_init(dict);
463n/a
464n/a _enable_callback_tracebacks = 0;
465n/a
466n/a pysqlite_BaseTypeAdapted = 0;
467n/a
468n/a /* Original comment from _bsddb.c in the Python core. This is also still
469n/a * needed nowadays for Python 2.3/2.4.
470n/a *
471n/a * PyEval_InitThreads is called here due to a quirk in python 1.5
472n/a * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>:
473n/a * The global interpreter lock is not initialized until the first
474n/a * thread is created using thread.start_new_thread() or fork() is
475n/a * called. that would cause the ALLOW_THREADS here to segfault due
476n/a * to a null pointer reference if no threads or child processes
477n/a * have been created. This works around that and is a no-op if
478n/a * threads have already been initialized.
479n/a * (see pybsddb-users mailing list post on 2002-08-07)
480n/a */
481n/a#ifdef WITH_THREAD
482n/a PyEval_InitThreads();
483n/a#endif
484n/a
485n/aerror:
486n/a if (PyErr_Occurred())
487n/a {
488n/a PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
489n/a Py_DECREF(module);
490n/a module = NULL;
491n/a }
492n/a return module;
493n/a}