Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | /* statement.c - the statement type |
| 2 | * |
Florent Xicluna | c934f32 | 2010-09-03 23:47:32 +0000 | [diff] [blame] | 3 | * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +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 | */ |
| 23 | |
| 24 | #include "statement.h" |
| 25 | #include "cursor.h" |
| 26 | #include "connection.h" |
| 27 | #include "microprotocols.h" |
| 28 | #include "prepare_protocol.h" |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 29 | #include "util.h" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 30 | |
| 31 | /* prototypes */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 32 | static int pysqlite_check_remaining_sql(const char* tail); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 33 | |
| 34 | typedef enum { |
| 35 | LINECOMMENT_1, |
| 36 | IN_LINECOMMENT, |
| 37 | COMMENTSTART_1, |
| 38 | IN_COMMENT, |
| 39 | COMMENTEND_1, |
| 40 | NORMAL |
| 41 | } parse_remaining_sql_state; |
| 42 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 43 | typedef enum { |
| 44 | TYPE_LONG, |
| 45 | TYPE_FLOAT, |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 46 | TYPE_UNICODE, |
| 47 | TYPE_BUFFER, |
| 48 | TYPE_UNKNOWN |
| 49 | } parameter_type; |
| 50 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 51 | int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 52 | { |
| 53 | const char* tail; |
| 54 | int rc; |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 55 | const char* sql_cstr; |
| 56 | Py_ssize_t sql_cstr_len; |
Berker Peksag | ab994ed | 2016-09-11 12:57:15 +0300 | [diff] [blame] | 57 | const char* p; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | |
| 59 | self->st = NULL; |
| 60 | self->in_use = 0; |
| 61 | |
Victor Stinner | c6a2320 | 2019-06-26 03:16:24 +0200 | [diff] [blame] | 62 | assert(PyUnicode_Check(sql)); |
| 63 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 64 | sql_cstr = PyUnicode_AsUTF8AndSize(sql, &sql_cstr_len); |
Guido van Rossum | fa9a121 | 2007-08-29 03:34:29 +0000 | [diff] [blame] | 65 | if (sql_cstr == NULL) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 66 | rc = PYSQLITE_SQL_WRONG_TYPE; |
| 67 | return rc; |
| 68 | } |
Serhiy Storchaka | 42d67af | 2014-09-11 13:29:05 +0300 | [diff] [blame] | 69 | if (strlen(sql_cstr) != (size_t)sql_cstr_len) { |
| 70 | PyErr_SetString(PyExc_ValueError, "the query contains a null character"); |
| 71 | return PYSQLITE_SQL_WRONG_TYPE; |
| 72 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 73 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 74 | self->in_weakreflist = NULL; |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 75 | self->sql = Py_NewRef(sql); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 76 | |
Berker Peksag | 4a926ca | 2017-02-26 18:22:38 +0300 | [diff] [blame] | 77 | /* Determine if the statement is a DML statement. |
| 78 | SELECT is the only exception. See #9924. */ |
| 79 | self->is_dml = 0; |
Berker Peksag | ab994ed | 2016-09-11 12:57:15 +0300 | [diff] [blame] | 80 | for (p = sql_cstr; *p != 0; p++) { |
| 81 | switch (*p) { |
| 82 | case ' ': |
| 83 | case '\r': |
| 84 | case '\n': |
| 85 | case '\t': |
| 86 | continue; |
| 87 | } |
| 88 | |
Berker Peksag | 8d1e190 | 2018-09-20 14:10:49 +0300 | [diff] [blame] | 89 | self->is_dml = (PyOS_strnicmp(p, "insert", 6) == 0) |
| 90 | || (PyOS_strnicmp(p, "update", 6) == 0) |
| 91 | || (PyOS_strnicmp(p, "delete", 6) == 0) |
| 92 | || (PyOS_strnicmp(p, "replace", 7) == 0); |
Berker Peksag | ab994ed | 2016-09-11 12:57:15 +0300 | [diff] [blame] | 93 | break; |
| 94 | } |
| 95 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 96 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | 5252694 | 2017-09-20 07:36:18 -0700 | [diff] [blame] | 97 | rc = sqlite3_prepare_v2(connection->db, |
| 98 | sql_cstr, |
| 99 | -1, |
| 100 | &self->st, |
| 101 | &tail); |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 102 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 103 | |
| 104 | self->db = connection->db; |
| 105 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 106 | if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 107 | (void)sqlite3_finalize(self->st); |
| 108 | self->st = NULL; |
| 109 | rc = PYSQLITE_TOO_MUCH_SQL; |
| 110 | } |
| 111 | |
| 112 | return rc; |
| 113 | } |
| 114 | |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 115 | int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 116 | { |
| 117 | int rc = SQLITE_OK; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 118 | const char *string; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 119 | Py_ssize_t buflen; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 120 | parameter_type paramtype; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 121 | |
| 122 | if (parameter == Py_None) { |
| 123 | rc = sqlite3_bind_null(self->st, pos); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 124 | goto final; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 127 | if (PyLong_CheckExact(parameter)) { |
| 128 | paramtype = TYPE_LONG; |
| 129 | } else if (PyFloat_CheckExact(parameter)) { |
| 130 | paramtype = TYPE_FLOAT; |
| 131 | } else if (PyUnicode_CheckExact(parameter)) { |
| 132 | paramtype = TYPE_UNICODE; |
| 133 | } else if (PyLong_Check(parameter)) { |
| 134 | paramtype = TYPE_LONG; |
| 135 | } else if (PyFloat_Check(parameter)) { |
| 136 | paramtype = TYPE_FLOAT; |
| 137 | } else if (PyUnicode_Check(parameter)) { |
Gerhard Häring | 6117f42 | 2008-09-22 06:04:51 +0000 | [diff] [blame] | 138 | paramtype = TYPE_UNICODE; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 139 | } else if (PyObject_CheckBuffer(parameter)) { |
| 140 | paramtype = TYPE_BUFFER; |
| 141 | } else { |
| 142 | paramtype = TYPE_UNKNOWN; |
| 143 | } |
| 144 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 145 | switch (paramtype) { |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 146 | case TYPE_LONG: { |
| 147 | sqlite_int64 value = _pysqlite_long_as_int64(parameter); |
| 148 | if (value == -1 && PyErr_Occurred()) |
| 149 | rc = -1; |
| 150 | else |
| 151 | rc = sqlite3_bind_int64(self->st, pos, value); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 152 | break; |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 153 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 154 | case TYPE_FLOAT: |
| 155 | rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter)); |
| 156 | break; |
| 157 | case TYPE_UNICODE: |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 158 | string = PyUnicode_AsUTF8AndSize(parameter, &buflen); |
Victor Stinner | 3f658be | 2013-11-18 01:36:29 +0100 | [diff] [blame] | 159 | if (string == NULL) |
| 160 | return -1; |
| 161 | if (buflen > INT_MAX) { |
| 162 | PyErr_SetString(PyExc_OverflowError, |
| 163 | "string longer than INT_MAX bytes"); |
| 164 | return -1; |
| 165 | } |
| 166 | rc = sqlite3_bind_text(self->st, pos, string, (int)buflen, SQLITE_TRANSIENT); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 167 | break; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 168 | case TYPE_BUFFER: { |
| 169 | Py_buffer view; |
| 170 | if (PyObject_GetBuffer(parameter, &view, PyBUF_SIMPLE) != 0) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 171 | PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer"); |
Victor Stinner | 3f658be | 2013-11-18 01:36:29 +0100 | [diff] [blame] | 172 | return -1; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 173 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 174 | if (view.len > INT_MAX) { |
Victor Stinner | 3f658be | 2013-11-18 01:36:29 +0100 | [diff] [blame] | 175 | PyErr_SetString(PyExc_OverflowError, |
| 176 | "BLOB longer than INT_MAX bytes"); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 177 | PyBuffer_Release(&view); |
Victor Stinner | 3f658be | 2013-11-18 01:36:29 +0100 | [diff] [blame] | 178 | return -1; |
| 179 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 180 | rc = sqlite3_bind_blob(self->st, pos, view.buf, (int)view.len, SQLITE_TRANSIENT); |
| 181 | PyBuffer_Release(&view); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 182 | break; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 183 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 184 | case TYPE_UNKNOWN: |
| 185 | rc = -1; |
| 186 | } |
| 187 | |
| 188 | final: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 189 | return rc; |
| 190 | } |
| 191 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 192 | /* returns 0 if the object is one of Python's internal ones that don't need to be adapted */ |
| 193 | static int _need_adapt(PyObject* obj) |
| 194 | { |
| 195 | if (pysqlite_BaseTypeAdapted) { |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | if (PyLong_CheckExact(obj) || PyFloat_CheckExact(obj) |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 200 | || PyUnicode_CheckExact(obj) || PyByteArray_CheckExact(obj)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 201 | return 0; |
| 202 | } else { |
| 203 | return 1; |
| 204 | } |
| 205 | } |
| 206 | |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 207 | void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 208 | { |
| 209 | PyObject* current_param; |
| 210 | PyObject* adapted; |
| 211 | const char* binding_name; |
| 212 | int i; |
| 213 | int rc; |
| 214 | int num_params_needed; |
Victor Stinner | afccb0a | 2013-11-18 02:07:29 +0100 | [diff] [blame] | 215 | Py_ssize_t num_params; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 216 | |
| 217 | Py_BEGIN_ALLOW_THREADS |
| 218 | num_params_needed = sqlite3_bind_parameter_count(self->st); |
| 219 | Py_END_ALLOW_THREADS |
| 220 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 221 | if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) { |
| 222 | /* parameters passed as sequence */ |
| 223 | if (PyTuple_CheckExact(parameters)) { |
| 224 | num_params = PyTuple_GET_SIZE(parameters); |
| 225 | } else if (PyList_CheckExact(parameters)) { |
| 226 | num_params = PyList_GET_SIZE(parameters); |
| 227 | } else { |
| 228 | num_params = PySequence_Size(parameters); |
Serhiy Storchaka | 0b419b7 | 2020-09-17 10:35:44 +0300 | [diff] [blame] | 229 | if (num_params == -1) { |
| 230 | return; |
| 231 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 232 | } |
| 233 | if (num_params != num_params_needed) { |
Victor Stinner | afccb0a | 2013-11-18 02:07:29 +0100 | [diff] [blame] | 234 | PyErr_Format(pysqlite_ProgrammingError, |
| 235 | "Incorrect number of bindings supplied. The current " |
| 236 | "statement uses %d, and there are %zd supplied.", |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 237 | num_params_needed, num_params); |
| 238 | return; |
| 239 | } |
| 240 | for (i = 0; i < num_params; i++) { |
| 241 | if (PyTuple_CheckExact(parameters)) { |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 242 | PyObject *item = PyTuple_GET_ITEM(parameters, i); |
| 243 | current_param = Py_NewRef(item); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 244 | } else if (PyList_CheckExact(parameters)) { |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 245 | PyObject *item = PyList_GetItem(parameters, i); |
| 246 | current_param = Py_XNewRef(item); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 247 | } else { |
| 248 | current_param = PySequence_GetItem(parameters, i); |
| 249 | } |
| 250 | if (!current_param) { |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | if (!_need_adapt(current_param)) { |
| 255 | adapted = current_param; |
| 256 | } else { |
Erlend Egeberg Aasland | cb6db8b | 2020-09-29 00:05:04 +0200 | [diff] [blame] | 257 | adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)pysqlite_PrepareProtocolType, current_param); |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 258 | Py_DECREF(current_param); |
| 259 | if (!adapted) { |
| 260 | return; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 264 | rc = pysqlite_statement_bind_parameter(self, i + 1, adapted); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 265 | Py_DECREF(adapted); |
| 266 | |
| 267 | if (rc != SQLITE_OK) { |
| 268 | if (!PyErr_Occurred()) { |
| 269 | PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i); |
| 270 | } |
| 271 | return; |
| 272 | } |
| 273 | } |
| 274 | } else if (PyDict_Check(parameters)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 275 | /* parameters passed as dictionary */ |
| 276 | for (i = 1; i <= num_params_needed; i++) { |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 277 | PyObject *binding_name_obj; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 278 | Py_BEGIN_ALLOW_THREADS |
| 279 | binding_name = sqlite3_bind_parameter_name(self->st, i); |
| 280 | Py_END_ALLOW_THREADS |
| 281 | if (!binding_name) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 282 | PyErr_Format(pysqlite_ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | |
| 286 | binding_name++; /* skip first char (the colon) */ |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 287 | binding_name_obj = PyUnicode_FromString(binding_name); |
| 288 | if (!binding_name_obj) { |
| 289 | return; |
| 290 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 291 | if (PyDict_CheckExact(parameters)) { |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 292 | PyObject *item = PyDict_GetItemWithError(parameters, binding_name_obj); |
| 293 | current_param = Py_XNewRef(item); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 294 | } else { |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 295 | current_param = PyObject_GetItem(parameters, binding_name_obj); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 296 | } |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 297 | Py_DECREF(binding_name_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 298 | if (!current_param) { |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 299 | if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_LookupError)) { |
Serhiy Storchaka | 8171580 | 2020-09-04 20:55:41 +0300 | [diff] [blame] | 300 | PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding parameter :%s.", binding_name); |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 301 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 305 | if (!_need_adapt(current_param)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 306 | adapted = current_param; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 307 | } else { |
Erlend Egeberg Aasland | cb6db8b | 2020-09-29 00:05:04 +0200 | [diff] [blame] | 308 | adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)pysqlite_PrepareProtocolType, current_param); |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 309 | Py_DECREF(current_param); |
| 310 | if (!adapted) { |
| 311 | return; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 312 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 315 | rc = pysqlite_statement_bind_parameter(self, i, adapted); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 316 | Py_DECREF(adapted); |
| 317 | |
| 318 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 319 | if (!PyErr_Occurred()) { |
| 320 | PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name); |
| 321 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | } |
| 325 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 326 | PyErr_SetString(PyExc_ValueError, "parameters are of unsupported type"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 330 | int pysqlite_statement_finalize(pysqlite_Statement* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 331 | { |
| 332 | int rc; |
| 333 | |
| 334 | rc = SQLITE_OK; |
| 335 | if (self->st) { |
| 336 | Py_BEGIN_ALLOW_THREADS |
| 337 | rc = sqlite3_finalize(self->st); |
| 338 | Py_END_ALLOW_THREADS |
| 339 | self->st = NULL; |
| 340 | } |
| 341 | |
| 342 | self->in_use = 0; |
| 343 | |
| 344 | return rc; |
| 345 | } |
| 346 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 347 | int pysqlite_statement_reset(pysqlite_Statement* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 348 | { |
| 349 | int rc; |
| 350 | |
| 351 | rc = SQLITE_OK; |
| 352 | |
| 353 | if (self->in_use && self->st) { |
| 354 | Py_BEGIN_ALLOW_THREADS |
| 355 | rc = sqlite3_reset(self->st); |
| 356 | Py_END_ALLOW_THREADS |
| 357 | |
| 358 | if (rc == SQLITE_OK) { |
| 359 | self->in_use = 0; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return rc; |
| 364 | } |
| 365 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 366 | void pysqlite_statement_mark_dirty(pysqlite_Statement* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 367 | { |
| 368 | self->in_use = 1; |
| 369 | } |
| 370 | |
Erlend Egeberg Aasland | bf838a6 | 2021-02-21 01:29:19 +0100 | [diff] [blame] | 371 | static void |
| 372 | pysqlite_statement_dealloc(pysqlite_Statement *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 373 | { |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 374 | PyTypeObject *tp = Py_TYPE(self); |
| 375 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 376 | if (self->st) { |
| 377 | Py_BEGIN_ALLOW_THREADS |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 378 | sqlite3_finalize(self->st); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 379 | Py_END_ALLOW_THREADS |
| 380 | } |
| 381 | |
| 382 | self->st = NULL; |
| 383 | |
| 384 | Py_XDECREF(self->sql); |
| 385 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 386 | if (self->in_weakreflist != NULL) { |
| 387 | PyObject_ClearWeakRefs((PyObject*)self); |
| 388 | } |
| 389 | |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 390 | tp->tp_free(self); |
| 391 | Py_DECREF(tp); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Checks if there is anything left in an SQL string after SQLite compiled it. |
| 396 | * This is used to check if somebody tried to execute more than one SQL command |
| 397 | * with one execute()/executemany() command, which the DB-API and we don't |
| 398 | * allow. |
| 399 | * |
| 400 | * Returns 1 if there is more left than should be. 0 if ok. |
| 401 | */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 402 | static int pysqlite_check_remaining_sql(const char* tail) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 403 | { |
| 404 | const char* pos = tail; |
| 405 | |
| 406 | parse_remaining_sql_state state = NORMAL; |
| 407 | |
| 408 | for (;;) { |
| 409 | switch (*pos) { |
| 410 | case 0: |
| 411 | return 0; |
| 412 | case '-': |
| 413 | if (state == NORMAL) { |
| 414 | state = LINECOMMENT_1; |
| 415 | } else if (state == LINECOMMENT_1) { |
| 416 | state = IN_LINECOMMENT; |
| 417 | } |
| 418 | break; |
| 419 | case ' ': |
| 420 | case '\t': |
| 421 | break; |
| 422 | case '\n': |
| 423 | case 13: |
| 424 | if (state == IN_LINECOMMENT) { |
| 425 | state = NORMAL; |
| 426 | } |
| 427 | break; |
| 428 | case '/': |
| 429 | if (state == NORMAL) { |
| 430 | state = COMMENTSTART_1; |
| 431 | } else if (state == COMMENTEND_1) { |
| 432 | state = NORMAL; |
| 433 | } else if (state == COMMENTSTART_1) { |
| 434 | return 1; |
| 435 | } |
| 436 | break; |
| 437 | case '*': |
| 438 | if (state == NORMAL) { |
| 439 | return 1; |
| 440 | } else if (state == LINECOMMENT_1) { |
| 441 | return 1; |
| 442 | } else if (state == COMMENTSTART_1) { |
| 443 | state = IN_COMMENT; |
| 444 | } else if (state == IN_COMMENT) { |
| 445 | state = COMMENTEND_1; |
| 446 | } |
| 447 | break; |
| 448 | default: |
| 449 | if (state == COMMENTEND_1) { |
| 450 | state = IN_COMMENT; |
| 451 | } else if (state == IN_LINECOMMENT) { |
| 452 | } else if (state == IN_COMMENT) { |
| 453 | } else { |
| 454 | return 1; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | pos++; |
| 459 | } |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 464 | static PyMemberDef stmt_members[] = { |
| 465 | {"__weaklistoffset__", T_PYSSIZET, offsetof(pysqlite_Statement, in_weakreflist), READONLY}, |
| 466 | {NULL}, |
| 467 | }; |
| 468 | static PyType_Slot stmt_slots[] = { |
| 469 | {Py_tp_members, stmt_members}, |
| 470 | {Py_tp_dealloc, pysqlite_statement_dealloc}, |
| 471 | {Py_tp_new, PyType_GenericNew}, |
| 472 | {0, NULL}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 475 | static PyType_Spec stmt_spec = { |
| 476 | .name = MODULE_NAME ".Statement", |
| 477 | .basicsize = sizeof(pysqlite_Statement), |
| 478 | .flags = Py_TPFLAGS_DEFAULT, |
| 479 | .slots = stmt_slots, |
| 480 | }; |
| 481 | PyTypeObject *pysqlite_StatementType = NULL; |
| 482 | |
| 483 | extern int pysqlite_statement_setup_types(PyObject *module) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 484 | { |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 485 | pysqlite_StatementType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &stmt_spec, NULL); |
| 486 | if (pysqlite_StatementType == NULL) { |
| 487 | return -1; |
| 488 | } |
| 489 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 490 | } |