1 | /* module.c - the module itself |
2 | * |
3 | * Copyright (C) 2004-2010 Gerhard Häring <[email protected]> |
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 | |
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. |
109 | [clinic start generated code]*/ |
110 | |
111 | static PyObject * |
112 | pysqlite_complete_statement_impl(PyObject *module, const char *statement) |
113 | /*[clinic end generated code: output=e55f1ff1952df558 input=ac45d257375bb828]*/ |
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 | This method is deprecated and will be removed in Python 3.12. |
130 | Shared cache is strongly discouraged by the SQLite 3 documentation. |
131 | If shared cache must be used, open the database in URI mode using |
132 | the cache=shared query parameter. |
133 | [clinic start generated code]*/ |
134 | |
135 | static PyObject * |
136 | pysqlite_enable_shared_cache_impl(PyObject *module, int do_enable) |
137 | /*[clinic end generated code: output=259c74eedee1516b input=26e40d5971d3487d]*/ |
138 | { |
139 | int rc; |
140 | |
141 | rc = sqlite3_enable_shared_cache(do_enable); |
142 | |
143 | if (rc != SQLITE_OK) { |
144 | PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed" ); |
145 | return NULL; |
146 | } else { |
147 | Py_RETURN_NONE; |
148 | } |
149 | } |
150 | |
151 | /*[clinic input] |
152 | _sqlite3.register_adapter as pysqlite_register_adapter |
153 | |
154 | type: object(type='PyTypeObject *') |
155 | caster: object |
156 | / |
157 | |
158 | Registers an adapter with sqlite3's adapter registry. |
159 | [clinic start generated code]*/ |
160 | |
161 | static PyObject * |
162 | pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type, |
163 | PyObject *caster) |
164 | /*[clinic end generated code: output=a287e8db18e8af23 input=b4bd87afcadc535d]*/ |
165 | { |
166 | int rc; |
167 | |
168 | /* a basic type is adapted; there's a performance optimization if that's not the case |
169 | * (99 % of all usages) */ |
170 | if (type == &PyLong_Type || type == &PyFloat_Type |
171 | || type == &PyUnicode_Type || type == &PyByteArray_Type) { |
172 | pysqlite_BaseTypeAdapted = 1; |
173 | } |
174 | |
175 | rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster); |
176 | if (rc == -1) |
177 | return NULL; |
178 | |
179 | Py_RETURN_NONE; |
180 | } |
181 | |
182 | /*[clinic input] |
183 | _sqlite3.register_converter as pysqlite_register_converter |
184 | |
185 | name as orig_name: unicode |
186 | converter as callable: object |
187 | / |
188 | |
189 | Registers a converter with sqlite3. |
190 | [clinic start generated code]*/ |
191 | |
192 | static PyObject * |
193 | pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name, |
194 | PyObject *callable) |
195 | /*[clinic end generated code: output=a2f2bfeed7230062 input=90f645419425d6c4]*/ |
196 | { |
197 | PyObject* name = NULL; |
198 | PyObject* retval = NULL; |
199 | _Py_IDENTIFIER(upper); |
200 | |
201 | /* convert the name to upper case */ |
202 | name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper); |
203 | if (!name) { |
204 | goto error; |
205 | } |
206 | |
207 | if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) { |
208 | goto error; |
209 | } |
210 | |
211 | retval = Py_NewRef(Py_None); |
212 | error: |
213 | Py_XDECREF(name); |
214 | return retval; |
215 | } |
216 | |
217 | /*[clinic input] |
218 | _sqlite3.enable_callback_tracebacks as pysqlite_enable_callback_trace |
219 | |
220 | enable: int |
221 | / |
222 | |
223 | Enable or disable callback functions throwing errors to stderr. |
224 | [clinic start generated code]*/ |
225 | |
226 | static PyObject * |
227 | pysqlite_enable_callback_trace_impl(PyObject *module, int enable) |
228 | /*[clinic end generated code: output=4ff1d051c698f194 input=cb79d3581eb77c40]*/ |
229 | { |
230 | _pysqlite_enable_callback_tracebacks = enable; |
231 | |
232 | Py_RETURN_NONE; |
233 | } |
234 | |
235 | /*[clinic input] |
236 | _sqlite3.adapt as pysqlite_adapt |
237 | |
238 | obj: object |
239 | proto: object(c_default='(PyObject*)pysqlite_PrepareProtocolType') = PrepareProtocolType |
240 | alt: object = NULL |
241 | / |
242 | |
243 | Adapt given object to given protocol. |
244 | [clinic start generated code]*/ |
245 | |
246 | static PyObject * |
247 | pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto, |
248 | PyObject *alt) |
249 | /*[clinic end generated code: output=0c3927c5fcd23dd9 input=46ca9564710ba48a]*/ |
250 | { |
251 | return pysqlite_microprotocols_adapt(obj, proto, alt); |
252 | } |
253 | |
254 | static int converters_init(PyObject* module) |
255 | { |
256 | _pysqlite_converters = PyDict_New(); |
257 | if (!_pysqlite_converters) { |
258 | return -1; |
259 | } |
260 | |
261 | int res = PyModule_AddObjectRef(module, "converters" , _pysqlite_converters); |
262 | Py_DECREF(_pysqlite_converters); |
263 | |
264 | return res; |
265 | } |
266 | |
267 | static PyMethodDef module_methods[] = { |
268 | {"connect" , (PyCFunction)(void(*)(void))module_connect, |
269 | METH_VARARGS | METH_KEYWORDS, module_connect_doc}, |
270 | PYSQLITE_ADAPT_METHODDEF |
271 | PYSQLITE_COMPLETE_STATEMENT_METHODDEF |
272 | PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF |
273 | PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF |
274 | PYSQLITE_REGISTER_ADAPTER_METHODDEF |
275 | PYSQLITE_REGISTER_CONVERTER_METHODDEF |
276 | {NULL, NULL} |
277 | }; |
278 | |
279 | static int |
280 | add_integer_constants(PyObject *module) { |
281 | #define ADD_INT(ival) \ |
282 | do { \ |
283 | if (PyModule_AddIntConstant(module, #ival, ival) < 0) { \ |
284 | return -1; \ |
285 | } \ |
286 | } while (0); \ |
287 | |
288 | ADD_INT(PARSE_DECLTYPES); |
289 | ADD_INT(PARSE_COLNAMES); |
290 | ADD_INT(SQLITE_OK); |
291 | ADD_INT(SQLITE_DENY); |
292 | ADD_INT(SQLITE_IGNORE); |
293 | ADD_INT(SQLITE_CREATE_INDEX); |
294 | ADD_INT(SQLITE_CREATE_TABLE); |
295 | ADD_INT(SQLITE_CREATE_TEMP_INDEX); |
296 | ADD_INT(SQLITE_CREATE_TEMP_TABLE); |
297 | ADD_INT(SQLITE_CREATE_TEMP_TRIGGER); |
298 | ADD_INT(SQLITE_CREATE_TEMP_VIEW); |
299 | ADD_INT(SQLITE_CREATE_TRIGGER); |
300 | ADD_INT(SQLITE_CREATE_VIEW); |
301 | ADD_INT(SQLITE_DELETE); |
302 | ADD_INT(SQLITE_DROP_INDEX); |
303 | ADD_INT(SQLITE_DROP_TABLE); |
304 | ADD_INT(SQLITE_DROP_TEMP_INDEX); |
305 | ADD_INT(SQLITE_DROP_TEMP_TABLE); |
306 | ADD_INT(SQLITE_DROP_TEMP_TRIGGER); |
307 | ADD_INT(SQLITE_DROP_TEMP_VIEW); |
308 | ADD_INT(SQLITE_DROP_TRIGGER); |
309 | ADD_INT(SQLITE_DROP_VIEW); |
310 | ADD_INT(SQLITE_INSERT); |
311 | ADD_INT(SQLITE_PRAGMA); |
312 | ADD_INT(SQLITE_READ); |
313 | ADD_INT(SQLITE_SELECT); |
314 | ADD_INT(SQLITE_TRANSACTION); |
315 | ADD_INT(SQLITE_UPDATE); |
316 | ADD_INT(SQLITE_ATTACH); |
317 | ADD_INT(SQLITE_DETACH); |
318 | ADD_INT(SQLITE_ALTER_TABLE); |
319 | ADD_INT(SQLITE_REINDEX); |
320 | ADD_INT(SQLITE_ANALYZE); |
321 | ADD_INT(SQLITE_CREATE_VTABLE); |
322 | ADD_INT(SQLITE_DROP_VTABLE); |
323 | ADD_INT(SQLITE_FUNCTION); |
324 | ADD_INT(SQLITE_SAVEPOINT); |
325 | #if SQLITE_VERSION_NUMBER >= 3008003 |
326 | ADD_INT(SQLITE_RECURSIVE); |
327 | #endif |
328 | ADD_INT(SQLITE_DONE); |
329 | #undef ADD_INT |
330 | return 0; |
331 | } |
332 | |
333 | static struct PyModuleDef _sqlite3module = { |
334 | PyModuleDef_HEAD_INIT, |
335 | "_sqlite3" , |
336 | NULL, |
337 | -1, |
338 | module_methods, |
339 | NULL, |
340 | NULL, |
341 | NULL, |
342 | NULL |
343 | }; |
344 | |
345 | #define ADD_TYPE(module, type) \ |
346 | do { \ |
347 | if (PyModule_AddType(module, &type) < 0) { \ |
348 | goto error; \ |
349 | } \ |
350 | } while (0) |
351 | |
352 | #define ADD_EXCEPTION(module, name, exc, base) \ |
353 | do { \ |
354 | exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \ |
355 | if (!exc) { \ |
356 | goto error; \ |
357 | } \ |
358 | int res = PyModule_AddObjectRef(module, name, exc); \ |
359 | Py_DECREF(exc); \ |
360 | if (res < 0) { \ |
361 | goto error; \ |
362 | } \ |
363 | } while (0) |
364 | |
365 | PyMODINIT_FUNC PyInit__sqlite3(void) |
366 | { |
367 | PyObject *module; |
368 | |
369 | if (sqlite3_libversion_number() < 3007015) { |
370 | PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.15 or higher required" ); |
371 | return NULL; |
372 | } |
373 | |
374 | int rc = sqlite3_initialize(); |
375 | if (rc != SQLITE_OK) { |
376 | PyErr_SetString(PyExc_ImportError, sqlite3_errstr(rc)); |
377 | return NULL; |
378 | } |
379 | |
380 | module = PyModule_Create(&_sqlite3module); |
381 | |
382 | if (!module || |
383 | (pysqlite_row_setup_types(module) < 0) || |
384 | (pysqlite_cursor_setup_types(module) < 0) || |
385 | (pysqlite_connection_setup_types(module) < 0) || |
386 | (pysqlite_cache_setup_types(module) < 0) || |
387 | (pysqlite_statement_setup_types(module) < 0) || |
388 | (pysqlite_prepare_protocol_setup_types(module) < 0) |
389 | ) { |
390 | goto error; |
391 | } |
392 | |
393 | ADD_TYPE(module, *pysqlite_ConnectionType); |
394 | ADD_TYPE(module, *pysqlite_CursorType); |
395 | ADD_TYPE(module, *pysqlite_PrepareProtocolType); |
396 | ADD_TYPE(module, *pysqlite_RowType); |
397 | |
398 | /*** Create DB-API Exception hierarchy */ |
399 | ADD_EXCEPTION(module, "Error" , pysqlite_Error, PyExc_Exception); |
400 | ADD_EXCEPTION(module, "Warning" , pysqlite_Warning, PyExc_Exception); |
401 | |
402 | /* Error subclasses */ |
403 | ADD_EXCEPTION(module, "InterfaceError" , pysqlite_InterfaceError, pysqlite_Error); |
404 | ADD_EXCEPTION(module, "DatabaseError" , pysqlite_DatabaseError, pysqlite_Error); |
405 | |
406 | /* pysqlite_DatabaseError subclasses */ |
407 | ADD_EXCEPTION(module, "InternalError" , pysqlite_InternalError, pysqlite_DatabaseError); |
408 | ADD_EXCEPTION(module, "OperationalError" , pysqlite_OperationalError, pysqlite_DatabaseError); |
409 | ADD_EXCEPTION(module, "ProgrammingError" , pysqlite_ProgrammingError, pysqlite_DatabaseError); |
410 | ADD_EXCEPTION(module, "IntegrityError" , pysqlite_IntegrityError, pysqlite_DatabaseError); |
411 | ADD_EXCEPTION(module, "DataError" , pysqlite_DataError, pysqlite_DatabaseError); |
412 | ADD_EXCEPTION(module, "NotSupportedError" , pysqlite_NotSupportedError, pysqlite_DatabaseError); |
413 | |
414 | /* Set integer constants */ |
415 | if (add_integer_constants(module) < 0) { |
416 | goto error; |
417 | } |
418 | |
419 | if (PyModule_AddStringConstant(module, "version" , PYSQLITE_VERSION) < 0) { |
420 | goto error; |
421 | } |
422 | |
423 | if (PyModule_AddStringConstant(module, "sqlite_version" , sqlite3_libversion())) { |
424 | goto error; |
425 | } |
426 | |
427 | /* initialize microprotocols layer */ |
428 | if (pysqlite_microprotocols_init(module) < 0) { |
429 | goto error; |
430 | } |
431 | |
432 | /* initialize the default converters */ |
433 | if (converters_init(module) < 0) { |
434 | goto error; |
435 | } |
436 | |
437 | return module; |
438 | |
439 | error: |
440 | sqlite3_shutdown(); |
441 | PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed" ); |
442 | Py_XDECREF(module); |
443 | return NULL; |
444 | } |
445 | |