blob: 324994641b4a4a865be88fc8b56dcc5644ab85cd [file] [log] [blame]
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001/* module.c - the module itself
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Gerhard Häringe7ea7452008-03-29 00:45:29 +00004 *
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
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +010032#if SQLITE_VERSION_NUMBER < 3007015
33#error "SQLite 3.7.15 or higher required"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#endif
35
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +010036#include "clinic/module.c.h"
37/*[clinic input]
38module _sqlite3
39[clinic start generated code]*/
40/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81e330492d57488e]*/
41
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042/* static objects at module-level */
43
Benjamin Peterson0a37a302017-12-31 10:04:13 -080044PyObject *pysqlite_Error = NULL;
45PyObject *pysqlite_Warning = NULL;
46PyObject *pysqlite_InterfaceError = NULL;
47PyObject *pysqlite_DatabaseError = NULL;
48PyObject *pysqlite_InternalError = NULL;
49PyObject *pysqlite_OperationalError = NULL;
50PyObject *pysqlite_ProgrammingError = NULL;
51PyObject *pysqlite_IntegrityError = NULL;
52PyObject *pysqlite_DataError = NULL;
53PyObject *pysqlite_NotSupportedError = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054
Benjamin Peterson7762e4d2018-07-09 21:20:23 -070055PyObject* _pysqlite_converters = NULL;
56int _pysqlite_enable_callback_tracebacks = 0;
Benjamin Peterson0a37a302017-12-31 10:04:13 -080057int pysqlite_BaseTypeAdapted = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058
59static 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
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010066 static char *kwlist[] = {
67 "database", "timeout", "detect_types", "isolation_level",
68 "check_same_thread", "factory", "cached_statements", "uri",
69 NULL
70 };
Anders Lorentsena22a1272017-11-07 01:47:43 +010071 PyObject* database;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072 int detect_types = 0;
73 PyObject* isolation_level;
74 PyObject* factory = NULL;
75 int check_same_thread = 1;
76 int cached_statements;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010077 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 double timeout = 5.0;
79
80 PyObject* result;
81
Anders Lorentsena22a1272017-11-07 01:47:43 +010082 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOip", kwlist,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010083 &database, &timeout, &detect_types,
84 &isolation_level, &check_same_thread,
85 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088 }
89
90 if (factory == NULL) {
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +020091 factory = (PyObject*)pysqlite_ConnectionType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092 }
93
94 result = PyObject_Call(factory, args, kwargs);
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +020095 if (result == NULL) {
96 return NULL;
97 }
98
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000099 return result;
100}
101
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000102PyDoc_STRVAR(module_connect_doc,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100103"connect(database[, timeout, detect_types, isolation_level,\n\
104 check_same_thread, factory, cached_statements, uri])\n\
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000105\n\
106Opens a connection to the SQLite database file *database*. You can use\n\
107\":memory:\" to open a database connection to a database that resides in\n\
108RAM instead of on disk.");
109
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100110/*[clinic input]
111_sqlite3.complete_statement as pysqlite_complete_statement
112
113 statement: str
114
115Checks if a string contains a complete SQL statement. Non-standard.
116[clinic start generated code]*/
117
118static PyObject *
119pysqlite_complete_statement_impl(PyObject *module, const char *statement)
120/*[clinic end generated code: output=e55f1ff1952df558 input=f6b24996b31c5c33]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000121{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000122 if (sqlite3_complete(statement)) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100123 return Py_NewRef(Py_True);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000124 } else {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100125 return Py_NewRef(Py_False);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127}
128
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100129/*[clinic input]
130_sqlite3.enable_shared_cache as pysqlite_enable_shared_cache
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000131
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100132 do_enable: int
133
134Enable or disable shared cache mode for the calling thread.
135
136Experimental/Non-standard.
137[clinic start generated code]*/
138
139static PyObject *
140pysqlite_enable_shared_cache_impl(PyObject *module, int do_enable)
141/*[clinic end generated code: output=259c74eedee1516b input=8400e41bc58b6b24]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143 int rc;
144
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000145 rc = sqlite3_enable_shared_cache(do_enable);
146
147 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000148 PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000149 return NULL;
150 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +0300151 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 }
153}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000154
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100155/*[clinic input]
156_sqlite3.register_adapter as pysqlite_register_adapter
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000157
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100158 type: object(type='PyTypeObject *')
159 caster: object
160 /
161
162Registers an adapter with pysqlite's adapter registry. Non-standard.
163[clinic start generated code]*/
164
165static PyObject *
166pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,
167 PyObject *caster)
168/*[clinic end generated code: output=a287e8db18e8af23 input=839dad90e2492725]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169{
Georg Brandl3dbca812008-07-23 16:10:53 +0000170 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000172 /* a basic type is adapted; there's a performance optimization if that's not the case
173 * (99 % of all usages) */
174 if (type == &PyLong_Type || type == &PyFloat_Type
Christian Heimes9c4756e2008-05-26 13:22:05 +0000175 || type == &PyUnicode_Type || type == &PyByteArray_Type) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000176 pysqlite_BaseTypeAdapted = 1;
177 }
178
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200179 rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster);
Georg Brandl3dbca812008-07-23 16:10:53 +0000180 if (rc == -1)
181 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182
Berker Peksagfe21de92016-04-09 07:34:39 +0300183 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000184}
185
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100186/*[clinic input]
187_sqlite3.register_converter as pysqlite_register_converter
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000188
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100189 name as orig_name: unicode
190 converter as callable: object
191 /
192
193Registers a converter with pysqlite. Non-standard.
194[clinic start generated code]*/
195
196static PyObject *
197pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
198 PyObject *callable)
199/*[clinic end generated code: output=a2f2bfeed7230062 input=e074cf7f4890544f]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000201 PyObject* name = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000202 PyObject* retval = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200203 _Py_IDENTIFIER(upper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000204
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000205 /* convert the name to upper case */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200206 name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000207 if (!name) {
208 goto error;
209 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000210
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700211 if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000212 goto error;
213 }
214
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100215 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000216error:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000217 Py_XDECREF(name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000218 return retval;
219}
220
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100221/*[clinic input]
222_sqlite3.enable_callback_tracebacks as pysqlite_enable_callback_trace
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000223
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100224 enable: int
225 /
226
227Enable or disable callback functions throwing errors to stderr.
228[clinic start generated code]*/
229
230static PyObject *
231pysqlite_enable_callback_trace_impl(PyObject *module, int enable)
232/*[clinic end generated code: output=4ff1d051c698f194 input=cb79d3581eb77c40]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000233{
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100234 _pysqlite_enable_callback_tracebacks = enable;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000235
Berker Peksagfe21de92016-04-09 07:34:39 +0300236 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237}
238
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100239/*[clinic input]
240_sqlite3.adapt as pysqlite_adapt
241
242 obj: object
243 proto: object(c_default='(PyObject*)pysqlite_PrepareProtocolType') = PrepareProtocolType
244 alt: object = NULL
245 /
246
247Adapt given object to given protocol. Non-standard.
248[clinic start generated code]*/
249
250static PyObject *
251pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto,
252 PyObject *alt)
253/*[clinic end generated code: output=0c3927c5fcd23dd9 input=a58ab77fb5ae22dd]*/
254{
255 return pysqlite_microprotocols_adapt(obj, proto, alt);
256}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000257
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100258static int converters_init(PyObject* module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700260 _pysqlite_converters = PyDict_New();
261 if (!_pysqlite_converters) {
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100262 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263 }
264
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100265 int res = PyModule_AddObjectRef(module, "converters", _pysqlite_converters);
266 Py_DECREF(_pysqlite_converters);
267
268 return res;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269}
270
271static PyMethodDef module_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200272 {"connect", (PyCFunction)(void(*)(void))module_connect,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000273 METH_VARARGS | METH_KEYWORDS, module_connect_doc},
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100274 PYSQLITE_ADAPT_METHODDEF
275 PYSQLITE_COMPLETE_STATEMENT_METHODDEF
276 PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF
277 PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF
278 PYSQLITE_REGISTER_ADAPTER_METHODDEF
279 PYSQLITE_REGISTER_CONVERTER_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000280 {NULL, NULL}
281};
282
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200283static int add_integer_constants(PyObject *module) {
284 int ret = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200286 ret += PyModule_AddIntMacro(module, PARSE_DECLTYPES);
287 ret += PyModule_AddIntMacro(module, PARSE_COLNAMES);
288 ret += PyModule_AddIntMacro(module, SQLITE_OK);
289 ret += PyModule_AddIntMacro(module, SQLITE_DENY);
290 ret += PyModule_AddIntMacro(module, SQLITE_IGNORE);
291 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_INDEX);
292 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TABLE);
293 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_INDEX);
294 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TABLE);
295 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TRIGGER);
296 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_VIEW);
297 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TRIGGER);
298 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VIEW);
299 ret += PyModule_AddIntMacro(module, SQLITE_DELETE);
300 ret += PyModule_AddIntMacro(module, SQLITE_DROP_INDEX);
301 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TABLE);
302 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_INDEX);
303 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TABLE);
304 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TRIGGER);
305 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_VIEW);
306 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TRIGGER);
307 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VIEW);
308 ret += PyModule_AddIntMacro(module, SQLITE_INSERT);
309 ret += PyModule_AddIntMacro(module, SQLITE_PRAGMA);
310 ret += PyModule_AddIntMacro(module, SQLITE_READ);
311 ret += PyModule_AddIntMacro(module, SQLITE_SELECT);
312 ret += PyModule_AddIntMacro(module, SQLITE_TRANSACTION);
313 ret += PyModule_AddIntMacro(module, SQLITE_UPDATE);
314 ret += PyModule_AddIntMacro(module, SQLITE_ATTACH);
315 ret += PyModule_AddIntMacro(module, SQLITE_DETACH);
316 ret += PyModule_AddIntMacro(module, SQLITE_ALTER_TABLE);
317 ret += PyModule_AddIntMacro(module, SQLITE_REINDEX);
318 ret += PyModule_AddIntMacro(module, SQLITE_ANALYZE);
319 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VTABLE);
320 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VTABLE);
321 ret += PyModule_AddIntMacro(module, SQLITE_FUNCTION);
322 ret += PyModule_AddIntMacro(module, SQLITE_SAVEPOINT);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300323#if SQLITE_VERSION_NUMBER >= 3008003
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200324 ret += PyModule_AddIntMacro(module, SQLITE_RECURSIVE);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300325#endif
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200326 ret += PyModule_AddIntMacro(module, SQLITE_DONE);
327 return ret;
328}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000329
330static struct PyModuleDef _sqlite3module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 PyModuleDef_HEAD_INIT,
332 "_sqlite3",
333 NULL,
334 -1,
335 module_methods,
336 NULL,
337 NULL,
338 NULL,
339 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000340};
341
Erlend Egeberg Aasland5eb45d72020-05-26 14:18:19 +0200342#define ADD_TYPE(module, type) \
343do { \
344 if (PyModule_AddType(module, &type) < 0) { \
Erlend Egeberg Aaslanddef91932021-04-14 16:50:16 +0200345 goto error; \
Erlend Egeberg Aasland5eb45d72020-05-26 14:18:19 +0200346 } \
347} while (0)
348
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200349#define ADD_EXCEPTION(module, name, exc, base) \
350do { \
351 exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \
352 if (!exc) { \
353 goto error; \
354 } \
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100355 int res = PyModule_AddObjectRef(module, name, exc); \
356 Py_DECREF(exc); \
357 if (res < 0) { \
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200358 goto error; \
359 } \
360} while (0)
361
Martin v. Löwis1a214512008-06-11 05:26:20 +0000362PyMODINIT_FUNC PyInit__sqlite3(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363{
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200364 PyObject *module;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000365
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100366 if (sqlite3_libversion_number() < 3007015) {
367 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.15 or higher required");
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200368 return NULL;
369 }
370
Erlend Egeberg Aaslanddef91932021-04-14 16:50:16 +0200371 int rc = sqlite3_initialize();
372 if (rc != SQLITE_OK) {
373 PyErr_SetString(PyExc_ImportError, sqlite3_errstr(rc));
374 return NULL;
375 }
376
Martin v. Löwis1a214512008-06-11 05:26:20 +0000377 module = PyModule_Create(&_sqlite3module);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000378
379 if (!module ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200380 (pysqlite_row_setup_types(module) < 0) ||
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200381 (pysqlite_cursor_setup_types(module) < 0) ||
382 (pysqlite_connection_setup_types(module) < 0) ||
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200383 (pysqlite_cache_setup_types(module) < 0) ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200384 (pysqlite_statement_setup_types(module) < 0) ||
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200385 (pysqlite_prepare_protocol_setup_types(module) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 ) {
Erlend Egeberg Aaslanddef91932021-04-14 16:50:16 +0200387 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388 }
389
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200390 ADD_TYPE(module, *pysqlite_ConnectionType);
391 ADD_TYPE(module, *pysqlite_CursorType);
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200392 ADD_TYPE(module, *pysqlite_PrepareProtocolType);
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200393 ADD_TYPE(module, *pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000395 /*** Create DB-API Exception hierarchy */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200396 ADD_EXCEPTION(module, "Error", pysqlite_Error, PyExc_Exception);
397 ADD_EXCEPTION(module, "Warning", pysqlite_Warning, PyExc_Exception);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000398
399 /* Error subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200400 ADD_EXCEPTION(module, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error);
401 ADD_EXCEPTION(module, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000403 /* pysqlite_DatabaseError subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200404 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);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000411 /* Set integer constants */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200412 if (add_integer_constants(module) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413 goto error;
414 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000415
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200416 if (PyModule_AddStringConstant(module, "version", PYSQLITE_VERSION) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000417 goto error;
418 }
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200419
420 if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) {
421 goto error;
422 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423
424 /* initialize microprotocols layer */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200425 if (pysqlite_microprotocols_init(module) < 0) {
426 goto error;
427 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000428
429 /* initialize the default converters */
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100430 if (converters_init(module) < 0) {
431 goto error;
432 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000433
Martin v. Löwis1a214512008-06-11 05:26:20 +0000434 return module;
Erlend Egeberg Aaslanddef91932021-04-14 16:50:16 +0200435
436error:
437 sqlite3_shutdown();
438 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
439 Py_XDECREF(module);
440 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000441}