Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1 | /* module.c - the module itself |
| 2 | * |
Florent Xicluna | c934f32 | 2010-09-03 23:47:32 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 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 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Erlend Egeberg Aasland | cf0b239 | 2021-01-06 01:02:43 +0100 | [diff] [blame] | 32 | #if SQLITE_VERSION_NUMBER < 3007015 |
| 33 | #error "SQLite 3.7.15 or higher required" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 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 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | /* static objects at module-level */ |
| 43 | |
Benjamin Peterson | 0a37a30 | 2017-12-31 10:04:13 -0800 | [diff] [blame] | 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; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 54 | |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 55 | PyObject* _pysqlite_converters = NULL; |
| 56 | int _pysqlite_enable_callback_tracebacks = 0; |
Benjamin Peterson | 0a37a30 | 2017-12-31 10:04:13 -0800 | [diff] [blame] | 57 | int pysqlite_BaseTypeAdapted = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 66 | static char *kwlist[] = { |
| 67 | "database", "timeout", "detect_types", "isolation_level", |
| 68 | "check_same_thread", "factory", "cached_statements", "uri", |
| 69 | NULL |
| 70 | }; |
Anders Lorentsen | a22a127 | 2017-11-07 01:47:43 +0100 | [diff] [blame] | 71 | PyObject* database; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 72 | int detect_types = 0; |
| 73 | PyObject* isolation_level; |
| 74 | PyObject* factory = NULL; |
| 75 | int check_same_thread = 1; |
| 76 | int cached_statements; |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 77 | int uri = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | double timeout = 5.0; |
| 79 | |
| 80 | PyObject* result; |
| 81 | |
Anders Lorentsen | a22a127 | 2017-11-07 01:47:43 +0100 | [diff] [blame] | 82 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOip", kwlist, |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 83 | &database, &timeout, &detect_types, |
| 84 | &isolation_level, &check_same_thread, |
| 85 | &factory, &cached_statements, &uri)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if (factory == NULL) { |
Erlend Egeberg Aasland | 256e54a | 2020-10-01 16:03:21 +0200 | [diff] [blame] | 91 | factory = (PyObject*)pysqlite_ConnectionType; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 94 | if (PySys_Audit("sqlite3.connect", "O", database) < 0) { |
| 95 | return NULL; |
| 96 | } |
| 97 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 98 | result = PyObject_Call(factory, args, kwargs); |
| 99 | |
| 100 | return result; |
| 101 | } |
| 102 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 103 | PyDoc_STRVAR(module_connect_doc, |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 104 | "connect(database[, timeout, detect_types, isolation_level,\n\ |
| 105 | check_same_thread, factory, cached_statements, uri])\n\ |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 106 | \n\ |
| 107 | Opens a connection to the SQLite database file *database*. You can use\n\ |
| 108 | \":memory:\" to open a database connection to a database that resides in\n\ |
| 109 | RAM instead of on disk."); |
| 110 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 111 | /*[clinic input] |
| 112 | _sqlite3.complete_statement as pysqlite_complete_statement |
| 113 | |
| 114 | statement: str |
| 115 | |
| 116 | Checks if a string contains a complete SQL statement. Non-standard. |
| 117 | [clinic start generated code]*/ |
| 118 | |
| 119 | static PyObject * |
| 120 | pysqlite_complete_statement_impl(PyObject *module, const char *statement) |
| 121 | /*[clinic end generated code: output=e55f1ff1952df558 input=f6b24996b31c5c33]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 122 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 123 | if (sqlite3_complete(statement)) { |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 124 | return Py_NewRef(Py_True); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 125 | } else { |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 126 | return Py_NewRef(Py_False); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 127 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 130 | /*[clinic input] |
| 131 | _sqlite3.enable_shared_cache as pysqlite_enable_shared_cache |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 132 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 133 | do_enable: int |
| 134 | |
| 135 | Enable or disable shared cache mode for the calling thread. |
| 136 | |
| 137 | Experimental/Non-standard. |
| 138 | [clinic start generated code]*/ |
| 139 | |
| 140 | static PyObject * |
| 141 | pysqlite_enable_shared_cache_impl(PyObject *module, int do_enable) |
| 142 | /*[clinic end generated code: output=259c74eedee1516b input=8400e41bc58b6b24]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 143 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 144 | int rc; |
| 145 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 146 | rc = sqlite3_enable_shared_cache(do_enable); |
| 147 | |
| 148 | if (rc != SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 149 | PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | } else { |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 152 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 155 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 156 | /*[clinic input] |
| 157 | _sqlite3.register_adapter as pysqlite_register_adapter |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 158 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 159 | type: object(type='PyTypeObject *') |
| 160 | caster: object |
| 161 | / |
| 162 | |
| 163 | Registers an adapter with pysqlite's adapter registry. Non-standard. |
| 164 | [clinic start generated code]*/ |
| 165 | |
| 166 | static PyObject * |
| 167 | pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type, |
| 168 | PyObject *caster) |
| 169 | /*[clinic end generated code: output=a287e8db18e8af23 input=839dad90e2492725]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 170 | { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 171 | int rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 172 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 173 | /* a basic type is adapted; there's a performance optimization if that's not the case |
| 174 | * (99 % of all usages) */ |
| 175 | if (type == &PyLong_Type || type == &PyFloat_Type |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 176 | || type == &PyUnicode_Type || type == &PyByteArray_Type) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 177 | pysqlite_BaseTypeAdapted = 1; |
| 178 | } |
| 179 | |
Erlend Egeberg Aasland | cb6db8b | 2020-09-29 00:05:04 +0200 | [diff] [blame] | 180 | rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 181 | if (rc == -1) |
| 182 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 183 | |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 184 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 187 | /*[clinic input] |
| 188 | _sqlite3.register_converter as pysqlite_register_converter |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 189 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 190 | name as orig_name: unicode |
| 191 | converter as callable: object |
| 192 | / |
| 193 | |
| 194 | Registers a converter with pysqlite. Non-standard. |
| 195 | [clinic start generated code]*/ |
| 196 | |
| 197 | static PyObject * |
| 198 | pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name, |
| 199 | PyObject *callable) |
| 200 | /*[clinic end generated code: output=a2f2bfeed7230062 input=e074cf7f4890544f]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 201 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 202 | PyObject* name = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 203 | PyObject* retval = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 204 | _Py_IDENTIFIER(upper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 205 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 206 | /* convert the name to upper case */ |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 207 | name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 208 | if (!name) { |
| 209 | goto error; |
| 210 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 211 | |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 212 | if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 213 | goto error; |
| 214 | } |
| 215 | |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 216 | retval = Py_NewRef(Py_None); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 217 | error: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 218 | Py_XDECREF(name); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 219 | return retval; |
| 220 | } |
| 221 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 222 | /*[clinic input] |
| 223 | _sqlite3.enable_callback_tracebacks as pysqlite_enable_callback_trace |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 224 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 225 | enable: int |
| 226 | / |
| 227 | |
| 228 | Enable or disable callback functions throwing errors to stderr. |
| 229 | [clinic start generated code]*/ |
| 230 | |
| 231 | static PyObject * |
| 232 | pysqlite_enable_callback_trace_impl(PyObject *module, int enable) |
| 233 | /*[clinic end generated code: output=4ff1d051c698f194 input=cb79d3581eb77c40]*/ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 234 | { |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 235 | _pysqlite_enable_callback_tracebacks = enable; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 236 | |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 237 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 240 | /*[clinic input] |
| 241 | _sqlite3.adapt as pysqlite_adapt |
| 242 | |
| 243 | obj: object |
| 244 | proto: object(c_default='(PyObject*)pysqlite_PrepareProtocolType') = PrepareProtocolType |
| 245 | alt: object = NULL |
| 246 | / |
| 247 | |
| 248 | Adapt given object to given protocol. Non-standard. |
| 249 | [clinic start generated code]*/ |
| 250 | |
| 251 | static PyObject * |
| 252 | pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto, |
| 253 | PyObject *alt) |
| 254 | /*[clinic end generated code: output=0c3927c5fcd23dd9 input=a58ab77fb5ae22dd]*/ |
| 255 | { |
| 256 | return pysqlite_microprotocols_adapt(obj, proto, alt); |
| 257 | } |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 258 | |
Erlend Egeberg Aasland | 789359f | 2020-11-04 20:31:51 +0100 | [diff] [blame] | 259 | static int converters_init(PyObject* module) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 260 | { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 261 | _pysqlite_converters = PyDict_New(); |
| 262 | if (!_pysqlite_converters) { |
Erlend Egeberg Aasland | 789359f | 2020-11-04 20:31:51 +0100 | [diff] [blame] | 263 | return -1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Erlend Egeberg Aasland | 789359f | 2020-11-04 20:31:51 +0100 | [diff] [blame] | 266 | int res = PyModule_AddObjectRef(module, "converters", _pysqlite_converters); |
| 267 | Py_DECREF(_pysqlite_converters); |
| 268 | |
| 269 | return res; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static PyMethodDef module_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 273 | {"connect", (PyCFunction)(void(*)(void))module_connect, |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 274 | METH_VARARGS | METH_KEYWORDS, module_connect_doc}, |
Erlend Egeberg Aasland | 7d21027 | 2020-10-31 07:07:44 +0100 | [diff] [blame] | 275 | PYSQLITE_ADAPT_METHODDEF |
| 276 | PYSQLITE_COMPLETE_STATEMENT_METHODDEF |
| 277 | PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF |
| 278 | PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF |
| 279 | PYSQLITE_REGISTER_ADAPTER_METHODDEF |
| 280 | PYSQLITE_REGISTER_CONVERTER_METHODDEF |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 281 | {NULL, NULL} |
| 282 | }; |
| 283 | |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 284 | static int add_integer_constants(PyObject *module) { |
| 285 | int ret = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 286 | |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 287 | ret += PyModule_AddIntMacro(module, PARSE_DECLTYPES); |
| 288 | ret += PyModule_AddIntMacro(module, PARSE_COLNAMES); |
| 289 | ret += PyModule_AddIntMacro(module, SQLITE_OK); |
| 290 | ret += PyModule_AddIntMacro(module, SQLITE_DENY); |
| 291 | ret += PyModule_AddIntMacro(module, SQLITE_IGNORE); |
| 292 | ret += PyModule_AddIntMacro(module, SQLITE_CREATE_INDEX); |
| 293 | ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TABLE); |
| 294 | ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_INDEX); |
| 295 | ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TABLE); |
| 296 | ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TRIGGER); |
| 297 | ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_VIEW); |
| 298 | ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TRIGGER); |
| 299 | ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VIEW); |
| 300 | ret += PyModule_AddIntMacro(module, SQLITE_DELETE); |
| 301 | ret += PyModule_AddIntMacro(module, SQLITE_DROP_INDEX); |
| 302 | ret += PyModule_AddIntMacro(module, SQLITE_DROP_TABLE); |
| 303 | ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_INDEX); |
| 304 | ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TABLE); |
| 305 | ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TRIGGER); |
| 306 | ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_VIEW); |
| 307 | ret += PyModule_AddIntMacro(module, SQLITE_DROP_TRIGGER); |
| 308 | ret += PyModule_AddIntMacro(module, SQLITE_DROP_VIEW); |
| 309 | ret += PyModule_AddIntMacro(module, SQLITE_INSERT); |
| 310 | ret += PyModule_AddIntMacro(module, SQLITE_PRAGMA); |
| 311 | ret += PyModule_AddIntMacro(module, SQLITE_READ); |
| 312 | ret += PyModule_AddIntMacro(module, SQLITE_SELECT); |
| 313 | ret += PyModule_AddIntMacro(module, SQLITE_TRANSACTION); |
| 314 | ret += PyModule_AddIntMacro(module, SQLITE_UPDATE); |
| 315 | ret += PyModule_AddIntMacro(module, SQLITE_ATTACH); |
| 316 | ret += PyModule_AddIntMacro(module, SQLITE_DETACH); |
| 317 | ret += PyModule_AddIntMacro(module, SQLITE_ALTER_TABLE); |
| 318 | ret += PyModule_AddIntMacro(module, SQLITE_REINDEX); |
| 319 | ret += PyModule_AddIntMacro(module, SQLITE_ANALYZE); |
| 320 | ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VTABLE); |
| 321 | ret += PyModule_AddIntMacro(module, SQLITE_DROP_VTABLE); |
| 322 | ret += PyModule_AddIntMacro(module, SQLITE_FUNCTION); |
| 323 | ret += PyModule_AddIntMacro(module, SQLITE_SAVEPOINT); |
Berker Peksag | 00b1ae0 | 2017-01-02 06:38:10 +0300 | [diff] [blame] | 324 | #if SQLITE_VERSION_NUMBER >= 3008003 |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 325 | ret += PyModule_AddIntMacro(module, SQLITE_RECURSIVE); |
Berker Peksag | 00b1ae0 | 2017-01-02 06:38:10 +0300 | [diff] [blame] | 326 | #endif |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 327 | ret += PyModule_AddIntMacro(module, SQLITE_DONE); |
| 328 | return ret; |
| 329 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 330 | |
| 331 | static struct PyModuleDef _sqlite3module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | PyModuleDef_HEAD_INIT, |
| 333 | "_sqlite3", |
| 334 | NULL, |
| 335 | -1, |
| 336 | module_methods, |
| 337 | NULL, |
| 338 | NULL, |
| 339 | NULL, |
| 340 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 341 | }; |
| 342 | |
Erlend Egeberg Aasland | 5eb45d7 | 2020-05-26 14:18:19 +0200 | [diff] [blame] | 343 | #define ADD_TYPE(module, type) \ |
| 344 | do { \ |
| 345 | if (PyModule_AddType(module, &type) < 0) { \ |
| 346 | Py_DECREF(module); \ |
| 347 | return NULL; \ |
| 348 | } \ |
| 349 | } while (0) |
| 350 | |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 351 | #define ADD_EXCEPTION(module, name, exc, base) \ |
| 352 | do { \ |
| 353 | exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \ |
| 354 | if (!exc) { \ |
| 355 | goto error; \ |
| 356 | } \ |
Erlend Egeberg Aasland | 789359f | 2020-11-04 20:31:51 +0100 | [diff] [blame] | 357 | int res = PyModule_AddObjectRef(module, name, exc); \ |
| 358 | Py_DECREF(exc); \ |
| 359 | if (res < 0) { \ |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 360 | goto error; \ |
| 361 | } \ |
| 362 | } while (0) |
| 363 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 364 | PyMODINIT_FUNC PyInit__sqlite3(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 365 | { |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 366 | PyObject *module; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 367 | |
Erlend Egeberg Aasland | cf0b239 | 2021-01-06 01:02:43 +0100 | [diff] [blame] | 368 | if (sqlite3_libversion_number() < 3007015) { |
| 369 | PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.15 or higher required"); |
Erlend Egeberg Aasland | 207c321 | 2020-09-07 23:26:54 +0200 | [diff] [blame] | 370 | return NULL; |
| 371 | } |
| 372 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 373 | module = PyModule_Create(&_sqlite3module); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 374 | |
| 375 | if (!module || |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 376 | (pysqlite_row_setup_types(module) < 0) || |
Erlend Egeberg Aasland | 256e54a | 2020-10-01 16:03:21 +0200 | [diff] [blame] | 377 | (pysqlite_cursor_setup_types(module) < 0) || |
| 378 | (pysqlite_connection_setup_types(module) < 0) || |
Erlend Egeberg Aasland | a937ab4 | 2020-09-27 14:14:50 +0200 | [diff] [blame] | 379 | (pysqlite_cache_setup_types(module) < 0) || |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 380 | (pysqlite_statement_setup_types(module) < 0) || |
Erlend Egeberg Aasland | cb6db8b | 2020-09-29 00:05:04 +0200 | [diff] [blame] | 381 | (pysqlite_prepare_protocol_setup_types(module) < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 382 | ) { |
Brett Cannon | e144507 | 2011-02-04 20:24:02 +0000 | [diff] [blame] | 383 | Py_XDECREF(module); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 384 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Erlend Egeberg Aasland | 256e54a | 2020-10-01 16:03:21 +0200 | [diff] [blame] | 387 | ADD_TYPE(module, *pysqlite_ConnectionType); |
| 388 | ADD_TYPE(module, *pysqlite_CursorType); |
Erlend Egeberg Aasland | cb6db8b | 2020-09-29 00:05:04 +0200 | [diff] [blame] | 389 | ADD_TYPE(module, *pysqlite_PrepareProtocolType); |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 390 | ADD_TYPE(module, *pysqlite_RowType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 391 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 392 | /*** Create DB-API Exception hierarchy */ |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 393 | ADD_EXCEPTION(module, "Error", pysqlite_Error, PyExc_Exception); |
| 394 | ADD_EXCEPTION(module, "Warning", pysqlite_Warning, PyExc_Exception); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 395 | |
| 396 | /* Error subclasses */ |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 397 | ADD_EXCEPTION(module, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error); |
| 398 | ADD_EXCEPTION(module, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 399 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 400 | /* pysqlite_DatabaseError subclasses */ |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 401 | ADD_EXCEPTION(module, "InternalError", pysqlite_InternalError, pysqlite_DatabaseError); |
| 402 | ADD_EXCEPTION(module, "OperationalError", pysqlite_OperationalError, pysqlite_DatabaseError); |
| 403 | ADD_EXCEPTION(module, "ProgrammingError", pysqlite_ProgrammingError, pysqlite_DatabaseError); |
| 404 | ADD_EXCEPTION(module, "IntegrityError", pysqlite_IntegrityError, pysqlite_DatabaseError); |
| 405 | ADD_EXCEPTION(module, "DataError", pysqlite_DataError, pysqlite_DatabaseError); |
| 406 | ADD_EXCEPTION(module, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 407 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 408 | /* Set integer constants */ |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 409 | if (add_integer_constants(module) < 0) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 410 | goto error; |
| 411 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 412 | |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 413 | if (PyModule_AddStringConstant(module, "version", PYSQLITE_VERSION) < 0) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 414 | goto error; |
| 415 | } |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 416 | |
| 417 | if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) { |
| 418 | goto error; |
| 419 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 420 | |
| 421 | /* initialize microprotocols layer */ |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 422 | if (pysqlite_microprotocols_init(module) < 0) { |
| 423 | goto error; |
| 424 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 425 | |
| 426 | /* initialize the default converters */ |
Erlend Egeberg Aasland | 789359f | 2020-11-04 20:31:51 +0100 | [diff] [blame] | 427 | if (converters_init(module) < 0) { |
| 428 | goto error; |
| 429 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 430 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 431 | error: |
| 432 | if (PyErr_Occurred()) |
| 433 | { |
| 434 | PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | Py_DECREF(module); |
| 436 | module = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 437 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 438 | return module; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 439 | } |