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 | 207c321 | 2020-09-07 23:26:54 +0200 | [diff] [blame] | 32 | #if SQLITE_VERSION_NUMBER < 3007003 |
| 33 | #error "SQLite 3.7.3 or higher required" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | /* static objects at module-level */ |
| 37 | |
Benjamin Peterson | 0a37a30 | 2017-12-31 10:04:13 -0800 | [diff] [blame] | 38 | PyObject *pysqlite_Error = NULL; |
| 39 | PyObject *pysqlite_Warning = NULL; |
| 40 | PyObject *pysqlite_InterfaceError = NULL; |
| 41 | PyObject *pysqlite_DatabaseError = NULL; |
| 42 | PyObject *pysqlite_InternalError = NULL; |
| 43 | PyObject *pysqlite_OperationalError = NULL; |
| 44 | PyObject *pysqlite_ProgrammingError = NULL; |
| 45 | PyObject *pysqlite_IntegrityError = NULL; |
| 46 | PyObject *pysqlite_DataError = NULL; |
| 47 | PyObject *pysqlite_NotSupportedError = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 48 | |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 49 | PyObject* _pysqlite_converters = NULL; |
| 50 | int _pysqlite_enable_callback_tracebacks = 0; |
Benjamin Peterson | 0a37a30 | 2017-12-31 10:04:13 -0800 | [diff] [blame] | 51 | int pysqlite_BaseTypeAdapted = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 52 | |
| 53 | static PyObject* module_connect(PyObject* self, PyObject* args, PyObject* |
| 54 | kwargs) |
| 55 | { |
| 56 | /* Python seems to have no way of extracting a single keyword-arg at |
| 57 | * C-level, so this code is redundant with the one in connection_init in |
| 58 | * connection.c and must always be copied from there ... */ |
| 59 | |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 60 | static char *kwlist[] = { |
| 61 | "database", "timeout", "detect_types", "isolation_level", |
| 62 | "check_same_thread", "factory", "cached_statements", "uri", |
| 63 | NULL |
| 64 | }; |
Anders Lorentsen | a22a127 | 2017-11-07 01:47:43 +0100 | [diff] [blame] | 65 | PyObject* database; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 66 | int detect_types = 0; |
| 67 | PyObject* isolation_level; |
| 68 | PyObject* factory = NULL; |
| 69 | int check_same_thread = 1; |
| 70 | int cached_statements; |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 71 | int uri = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 72 | double timeout = 5.0; |
| 73 | |
| 74 | PyObject* result; |
| 75 | |
Anders Lorentsen | a22a127 | 2017-11-07 01:47:43 +0100 | [diff] [blame] | 76 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOip", kwlist, |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 77 | &database, &timeout, &detect_types, |
| 78 | &isolation_level, &check_same_thread, |
| 79 | &factory, &cached_statements, &uri)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 80 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (factory == NULL) { |
Erlend Egeberg Aasland | 256e54a | 2020-10-01 16:03:21 +0200 | [diff] [blame^] | 85 | factory = (PyObject*)pysqlite_ConnectionType; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 88 | if (PySys_Audit("sqlite3.connect", "O", database) < 0) { |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 92 | result = PyObject_Call(factory, args, kwargs); |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 97 | PyDoc_STRVAR(module_connect_doc, |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 98 | "connect(database[, timeout, detect_types, isolation_level,\n\ |
| 99 | check_same_thread, factory, cached_statements, uri])\n\ |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 100 | \n\ |
| 101 | Opens a connection to the SQLite database file *database*. You can use\n\ |
| 102 | \":memory:\" to open a database connection to a database that resides in\n\ |
| 103 | RAM instead of on disk."); |
| 104 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 105 | static PyObject* module_complete(PyObject* self, PyObject* args, PyObject* |
| 106 | kwargs) |
| 107 | { |
Alex Henrie | 188bb5b | 2020-01-29 21:12:53 -0700 | [diff] [blame] | 108 | static char *kwlist[] = {"statement", NULL}; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 109 | char* statement; |
| 110 | |
| 111 | PyObject* result; |
| 112 | |
| 113 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement)) |
| 114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (sqlite3_complete(statement)) { |
| 119 | result = Py_True; |
| 120 | } else { |
| 121 | result = Py_False; |
| 122 | } |
| 123 | |
| 124 | Py_INCREF(result); |
| 125 | |
| 126 | return result; |
| 127 | } |
| 128 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 129 | PyDoc_STRVAR(module_complete_doc, |
| 130 | "complete_statement(sql)\n\ |
| 131 | \n\ |
| 132 | Checks if a string contains a complete SQL statement. Non-standard."); |
| 133 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 134 | static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject* |
| 135 | kwargs) |
| 136 | { |
Alex Henrie | 188bb5b | 2020-01-29 21:12:53 -0700 | [diff] [blame] | 137 | static char *kwlist[] = {"do_enable", NULL}; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 138 | int do_enable; |
| 139 | int rc; |
| 140 | |
| 141 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable)) |
| 142 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 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 | |
| 156 | PyDoc_STRVAR(module_enable_shared_cache_doc, |
| 157 | "enable_shared_cache(do_enable)\n\ |
| 158 | \n\ |
| 159 | Enable or disable shared cache mode for the calling thread.\n\ |
| 160 | Experimental/Non-standard."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 161 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 162 | static PyObject* module_register_adapter(PyObject* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 163 | { |
| 164 | PyTypeObject* type; |
| 165 | PyObject* caster; |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 166 | int rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 167 | |
| 168 | if (!PyArg_ParseTuple(args, "OO", &type, &caster)) { |
| 169 | return NULL; |
| 170 | } |
| 171 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 172 | /* 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 Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 175 | || type == &PyUnicode_Type || type == &PyByteArray_Type) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 176 | pysqlite_BaseTypeAdapted = 1; |
| 177 | } |
| 178 | |
Erlend Egeberg Aasland | cb6db8b | 2020-09-29 00:05:04 +0200 | [diff] [blame] | 179 | rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 180 | if (rc == -1) |
| 181 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 182 | |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 183 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 186 | PyDoc_STRVAR(module_register_adapter_doc, |
| 187 | "register_adapter(type, callable)\n\ |
| 188 | \n\ |
| 189 | Registers an adapter with pysqlite's adapter registry. Non-standard."); |
| 190 | |
| 191 | static PyObject* module_register_converter(PyObject* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 192 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 193 | PyObject* orig_name; |
| 194 | PyObject* name = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 195 | PyObject* callable; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 196 | PyObject* retval = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 197 | _Py_IDENTIFIER(upper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 199 | if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 200 | return NULL; |
| 201 | } |
| 202 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 203 | /* convert the name to upper case */ |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 204 | name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 205 | if (!name) { |
| 206 | goto error; |
| 207 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 208 | |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 209 | if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 210 | goto error; |
| 211 | } |
| 212 | |
| 213 | Py_INCREF(Py_None); |
| 214 | retval = Py_None; |
| 215 | error: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 216 | Py_XDECREF(name); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 217 | return retval; |
| 218 | } |
| 219 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 220 | PyDoc_STRVAR(module_register_converter_doc, |
| 221 | "register_converter(typename, callable)\n\ |
| 222 | \n\ |
| 223 | Registers a converter with pysqlite. Non-standard."); |
| 224 | |
| 225 | static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 226 | { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 227 | if (!PyArg_ParseTuple(args, "i", &_pysqlite_enable_callback_tracebacks)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 228 | return NULL; |
| 229 | } |
| 230 | |
Berker Peksag | fe21de9 | 2016-04-09 07:34:39 +0300 | [diff] [blame] | 231 | Py_RETURN_NONE; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 234 | PyDoc_STRVAR(enable_callback_tracebacks_doc, |
| 235 | "enable_callback_tracebacks(flag)\n\ |
| 236 | \n\ |
| 237 | Enable or disable callback functions throwing errors to stderr."); |
| 238 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 239 | static void converters_init(PyObject* dict) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 240 | { |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 241 | _pysqlite_converters = PyDict_New(); |
| 242 | if (!_pysqlite_converters) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
Benjamin Peterson | 7762e4d | 2018-07-09 21:20:23 -0700 | [diff] [blame] | 246 | PyDict_SetItemString(dict, "converters", _pysqlite_converters); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static PyMethodDef module_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 250 | {"connect", (PyCFunction)(void(*)(void))module_connect, |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 251 | METH_VARARGS | METH_KEYWORDS, module_connect_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 252 | {"complete_statement", (PyCFunction)(void(*)(void))module_complete, |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 253 | METH_VARARGS | METH_KEYWORDS, module_complete_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 254 | {"enable_shared_cache", (PyCFunction)(void(*)(void))module_enable_shared_cache, |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 255 | METH_VARARGS | METH_KEYWORDS, module_enable_shared_cache_doc}, |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 256 | {"register_adapter", (PyCFunction)module_register_adapter, |
| 257 | METH_VARARGS, module_register_adapter_doc}, |
| 258 | {"register_converter", (PyCFunction)module_register_converter, |
| 259 | METH_VARARGS, module_register_converter_doc}, |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 260 | {"adapt", (PyCFunction)pysqlite_adapt, METH_VARARGS, |
| 261 | pysqlite_adapt_doc}, |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 262 | {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks, |
| 263 | METH_VARARGS, enable_callback_tracebacks_doc}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 264 | {NULL, NULL} |
| 265 | }; |
| 266 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 267 | struct _IntConstantPair { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 268 | const char *constant_name; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 269 | int constant_value; |
| 270 | }; |
| 271 | |
| 272 | typedef struct _IntConstantPair IntConstantPair; |
| 273 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 274 | static const IntConstantPair _int_constants[] = { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 275 | {"PARSE_DECLTYPES", PARSE_DECLTYPES}, |
| 276 | {"PARSE_COLNAMES", PARSE_COLNAMES}, |
| 277 | |
| 278 | {"SQLITE_OK", SQLITE_OK}, |
| 279 | {"SQLITE_DENY", SQLITE_DENY}, |
| 280 | {"SQLITE_IGNORE", SQLITE_IGNORE}, |
| 281 | {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX}, |
| 282 | {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE}, |
| 283 | {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX}, |
| 284 | {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE}, |
| 285 | {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER}, |
| 286 | {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW}, |
| 287 | {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER}, |
| 288 | {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW}, |
| 289 | {"SQLITE_DELETE", SQLITE_DELETE}, |
| 290 | {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX}, |
| 291 | {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE}, |
| 292 | {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX}, |
| 293 | {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE}, |
| 294 | {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER}, |
| 295 | {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW}, |
| 296 | {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER}, |
| 297 | {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW}, |
| 298 | {"SQLITE_INSERT", SQLITE_INSERT}, |
| 299 | {"SQLITE_PRAGMA", SQLITE_PRAGMA}, |
| 300 | {"SQLITE_READ", SQLITE_READ}, |
| 301 | {"SQLITE_SELECT", SQLITE_SELECT}, |
| 302 | {"SQLITE_TRANSACTION", SQLITE_TRANSACTION}, |
| 303 | {"SQLITE_UPDATE", SQLITE_UPDATE}, |
| 304 | {"SQLITE_ATTACH", SQLITE_ATTACH}, |
| 305 | {"SQLITE_DETACH", SQLITE_DETACH}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 306 | {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE}, |
| 307 | {"SQLITE_REINDEX", SQLITE_REINDEX}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 308 | {"SQLITE_ANALYZE", SQLITE_ANALYZE}, |
Berker Peksag | 00b1ae0 | 2017-01-02 06:38:10 +0300 | [diff] [blame] | 309 | {"SQLITE_CREATE_VTABLE", SQLITE_CREATE_VTABLE}, |
| 310 | {"SQLITE_DROP_VTABLE", SQLITE_DROP_VTABLE}, |
Berker Peksag | 00b1ae0 | 2017-01-02 06:38:10 +0300 | [diff] [blame] | 311 | {"SQLITE_FUNCTION", SQLITE_FUNCTION}, |
Berker Peksag | 00b1ae0 | 2017-01-02 06:38:10 +0300 | [diff] [blame] | 312 | {"SQLITE_SAVEPOINT", SQLITE_SAVEPOINT}, |
Berker Peksag | 00b1ae0 | 2017-01-02 06:38:10 +0300 | [diff] [blame] | 313 | #if SQLITE_VERSION_NUMBER >= 3008003 |
| 314 | {"SQLITE_RECURSIVE", SQLITE_RECURSIVE}, |
| 315 | #endif |
Emanuele Gaifas | d7aed41 | 2018-03-10 23:08:31 +0100 | [diff] [blame] | 316 | {"SQLITE_DONE", SQLITE_DONE}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 317 | {(char*)NULL, 0} |
| 318 | }; |
| 319 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 320 | |
| 321 | static struct PyModuleDef _sqlite3module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | PyModuleDef_HEAD_INIT, |
| 323 | "_sqlite3", |
| 324 | NULL, |
| 325 | -1, |
| 326 | module_methods, |
| 327 | NULL, |
| 328 | NULL, |
| 329 | NULL, |
| 330 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
Erlend Egeberg Aasland | 5eb45d7 | 2020-05-26 14:18:19 +0200 | [diff] [blame] | 333 | #define ADD_TYPE(module, type) \ |
| 334 | do { \ |
| 335 | if (PyModule_AddType(module, &type) < 0) { \ |
| 336 | Py_DECREF(module); \ |
| 337 | return NULL; \ |
| 338 | } \ |
| 339 | } while (0) |
| 340 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 341 | PyMODINIT_FUNC PyInit__sqlite3(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 342 | { |
| 343 | PyObject *module, *dict; |
| 344 | PyObject *tmp_obj; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 345 | int i; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | |
Erlend Egeberg Aasland | 207c321 | 2020-09-07 23:26:54 +0200 | [diff] [blame] | 347 | if (sqlite3_libversion_number() < 3007003) { |
| 348 | PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.3 or higher required"); |
| 349 | return NULL; |
| 350 | } |
| 351 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 352 | module = PyModule_Create(&_sqlite3module); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 353 | |
| 354 | if (!module || |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 355 | (pysqlite_row_setup_types(module) < 0) || |
Erlend Egeberg Aasland | 256e54a | 2020-10-01 16:03:21 +0200 | [diff] [blame^] | 356 | (pysqlite_cursor_setup_types(module) < 0) || |
| 357 | (pysqlite_connection_setup_types(module) < 0) || |
Erlend Egeberg Aasland | a937ab4 | 2020-09-27 14:14:50 +0200 | [diff] [blame] | 358 | (pysqlite_cache_setup_types(module) < 0) || |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 359 | (pysqlite_statement_setup_types(module) < 0) || |
Erlend Egeberg Aasland | cb6db8b | 2020-09-29 00:05:04 +0200 | [diff] [blame] | 360 | (pysqlite_prepare_protocol_setup_types(module) < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 361 | ) { |
Brett Cannon | e144507 | 2011-02-04 20:24:02 +0000 | [diff] [blame] | 362 | Py_XDECREF(module); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 363 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Erlend Egeberg Aasland | 256e54a | 2020-10-01 16:03:21 +0200 | [diff] [blame^] | 366 | ADD_TYPE(module, *pysqlite_ConnectionType); |
| 367 | ADD_TYPE(module, *pysqlite_CursorType); |
Erlend Egeberg Aasland | cb6db8b | 2020-09-29 00:05:04 +0200 | [diff] [blame] | 368 | ADD_TYPE(module, *pysqlite_PrepareProtocolType); |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 369 | ADD_TYPE(module, *pysqlite_RowType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 370 | |
| 371 | if (!(dict = PyModule_GetDict(module))) { |
| 372 | goto error; |
| 373 | } |
| 374 | |
| 375 | /*** Create DB-API Exception hierarchy */ |
| 376 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 377 | if (!(pysqlite_Error = PyErr_NewException(MODULE_NAME ".Error", PyExc_Exception, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 378 | goto error; |
| 379 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 380 | PyDict_SetItemString(dict, "Error", pysqlite_Error); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 382 | if (!(pysqlite_Warning = PyErr_NewException(MODULE_NAME ".Warning", PyExc_Exception, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 383 | goto error; |
| 384 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 385 | PyDict_SetItemString(dict, "Warning", pysqlite_Warning); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 386 | |
| 387 | /* Error subclasses */ |
| 388 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 389 | if (!(pysqlite_InterfaceError = PyErr_NewException(MODULE_NAME ".InterfaceError", pysqlite_Error, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 390 | goto error; |
| 391 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 392 | PyDict_SetItemString(dict, "InterfaceError", pysqlite_InterfaceError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 393 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 394 | if (!(pysqlite_DatabaseError = PyErr_NewException(MODULE_NAME ".DatabaseError", pysqlite_Error, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 395 | goto error; |
| 396 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 397 | PyDict_SetItemString(dict, "DatabaseError", pysqlite_DatabaseError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 398 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 399 | /* pysqlite_DatabaseError subclasses */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 400 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 401 | if (!(pysqlite_InternalError = PyErr_NewException(MODULE_NAME ".InternalError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 402 | goto error; |
| 403 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 404 | PyDict_SetItemString(dict, "InternalError", pysqlite_InternalError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 405 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 406 | if (!(pysqlite_OperationalError = PyErr_NewException(MODULE_NAME ".OperationalError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 407 | goto error; |
| 408 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 409 | PyDict_SetItemString(dict, "OperationalError", pysqlite_OperationalError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 410 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 411 | if (!(pysqlite_ProgrammingError = PyErr_NewException(MODULE_NAME ".ProgrammingError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 412 | goto error; |
| 413 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 414 | PyDict_SetItemString(dict, "ProgrammingError", pysqlite_ProgrammingError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 415 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 416 | if (!(pysqlite_IntegrityError = PyErr_NewException(MODULE_NAME ".IntegrityError", pysqlite_DatabaseError,NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 417 | goto error; |
| 418 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 419 | PyDict_SetItemString(dict, "IntegrityError", pysqlite_IntegrityError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 420 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 421 | if (!(pysqlite_DataError = PyErr_NewException(MODULE_NAME ".DataError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 422 | goto error; |
| 423 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 424 | PyDict_SetItemString(dict, "DataError", pysqlite_DataError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 425 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 426 | if (!(pysqlite_NotSupportedError = PyErr_NewException(MODULE_NAME ".NotSupportedError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 427 | goto error; |
| 428 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 429 | PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 430 | |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 431 | /* In Python 2.x, setting Connection.text_factory to |
| 432 | OptimizedUnicode caused Unicode objects to be returned for |
| 433 | non-ASCII data and bytestrings to be returned for ASCII data. |
| 434 | Now OptimizedUnicode is an alias for str, so it has no |
| 435 | effect. */ |
| 436 | Py_INCREF((PyObject*)&PyUnicode_Type); |
| 437 | PyDict_SetItemString(dict, "OptimizedUnicode", (PyObject*)&PyUnicode_Type); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 438 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 439 | /* Set integer constants */ |
Serhiy Storchaka | 0b3ec19 | 2017-03-23 17:53:47 +0200 | [diff] [blame] | 440 | for (i = 0; _int_constants[i].constant_name != NULL; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 441 | tmp_obj = PyLong_FromLong(_int_constants[i].constant_value); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 442 | if (!tmp_obj) { |
| 443 | goto error; |
| 444 | } |
| 445 | PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj); |
| 446 | Py_DECREF(tmp_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 447 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 448 | |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 449 | if (!(tmp_obj = PyUnicode_FromString(PYSQLITE_VERSION))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 450 | goto error; |
| 451 | } |
| 452 | PyDict_SetItemString(dict, "version", tmp_obj); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 453 | Py_DECREF(tmp_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 454 | |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 455 | if (!(tmp_obj = PyUnicode_FromString(sqlite3_libversion()))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 456 | goto error; |
| 457 | } |
| 458 | PyDict_SetItemString(dict, "sqlite_version", tmp_obj); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 459 | Py_DECREF(tmp_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 460 | |
| 461 | /* initialize microprotocols layer */ |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 462 | pysqlite_microprotocols_init(dict); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 463 | |
| 464 | /* initialize the default converters */ |
| 465 | converters_init(dict); |
| 466 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 467 | error: |
| 468 | if (PyErr_Occurred()) |
| 469 | { |
| 470 | PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | Py_DECREF(module); |
| 472 | module = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 473 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 474 | return module; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 475 | } |