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