1 /* module.c - the module itself
2 *
3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
4 *
5 * This file is part of pysqlite.
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24 #include "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 < 3007015
33 #error "SQLite 3.7.15 or higher required"
34 #endif
35
36 #include "clinic/module.c.h"
37 /*[clinic input]
38 module _sqlite3
39 [clinic start generated code]*/
40 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=81e330492d57488e]*/
41
42 /* static objects at module-level */
43
44 PyObject *pysqlite_Error = NULL;
45 PyObject *pysqlite_Warning = NULL;
46 PyObject *pysqlite_InterfaceError = NULL;
47 PyObject *pysqlite_DatabaseError = NULL;
48 PyObject *pysqlite_InternalError = NULL;
49 PyObject *pysqlite_OperationalError = NULL;
50 PyObject *pysqlite_ProgrammingError = NULL;
51 PyObject *pysqlite_IntegrityError = NULL;
52 PyObject *pysqlite_DataError = NULL;
53 PyObject *pysqlite_NotSupportedError = NULL;
54
55 PyObject* _pysqlite_converters = NULL;
56 int _pysqlite_enable_callback_tracebacks = 0;
57 int pysqlite_BaseTypeAdapted = 0;
58
module_connect(PyObject * self,PyObject * args,PyObject * kwargs)59 static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
60 kwargs)
61 {
62 /* Python seems to have no way of extracting a single keyword-arg at
63 * C-level, so this code is redundant with the one in connection_init in
64 * connection.c and must always be copied from there ... */
65
66 static char *kwlist[] = {
67 "database", "timeout", "detect_types", "isolation_level",
68 "check_same_thread", "factory", "cached_statements", "uri",
69 NULL
70 };
71 PyObject* database;
72 int detect_types = 0;
73 PyObject* isolation_level;
74 PyObject* factory = NULL;
75 int check_same_thread = 1;
76 int cached_statements;
77 int uri = 0;
78 double timeout = 5.0;
79
80 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOip", kwlist,
81 &database, &timeout, &detect_types,
82 &isolation_level, &check_same_thread,
83 &factory, &cached_statements, &uri))
84 {
85 return NULL;
86 }
87
88 if (factory == NULL) {
89 factory = (PyObject*)pysqlite_ConnectionType;
90 }
91
92 return PyObject_Call(factory, args, kwargs);
93 }
94
95 PyDoc_STRVAR(module_connect_doc,
96 "connect(database[, timeout, detect_types, isolation_level,\n\
97 check_same_thread, factory, cached_statements, uri])\n\
98 \n\
99 Opens a connection to the SQLite database file *database*. You can use\n\
100 \":memory:\" to open a database connection to a database that resides in\n\
101 RAM instead of on disk.");
102
103 /*[clinic input]
104 _sqlite3.complete_statement as pysqlite_complete_statement
105
106 statement: str
107
108 Checks if a string contains a complete SQL statement. Non-standard.
109 [clinic start generated code]*/
110
111 static PyObject *
pysqlite_complete_statement_impl(PyObject * module,const char * statement)112 pysqlite_complete_statement_impl(PyObject *module, const char *statement)
113 /*[clinic end generated code: output=e55f1ff1952df558 input=f6b24996b31c5c33]*/
114 {
115 if (sqlite3_complete(statement)) {
116 return Py_NewRef(Py_True);
117 } else {
118 return Py_NewRef(Py_False);
119 }
120 }
121
122 /*[clinic input]
123 _sqlite3.enable_shared_cache as pysqlite_enable_shared_cache
124
125 do_enable: int
126
127 Enable or disable shared cache mode for the calling thread.
128
129 Experimental/Non-standard.
130 [clinic start generated code]*/
131
132 static PyObject *
pysqlite_enable_shared_cache_impl(PyObject * module,int do_enable)133 pysqlite_enable_shared_cache_impl(PyObject *module, int do_enable)
134 /*[clinic end generated code: output=259c74eedee1516b input=8400e41bc58b6b24]*/
135 {
136 int rc;
137
138 rc = sqlite3_enable_shared_cache(do_enable);
139
140 if (rc != SQLITE_OK) {
141 PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
142 return NULL;
143 } else {
144 Py_RETURN_NONE;
145 }
146 }
147
148 /*[clinic input]
149 _sqlite3.register_adapter as pysqlite_register_adapter
150
151 type: object(type='PyTypeObject *')
152 caster: object
153 /
154
155 Registers an adapter with pysqlite's adapter registry. Non-standard.
156 [clinic start generated code]*/
157
158 static PyObject *
pysqlite_register_adapter_impl(PyObject * module,PyTypeObject * type,PyObject * caster)159 pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,
160 PyObject *caster)
161 /*[clinic end generated code: output=a287e8db18e8af23 input=839dad90e2492725]*/
162 {
163 int rc;
164
165 /* a basic type is adapted; there's a performance optimization if that's not the case
166 * (99 % of all usages) */
167 if (type == &PyLong_Type || type == &PyFloat_Type
168 || type == &PyUnicode_Type || type == &PyByteArray_Type) {
169 pysqlite_BaseTypeAdapted = 1;
170 }
171
172 rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster);
173 if (rc == -1)
174 return NULL;
175
176 Py_RETURN_NONE;
177 }
178
179 /*[clinic input]
180 _sqlite3.register_converter as pysqlite_register_converter
181
182 name as orig_name: unicode
183 converter as callable: object
184 /
185
186 Registers a converter with pysqlite. Non-standard.
187 [clinic start generated code]*/
188
189 static PyObject *
pysqlite_register_converter_impl(PyObject * module,PyObject * orig_name,PyObject * callable)190 pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
191 PyObject *callable)
192 /*[clinic end generated code: output=a2f2bfeed7230062 input=e074cf7f4890544f]*/
193 {
194 PyObject* name = NULL;
195 PyObject* retval = NULL;
196 _Py_IDENTIFIER(upper);
197
198 /* convert the name to upper case */
199 name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper);
200 if (!name) {
201 goto error;
202 }
203
204 if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) {
205 goto error;
206 }
207
208 retval = Py_NewRef(Py_None);
209 error:
210 Py_XDECREF(name);
211 return retval;
212 }
213
214 /*[clinic input]
215 _sqlite3.enable_callback_tracebacks as pysqlite_enable_callback_trace
216
217 enable: int
218 /
219
220 Enable or disable callback functions throwing errors to stderr.
221 [clinic start generated code]*/
222
223 static PyObject *
pysqlite_enable_callback_trace_impl(PyObject * module,int enable)224 pysqlite_enable_callback_trace_impl(PyObject *module, int enable)
225 /*[clinic end generated code: output=4ff1d051c698f194 input=cb79d3581eb77c40]*/
226 {
227 _pysqlite_enable_callback_tracebacks = enable;
228
229 Py_RETURN_NONE;
230 }
231
232 /*[clinic input]
233 _sqlite3.adapt as pysqlite_adapt
234
235 obj: object
236 proto: object(c_default='(PyObject*)pysqlite_PrepareProtocolType') = PrepareProtocolType
237 alt: object = NULL
238 /
239
240 Adapt given object to given protocol. Non-standard.
241 [clinic start generated code]*/
242
243 static PyObject *
pysqlite_adapt_impl(PyObject * module,PyObject * obj,PyObject * proto,PyObject * alt)244 pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto,
245 PyObject *alt)
246 /*[clinic end generated code: output=0c3927c5fcd23dd9 input=a58ab77fb5ae22dd]*/
247 {
248 return pysqlite_microprotocols_adapt(obj, proto, alt);
249 }
250
converters_init(PyObject * module)251 static int converters_init(PyObject* module)
252 {
253 _pysqlite_converters = PyDict_New();
254 if (!_pysqlite_converters) {
255 return -1;
256 }
257
258 int res = PyModule_AddObjectRef(module, "converters", _pysqlite_converters);
259 Py_DECREF(_pysqlite_converters);
260
261 return res;
262 }
263
264 static PyMethodDef module_methods[] = {
265 {"connect", (PyCFunction)(void(*)(void))module_connect,
266 METH_VARARGS | METH_KEYWORDS, module_connect_doc},
267 PYSQLITE_ADAPT_METHODDEF
268 PYSQLITE_COMPLETE_STATEMENT_METHODDEF
269 PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF
270 PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF
271 PYSQLITE_REGISTER_ADAPTER_METHODDEF
272 PYSQLITE_REGISTER_CONVERTER_METHODDEF
273 {NULL, NULL}
274 };
275
276 static int
add_integer_constants(PyObject * module)277 add_integer_constants(PyObject *module) {
278 #define ADD_INT(ival) \
279 do { \
280 if (PyModule_AddIntConstant(module, #ival, ival) < 0) { \
281 return -1; \
282 } \
283 } while (0); \
284
285 ADD_INT(PARSE_DECLTYPES);
286 ADD_INT(PARSE_COLNAMES);
287 ADD_INT(SQLITE_OK);
288 ADD_INT(SQLITE_DENY);
289 ADD_INT(SQLITE_IGNORE);
290 ADD_INT(SQLITE_CREATE_INDEX);
291 ADD_INT(SQLITE_CREATE_TABLE);
292 ADD_INT(SQLITE_CREATE_TEMP_INDEX);
293 ADD_INT(SQLITE_CREATE_TEMP_TABLE);
294 ADD_INT(SQLITE_CREATE_TEMP_TRIGGER);
295 ADD_INT(SQLITE_CREATE_TEMP_VIEW);
296 ADD_INT(SQLITE_CREATE_TRIGGER);
297 ADD_INT(SQLITE_CREATE_VIEW);
298 ADD_INT(SQLITE_DELETE);
299 ADD_INT(SQLITE_DROP_INDEX);
300 ADD_INT(SQLITE_DROP_TABLE);
301 ADD_INT(SQLITE_DROP_TEMP_INDEX);
302 ADD_INT(SQLITE_DROP_TEMP_TABLE);
303 ADD_INT(SQLITE_DROP_TEMP_TRIGGER);
304 ADD_INT(SQLITE_DROP_TEMP_VIEW);
305 ADD_INT(SQLITE_DROP_TRIGGER);
306 ADD_INT(SQLITE_DROP_VIEW);
307 ADD_INT(SQLITE_INSERT);
308 ADD_INT(SQLITE_PRAGMA);
309 ADD_INT(SQLITE_READ);
310 ADD_INT(SQLITE_SELECT);
311 ADD_INT(SQLITE_TRANSACTION);
312 ADD_INT(SQLITE_UPDATE);
313 ADD_INT(SQLITE_ATTACH);
314 ADD_INT(SQLITE_DETACH);
315 ADD_INT(SQLITE_ALTER_TABLE);
316 ADD_INT(SQLITE_REINDEX);
317 ADD_INT(SQLITE_ANALYZE);
318 ADD_INT(SQLITE_CREATE_VTABLE);
319 ADD_INT(SQLITE_DROP_VTABLE);
320 ADD_INT(SQLITE_FUNCTION);
321 ADD_INT(SQLITE_SAVEPOINT);
322 #if SQLITE_VERSION_NUMBER >= 3008003
323 ADD_INT(SQLITE_RECURSIVE);
324 #endif
325 ADD_INT(SQLITE_DONE);
326 #undef ADD_INT
327 return 0;
328 }
329
330 static struct PyModuleDef _sqlite3module = {
331 PyModuleDef_HEAD_INIT,
332 "_sqlite3",
333 NULL,
334 -1,
335 module_methods,
336 NULL,
337 NULL,
338 NULL,
339 NULL
340 };
341
342 #define ADD_TYPE(module, type) \
343 do { \
344 if (PyModule_AddType(module, &type) < 0) { \
345 goto error; \
346 } \
347 } while (0)
348
349 #define ADD_EXCEPTION(module, name, exc, base) \
350 do { \
351 exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \
352 if (!exc) { \
353 goto error; \
354 } \
355 int res = PyModule_AddObjectRef(module, name, exc); \
356 Py_DECREF(exc); \
357 if (res < 0) { \
358 goto error; \
359 } \
360 } while (0)
361
PyInit__sqlite3(void)362 PyMODINIT_FUNC PyInit__sqlite3(void)
363 {
364 PyObject *module;
365
366 if (sqlite3_libversion_number() < 3007015) {
367 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.15 or higher required");
368 return NULL;
369 }
370
371 int rc = sqlite3_initialize();
372 if (rc != SQLITE_OK) {
373 PyErr_SetString(PyExc_ImportError, sqlite3_errstr(rc));
374 return NULL;
375 }
376
377 module = PyModule_Create(&_sqlite3module);
378
379 if (!module ||
380 (pysqlite_row_setup_types(module) < 0) ||
381 (pysqlite_cursor_setup_types(module) < 0) ||
382 (pysqlite_connection_setup_types(module) < 0) ||
383 (pysqlite_cache_setup_types(module) < 0) ||
384 (pysqlite_statement_setup_types(module) < 0) ||
385 (pysqlite_prepare_protocol_setup_types(module) < 0)
386 ) {
387 goto error;
388 }
389
390 ADD_TYPE(module, *pysqlite_ConnectionType);
391 ADD_TYPE(module, *pysqlite_CursorType);
392 ADD_TYPE(module, *pysqlite_PrepareProtocolType);
393 ADD_TYPE(module, *pysqlite_RowType);
394
395 /*** Create DB-API Exception hierarchy */
396 ADD_EXCEPTION(module, "Error", pysqlite_Error, PyExc_Exception);
397 ADD_EXCEPTION(module, "Warning", pysqlite_Warning, PyExc_Exception);
398
399 /* Error subclasses */
400 ADD_EXCEPTION(module, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error);
401 ADD_EXCEPTION(module, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error);
402
403 /* pysqlite_DatabaseError subclasses */
404 ADD_EXCEPTION(module, "InternalError", pysqlite_InternalError, pysqlite_DatabaseError);
405 ADD_EXCEPTION(module, "OperationalError", pysqlite_OperationalError, pysqlite_DatabaseError);
406 ADD_EXCEPTION(module, "ProgrammingError", pysqlite_ProgrammingError, pysqlite_DatabaseError);
407 ADD_EXCEPTION(module, "IntegrityError", pysqlite_IntegrityError, pysqlite_DatabaseError);
408 ADD_EXCEPTION(module, "DataError", pysqlite_DataError, pysqlite_DatabaseError);
409 ADD_EXCEPTION(module, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError);
410
411 /* Set integer constants */
412 if (add_integer_constants(module) < 0) {
413 goto error;
414 }
415
416 if (PyModule_AddStringConstant(module, "version", PYSQLITE_VERSION) < 0) {
417 goto error;
418 }
419
420 if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) {
421 goto error;
422 }
423
424 /* initialize microprotocols layer */
425 if (pysqlite_microprotocols_init(module) < 0) {
426 goto error;
427 }
428
429 /* initialize the default converters */
430 if (converters_init(module) < 0) {
431 goto error;
432 }
433
434 return module;
435
436 error:
437 sqlite3_shutdown();
438 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
439 Py_XDECREF(module);
440 return NULL;
441 }
442